countOnes

Undocumented in source. Be warned that the author may not have intended to support it.
@safe pure nothrow @nogc @trusted
size_t
countOnes
(
Blocks
bool blockAlignedLength = false
)
(
const scope auto ref Blocks blocks
,
size_t length
)
if (
isBlocks!Blocks
)

Examples

Test countOnes with full blocks.

alias Block = size_t;
enum bitsPerBlock = 8*Block.sizeof;

enum blockCount = 3;
Block[blockCount] x;
const length = 8*x.sizeof;
assert(countOnes(x, length) == 0);

x[0] = 1;
assert(countOnes(x, length) == 1);

x[0] = 1+2;
assert(countOnes(x, length) == 2);

x[0] = 1+2;
x[1] = 1+2;
assert(countOnes(x, length) == 4);

x[0] = 1+2;
x[1] = 1+2;
x[2] = 1+2;
assert(countOnes(x, length) == 6);

Test countOnes with partial blocks.

alias Block = size_t;
static void test(uint blockCount)()
{
    Block[blockCount] x;
    foreach (const length; 1 .. 8*x.sizeof)
    {
        assert(countOnes(x, length) == 0);

        x[0] |= 1UL << (length-1);
        assert(countOnes(x, length) == 1);
        x[0] = 0;
    }
}
test!1();
test!2();
test!3();

Meta