smjg.libs.util.bitarray

The smjg.libs.util.bitarray module provides data types to represent bit pointers and bit arrays that don't necessarily begin on byte boundaries.

In terms of the internal representation, bits are indexed within bytes in a way that corresponds to the platform byte order. So on little-endian platforms, bit 0 is the least significant bit, and bit 7 the most significant. On big-endian platforms, the convention is reversed. This is significant if a bit array is constructed from or converted to an integer array, and has the effect that the indexing order of bits matches (in one direction or the other) the significance order of bits in the integers involved.

Todo:
Test on big-endian platforms.
BUGS:
Not yet supported on 64-bit platforms.
struct BitPointer;

The BitPointer structure implements a bit pointer that can point to any bit within a byte.

inout this(inout(BitArray) data);

Constructs a new BitPointer to point to the beginning of a BitArray.

inout this(inout(void)* data);

Constructs a new BitPointer to point to the first bit of a byte.

const @property bool value();
@property bool value(bool b);

Dereferences the bit pointer.

const bool opIndex(int i);
bool opIndexAssign(bool b, int i);

Accesses the bit at position i relative to this bit pointer.

inout inout(BitArray) opSlice(int begin, int end);
BitArray opSliceAssign(bool value, int begin, int end);
BitArray opSliceAssign(in BitArray value, int begin, int end);

Accesses the specified range of bits relative to this bit pointer.

Todo:
optimise assignment.
inout inout(BitPointer) opAdd(int i);
Returns:
this pointer incremented by i bits.
inout inout(BitPointer) opSub(int i);
Returns:
this pointer decremented by i bits.
const int opSub(in BitPointer p);
Returns:
the displacement in bits of this BitPointer from p.
const int opCmp(in BitPointer p);

Compares this BitPointer with another in the address to which they point.

inout inout(ubyte)* opCast(T : inout(ubyte)*)();
Returns:
the address of the byte within which this BitPointer points.
inout @property inout(ubyte)* bytePointer();
Returns:
the address of the byte within which this BitPointer points.
Since:
0.03
const @property ubyte bitOffset();
Returns:
the bit offset of the bit pointed to within its byte, which is between 0 and 7.
Since:
0.03
struct BitArray;

The BitArray structure implements a bit array that can start at any point within a byte.

this(int len);

Constructs a new BitArray with the length len.

this(const(bool[]) bits);

Constructs a new BitArray from an array of booleans.

inout this(inout(void[]) data);

Constructs a new BitArray to wrap a D array.

const int opEquals(in BitArray ba);

Compares two BitArrays for equality of the data they contain.

Todo:
optimise for arrays byte-aligned with each other.
const bool opIndex(int i);
bool opIndexAssign(bool b, int i);

Accesses the bit at position i.

inout inout(BitArray) opSlice(int begin, int end);
BitArray opSliceAssign(bool value, int begin, int end);
BitArray opSliceAssign(in BitArray value, int begin, int end);

Accesses the specified slice of this array.

BitArray opSliceAssign(bool value);

Assigns value to all elements of this BitArray.

BitArray opSliceAssign(in BitArray value);

Assigns a BitArray in-place to this one.

inout inout(BitArray) opCat(in BitArray value);

Concatenates two BitArray objects.

inout inout(BitArray) opCat(bool value);

Concatenates this BitArray with a bit.

BitArray opCatAssign(in BitArray value);
BitArray opCatAssign(bool value);

Appends value to this BitArray.

inout @property inout(BitPointer) ptr();

A BitPointer pointing to the first element of the array.

int opApply(int delegate(ref bool) dg);
const int opApply(int delegate(bool) dg);
int opApply(int delegate(int, ref bool) dg);
const int opApply(int delegate(int, bool) dg);

Iterates over the elements of this BitArray.

inout inout(BitArray) opUnary(string Op : "~")();

Inverts the bits of the array.

BitArray opOpAssign(string Op)(in BitArray ba);

Modifies this array by performing a bitwise operation with ba.

Todo:
optimise
BitArray opOpAssign(string Op)(in BitArray ba);

Modifies this array by performing a bitwise AND-NOT operation with ba.

Todo:
optimise
inout inout(BitArray) opBinary(string Op)(in BitArray ba);

Performs a bitwise operation on this array and ba.

const @property BitArray dup();
Returns:
a byte-aligned copy of this array.
const @property immutable(BitArray) idup();
Returns:
an immutable, byte-aligned copy of this array.
inout @property inout(BitArray) byteAlign();
Returns:
this BitArray, if it is already byte aligned, otherwise a byte-aligned copy.
const @property int length();
@property int length(int len);
Returns:
the length in bits of this BitArray.
@property string toString();
Returns:
a string representation of this BitArray.
string toStringBySpec(char falseAs, char trueAs);
string toStringBySpec(dchar falseAs, dchar trueAs);

Creates a custom string representation of this array.

Params:
char falseAs character used to represent a false (0) byte.
char trueAs character used to represent a true (1) byte.
inout inout(U)[] to(T : U[], U)();
inout inout(U)[] toRev(T : U[], U)();
alias toLittleEndian;
alias toBigEndian;

Converts this BitArray to an integer array.

to uses the native bit order; toRev uses the reverse of this.

All of these functions return an in-place view of the bit array if possible (i.e. native bit order, and begins and ends on byte boundaries), otherwise a copy.