The OpenD Programming Language

BitArray

A dynamic array of bits. Each bit in a BitArray can be manipulated individually or by the standard bitwise operators &, |, ^, ~, >>, << and also by other effective member functions; most of them work relative to the BitArray's dimension (see dim), instead of its length.

Constructors

this
this(bool[] ba)

Creates a BitArray from a bool array, such that bool values read from left to right correspond to subsequent bits in the BitArray.

this
this(void[] v, size_t numbits)

Creates a BitArray from the raw contents of the source array. The source array is not copied but simply acts as the underlying array of bits, which stores data as size_t units.

Members

Functions

count
size_t count()

Counts all the set bits in the BitArray

flip
void flip()

Flips all the bits in the BitArray

flip
void flip(size_t pos)

Flips a single bit, specified by pos

opApply
int opApply(int delegate(ref bool) dg)
int opApply(int delegate(bool) dg)
int opApply(int delegate(size_t, ref bool) dg)
int opApply(int delegate(size_t, bool) dg)

Support for foreach loops for BitArray.

opBinary
BitArray opBinary(BitArray e2)

Support for binary bitwise operators for BitArray.

opBinary
BitArray opBinary(bool b)
BitArray opBinary(BitArray b)
opBinaryRight
BitArray opBinaryRight(bool b)

Support for binary operator ~ for BitArray.

opCast
inout(void)[] opCast()

Convert to void[].

opCast
inout(size_t)[] opCast()

Convert to size_t[].

opCmp
int opCmp(BitArray a2)

Supports comparison operators for BitArray.

opEquals
bool opEquals(BitArray a2)

Support for operators == and != for BitArray.

opIndex
bool opIndex(size_t i)

Gets the i'th bit in the BitArray.

opIndexAssign
bool opIndexAssign(bool b, size_t i)

Sets the i'th bit in the BitArray.

opOpAssign
BitArray opOpAssign(BitArray e2)

Support for operator op= for BitArray.

opOpAssign
BitArray opOpAssign(bool b)
BitArray opOpAssign(BitArray b)

Support for operator ~= for BitArray. Warning: This will overwrite a bit in the final word of the current underlying data regardless of whether it is shared between BitArray objects. i.e. D dynamic array concatenation semantics are not followed

opOpAssign
void opOpAssign(size_t nbits)

Operator <<= support.

opOpAssign
void opOpAssign(size_t nbits)

Operator >>= support.

opSliceAssign
void opSliceAssign(bool val)

Sets all the values in the BitArray to the value specified by val.

opSliceAssign
void opSliceAssign(bool val, size_t start, size_t end)

Sets the bits of a slice of BitArray starting at index start and ends at index ($D end - 1) with the values specified by val.

opUnary
BitArray opUnary()

Support for unary operator ~ for BitArray.

toHash
size_t toHash()

Support for hashing for BitArray.

toString
void toString(W sink, FormatSpec!char fmt)

Return a string representation of this BitArray.

Properties

bitsSet
auto bitsSet [@property getter]

Return a lazy range of the indices of set bits.

dim
size_t dim [@property getter]
dup
BitArray dup [@property getter]

Duplicates the BitArray and its contents.

length
size_t length [@property getter]
length
size_t length [@property setter]

Sets the amount of bits in the BitArray. Warning: increasing length may overwrite bits in the final word of the current underlying data regardless of whether it is shared between BitArray objects. i.e. D dynamic array extension semantics are not followed.

reverse
BitArray reverse [@property getter]

Reverses the bits of the BitArray.

sort
BitArray sort [@property getter]

Sorts the BitArray's elements.

Examples

Slicing & bitsSet

import std.algorithm.comparison : equal;
import std.range : iota;

bool[] buf = new bool[64 * 3];
buf[0 .. 64] = true;
BitArray b = BitArray(buf);
assert(b.bitsSet.equal(iota(0, 64)));
b <<= 64;
assert(b.bitsSet.equal(iota(64, 128)));

Concatenation and appending

import std.algorithm.comparison : equal;

auto b = BitArray([1, 0]);
b ~= true;
assert(b[2] == 1);
b ~= BitArray([0, 1]);
auto c = BitArray([1, 0, 1, 0, 1]);
assert(b == c);
assert(b.bitsSet.equal([0, 2, 4]));

Bit flipping

import std.algorithm.comparison : equal;

auto b = BitArray([1, 1, 0, 1]);
b &= BitArray([0, 1, 1, 0]);
assert(b.bitsSet.equal([1]));
b.flip;
assert(b.bitsSet.equal([0, 2, 3]));

String format of bitarrays

import std.format : format;
auto b = BitArray([1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);
assert(format("%b", b) == "1_00001111_00001111");
import std.format : format;

BitArray b;

b = BitArray([]);
assert(format("%s", b) == "[]");
assert(format("%b", b) is null);

b = BitArray([1]);
assert(format("%s", b) == "[1]");
assert(format("%b", b) == "1");

b = BitArray([0, 0, 0, 0]);
assert(format("%b", b) == "0000");

b = BitArray([0, 0, 0, 0, 1, 1, 1, 1]);
assert(format("%s", b) == "[0, 0, 0, 0, 1, 1, 1, 1]");
assert(format("%b", b) == "00001111");

b = BitArray([0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);
assert(format("%s", b) == "[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]");
assert(format("%b", b) == "00001111_00001111");

b = BitArray([1, 0, 0, 0, 0, 1, 1, 1, 1]);
assert(format("%b", b) == "1_00001111");

b = BitArray([1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);
assert(format("%b", b) == "1_00001111_00001111");

Meta