/// <summary> /// Disposes of this container and deallocates its memory immediately. /// </summary> public void Dispose() { #if ENABLE_UNITY_COLLECTIONS_CHECKS DisposeSentinel.Dispose(ref m_Safety, ref m_DisposeSentinel); #endif m_BitArray.Dispose(); }
public void UnsafeBitArray_FindWithPattern() { var test = new UnsafeBitArray(512, Allocator.Persistent, NativeArrayOptions.ClearMemory); // Separated test for some more interesting patterns findWithPattern(ref test, 0x81, 1); findWithPattern(ref test, 0x81, 2); findWithPattern(ref test, 0x81, 3); findWithPattern(ref test, 0x81, 6); findWithPattern(ref test, 0x88, 3); findWithPattern(ref test, 0x99, 2); findWithPattern(ref test, 0xaa, 1); findWithPattern(ref test, 0xc3, 1); findWithPattern(ref test, 0xc3, 2); findWithPattern(ref test, 0xc3, 4); findWithPattern(ref test, 0xe7, 1); findWithPattern(ref test, 0xe7, 2); // Test all patterns for (int i = 0; i < 256; i++) { findWithPattern(ref test, (byte)i, 1); } test.Dispose(); }
public void UnsafeBitArray_CopyBetweenBitArrays() { var numBits = 512; var test0 = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory); var test1 = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory); var test2 = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory); for (int pos = 0; pos < test0.Length; pos += 64) { test0.SetBits(pos, 0xaaaaaaaaaaaaaaaaul, 64); test1.SetBits(pos, 0x5555555555555555ul, 64); } var numCopyBits = 255; test0.SetBits(13, true, numCopyBits); test1.Copy(1, ref test0, 13, numCopyBits); Assert.AreEqual(true, test1.TestAll(1, numCopyBits)); test2.Copy(43, ref test1, 1, numCopyBits); Assert.AreEqual(true, test2.TestAll(43, numCopyBits)); test0.Dispose(); test1.Dispose(); test2.Dispose(); }
// Simple grassfire to calculate shortest distance along path to target position for every position. // - i.e. breadth-first expansion from target position. static void CalculateShortestWalkableDistancesToTarget(int rowCount, int colCount, byte *gridWalls, CartesianGridCoordinates targetPosition, NativeArray <int> targetDistances) { var cellCount = rowCount * colCount; var closed = new UnsafeBitArray(cellCount, Allocator.Temp, NativeArrayOptions.ClearMemory); var pending = new UnsafeBitArray(cellCount, Allocator.Temp, NativeArrayOptions.ClearMemory); var open = new UnsafeRingQueue <int>(cellCount, Allocator.Temp); var targetCellIndex = (targetPosition.y * colCount) + targetPosition.x; var cellIndexNorth = targetCellIndex + colCount; var cellIndexSouth = targetCellIndex - colCount; var cellIndexWest = targetCellIndex - 1; var cellIndexEast = targetCellIndex + 1; for (int i = 0; i < targetDistances.Length; i++) { targetDistances[i] = -1; } targetDistances[targetCellIndex] = 0; pending.Set(targetCellIndex, true); closed.Set(targetCellIndex, true); var validDirections = CartesianGridMovement.ValidDirections(targetPosition, rowCount, colCount, gridWalls); var validNorth = ((validDirections & (byte)CartesianGridDirectionBit.North) != 0); var validSouth = ((validDirections & (byte)CartesianGridDirectionBit.South) != 0); var validWest = ((validDirections & (byte)CartesianGridDirectionBit.West) != 0); var validEast = ((validDirections & (byte)CartesianGridDirectionBit.East) != 0); if (validNorth) { open.Enqueue(cellIndexNorth); pending.Set(cellIndexNorth, true); } if (validSouth) { open.Enqueue(cellIndexSouth); pending.Set(cellIndexSouth, true); } if (validWest) { open.Enqueue(cellIndexWest); pending.Set(cellIndexWest, true); } if (validEast) { open.Enqueue(cellIndexEast); pending.Set(cellIndexEast, true); } CalculateShortestWalkableDistancesToTargetInner(rowCount, colCount, gridWalls, targetDistances, pending, closed, open); closed.Dispose(); pending.Dispose(); open.Dispose(); }
public void UnsafeBitArray_GetBits() { var numBits = 256; var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory); GetBitsTest(ref test, 0, 5); GetBitsTest(ref test, 1, 3); GetBitsTest(ref test, 1, 64); GetBitsTest(ref test, 62, 5); GetBitsTest(ref test, 127, 3); test.Dispose(); }
public unsafe void UnsafeBitArray_Throws() { var numBits = 256; var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory); Assert.DoesNotThrow(() => { test.TestAll(0, numBits); }); Assert.Throws <ArgumentException>(() => { test.IsSet(-1); }); Assert.Throws <ArgumentException>(() => { test.IsSet(numBits); }); Assert.Throws <ArgumentException>(() => { new UnsafeBitArray(null, 7); /* check sizeInBytes must be multiple of 8-bytes. */ }); test.Dispose(); }
public void UnsafeBitArray_FindInTinyBitArray() { var test = new UnsafeBitArray(3, Allocator.Persistent, NativeArrayOptions.ClearMemory); Assert.AreEqual(3, test.Length); test.SetBits(0, 0x55, test.Length); Assert.AreEqual(1, test.Find(0, 1)); Assert.AreEqual(1, test.Find(0, test.Length, 1)); test.SetBits(1, true, 1); Assert.True(test.TestAll(0, test.Length)); Assert.AreEqual(int.MaxValue, test.Find(0, test.Length, 1)); test.Dispose(); }
public unsafe void UnsafeBitArray_Copy_Throws() { var numBits = 512; var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory); Assert.Throws <ArgumentException>(() => { CopyBitsTest(ref test, 0, numBits - 1, 16); }); // short up to 64-bits copy out of bounds Assert.Throws <ArgumentException>(() => { CopyBitsTest(ref test, numBits - 1, 0, 16); }); // short up to 64-bits copy out of bounds Assert.Throws <ArgumentException>(() => { CopyBitsTest(ref test, 0, numBits - 1, 80); }); // short up to 128-bits copy out of bounds Assert.Throws <ArgumentException>(() => { CopyBitsTest(ref test, numBits - 1, 0, 80); }); // short up to 128-bits copy out of bounds Assert.Throws <ArgumentException>(() => { CopyBitsTest(ref test, 1, numBits - 7, 127); }); // long copy aligned Assert.Throws <ArgumentException>(() => { CopyBitsTest(ref test, numBits - 7, 1, 127); }); // long copy aligned Assert.Throws <ArgumentException>(() => { CopyBitsTest(ref test, 2, numBits - 1, 127); }); // long copy unaligned Assert.Throws <ArgumentException>(() => { CopyBitsTest(ref test, numBits - 1, 2, 127); }); // long copy unaligned test.Dispose(); }
public void UnsafeBitArray_Get_Set() { var numBits = 256; var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory); Assert.False(test.IsSet(123)); test.Set(123, true); Assert.True(test.IsSet(123)); Assert.False(test.TestAll(0, numBits)); Assert.False(test.TestNone(0, numBits)); Assert.True(test.TestAny(0, numBits)); Assert.AreEqual(1, test.CountBits(0, numBits)); Assert.False(test.TestAll(0, 122)); Assert.True(test.TestNone(0, 122)); Assert.False(test.TestAny(0, 122)); test.Clear(); Assert.False(test.IsSet(123)); Assert.AreEqual(0, test.CountBits(0, numBits)); test.SetBits(40, true, 4); Assert.AreEqual(4, test.CountBits(0, numBits)); test.SetBits(0, true, numBits); Assert.False(test.TestNone(0, numBits)); Assert.True(test.TestAll(0, numBits)); test.SetBits(0, false, numBits); Assert.True(test.TestNone(0, numBits)); Assert.False(test.TestAll(0, numBits)); test.SetBits(123, true, 7); Assert.True(test.TestAll(123, 7)); test.Dispose(); }
public void UnsafeBitArray_Copy() { var numBits = 512; var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory); CopyBitsTest(ref test, 1, 16, 12); // short up to 64-bits copy CopyBitsTest(ref test, 1, 80, 63); // short up to 64-bits copy CopyBitsTest(ref test, 1, 11, 12); // short up to 64-bits copy overlapped CopyBitsTest(ref test, 11, 1, 12); // short up to 64-bits copy overlapped CopyBitsTest(ref test, 1, 16, 76); // short up to 128-bits copy CopyBitsTest(ref test, 1, 80, 127); // short up to 128-bits copy CopyBitsTest(ref test, 1, 11, 76); // short up to 128-bits copy overlapped CopyBitsTest(ref test, 11, 1, 76); // short up to 128-bits copy overlapped CopyBitsTest(ref test, 1, 81, 255); // long copy aligned CopyBitsTest(ref test, 8, 0, 255); // long copy overlapped aligned CopyBitsTest(ref test, 1, 80, 255); // long copy unaligned CopyBitsTest(ref test, 80, 1, 255); // long copy overlapped unaligned test.Dispose(); }
public void CheckInternalConsistency(object[] managedComponentData) { Assert.IsTrue(ManagedChangesTracker.Empty); var managedComponentIndices = new UnsafeBitArray(m_ManagedComponentIndex, Allocator.Temp); Assert.IsTrue(managedComponentData.Length >= m_ManagedComponentIndex); for (int i = m_ManagedComponentIndex; i < managedComponentData.Length; ++i) { Assert.IsNull(managedComponentData[i]); } // Iterate by archetype var entityCountByArchetype = 0; for (var i = 0; i < m_Archetypes.Length; ++i) { var archetype = m_Archetypes.Ptr[i]; int managedTypeBegin = archetype->FirstManagedComponent; int managedTypeEnd = archetype->ManagedComponentsEnd; var countInArchetype = 0; for (var j = 0; j < archetype->Chunks.Count; ++j) { var chunk = archetype->Chunks.p[j]; Assert.IsTrue(chunk->Archetype == archetype); Assert.IsTrue(chunk->Capacity >= chunk->Count); Assert.AreEqual(chunk->Count, archetype->Chunks.GetChunkEntityCount(j)); var chunkEntities = (Entity *)chunk->Buffer; AssertEntitiesExist(chunkEntities, chunk->Count); if (!chunk->Locked) { if (chunk->Count < chunk->Capacity) { if (archetype->NumSharedComponents == 0) { Assert.IsTrue(chunk->ListWithEmptySlotsIndex >= 0 && chunk->ListWithEmptySlotsIndex < archetype->ChunksWithEmptySlots.Length); Assert.IsTrue( chunk == archetype->ChunksWithEmptySlots.Ptr[chunk->ListWithEmptySlotsIndex]); } else { Assert.IsTrue(archetype->FreeChunksBySharedComponents.Contains(chunk)); } } } countInArchetype += chunk->Count; if (chunk->Archetype->HasChunkHeader) // Chunk entities with chunk components are not supported { Assert.IsFalse(chunk->Archetype->HasChunkComponents); } Assert.AreEqual(chunk->Archetype->HasChunkComponents, chunk->metaChunkEntity != Entity.Null); if (chunk->metaChunkEntity != Entity.Null) { var chunkHeaderTypeIndex = TypeManager.GetTypeIndex <ChunkHeader>(); AssertEntitiesExist(&chunk->metaChunkEntity, 1); AssertEntityHasComponent(chunk->metaChunkEntity, chunkHeaderTypeIndex); var chunkHeader = *(ChunkHeader *)GetComponentDataWithTypeRO(chunk->metaChunkEntity, chunkHeaderTypeIndex); Assert.IsTrue(chunk == chunkHeader.ArchetypeChunk.m_Chunk); var metaChunk = GetChunk(chunk->metaChunkEntity); Assert.IsTrue(metaChunk->Archetype == chunk->Archetype->MetaChunkArchetype); } for (int iType = managedTypeBegin; iType < managedTypeEnd; ++iType) { var managedIndicesInChunk = (int *)(chunk->Buffer + archetype->Offsets[iType]); for (int ie = 0; ie < chunk->Count; ++ie) { var index = managedIndicesInChunk[ie]; if (index == 0) { continue; } Assert.IsTrue(index < m_ManagedComponentIndex, "Managed component index in chunk is out of range."); Assert.IsFalse(managedComponentIndices.IsSet(index), "Managed component index is used multiple times."); managedComponentIndices.Set(index, true); } } } Assert.AreEqual(countInArchetype, archetype->EntityCount); entityCountByArchetype += countInArchetype; } for (int i = 0; i < m_ManagedComponentIndex; ++i) { Assert.AreEqual(managedComponentData[i] != null, managedComponentIndices.IsSet(i)); } var freeManagedIndices = (int *)m_ManagedComponentFreeIndex.Ptr; var freeManagedCount = m_ManagedComponentFreeIndex.Length / sizeof(int); for (int i = 0; i < freeManagedCount; ++i) { var index = freeManagedIndices[i]; Assert.IsTrue(0 < index && index < m_ManagedComponentIndex, "Managed component index in free list is out of range."); Assert.IsFalse(managedComponentIndices.IsSet(index), "Managed component was marked as free but is used in chunk."); managedComponentIndices.Set(index, true); } Assert.IsTrue(m_ManagedComponentIndex - 1 == 0 || managedComponentIndices.TestAll(1, m_ManagedComponentIndex - 1), "Managed component index has leaked."); managedComponentIndices.Dispose(); // Iterate by free list Assert.IsTrue(m_EntityInChunkByEntity[m_NextFreeEntityIndex].Chunk == null); var entityCountByFreeList = EntitiesCapacity; int freeIndex = m_NextFreeEntityIndex; while (freeIndex != -1) { Assert.IsTrue(m_EntityInChunkByEntity[freeIndex].Chunk == null); Assert.IsTrue(freeIndex < EntitiesCapacity); freeIndex = m_EntityInChunkByEntity[freeIndex].IndexInChunk; entityCountByFreeList--; } // iterate by entities var entityCountByEntities = 0; var entityType = TypeManager.GetTypeIndex <Entity>(); for (var i = 0; i != EntitiesCapacity; i++) { var chunk = m_EntityInChunkByEntity[i].Chunk; if (chunk == null) { continue; } entityCountByEntities++; var archetype = m_ArchetypeByEntity[i]; Assert.AreEqual((IntPtr)archetype, (IntPtr)chunk->Archetype); Assert.AreEqual(entityType, archetype->Types[0].TypeIndex); Assert.IsTrue(m_EntityInChunkByEntity[i].IndexInChunk < m_EntityInChunkByEntity[i].Chunk->Count); var entity = *(Entity *)ChunkDataUtility.GetComponentDataRO(m_EntityInChunkByEntity[i].Chunk, m_EntityInChunkByEntity[i].IndexInChunk, 0); Assert.AreEqual(i, entity.Index); Assert.AreEqual(m_VersionByEntity[i], entity.Version); Assert.IsTrue(Exists(entity)); } Assert.AreEqual(entityCountByEntities, entityCountByArchetype); // Enabling this fails SerializeEntitiesWorksWithBlobAssetReferences. // There is some special entity 0 usage in the serialization code. // @TODO: Review with simon looks like a potential leak? //Assert.AreEqual(entityCountByEntities, entityCountByFreeList); }
// Simple grassfire to calculate shortest distance along path to target position for every position. // - i.e. breadth-first expansion from target position. static void CalculateShortestWalkableDistancesToTarget(NativeArray <int> targetDistances, int rowCount, byte *gridWalls, CartesianGridCoordinates targetPosition, CartesianGridOnCubeFace cubeFace, float4x4 *faceLocalToLocal) { var cellCount = rowCount * rowCount; var closed = new UnsafeBitArray(6 * cellCount, Allocator.Temp, NativeArrayOptions.ClearMemory); var pending = new UnsafeBitArray(6 * cellCount, Allocator.Temp, NativeArrayOptions.ClearMemory); var open = new UnsafeRingQueue <int>(6 * cellCount, Allocator.Temp); var faceIndex = cubeFace.Value; var faceTargetCellIndex = (targetPosition.y * rowCount) + targetPosition.x; for (int i = 0; i < targetDistances.Length; i++) { targetDistances[i] = -1; } var targetCellIndex = (faceIndex * cellCount) + faceTargetCellIndex; targetDistances[targetCellIndex] = 0; pending.Set(targetCellIndex, true); closed.Set(targetCellIndex, true); var cellIndexNorth = CartesianGridOnCubeUtility.CellIndexNorth(targetCellIndex, rowCount, faceLocalToLocal); var cellIndexSouth = CartesianGridOnCubeUtility.CellIndexSouth(targetCellIndex, rowCount, faceLocalToLocal); var cellIndexWest = CartesianGridOnCubeUtility.CellIndexWest(targetCellIndex, rowCount, faceLocalToLocal); var cellIndexEast = CartesianGridOnCubeUtility.CellIndexEast(targetCellIndex, rowCount, faceLocalToLocal); var rowStride = (rowCount + 1) / 2; var faceStride = rowCount * rowStride; var faceGridWallsOffset = faceIndex * faceStride; var validDirections = CartesianGridMovement.ValidDirections(targetPosition, rowCount, rowCount, gridWalls + faceGridWallsOffset); var validNorth = ((validDirections & (byte)CartesianGridDirectionBit.North) != 0); var validSouth = ((validDirections & (byte)CartesianGridDirectionBit.South) != 0); var validWest = ((validDirections & (byte)CartesianGridDirectionBit.West) != 0); var validEast = ((validDirections & (byte)CartesianGridDirectionBit.East) != 0); if (validNorth) { open.Enqueue(cellIndexNorth); pending.Set(cellIndexNorth, true); } if (validSouth) { open.Enqueue(cellIndexSouth); pending.Set(cellIndexSouth, true); } if (validWest) { open.Enqueue(cellIndexWest); pending.Set(cellIndexWest, true); } if (validEast) { open.Enqueue(cellIndexEast); pending.Set(cellIndexEast, true); } CalculateShortestWalkableDistancesToTargetInner(targetDistances, rowCount, gridWalls, faceLocalToLocal, pending, closed, open); closed.Dispose(); pending.Dispose(); open.Dispose(); }
public void UnsafeBitArray_Get_Set_Short() { var numBits = 31; var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory); Assert.False(test.IsSet(13)); test.Set(13, true); Assert.True(test.IsSet(13)); Assert.False(test.TestAll(0, numBits)); Assert.False(test.TestNone(0, numBits)); Assert.True(test.TestAny(0, numBits)); Assert.AreEqual(1, test.CountBits(0, numBits)); Assert.False(test.TestAll(0, 12)); Assert.True(test.TestNone(0, 12)); Assert.False(test.TestAny(0, 12)); test.Clear(); Assert.False(test.IsSet(13)); Assert.AreEqual(0, test.CountBits(0, numBits)); test.SetBits(4, true, 4); Assert.AreEqual(4, test.CountBits(0, numBits)); test.SetBits(0, true, numBits); Assert.False(test.TestNone(0, numBits)); Assert.True(test.TestAll(0, numBits)); test.SetBits(0, false, numBits); Assert.True(test.TestNone(0, numBits)); Assert.False(test.TestAll(0, numBits)); test.SetBits(13, true, 7); Assert.True(test.TestAll(13, 7)); test.Clear(); test.SetBits(4, true, 4); Assert.AreEqual(false, test.IsSet(3)); Assert.AreEqual(true, test.TestAll(4, 4)); Assert.AreEqual(false, test.IsSet(18)); Assert.AreEqual(4, test.CountBits(4, 4)); Assert.AreEqual(4, test.CountBits(0, numBits)); test.Clear(); test.SetBits(5, true, 2); Assert.AreEqual(false, test.IsSet(4)); Assert.AreEqual(true, test.TestAll(5, 2)); Assert.AreEqual(false, test.IsSet(17)); Assert.AreEqual(2, test.CountBits(4, 4)); Assert.AreEqual(2, test.CountBits(0, numBits)); test.Clear(); test.SetBits(6, true, 4); Assert.AreEqual(false, test.IsSet(5)); Assert.AreEqual(true, test.TestAll(6, 4)); Assert.AreEqual(false, test.IsSet(10)); Assert.AreEqual(4, test.CountBits(6, 4)); Assert.AreEqual(4, test.CountBits(0, numBits)); test.Dispose(); }