StaticBitArray

Undocumented in source.

Members

Aliases

Block
alias Block = DefaultBlock
Undocumented in source.

Functions

indexOfFirstOne
size_t indexOfFirstOne()

Find index of first set (one) bit or typeof(return).max if no bit set.

indexOfFirstZero
size_t indexOfFirstZero()

Find index of first cleared (zero) bit or typeof(return).max if no bit set.

opIndex
bool opIndex(size_t idx)

Gets the idx'th bit.

opIndexAssign
bool opIndexAssign(bool b, size_t idx)

Sets the idx'th bit.

reset
void reset()

Reset all bits (to zero).

Manifest constants

bitsPerBlock
enum bitsPerBlock;

Number of bits per Block.

blockCount
enum blockCount;

Number of blocks of type Block.

length
enum length;

Number of bits.

Examples

enum blockCount = 2;
enum length = blockCount * 8*DefaultBlock.sizeof - 1; // 2 blocks minus one

StaticBitArray!(length) x;
static assert(x.blockCount == blockCount);

// import std.exception: assertThrown;
// import core.exception : AssertError;
// assertThrown!AssertError(x[length] = false);

x[length/2 - 1] = true;
assert(x[length/2 - 1]);

x[length/2 - 1] = false;
assert(!x[length/2 - 1]);

x[length - 1] = true;
assert(x[length - 1]);

x[length - 1] = false;
assert(!x[length - 1]);

Test indexOfFirstZero for multi set zeros.

static void test(bool blockAlignedLength)()
{
    static if (blockAlignedLength)
    {
        const n = 2 * 8*DefaultBlock.sizeof;
    }
    else
    {
        const n = 2 * 8*DefaultBlock.sizeof + 1;
    }
    alias BA = StaticBitArray!(n);

    auto a = BA();

    a[0] = false;
    a[BA.bitsPerBlock/2] = false;
    a[BA.bitsPerBlock - 1] = false;
    assert(a.indexOfFirstZero == 0);
}
test!(false)();
test!(true)();

Test indexOfFirstOne for multi set ones.

static void test(bool blockAlignedLength)()
{
    static if (blockAlignedLength)
    {
        const n = 2 * 8*Block.sizeof;
    }
    else
    {
        const n = 2 * 8*DefaultBlock.sizeof + 1;
    }
    alias BA = StaticBitArray!(n);

    auto a = BA();

    a[0] = true;
    a[BA.bitsPerBlock/2] = true;
    a[BA.bitsPerBlock - 1] = true;
    assert(a.indexOfFirstOne == 0);
}
test!(false)();

Meta