BitRange

Range over bit set. Each element is the bit number that is set.

This is more efficient than testing each bit in a sparsely populated bit set. Note that the first bit in the bit set would be bit 0.

Constructors

this
this(const(size_t)* bitarr, size_t numBits)

Construct a BitRange.

Members

Functions

empty
bool empty()

Range functions

front
size_t front()
popFront
void popFront()

Range functions

Manifest constants

bitsPerWord
enum bitsPerWord;

Number of bits in each size_t

Examples

import core.stdc.stdlib : malloc, free;
import core.stdc.string : memset;

// initialize a bit array
enum nBytes = (100 + BitRange.bitsPerWord - 1) / 8;
size_t *bitArr = cast(size_t *)malloc(nBytes);
scope(exit) free(bitArr);
memset(bitArr, 0, nBytes);

// set some bits
bts(bitArr, 48);
bts(bitArr, 24);
bts(bitArr, 95);
bts(bitArr, 78);

enum sum = 48 + 24 + 95 + 78;

// iterate
size_t testSum;
size_t nBits;
foreach (b; BitRange(bitArr, 100))
{
    testSum += b;
    ++nBits;
}

assert(testSum == sum);
assert(nBits == 4);

Meta