Find index of first set (one) bit or typeof(return).max if no bit set.
Find index of first cleared (zero) bit or typeof(return).max if no bit set.
Gets the idx'th bit.
Sets the idx'th bit.
Reset all bits (to zero).
Number of bits per Block.
Number of blocks of type Block.
Number of bits.
1 enum blockCount = 2; 2 enum length = blockCount * 8*DefaultBlock.sizeof - 1; // 2 blocks minus one 3 4 StaticBitArray!(length) x; 5 static assert(x.blockCount == blockCount); 6 7 // import std.exception: assertThrown; 8 // import core.exception : AssertError; 9 // assertThrown!AssertError(x[length] = false); 10 11 x[length/2 - 1] = true; 12 assert(x[length/2 - 1]); 13 14 x[length/2 - 1] = false; 15 assert(!x[length/2 - 1]); 16 17 x[length - 1] = true; 18 assert(x[length - 1]); 19 20 x[length - 1] = false; 21 assert(!x[length - 1]);
Test indexOfFirstZero for multi set zeros.
1 static void test(bool blockAlignedLength)() 2 { 3 static if (blockAlignedLength) 4 { 5 const n = 2 * 8*DefaultBlock.sizeof; 6 } 7 else 8 { 9 const n = 2 * 8*DefaultBlock.sizeof + 1; 10 } 11 alias BA = StaticBitArray!(n); 12 13 auto a = BA(); 14 15 a[0] = false; 16 a[BA.bitsPerBlock/2] = false; 17 a[BA.bitsPerBlock - 1] = false; 18 assert(a.indexOfFirstZero == 0); 19 } 20 test!(false)(); 21 test!(true)();
Test indexOfFirstOne for multi set ones.
1 static void test(bool blockAlignedLength)() 2 { 3 static if (blockAlignedLength) 4 { 5 const n = 2 * 8*Block.sizeof; 6 } 7 else 8 { 9 const n = 2 * 8*DefaultBlock.sizeof + 1; 10 } 11 alias BA = StaticBitArray!(n); 12 13 auto a = BA(); 14 15 a[0] = true; 16 a[BA.bitsPerBlock/2] = true; 17 a[BA.bitsPerBlock - 1] = true; 18 assert(a.indexOfFirstOne == 0); 19 } 20 test!(false)();