Module agora.common.BitMask

This is an implementation of a bitmask that once initialized will have a fixed number of bits for holding true / false (1 / 0) values. It allocates the required number of ubytes in the constructor and does not allow reading or writing to bits beyond the fixed count which is set during construction. This type is created for use as the validators signing bitmask and any changes should ensure that it does not compromise that use.

Example

More set than unset

auto bitmask = BitMask.fromString("01011");
only(1,3,4).each!(i => assert(bitmask[i]));
only(0,2).each!(i => assert(!bitmask[i]));
assert(bitmask.length == 5);

Example

Test with more than 8 bits

auto bitmask = BitMask.fromString("111011111");
assert(bitmask.length == 9);
assert(bitmask.toString == "111011111");
auto bitmask_copy = BitMask.fromString("001011101");
bitmask_copy = bitmask;
assert(!bitmask_copy[3]);
only(0,1,2,4,5).each!(i => assert(bitmask_copy[i]));

Example

Test serialization

auto bitmask = BitMask(12);
testSymmetry(bitmask);
bitmask[1] = true;
auto bitmask2 = bitmask.serializeFull.deserializeFull!BitMask;
assert(bitmask2.length == bitmask.length);
assert(bitmask2.setCount == bitmask.setCount);
assert(bitmask2 == bitmask);

Example

Test percentage

auto bitmask50 = BitMask.fromString("010110");
assert(bitmask50.percentage == 50);
auto bitmask85 = BitMask.fromString("0111111");
assert(bitmask85.percentage == 85);
auto bitmask0 = BitMask.fromString("0000000");
assert(bitmask0.percentage == 0);
auto bitmask100 = BitMask.fromString("1111111111111111111111111");
assert(bitmask100.percentage == 100);