tango.core.BitArray

This module contains a packed bit array implementation in the style of D's built-in dynamic arrays.

License:

BSD style: see license.txt

Authors:

Walter Bright, Sean Kelly
struct BitArray
This struct represents an array of boolean values, each of which occupy one bit of memory for storage. Thus an array of 32 bits would occupy the same space as one integer value. The typical array operations--such as indexing and sorting--are supported, as well as bitwise operations such as and, or, xor, and complement.
this(size_t _len, size_t* _ptr)
This initializes a BitArray of bits.length bits, where each bit value matches the corresponding boolean value in bits.

Parameters:

bitsThe initialization value.

Returns:

A BitArray with the same number and sequence of elements as bits.
size_t length() [@property, const]
Get the number of bits in this array.

Returns:

The number of bits in this array.
void length(const size_t newlen) [@property]
Resizes this array to newlen bits. If newlen is larger than the current length, the new bits will be initialized to zero.

Parameters:

newlenThe number of bits this array should contain.
size_t dim() [@property]
Gets the length of a uint array large enough to hold all stored bits.

Returns:

The size a uint array would have to be to store this array.
BitArray dup() [@property]
Duplicates this array, much like the dup property for built-in arrays.

Returns:

A duplicate of this array.
this()
Resets the length of this array to bits.length and then initializes this
Resizes this array to hold bits.length bits and initializes each bit value to match the corresponding boolean value in bits.

Parameters:

bitsThe initialization value.
BitArray opSliceAssign(BitArray rhs)
Copy the bits from one array into this array. This is not a shallow copy.

Parameters:

rhsA BitArray with at least the same number of bits as this bit array.

Returns:

A shallow copy of this array.

1
2
3
4
5
6
BitArray ba = [0,1,0,1,0];
BitArray ba2;
ba2.length = ba.length;
ba2[] = ba; // perform the copy
ba[0] = true;
assert(ba2[0] == false);
void init(void[] target, size_t numbits)
Map BitArray onto target, with numbits being the number of bits in the array. Does not copy the data. This is the inverse of opCast.

Parameters:

targetThe array to map.
numbitsThe number of bits to map in target.
BitArray reverse() [@property]
Reverses the contents of this array in place, much like the reverse property for built-in arrays.

Returns:

A shallow copy of this array.
BitArray sort() [@property]
Sorts this array in place, with zero entries sorting before one. This is equivalent to the sort property for built-in arrays.

Returns:

A shallow copy of this array.
int opApply(scope int delegate(ref bool) dg)
int opApply(scope int delegate(ref size_t, ref bool) dg)
Operates on all bits in this array.

Parameters:

dgThe supplied code as a delegate.
bool opEquals(ref const(BitArray) rhs) [const]
Compares this array to another for equality. Two bit arrays are equal if they are the same size and contain the same series of bits.

Parameters:

rhsThe array to compare against.

Returns:

false if not equal and non-zero otherwise.
int opCmp(const ref BitArray rhs) [const]
Performs a lexicographical comparison of this array to the supplied array.

Parameters:

rhsThe array to compare against.

Returns:

A value less than zero if this array sorts before the supplied array, zero if the arrays are equavalent, and a value greater than zero if this array sorts after the supplied array.
void[] opCast()
Convert this array to a void array.

Returns:

This array represented as a void array.
bool opIndex(size_t pos)
Support for index operations, much like the behavior of built-in arrays.

Parameters:

posThe desired index position.

In:

pos must be less than the length of this array.

Returns:

The value of the bit at pos.
BitArray opCom()
Generates a copy of this array with the unary complement operation applied.

Returns:

A new array which is the complement of this array.
BitArray opAnd(BitArray rhs)
Generates a new array which is the result of a bitwise and operation between this array and the supplied array.

Parameters:

rhsThe array with which to perform the bitwise and operation.

In:

rhs.length must equal the length of this array.

Returns:

A new array which is the result of a bitwise and with this array and the supplied array.
BitArray opOr(BitArray rhs)
Generates a new array which is the result of a bitwise or operation between this array and the supplied array.

Parameters:

rhsThe array with which to perform the bitwise or operation.

In:

rhs.length must equal the length of this array.

Returns:

A new array which is the result of a bitwise or with this array and the supplied array.
BitArray opXor(BitArray rhs)
Generates a new array which is the result of a bitwise xor operation between this array and the supplied array.

Parameters:

rhsThe array with which to perform the bitwise xor operation.

In:

rhs.length must equal the length of this array.

Returns:

A new array which is the result of a bitwise xor with this array and the supplied array.
BitArray opSub(BitArray rhs)
Generates a new array which is the result of this array minus the supplied array. a - b for BitArrays means the same thing as a & ~b.

Parameters:

rhsThe array with which to perform the subtraction operation.

In:

rhs.length must equal the length of this array.

Returns:

A new array which is the result of this array minus the supplied array.
BitArray opCat(bool rhs)
BitArray opCat_r(bool lhs)
BitArray opCat(BitArray rhs)
Generates a new array which is the result of this array concatenated with the supplied array.

Parameters:

rhsThe array with which to perform the concatenation operation.

Returns:

A new array which is the result of this array concatenated with the supplied array.
bool opIndexAssign(bool b, size_t pos)
Support for index operations, much like the behavior of built-in arrays.

Parameters:

bThe new bit value to set.
posThe desired index position.

In:

pos must be less than the length of this array.

Returns:

The new value of the bit at pos.
BitArray opAndAssign(BitArray rhs)
Updates the contents of this array with the result of a bitwise and operation between this array and the supplied array.

Parameters:

rhsThe array with which to perform the bitwise and operation.

In:

rhs.length must equal the length of this array.

Returns:

A shallow copy of this array.
BitArray opOrAssign(BitArray rhs)
Updates the contents of this array with the result of a bitwise or operation between this array and the supplied array.

Parameters:

rhsThe array with which to perform the bitwise or operation.

In:

rhs.length must equal the length of this array.

Returns:

A shallow copy of this array.
BitArray opXorAssign(BitArray rhs)
Updates the contents of this array with the result of a bitwise xor operation between this array and the supplied array.

Parameters:

rhsThe array with which to perform the bitwise xor operation.

In:

rhs.length must equal the length of this array.

Returns:

A shallow copy of this array.
BitArray opSubAssign(BitArray rhs)
Updates the contents of this array with the result of this array minus the supplied array. a - b for BitArrays means the same thing as a & ~b.

Parameters:

rhsThe array with which to perform the subtraction operation.

In:

rhs.length must equal the length of this array.

Returns:

A shallow copy of this array.
BitArray opCatAssign(bool b)
BitArray opCatAssign(BitArray rhs)
Updates the contents of this array with the result of this array concatenated with the supplied array.

Parameters:

rhsThe array with which to perform the concatenation operation.

Returns:

A shallow copy of this array.