public unsafe void UnsafeBitArray_Find_With_Begin_End() { var numBits = 512; using (var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory)) { Assert.AreEqual(0, test.Find(0, 2, 1)); Assert.AreEqual(1, test.Find(1, 2, 1)); test.SetBits(0, true, 6); Assert.AreEqual(int.MaxValue, test.Find(0, 2, 1)); for (var j = 0; j < 64; ++j) { for (var i = 0; i < 256; ++i) { var numBitsToFind = 7 + i; var padding = 11; var begin = 37 + j; var end = begin + padding + numBitsToFind; var count = end - begin; test.Clear(); test.SetBits(begin, true, count); test.SetBits(begin + padding + 1, false, numBitsToFind - 1); Assert.AreEqual(begin + padding + 1, test.Find(begin, count, numBitsToFind - 1)); //, $"{j}/{i}: begin {begin}, end {end}, count {count}, numBitsToFind {numBitsToFind}"); Assert.AreEqual(int.MaxValue, test.Find(begin, count, numBitsToFind)); //, $"{j}/{i}: begin {begin}, end {end}, count {count}, numBitsToFind {numBitsToFind}"); } } } }
public unsafe void UnsafeBitArray_Find_Throws() { var numBits = 512; using (var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory)) { Assert.Throws <ArgumentException>(() => { test.Find(0, 0, 1); }); // empty range Assert.Throws <ArgumentException>(() => { test.Find(0, 1, 0); }); // zero bits Assert.Throws <ArgumentException>(() => { test.Find(0, 1, 2); }); // numBits is larger than range Assert.Throws <ArgumentException>(() => { test.Find(10, 0, 0); }); // empty range, numBits is less than 1 Assert.Throws <ArgumentException>(() => { test.Find(1, 10, -2); }); // numBits can't be negative } }
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 void UnsafeBitArray_FindLastUnsetBit([NUnit.Framework.Range(1, 64)] int numBits) { using (var bits = new UnsafeBitArray(numBits, Allocator.Persistent)) { // Set all bits to one then unset a single bit to find. for (int i = 0; i < numBits; ++i) { bits.SetBits(0, true, numBits); bits.Set(i, false); Assert.AreEqual(i, bits.Find(0, 1)); } } }
void findWithPattern(ref UnsafeBitArray test, byte pattern, int numBits) { for (int pos = 0; pos < test.Length; pos += 8) { test.SetBits(pos, pattern, 8); } var bitCount = math.countbits((int)pattern); var numEmptyBits = test.Length - (test.Length / 8 * bitCount); for (int i = 0; i < numEmptyBits; i += numBits) { var pos = test.Find(0, numBits); Assert.AreNotEqual(int.MaxValue, pos, $"{i}"); test.SetBits(pos, true, numBits); } Assert.True(test.TestAll(0, test.Length)); }
public unsafe void UnsafeBitArray_Find() { var numBits = 512; using (var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory)) { test.SetBits(0, true, 11); for (var i = 0; i < 256; ++i) { Assert.AreEqual(11, test.Find(0, i + 1)); } for (var j = 0; j < 64; ++j) { for (var i = 0; i < 256; ++i) { var numBitsToFind = 7 + i; var pos = 37 + j; test.SetBits(0, true, test.Length); test.SetBits(pos, false, numBitsToFind); Assert.AreEqual(pos, test.Find(0, numBitsToFind), $"{j}/{i}: pos {pos}, numBitsToFind {numBitsToFind}"); Assert.AreEqual(pos, test.Find(pos, numBitsToFind), $"{j}/{i}:pos {pos}, numBitsToFind {numBitsToFind}"); Assert.AreEqual(pos, test.Find(0, numBitsToFind), $"{j}/{i}: pos {pos}, numBitsToFind {numBitsToFind}"); Assert.AreEqual(pos, test.Find(pos, numBitsToFind), $"{j}/{i}: pos {pos}, numBitsToFind {numBitsToFind}"); Assert.IsTrue(test.TestNone(test.Find(0, numBitsToFind), numBitsToFind)); Assert.AreEqual(int.MaxValue, test.Find(pos + 1, numBitsToFind), $"{j}/{i}: pos {pos}, numBitsToFind {numBitsToFind}"); } } } }
/// <summary> /// Performs a linear search for a consecutive sequence of 0s of a given length in the bit array. /// </summary> /// <param name="pos">Position to start search in bit array.</param> /// <param name="numBits">Number of 0-bits to find.</param> /// <returns>Returns index of first bit of 0-bit range, or int.MaxValue if 0-bit range is not found.</returns> public int Find(int pos, int numBits) { CheckRead(); return(m_BitArray.Find(pos, numBits)); }