static void CopyBitsTest(ref UnsafeBitArray test, int dstPos, int srcPos, int numBits) { for (int pos = 0; pos < test.Length; pos += 64) { test.SetBits(pos, 0xaaaaaaaaaaaaaaaaul, 64); } test.SetBits(srcPos, true, numBits); test.Copy(dstPos, srcPos, numBits); Assert.AreEqual(true, test.TestAll(dstPos, numBits)); for (int pos = 0; pos < test.Length; ++pos) { if ((pos >= dstPos && pos < dstPos + numBits) || (pos >= srcPos && pos < srcPos + numBits)) { Assert.AreEqual(true, test.IsSet(pos)); } else { Assert.AreEqual((0 != (pos & 1)), test.IsSet(pos)); } } test.Clear(); }
static void CopyBitsTest(ref UnsafeBitArray dstBitArray, int dstPos, ref UnsafeBitArray srcBitArray, int srcPos, int numBits) { for (int pos = 0; pos < dstBitArray.Length; pos += 64) { dstBitArray.SetBits(pos, 0xaaaaaaaaaaaaaaaaul, 64); } srcBitArray.SetBits(srcPos, true, numBits); dstBitArray.Copy(dstPos, ref srcBitArray, srcPos, numBits); Assert.AreEqual(true, dstBitArray.TestAll(dstPos, numBits)); for (int pos = 0; pos < dstBitArray.Length; ++pos) { if ((pos >= dstPos && pos < dstPos + numBits) || (pos >= srcPos && pos < srcPos + numBits)) { Assert.AreEqual(true, dstBitArray.IsSet(pos)); } else { Assert.AreEqual((0 != (pos & 1)), dstBitArray.IsSet(pos)); } } dstBitArray.Clear(); }
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 unsafe void UnsafeBitArray_Throws() { var numBits = 256; using (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); }); // GetBits numBits must be 1-64. Assert.Throws <ArgumentException>(() => { test.GetBits(0, 0); }); Assert.Throws <ArgumentException>(() => { test.GetBits(0, 65); }); Assert.DoesNotThrow(() => { test.GetBits(63, 2); }); Assert.Throws <ArgumentException>(() => { new UnsafeBitArray(null, 7); /* check sizeInBytes must be multiple of 8-bytes. */ }); } }
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(); }
/// <summary> /// Returns true is bit at position is set. /// </summary> /// <param name="pos">Position in bit array.</param> /// <returns>Returns true if bit is set.</returns> public bool IsSet(int pos) { CheckRead(); return(m_BitArray.IsSet(pos)); }
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); }
static void CalculateShortestWalkableDistancesToTargetInner(NativeArray <int> targetDistances, int rowCount, byte *gridWalls, float4x4 *faceLocalToLocal, UnsafeBitArray pending, UnsafeBitArray closed, UnsafeRingQueue <int> open) { var cellCount = rowCount * rowCount; var maxPathLength = 6 * (cellCount + 1); while (open.Length > 0) { var cellIndex = open.Dequeue(); var cellPosition = CartesianGridOnCubeUtility.CellFaceCoordinates(cellIndex, rowCount); var faceIndex = CartesianGridOnCubeUtility.CellFaceIndex(cellIndex, rowCount); var rowStride = (rowCount + 1) / 2; var faceStride = rowCount * rowStride; var faceGridWallsOffset = faceIndex * faceStride; var validDirections = CartesianGridMovement.ValidDirections(cellPosition, 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); var cellIndexNorth = CartesianGridOnCubeUtility.CellIndexNorth(cellIndex, rowCount, faceLocalToLocal); var cellIndexSouth = CartesianGridOnCubeUtility.CellIndexSouth(cellIndex, rowCount, faceLocalToLocal); var cellIndexWest = CartesianGridOnCubeUtility.CellIndexWest(cellIndex, rowCount, faceLocalToLocal); var cellIndexEast = CartesianGridOnCubeUtility.CellIndexEast(cellIndex, rowCount, faceLocalToLocal); var distanceNorth = maxPathLength; var distanceSouth = maxPathLength; var distanceEast = maxPathLength; var distanceWest = maxPathLength; var closedNorth = false; var closedSouth = false; var closedWest = false; var closedEast = false; if (validNorth) { if (closed.IsSet(cellIndexNorth)) { distanceNorth = targetDistances[cellIndexNorth]; closedNorth = true; } else if (!pending.IsSet(cellIndexNorth)) { open.Enqueue(cellIndexNorth); pending.Set(cellIndexNorth, true); } } if (validSouth) { if (closed.IsSet(cellIndexSouth)) { distanceSouth = targetDistances[cellIndexSouth]; closedSouth = true; } else if (!pending.IsSet(cellIndexSouth)) { open.Enqueue(cellIndexSouth); pending.Set(cellIndexSouth, true); } } if (validWest) { if (closed.IsSet(cellIndexWest)) { distanceWest = targetDistances[cellIndexWest]; closedWest = true; } else if (!pending.IsSet(cellIndexWest)) { open.Enqueue(cellIndexWest); pending.Set(cellIndexWest, true); } } if (validEast) { if (closed.IsSet(cellIndexEast)) { distanceEast = targetDistances[cellIndexEast]; closedEast = true; } else if (!pending.IsSet(cellIndexEast)) { open.Enqueue(cellIndexEast); pending.Set(cellIndexEast, true); } } var closedNeighbor = closedNorth || closedSouth || closedWest || closedEast; Assert.IsTrue(closedNeighbor); var bestDist = math.cmin(new int4(distanceNorth, distanceSouth, distanceEast, distanceWest)) + 1; Assert.IsFalse(bestDist > (maxPathLength + 1)); targetDistances[cellIndex] = bestDist; closed.Set(cellIndex, true); } }
static void CalculateShortestWalkableDistancesToTargetInner(int rowCount, int colCount, byte *gridWalls, NativeArray <int> targetDistances, UnsafeBitArray pending, UnsafeBitArray closed, UnsafeRingQueue <int> open) { var cellCount = rowCount * colCount; while (open.Count > 0) { var cellIndex = open.Dequeue(); var y = cellIndex / colCount; var x = cellIndex - (y * colCount); var cellPosition = new CartesianGridCoordinates { x = (short)x, y = (short)y }; var validDirections = CartesianGridMovement.ValidDirections(cellPosition, 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); var cellIndexNorth = cellIndex + colCount; var cellIndexSouth = cellIndex - colCount; var cellIndexWest = cellIndex - 1; var cellIndexEast = cellIndex + 1; var distanceNorth = cellCount + 1; var distanceSouth = cellCount + 1; var distanceEast = cellCount + 1; var distanceWest = cellCount + 1; if (validNorth) { if (closed.IsSet(cellIndexNorth)) { distanceNorth = targetDistances[cellIndexNorth]; } else if (!pending.IsSet(cellIndexNorth)) { open.Enqueue(cellIndexNorth); pending.Set(cellIndexNorth, true); } } if (validSouth) { if (closed.IsSet(cellIndexSouth)) { distanceSouth = targetDistances[cellIndexSouth]; } else if (!pending.IsSet(cellIndexSouth)) { open.Enqueue(cellIndexSouth); pending.Set(cellIndexSouth, true); } } if (validWest) { if (closed.IsSet(cellIndexWest)) { distanceWest = targetDistances[cellIndexWest]; } else if (!pending.IsSet(cellIndexWest)) { open.Enqueue(cellIndexWest); pending.Set(cellIndexWest, true); } } if (validEast) { if (closed.IsSet(cellIndexEast)) { distanceEast = targetDistances[cellIndexEast]; } else if (!pending.IsSet(cellIndexEast)) { open.Enqueue(cellIndexEast); pending.Set(cellIndexEast, true); } } var bestDist = math.cmin(new int4(distanceNorth, distanceSouth, distanceEast, distanceWest)) + 1; targetDistances[cellIndex] = bestDist; closed.Set(cellIndex, true); } }
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(); }