public void EnsureCapacity(int count, IUnmanagedMemoryPool pool) { if (count > Span.Length) { Resize(count, pool); } }
public void EnsureCapacity(int count, IUnmanagedMemoryPool pool) { if (count >= CapacityMask) { Resize(count, pool); } }
public QuickSet(int initialCapacity, int tableSizePower, IUnmanagedMemoryPool pool, TEqualityComparer comparer) { pool.TakeAtLeast <T>(initialCapacity, out var span); pool.TakeAtLeast <int>(span.Length << tableSizePower, out var tableSpan); //No guarantee that the table is clean; clear it. tableSpan.Clear(0, tableSpan.Length); this = new QuickSet <T, TEqualityComparer>(ref span, ref tableSpan, comparer, tableSizePower); }
public void Dispose(IUnmanagedMemoryPool pool) { Keys.ClearManagedReferences(0, Count); Values.ClearManagedReferences(0, Count); pool.Return(ref Keys); pool.Return(ref Values); pool.Return(ref Table); }
public QuickQueue(int minimumInitialCount, IUnmanagedMemoryPool pool) { pool.TakeAtLeast(minimumInitialCount, out Span); Count = 0; CapacityMask = GetCapacityMask(Span.Length); FirstIndex = 0; LastIndex = CapacityMask; ValidateSpanCapacity(ref Span, CapacityMask); }
public QuickDictionary(int initialCapacity, int tableSizePower, IUnmanagedMemoryPool pool, TEqualityComparer comparer) { pool.TakeAtLeast <TKey>(initialCapacity, out var keySpan); pool.TakeAtLeast <TValue>(keySpan.Length, out var valueSpan); pool.TakeAtLeast <int>(keySpan.Length << tableSizePower, out var tableSpan); //No guarantee that the table is clean; clear it. tableSpan.Clear(0, tableSpan.Length); this = new QuickDictionary <TKey, TValue, TEqualityComparer>(ref keySpan, ref valueSpan, ref tableSpan, comparer, tableSizePower); }
void InternalResize(int newSize, IUnmanagedMemoryPool pool) { var oldAvailableIds = availableIds; pool.TakeAtLeast(newSize, out availableIds); Debug.Assert(oldAvailableIds.Length != availableIds.Length, "Did you really mean to resize this? Nothing changed!"); oldAvailableIds.CopyTo(0, ref availableIds, 0, availableIdCount); pool.Return(ref oldAvailableIds); }
public void CustomAngularDamping(int bodyIndex, float angularDamping, IUnmanagedMemoryPool pool) { if (angularDamping == AngularDamping) { _angularDampingDictionary.FastRemove(bodyIndex); return; } _angularDampingDictionary.Add(bodyIndex, angularDamping, pool); }
/// <summary> /// Shrinks the available ids queue to the smallest size that can fit the given count and the current available id count. /// </summary> /// <param name="minimumCount">Number of elements to guarantee space for in the available ids queue.</param> public void Compact(int minimumCount, IUnmanagedMemoryPool pool) { Debug.Assert(availableIds.Allocated); var targetLength = BufferPool.GetCapacityForCount <int>(Math.Max(minimumCount, availableIdCount)); if (availableIds.Length > targetLength) { InternalResize(targetLength, pool); } }
/// <summary> /// Shrinks the internal buffers to the smallest acceptable size and releases the old buffers to the pools. /// </summary> /// <param name="element">Element to add.</param> /// <param name="pool">Pool used for spans.</param> public void Compact(IUnmanagedMemoryPool pool) { Validate(); var targetCapacity = pool.GetCapacityForCount <T>(Count); if (targetCapacity < Span.Length) { Resize(Count, pool); } }
public DefaultPoseIntegratorCallbacks(IUnmanagedMemoryPool pool, Vector3?gravity = null, float linearDamping = .03f, float angularDamping = .03f) : this() { Gravity = gravity ?? new Vector3(0, -9.81f, 0); LinearDamping = linearDamping; AngularDamping = angularDamping; _angularDampingDictionary = new QuickDictionary <int, float, IntComparer>(1, pool); _linearDampingDictionary = new QuickDictionary <int, float, IntComparer>(1, pool); _linearDampingDictionary.Add(-1, LinearDamping, pool); _angularDampingDictionary.Add(-1, AngularDamping, pool); }
public void CustomLinearDamping(int bodyIndex, float linearDamping, IUnmanagedMemoryPool pool) { if (linearDamping == LinearDamping) { _linearDampingDictionary.FastRemove(bodyIndex); return; } _linearDampingDictionary.Add(bodyIndex, linearDamping, pool); }
public void Return(int id, IUnmanagedMemoryPool pool) { Debug.Assert(availableIds.Allocated); if (availableIdCount == availableIds.Length) { var oldAvailableIds = availableIds; pool.TakeAtLeast(Math.Max(availableIdCount * 2, availableIds.Length), out availableIds); oldAvailableIds.CopyTo(0, ref availableIds, 0, availableIdCount); pool.Return(ref oldAvailableIds); } ReturnUnsafely(id); }
public void Resize(int newSize, IUnmanagedMemoryPool pool) { var targetCapacity = pool.GetCapacityForCount <T>(newSize); if (targetCapacity != Span.Length) { var oldQueue = this; pool.TakeAtLeast <T>(newSize, out var newSpan); Resize(ref newSpan, out var oldSpan); oldQueue.Dispose(pool); } }
public bool AddRef(ref T element, IUnmanagedMemoryPool pool) { if (Count == Span.Length) { //There's no room left; resize. Resize(Count * 2, pool); //Note that this is tested before any indices are found. //If we resized only after determining that it was going to be added, //the potential resize would invalidate the computed indices. } return(AddUnsafelyRef(ref element)); }
public void Resize(int newSize, IUnmanagedMemoryPool pool) { var targetCapacity = pool.GetCapacityForCount <T>(newSize); if (targetCapacity != Span.Length) { var oldSet = this; pool.TakeAtLeast <T>(newSize, out var newSpan); pool.TakeAtLeast <int>(newSpan.Length << TablePowerOffset, out var newTableSpan); //There is no guarantee that the table retrieved from the pool is clean. Clear it! newTableSpan.Clear(0, newTableSpan.Length); Resize(ref newSpan, ref newTableSpan, out var oldSpan, out var oldTableSpan); oldSet.Dispose(pool); } }
/// <summary> /// Ensures that the underlying id queue can hold at least a certain number of ids. /// </summary> /// <param name="count">Number of elements to preallocate space for in the available ids queue.</param> /// <param name="pool">Pool to pull resized spans from.</param> public void EnsureCapacity(int count, IUnmanagedMemoryPool pool) { if (!availableIds.Allocated) { //If this was disposed, we must explicitly rehydrate it. this = new IdPool(count, pool); } else { if (availableIds.Length < count) { InternalResize(count, pool); } } }
public void Resize(int newSize, IUnmanagedMemoryPool pool) { var targetKeyCapacity = pool.GetCapacityForCount <TKey>(newSize); if (targetKeyCapacity != Keys.Length) { pool.TakeAtLeast <TKey>(newSize, out var newKeySpan); pool.TakeAtLeast <TValue>(newKeySpan.Length, out var newValueSpan); pool.TakeAtLeast <int>(newKeySpan.Length << TablePowerOffset, out var newTableSpan); //There is no guarantee that the table retrieved from the pool is clean. Clear it! newTableSpan.Clear(0, newTableSpan.Length); var oldDictionary = this; Resize(ref newKeySpan, ref newValueSpan, ref newTableSpan, out var oldKeySpan, out var oldValueSpan, out var oldTableSpan); oldDictionary.Dispose(pool); } }
/// <summary> /// Resizes the underlying buffer to the smallest size required to hold the given count and the current available id count. /// </summary> /// <param name="count">Number of elements to guarantee space for in the available ids queue.</param> public void Resize(int count, IUnmanagedMemoryPool pool) { if (!availableIds.Allocated) { //If this was disposed, we must explicitly rehydrate it. this = new IdPool(count, pool); } else { var targetLength = BufferPool.GetCapacityForCount <int>(Math.Max(count, availableIdCount)); if (availableIds.Length != targetLength) { InternalResize(targetLength, pool); } } }
public static void TestSetResizing(IUnmanagedMemoryPool pool) { Random random = new Random(5); var set = new QuickSet <int, PrimitiveComparer <int> >(4, pool); HashSet <int> controlSet = new HashSet <int>(); for (int iterationIndex = 0; iterationIndex < 100000; ++iterationIndex) { if (random.NextDouble() < 0.7) { set.Add(iterationIndex, pool); controlSet.Add(iterationIndex); } if (random.NextDouble() < 0.2) { var indexToRemove = random.Next(set.Count); var toRemove = set[indexToRemove]; set.FastRemove(toRemove); controlSet.Remove(toRemove); } if (iterationIndex % 1000 == 0) { set.EnsureCapacity(set.Count * 3, pool); } else if (iterationIndex % 7777 == 0) { set.Compact(pool); } } Debug.Assert(set.Count == controlSet.Count); for (int i = 0; i < set.Count; ++i) { Debug.Assert(controlSet.Contains(set[i])); } foreach (var element in controlSet) { Debug.Assert(set.Contains(element)); } set.Dispose(pool); }
public static void TestDictionaryResizing(IUnmanagedMemoryPool pool) { Random random = new Random(5); var dictionary = new QuickDictionary <int, int, PrimitiveComparer <int> >(4, pool); Dictionary <int, int> controlDictionary = new Dictionary <int, int>(); for (int iterationIndex = 0; iterationIndex < 100000; ++iterationIndex) { if (random.NextDouble() < 0.7) { dictionary.Add(iterationIndex, iterationIndex, pool); controlDictionary.Add(iterationIndex, iterationIndex); } if (random.NextDouble() < 0.2) { var indexToRemove = random.Next(dictionary.Count); var toRemove = dictionary.Keys[indexToRemove]; dictionary.FastRemove(toRemove); controlDictionary.Remove(toRemove); } if (iterationIndex % 1000 == 0) { dictionary.EnsureCapacity(dictionary.Count * 3, pool); } else if (iterationIndex % 7777 == 0) { dictionary.Compact(pool); } } Debug.Assert(dictionary.Count == controlDictionary.Count); for (int i = 0; i < dictionary.Count; ++i) { Debug.Assert(controlDictionary.ContainsKey(dictionary.Keys[i])); } foreach (var element in controlDictionary.Keys) { Debug.Assert(dictionary.ContainsKey(element)); } dictionary.Dispose(pool); }
public static void TestQueueResizing(IUnmanagedMemoryPool pool) { Random random = new Random(5); var queue = new QuickQueue <int>(4, pool); Queue <int> controlQueue = new Queue <int>(); for (int iterationIndex = 0; iterationIndex < 1000000; ++iterationIndex) { if (random.NextDouble() < 0.7) { queue.Enqueue(iterationIndex, pool); controlQueue.Enqueue(iterationIndex); } if (random.NextDouble() < 0.2) { queue.Dequeue(); controlQueue.Dequeue(); } if (iterationIndex % 1000 == 0) { queue.EnsureCapacity(queue.Count * 3, pool); } else if (iterationIndex % 7777 == 0) { queue.Compact(pool); } } Debug.Assert(queue.Count == controlQueue.Count, "e"); while (queue.Count > 0) { var a = queue.Dequeue(); var b = controlQueue.Dequeue(); Debug.Assert(a == b); Debug.Assert(queue.Count == controlQueue.Count); } queue.Dispose(pool); }
public static void TestListResizing(IUnmanagedMemoryPool pool) { Random random = new Random(5); var list = new QuickList <int>(4, pool); List <int> controlList = new List <int>(); for (int iterationIndex = 0; iterationIndex < 100000; ++iterationIndex) { if (random.NextDouble() < 0.7) { list.Add(iterationIndex, pool); controlList.Add(iterationIndex); } if (random.NextDouble() < 0.2) { var indexToRemove = random.Next(list.Count); list.RemoveAt(indexToRemove); controlList.RemoveAt(indexToRemove); } if (iterationIndex % 1000 == 0) { list.EnsureCapacity(list.Count * 3, pool); } else if (iterationIndex % 7777 == 0) { list.Compact(pool); } } Debug.Assert(list.Count == controlList.Count); for (int i = 0; i < list.Count; ++i) { var a = list[i]; var b = controlList[i]; Debug.Assert(a == b); Debug.Assert(list.Count == controlList.Count); } list.Dispose(pool); }
public bool Add(T element, IUnmanagedMemoryPool pool) { return(AddRef(ref element, pool)); }
public bool AddAndReplace(T element, IUnmanagedMemoryPool pool) { return(AddAndReplaceUnsafelyRef(ref element)); }
public void Dispose(IUnmanagedMemoryPool pool) { pool.Return(ref Span); pool.Return(ref Table); }
public QuickSet(int initialCapacity, IUnmanagedMemoryPool pool) : this(initialCapacity, 2, pool, default) { }
public QuickSet(int initialCapacity, int tableSizePower, IUnmanagedMemoryPool pool) : this(initialCapacity, tableSizePower, pool, default) { }
public void Dispose(IUnmanagedMemoryPool pool) { pool.Return(ref Keys); pool.Return(ref Values); pool.Return(ref Table); }
public IdPool(int initialCapacity, IUnmanagedMemoryPool pool) { nextIndex = 0; availableIdCount = 0; pool.TakeAtLeast(initialCapacity, out availableIds); }