public void ExpandBy(int minimumFreeStateCapacity, int minimumFreeActionCapacity) { if (ActionLookup.Count() + minimumFreeActionCapacity > ActionLookup.Capacity) { ActionLookup.Capacity = Math.Max(ActionLookup.Count() + minimumFreeActionCapacity, ActionLookup.Capacity * 2); } if (ResultingStateLookup.Count() + minimumFreeStateCapacity > ResultingStateLookup.Capacity) { ResultingStateLookup.Capacity = Math.Max(ResultingStateLookup.Count() + minimumFreeStateCapacity, ResultingStateLookup.Capacity * 2); } if (PredecessorGraph.Count() + minimumFreeStateCapacity > PredecessorGraph.Capacity) { PredecessorGraph.Capacity = Math.Max(PredecessorGraph.Count() + minimumFreeStateCapacity, PredecessorGraph.Capacity * 2); } if (StateInfoLookup.Count() + minimumFreeStateCapacity > StateInfoLookup.Capacity) { StateInfoLookup.Capacity = Math.Max(StateInfoLookup.Count() + minimumFreeStateCapacity, StateInfoLookup.Capacity * 2); } if (ActionInfoLookup.Count() + minimumFreeActionCapacity > ActionInfoLookup.Capacity) { ActionInfoLookup.Capacity = Math.Max(ActionInfoLookup.Count() + minimumFreeActionCapacity, ActionInfoLookup.Capacity * 2); } if (StateTransitionInfoLookup.Count() + minimumFreeActionCapacity > StateTransitionInfoLookup.Capacity) { StateTransitionInfoLookup.Capacity = Math.Max(StateTransitionInfoLookup.Count() + minimumFreeActionCapacity, StateTransitionInfoLookup.Capacity * 2); } }
static void ExpectedCount <TKey, TValue>(ref NativeMultiHashMap <TKey, TValue> container, int expected) where TKey : struct, IEquatable <TKey> where TValue : struct { Assert.AreEqual(expected == 0, container.IsEmpty); Assert.AreEqual(expected, container.Count()); }
public void Execute() { var capacityNeeded = InputStateExpansionInfo.Count; planGraph.ExpandBy(capacityNeeded, capacityNeeded); if (BinnedStateKeys.Count() + capacityNeeded > BinnedStateKeys.Capacity) { BinnedStateKeys.Capacity = Math.Max(BinnedStateKeys.Count() + capacityNeeded, BinnedStateKeys.Capacity * 2); } while (InputStateExpansionInfo.TryDequeue(out var item)) { OutputStateExpansionInfo.Add(item); } }
public void Execute() { if (SplitIndicesMap.Count() == 0) { return; } var roadIndices = SplitIndicesMap.GetKeyArray(Allocator.Temp); var intersectionMetaData = SplitIndicesMap.GetValueArray(Allocator.Temp); // Sort intersection metadata by spline interpolation factor for (var i = 1; i < roadIndices.Length; i++) { var j = i - 1; var prevIndex = roadIndices[j]; while (i < roadIndices.Length && roadIndices[i] == prevIndex) { i++; } var subArray = intersectionMetaData.GetSubArray(j, i - j); subArray.Sort(new IntersectionPointComparer()); } IntersectionMetaData.AddRange(intersectionMetaData); intersectionMetaData.Dispose(); RemoveDuplicateIntersectionRecords(roadIndices); roadIndices.Dispose(); // Identify all unique intersection points and save them to a list for (var i = 0; i < IntersectionMetaData.Length; i++) { var foundDuplicate = false; var metaData = IntersectionMetaData[i]; var intersectionEntityIndex = 0; // Search all known unique intersection points for a duplicate for (var j = 0; j < UniqueIntersectionPoints.Length; j++) { if (!Utilities.GeometryUtility.ApproximatelyEqual(UniqueIntersectionPoints[j], metaData.Point)) { continue; } foundDuplicate = true; intersectionEntityIndex = j; break; } if (!foundDuplicate) { UniqueIntersectionPoints.Add(metaData.Point); intersectionEntityIndex = UniqueIntersectionPoints.Length - 1; } metaData.IntersectionEntityIndex = intersectionEntityIndex; IntersectionMetaData[i] = metaData; } }
public void NativeMultiHashMap_ForEach([Values(10, 1000)] int n) { var seenKeys = new NativeArray <int>(n, Allocator.Temp); var seenValues = new NativeArray <int>(n * 2, Allocator.Temp); using (var container = new NativeMultiHashMap <int, int>(1, Allocator.Temp)) { for (int i = 0; i < n; ++i) { container.Add(i, i); container.Add(i, i + n); } var count = 0; foreach (var kv in container) { if (kv.Value < n) { Assert.AreEqual(kv.Key, kv.Value); } else { Assert.AreEqual(kv.Key + n, kv.Value); } seenKeys[kv.Key] = seenKeys[kv.Key] + 1; seenValues[kv.Value] = seenValues[kv.Value] + 1; ++count; } Assert.AreEqual(container.Count(), count); for (int i = 0; i < n; i++) { Assert.AreEqual(2, seenKeys[i], $"Incorrect key count {i}"); Assert.AreEqual(1, seenValues[i], $"Incorrect value count {i}"); Assert.AreEqual(1, seenValues[i + n], $"Incorrect value count {i + n}"); } } }
public void Execute() { if (MaterialToSubMeshMap.Count() == 0) { return; } // Copy the values array of MaterialToSubMeshMap to the SubMeshRecords NativeList { var subMeshRecords = MaterialToSubMeshMap.GetValueArray(Allocator.Temp); SubMeshRecords.AddRange(subMeshRecords); SubMeshMap.ResizeUninitialized(SubMeshRecords.Length); subMeshRecords.Dispose(); } var keys = MaterialToSubMeshMap.GetKeyArray(Allocator.Temp); for (var i = 0; i < SubMeshRecords.Length; i++) { var subMesh = SubMeshRecords[i]; SubMeshMap[i] = new MappedSubMeshIndices { TriangleIndexOffset = m_TriangleIndexOffset, VertexStartIndex = m_VertexCount, TriangleStartIndex = m_TriangleCount }; m_VertexCount += subMesh.VertexCount; m_TriangleCount += subMesh.TriangleCount; m_TriangleIndexOffset += subMesh.VertexCount; var material = keys[i]; if (i == SubMeshRecords.Length - 1 || material != keys[i + 1]) { AddSubMesh(material); } } Vertices.ResizeUninitialized(m_VertexCount); Triangles.ResizeUninitialized(m_TriangleCount); }
protected override void OnCreate() { stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Restart(); stopwatch.Start(); int i_len = 10000; NativeMultiHashMap <int, int> dic = new NativeMultiHashMap <int, int> (i_len, Allocator.Temp); NativeMultiHashMap <int, int> dic2 = new NativeMultiHashMap <int, int> (i_len, Allocator.TempJob); NativeHashMap <int, int> dic3 = new NativeHashMap <int, int> (i_len, Allocator.Temp); for (int i = 0; i < i_len; i++) { var v2 = new Vector2Int(Random.Range(10, -10), Random.Range(-1000, 1000)); dic.Add(v2.x, v2.y); dic2.Add(v2.x, v2.y); dic3.TryAdd(v2.x, v2.y); // Debug.Log ( "v2: " + v2 ) ; } Debug.Log("populate hashmaps " + stopwatch.ElapsedMilliseconds + "ms"); stopwatch.Restart(); // NativeMultiHashMap <int,int> dic = nmhm_newBornEntitiesPerTile ; var withDuplicates = dic.GetKeyArray(Allocator.Temp); Debug.Log("multi hashmap get keys " + stopwatch.ElapsedMilliseconds + "ms"); stopwatch.Restart(); NativeArray <int> withDuplicates2 = new NativeArray <int> (dic2.Count(), Allocator.TempJob, NativeArrayOptions.UninitializedMemory); Dependency = new GetArray( ) { dic = dic2, withDuplicates = withDuplicates2 }.Schedule(Dependency); Dependency.Complete(); Debug.Log("multi hashmap get keys job burst B " + stopwatch.ElapsedMilliseconds + "ms"); stopwatch.Restart(); var noDuplicates = dic3.GetKeyArray(Allocator.Temp); Debug.Log("hashmap get keys " + stopwatch.ElapsedMilliseconds + "ms"); /* * for ( int i = 0; i < noDuplicates.Length; i ++ ) * { * Debug.Log ( "no duplicates: " + noDuplicates [i] ) ; * } */ stopwatch.Restart(); withDuplicates.Sort(); Debug.Log("sort A " + stopwatch.ElapsedMilliseconds + "ms"); stopwatch.Restart(); int uniqueCount = withDuplicates.Unique(); Debug.Log("multi hashmap unique A " + stopwatch.ElapsedMilliseconds + "ms"); Debug.Log("uniqueCount " + uniqueCount); stopwatch.Restart(); Dependency = new Sort( ) { withDuplicates = withDuplicates2 }.Schedule(Dependency); Dependency.Complete(); Debug.Log("sort job burst B " + stopwatch.ElapsedMilliseconds + "ms"); stopwatch.Restart(); NativeArray <int> na_i = new NativeArray <int> (3, Allocator.TempJob); Dependency = new Unique( ) { i = na_i, withDuplicates = withDuplicates2 }.Schedule(Dependency); Dependency.Complete(); uniqueCount = na_i [0]; Debug.Log("multi hashmap unique job burst B " + stopwatch.ElapsedMilliseconds + "ms"); Debug.Log("uniqueCount " + uniqueCount); stopwatch.Restart(); Debug.Log("uniqueCount hashmap " + noDuplicates.Length); /* * for ( int i = 0; i < i_len; i ++ ) * { * Debug.Log ( "B: " + withDuplicates [i] ) ; * } */ withDuplicates2.Dispose(); dic2.Dispose(); withDuplicates.Dispose(); noDuplicates.Dispose(); dic.Dispose(); Debug.LogError("Stop"); }
public void Execute() { var statesToUpdateLength = SelectedStates.Count(); var statesToUpdate = new NativeMultiHashMap <int, TStateKey>(statesToUpdateLength, Allocator.Temp); var currentHorizon = new NativeList <TStateKey>(statesToUpdateLength, Allocator.Temp); var nextHorizon = new NativeList <TStateKey>(statesToUpdateLength, Allocator.Temp); var maxDepth = int.MinValue; var selectedStateKeys = SelectedStates.GetKeyArray(Allocator.Temp); for (int i = 0; i < selectedStateKeys.Length; i++) { var stateKey = selectedStateKeys[i]; int stateDepth = int.MinValue; SelectedStates.TryGetFirstValue(stateKey, out var selectedDepth, out var iterator); do { stateDepth = math.max(stateDepth, selectedDepth); }while (SelectedStates.TryGetNextValue(out selectedDepth, ref iterator)); // Update depth map DepthMap[stateKey] = stateDepth; // Queue state and track max depth of backpropagation statesToUpdate.AddValueIfUnique(stateDepth, stateKey); maxDepth = math.max(maxDepth, stateDepth); } selectedStateKeys.Dispose(); var actionLookup = planGraph.ActionLookup; var resultingStateLookup = planGraph.ResultingStateLookup; var actionInfoLookup = planGraph.ActionInfoLookup; var stateInfoLookup = planGraph.StateInfoLookup; var stateTransitionInfoLookup = planGraph.StateTransitionInfoLookup; var predecessorLookup = planGraph.PredecessorGraph; var depth = maxDepth; // Pull states from statesToUpdate if (statesToUpdate.TryGetFirstValue(depth, out var stateToAdd, out var stateIterator)) { do { currentHorizon.AddIfUnique(stateToAdd); }while (statesToUpdate.TryGetNextValue(out stateToAdd, ref stateIterator)); } // Update values from leaf state(s) to root while (depth >= 0) { for (int i = 0; i < currentHorizon.Length; i++) { var stateKey = currentHorizon[i]; var updateState = true; if (actionLookup.TryGetFirstValue(stateKey, out var actionKey, out var stateActionIterator)) { // Expanded state. Only update if one or more actions have updated. updateState = false; // Update all actions do { updateState |= UpdateCumulativeReward(new StateActionPair <TStateKey, TActionKey>(stateKey, actionKey), resultingStateLookup, stateInfoLookup, actionInfoLookup, stateTransitionInfoLookup); }while (actionLookup.TryGetNextValue(out actionKey, ref stateActionIterator)); } if (!updateState) { continue; } // Update state if (UpdateStateValue(stateKey, actionLookup, stateInfoLookup, actionInfoLookup)) { // If a change has occured, update predecessors if (predecessorLookup.TryGetFirstValue(stateKey, out var predecessorStateKey, out var predecessorIterator)) { do { nextHorizon.AddIfUnique(predecessorStateKey); }while (predecessorLookup.TryGetNextValue(out predecessorStateKey, ref predecessorIterator)); } } } var temp = currentHorizon; currentHorizon = nextHorizon; nextHorizon = temp; nextHorizon.Clear(); depth--; // pull out states from statesToUpdate if (statesToUpdate.TryGetFirstValue(depth, out stateToAdd, out stateIterator)) { do { currentHorizon.AddIfUnique(stateToAdd); }while (statesToUpdate.TryGetNextValue(out stateToAdd, ref stateIterator)); } } // new: continue propagating complete flag changes while (currentHorizon.Length > 0) { for (int i = 0; i < currentHorizon.Length; i++) { var stateKey = currentHorizon[i]; var updateState = false; // Update all actions actionLookup.TryGetFirstValue(stateKey, out var actionKey, out var stateActionIterator); do { var stateActionPair = new StateActionPair <TStateKey, TActionKey>(stateKey, actionKey); if (UpdateSubplanCompleteStatus(stateActionPair, out var updatedActionInfo)) { updateState = true; // Write back updated info actionInfoLookup[stateActionPair] = updatedActionInfo; } }while (actionLookup.TryGetNextValue(out actionKey, ref stateActionIterator)); // Update state if (updateState && UpdateSubplanCompleteStatus(stateKey, out var updatedStateInfo)) { // Write back updated info stateInfoLookup[stateKey] = updatedStateInfo; // If a change has occured, update predecessors if (predecessorLookup.TryGetFirstValue(stateKey, out var predecessorStateKey, out var predecessorIterator)) { do { nextHorizon.AddIfUnique(predecessorStateKey); }while (predecessorLookup.TryGetNextValue(out predecessorStateKey, ref predecessorIterator)); } } } var temp = currentHorizon; currentHorizon = nextHorizon; nextHorizon = temp; nextHorizon.Clear(); } currentHorizon.Dispose(); nextHorizon.Dispose(); statesToUpdate.Dispose(); }
public void Execute() { CornerEntities.ResizeUninitialized(IntersectionsToRoadsMap.Count()); Transaction.CreateEntity(CornerArchetype, CornerEntities); }