Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions src/core/bitop.d
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,143 @@ int bts(size_t* p, size_t bitnum) pure @system;
assert(array[1] == 0x100);
}

/**
* 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.
*/
struct BitRange
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

{
/// Number of bits in each size_t
enum bitsPerWord = size_t.sizeof * 8;

private
{
const(size_t)*bits; // points at next word of bits to check
size_t cur; // needed to allow searching bits using bsf
size_t idx; // index of current set bit
size_t len; // number of bits in the bit set.
}
@nogc nothrow pure:

/**
* Construct a BitRange.
*
* Params:
* bitarr - The array of bits to iterate over
* numBits - The total number of valid bits in the given bit array
*/
this(const(size_t)* bitarr, size_t numBits) @system
{
bits = bitarr;
len = numBits;
if (len)
{
// prime the first bit
cur = *bits++ ^ 1;
popFront();
}
}

/// Range functions
size_t front()
{
assert(!empty);
return idx;
}

/// ditto
bool empty() const
{
return idx >= len;
}

/// ditto
void popFront() @system
{
// clear the current bit
auto curbit = idx % bitsPerWord;
cur ^= size_t(1) << curbit;
if(!cur)
{
// find next size_t with set bit
idx -= curbit;
while (!cur)
{
if ((idx += bitsPerWord) >= len)
// now empty
return;
cur = *bits++;
}
idx += bsf(cur);
}
else
{
idx += bsf(cur) - curbit;
}
}
}

///
@system unittest
{
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);
}

@system unittest
{
void testIt(size_t numBits, size_t[] bitsToTest...)
{
import core.stdc.stdlib : malloc, free;
import core.stdc.string : memset;
immutable numBytes = (numBits + size_t.sizeof * 8 - 1) / 8;
size_t* bitArr = cast(size_t *)malloc(numBytes);
scope(exit) free(bitArr);
memset(bitArr, 0, numBytes);
foreach(b; bitsToTest)
bts(bitArr, b);
auto br = BitRange(bitArr, numBits);
foreach(b; bitsToTest)
{
assert(!br.empty);
assert(b == br.front);
br.popFront();
}
assert(br.empty);
}

testIt(100, 0, 1, 31, 63, 85);
testIt(100, 6, 45, 89, 92, 99);
}

/**
* Swaps bytes in a 4 byte uint end-to-end, i.e. byte 0 becomes
* byte 3, byte 1 becomes byte 2, byte 2 becomes byte 1, byte 3
Expand Down
Loading