unsafe static void TestQuickInlining() { { var pool = new PassthroughArrayPool <double>(); var intPool = new PassthroughArrayPool <int>(); QuickSet <double, Array <double>, Array <int>, PrimitiveComparer <double> > .Create(pool, intPool, 2, 3, out var set); set.AddUnsafely(5); var item = set[0]; Console.WriteLine($"Managed Item: {item}"); var comparer = default(PrimitiveComparer <double>); var hash = comparer.Hash(ref item); Console.WriteLine($"Hash: {hash}"); } { var pool = new BufferPool().SpecializeFor <int>(); QuickSet <int, Buffer <int>, Buffer <int>, PrimitiveComparer <int> > .Create(pool, pool, 2, 3, out var set); set.AddUnsafely(5); var item = set[0]; pool.Raw.Clear(); } }
unsafe void Allocate <TBodyReferenceGetter>(int constraintHandle, ref int constraintBodyHandles, int bodyCount, Bodies bodies, int typeId, BufferPool pool, TBodyReferenceGetter bodyReferenceGetter, int minimumBodyCapacity, int minimumReferenceCapacity) where TBodyReferenceGetter : struct, IBodyReferenceGetter { var fallbackPool = pool.SpecializeFor <FallbackReference>(); var intPool = pool.SpecializeFor <int>(); var minimumReferencePower = SpanHelper.GetContainingPowerOf2(minimumReferenceCapacity); EnsureCapacity(Math.Max(bodyConstraintReferences.Count + bodyCount, minimumBodyCapacity), pool); for (int i = 0; i < bodyCount; ++i) { var bodyReference = bodyReferenceGetter.GetBodyReference(bodies, Unsafe.Add(ref constraintBodyHandles, i)); var bodyAlreadyListed = bodyConstraintReferences.GetTableIndices(ref bodyReference, out var tableIndex, out var elementIndex); //If an entry for this body does not yet exist, we'll create one. if (!bodyAlreadyListed) { elementIndex = bodyConstraintReferences.Count; } ref var constraintReferences = ref bodyConstraintReferences.Values[elementIndex]; if (!bodyAlreadyListed) { //The body is not already contained. Create a list for it. QuickSet <FallbackReference, Buffer <FallbackReference>, Buffer <int>, FallbackReferenceComparer> .Create(fallbackPool, intPool, minimumReferencePower, 2, out constraintReferences); bodyConstraintReferences.Keys[elementIndex] = bodyReference; bodyConstraintReferences.Table[tableIndex] = elementIndex + 1; ++bodyConstraintReferences.Count; } var fallbackReference = new FallbackReference { ConstraintHandle = constraintHandle, IndexInConstraint = i }; constraintReferences.Add(ref fallbackReference, fallbackPool, intPool); }
public MeshCache(Device device, BufferPool pool, int initialSizeInVertices = 1 << 22) { Pool = pool; pool.Take(initialSizeInVertices, out vertices); TriangleBuffer = new StructuredBuffer <Vector3>(device, initialSizeInVertices, "Mesh Cache Vertex Buffer"); allocator = new Allocator(initialSizeInVertices); QuickList <UploadRequest, Buffer <UploadRequest> > .Create(pool.SpecializeFor <UploadRequest>(), 128, out pendingUploads); QuickList <ulong, Buffer <ulong> > .Create(pool.SpecializeFor <ulong>(), 128, out requestedIds); QuickSet <ulong, Buffer <ulong>, Buffer <int>, PrimitiveComparer <ulong> > .Create(pool.SpecializeFor <ulong>(), pool.SpecializeFor <int>(), 8, 3, out previouslyAllocatedIds); }
public static void TestSetResizing <TSpan, TPool>(TPool pool) where TSpan : ISpan <int> where TPool : IMemoryPool <int, TSpan> { Random random = new Random(5); QuickSet <int, TSpan, TSpan, PrimitiveComparer <int> > .Create(pool, pool, 2, 3, out var set); HashSet <int> controlSet = new HashSet <int>(); for (int iterationIndex = 0; iterationIndex < 100000; ++iterationIndex) { if (random.NextDouble() < 0.7) { set.Add(iterationIndex, pool, 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, pool); } else if (iterationIndex % 7777 == 0) { set.Compact(pool, 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, pool); }