Bitset¶
| Spice | |
|---|---|
BitSet struct¶
A BitSet stores a fixed number of bits in a compact form, packing 64 bits into every machine word. Compared to a set of integers it is far denser and offers fast bitwise operations (and/or/xor/not) over the whole sequence.
The number of bits is fixed at construction time.
Time complexity:
Access (test/set/clear/flip): O(1)
Bitwise ops (and/or/xor/not): O(n / 64)
count/any/none/all: O(n / 64)
Invariant: bits beyond numBits in the last word are always kept zero, so that count(), any() and all() never observe stale padding bits.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a bit set holding the given number of bits, all initialized to 0
Parameters
| Name | Type | Description |
|---|---|---|
numBits |
unsigned long |
Number of bits in the set (default: 0l) |
ctor¶
| Spice | |
|---|---|
Construct a bit set holding the given number of bits, all initialized to 0
Parameters
| Name | Type | Description |
|---|---|---|
numBits |
unsigned int |
Number of bits in the set |
ctor¶
| Spice | |
|---|---|
Construct a bit set holding the given number of bits, all initialized to the given value
Parameters
| Name | Type | Description |
|---|---|---|
numBits |
unsigned long |
Number of bits in the set |
initialValue |
bool |
Value to initialize every bit to |
ctor¶
| Spice | |
|---|---|
Construct a bit set as a deep copy of another bit set
Parameters
| Name | Type | Description |
|---|---|---|
original |
const BitSet& |
Bit set to copy |
Methods¶
set¶
| Spice | |
|---|---|
Sets the bit at the given index to 1
Parameters
| Name | Type | Description |
|---|---|---|
index |
unsigned long |
set¶
| Spice | |
|---|---|
Sets the bit at the given index to the given value
Parameters
| Name | Type | Description |
|---|---|---|
index |
unsigned long |
|
value |
bool |
clear¶
| Spice | |
|---|---|
Clears (sets to 0) the bit at the given index
Parameters
| Name | Type | Description |
|---|---|---|
index |
unsigned long |
flip¶
| Spice | |
|---|---|
Flips (toggles) the bit at the given index
Parameters
| Name | Type | Description |
|---|---|---|
index |
unsigned long |
test¶
| Spice | |
|---|---|
Returns the value of the bit at the given index
Parameters
| Name | Type | Description |
|---|---|---|
index |
unsigned long |
Returns: bool — true if the bit is set, false otherwise
setAll¶
| Spice | |
|---|---|
Sets all bits in the set to 1
clearAll¶
| Spice | |
|---|---|
Clears all bits in the set to 0
flipAll¶
| Spice | |
|---|---|
Flips (toggles) all bits in the set
count¶
| Spice | |
|---|---|
Returns the number of bits that are set to 1
Returns: unsigned long — Number of set bits
getSize¶
| Spice | |
|---|---|
Returns the number of bits in the set
Returns: unsigned long — Number of bits
any¶
| Spice | |
|---|---|
Checks whether at least one bit is set
Returns: bool — true if any bit is set
none¶
| Spice | |
|---|---|
Checks whether no bit is set
Returns: bool — true if no bit is set
all¶
| Spice | |
|---|---|
Checks whether all bits are set
Returns: bool — true if every bit is set
toString¶
| Spice | |
|---|---|
Builds a string representation of the set, with the highest index bit first (MSB-first)
Returns: String — String of '0' and '1' characters
Operators¶
operator=¶
| Spice | |
|---|---|
Copy-assign the contents of another bit set into this one
Parameters
| Name | Type | Description |
|---|---|---|
newValue |
const BitSet& |
Bit set to copy from |
operator[]¶
| Spice | |
|---|---|
Returns the value of the bit at the given index
Parameters
| Name | Type | Description |
|---|---|---|
bs |
const BitSet& |
|
index |
unsigned long |
Returns: bool — true if the bit is set, false otherwise
operator==¶
| Spice | |
|---|---|
Check if two bit sets are equal, i.e. they have the same size and the same bits set
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const BitSet& |
|
rhs |
const BitSet& |
Returns: bool — Equal or not equal
operator!=¶
| Spice | |
|---|---|
Check if two bit sets are not equal
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const BitSet& |
|
rhs |
const BitSet& |
Returns: bool — Not equal or equal
operator&¶
| Spice | |
|---|---|
Compute the bitwise AND of two equally sized bit sets
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const BitSet& |
|
rhs |
const BitSet& |
Returns: BitSet — New bit set holding the bitwise AND
operator|¶
| Spice | |
|---|---|
Compute the bitwise OR of two equally sized bit sets
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const BitSet& |
|
rhs |
const BitSet& |
Returns: BitSet — New bit set holding the bitwise OR
operator^¶
| Spice | |
|---|---|
Compute the bitwise XOR of two equally sized bit sets
Parameters
| Name | Type | Description |
|---|---|---|
lhs |
const BitSet& |
|
rhs |
const BitSet& |
Returns: BitSet — New bit set holding the bitwise XOR
operator~¶
| Spice | |
|---|---|
Compute the bitwise NOT (complement) of a bit set
Parameters
| Name | Type | Description |
|---|---|---|
bs |
const BitSet& |
Returns: BitSet — New bit set holding the bitwise complement