BDE 4.14.0 Production release
|
#include <bdlc_bitarray.h>
Public Types | |
enum | { k_BITS_PER_UINT64 = 64 } |
Public Member Functions | |
BitArray (bslma::Allocator *basicAllocator=0) | |
BitArray (bsl::size_t initialLength, bslma::Allocator *basicAllocator=0) | |
BitArray (bsl::size_t initialLength, bool value, bslma::Allocator *basicAllocator=0) | |
BitArray (const BitArray &original, bslma::Allocator *basicAllocator=0) | |
~BitArray () | |
Destroy this object. | |
BitArray & | operator= (const BitArray &rhs) |
BitArray & | operator&= (const BitArray &rhs) |
BitArray & | operator-= (const BitArray &rhs) |
BitArray & | operator|= (const BitArray &rhs) |
BitArray & | operator^= (const BitArray &rhs) |
BitArray & | operator<<= (bsl::size_t numBits) |
BitArray & | operator>>= (bsl::size_t numBits) |
void | andEqual (bsl::size_t index, bool value) |
void | andEqual (bsl::size_t dstIndex, const BitArray &srcArray, bsl::size_t srcIndex, bsl::size_t numBits) |
void | append (bool value) |
void | append (bool value, bsl::size_t numBits) |
void | append (const BitArray &srcArray) |
void | append (const BitArray &srcArray, bsl::size_t srcIndex, bsl::size_t numBits) |
void | assign (bsl::size_t index, bool value) |
void | assign (bsl::size_t index, bool value, bsl::size_t numBits) |
void | assign (bsl::size_t dstIndex, const BitArray &srcArray, bsl::size_t srcIndex, bsl::size_t numBits) |
void | assign0 (bsl::size_t index) |
void | assign0 (bsl::size_t index, bsl::size_t numBits) |
void | assign1 (bsl::size_t index) |
void | assign1 (bsl::size_t index, bsl::size_t numBits) |
void | assignAll (bool value) |
Set all bits in this array to the specified value . | |
void | assignAll0 () |
Set to 0 the value of every bit in this array. | |
void | assignAll1 () |
Set to 1 the value of every bit in this array. | |
void | assignBits (bsl::size_t index, bsl::uint64_t srcBits, bsl::size_t numBits) |
void | insert (bsl::size_t dstIndex, bool value) |
void | insert (bsl::size_t dstIndex, bool value, bsl::size_t numBits) |
void | insert (bsl::size_t dstIndex, const BitArray &srcArray) |
void | insert (bsl::size_t dstIndex, const BitArray &srcArray, bsl::size_t srcIndex, bsl::size_t numBits) |
void | minusEqual (bsl::size_t index, bool value) |
void | minusEqual (bsl::size_t dstIndex, const BitArray &srcArray, bsl::size_t srcIndex, bsl::size_t numBits) |
void | orEqual (bsl::size_t index, bool value) |
void | orEqual (bsl::size_t dstIndex, const BitArray &srcArray, bsl::size_t srcIndex, bsl::size_t numBits) |
void | remove (bsl::size_t index) |
void | remove (bsl::size_t index, bsl::size_t numBits) |
void | removeAll () |
void | reserveCapacity (bsl::size_t numBits) |
void | rotateLeft (bsl::size_t numBits) |
void | rotateRight (bsl::size_t numBits) |
void | setLength (bsl::size_t newLength, bool value=false) |
void | swapBits (bsl::size_t index1, bsl::size_t index2) |
void | toggle (bsl::size_t index) |
void | toggle (bsl::size_t index, bsl::size_t numBits) |
void | toggleAll () |
void | xorEqual (bsl::size_t index, bool value) |
void | xorEqual (bsl::size_t dstIndex, const BitArray &srcArray, bsl::size_t srcIndex, bsl::size_t numBits) |
template<class STREAM > | |
STREAM & | bdexStreamIn (STREAM &stream, int version) |
void | swap (BitArray &other) |
bool | operator[] (bsl::size_t index) const |
bsl::uint64_t | bits (bsl::size_t index, bsl::size_t numBits) const |
bsl::size_t | find0AtMaxIndex (bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const |
bsl::size_t | find0AtMinIndex (bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const |
bsl::size_t | find1AtMaxIndex (bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const |
bsl::size_t | find1AtMinIndex (bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const |
bool | isAny0 () const |
bool | isAny1 () const |
bool | isEmpty () const |
bsl::size_t | length () const |
Return the number of bits in this array. | |
bsl::size_t | num0 (bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const |
bsl::size_t | num1 (bsl::size_t begin=0, bsl::size_t end=k_INVALID_INDEX) const |
bslma::Allocator * | allocator () const |
Return the allocator used by this object to supply memory. | |
template<class STREAM > | |
STREAM & | bdexStreamOut (STREAM &stream, int version) const |
bsl::ostream & | print (bsl::ostream &stream, int level=0, int spacesPerLevel=4) const |
Static Public Member Functions | |
static int | maxSupportedBdexVersion (int versionSelector) |
static int | maxSupportedBdexVersion () |
Static Public Attributes | |
static const bsl::size_t | k_INVALID_INDEX |
Friends | |
bool | operator== (const BitArray &, const BitArray &) |
This class implements an efficient, value-semantic array of boolean (a.k.a. bit, i.e., binary digit) values stored in contiguous memory. The physical capacity of this array may grow, but never shrinks. Capacity may be reserved initially via a constructor, or at any time thereafter by using the reserveCapacity
method; otherwise, capacity will be increased automatically as needed. Note that capacity is not a salient attribute of this object, and, as such, does not contribute to overall value. Also note that this class provides an implicit no-throw guarantee for all methods (including manipulators) that do not attempt to alter capacity.
See bdlc_bitarray
|
explicit |
|
explicit |
bdlc::BitArray::BitArray | ( | bsl::size_t | initialLength, |
bool | value, | ||
bslma::Allocator * | basicAllocator = 0 |
||
) |
Create an array of binary digits (bits). By default, the array is empty and has a capacity of 0 bits. Optionally specify the initialLength
(in bits) of the array. If initialLength
is not specified, the default length is 0. If initialLength
is specified, optionally specify the value
for each bit in the initialLength
. If value
is not specified, the default value for each bit is false
(0). Optionally specify a basicAllocator
used to supply memory. If basicAllocator
is 0, the currently installed default allocator is used.
bdlc::BitArray::BitArray | ( | const BitArray & | original, |
bslma::Allocator * | basicAllocator = 0 |
||
) |
Create an array of binary digits (bits) having the same value as the specified original
array. Optionally specify a basicAllocator
used to supply memory. If basicAllocator
is 0, the currently installed default allocator is used.
bdlc::BitArray::~BitArray | ( | ) |
|
inline |
|
inline |
Bitwise AND the specified numBits
in this array, beginning at the specified dstIndex
, with values from the specified srcArray
, beginning at the specified srcIndex
(retaining the results). The behavior is undefined unless dstIndex + numBits <= length()
and srcIndex + numBits <= srcArray.length()
.
|
inline |
AND the bit at the specified index
in this array with the specified value
(retaining the result). The behavior is undefined unless index < length()
.
|
inline |
Append to this array the specified value
. Note that this method has the same behavior as:
|
inline |
|
inline |
|
inline |
|
inline |
Replace the specified numBits
in this array, beginning at the specified dstIndex
, with values from the specified srcArray
beginning at the specified srcIndex
. The behavior is undefined unless dstIndex + numBits <= length()
and srcIndex + numBits <= srcArray.length()
. Note that, absent aliasing, this method has the same behavior as, but is more efficient than:
|
inline |
Set the value at the specified index
in this array to the specified value
. The behavior is undefined unless index < length()
.
|
inline |
Set the value of the specified numBits
bits starting at the specified index
in this array to the specified value
. The behavior is undefined unless index + numBits < length()
.
|
inline |
Set to 0 the value of the bit at the specified index
in this array. The behavior is undefined unless index < length()
.
|
inline |
Set to 0 the specified numBits
values in this array, beginning at the specified index
. The behavior is undefined unless index + numBits <= length()
.
|
inline |
Set to 1 the value of the bit at the specified index
in this array. The behavior is undefined unless index < length()
.
|
inline |
Set to 1 the specified numBits
values in this array, beginning at the specified index
. The behavior is undefined unless index + numBits <= length()
.
|
inline |
|
inline |
|
inline |
|
inline |
Assign the low-order specified numBits
from the specified srcBits
to this object, starting at the specified index
. The behavior is undefined unless numBits <= k_BITS_PER_UINT64
and index + numBits <= length()
.
STREAM & bdlc::BitArray::bdexStreamIn | ( | STREAM & | stream, |
int | version | ||
) |
Assign to this object the value read from the specified input stream
using the specified version
format, and return a reference to stream
. If stream
is initially invalid, this operation has no effect. If version
is not supported, this object is unaltered and stream
is invalidated, but otherwise unmodified. If version
is supported but stream
becomes invalid during this operation, this object has an undefined, but valid, state. Note that no version is read from stream
. See the bslx
package-level documentation for more information on BDEX streaming of value-semantic types and containers.
STREAM & bdlc::BitArray::bdexStreamOut | ( | STREAM & | stream, |
int | version | ||
) | const |
Write the value of this object, using the specified version
format, to the specified output stream
, and return a reference to stream
. If stream
is initially invalid, this operation has no effect. If version
is not supported, stream
is invalidated, but otherwise unmodified. Note that version
is not written to stream
. See the bslx
package-level documentation for more information on BDEX streaming of value-semantic types and containers.
|
inline |
Return the specified numBits
beginning at the specified index
in this array as the low-order bits of the returned value. The behavior is undefined unless numBits <= sizeof(uint64_t) * CHAR_BIT
and index + numBits <= length()
.
|
inline |
Return the index of the most-significant 0 bit in this array in the range optionally specified by begin
and end
, and k_INVALID_INDEX
otherwise. The range is [begin .. effectiveEnd)
, where effectiveEnd == length()
if end
is not specified and effectiveEnd == end
otherwise. The behavior is undefined unless begin <= effectiveEnd <= length()
.
|
inline |
Return the index of the least-significant 0 bit in this array in the range optionally specified by begin
and end
, and k_INVALID_INDEX
otherwise. The range is [begin .. effectiveEnd)
, where effectiveEnd == length()
if end
is not specified and effectiveEnd == end
otherwise. The behavior is undefined unless begin <= effectiveEnd <= length()
.
|
inline |
Return the index of the most-significant 1 bit in this array in the range optionally specified by begin
and end
, and k_INVALID_INDEX
otherwise. The range is [begin .. effectiveEnd)
, where effectiveEnd == length()
if end
is not specified and effectiveEnd == end
otherwise. The behavior is undefined unless begin <= effectiveEnd <= length()
.
|
inline |
Return the index of the least-significant 1 bit in this array in the range optionally specified by begin
and end
, and k_INVALID_INDEX
otherwise. The range is [begin .. effectiveEnd)
, where effectiveEnd == length()
if end
is not specified and effectiveEnd == end
otherwise. The behavior is undefined unless begin <= effectiveEnd <= length()
.
|
inline |
Insert into this array at the specified dstIndex
the specified value
. All values with indices at or above dstIndex
in this array are shifted up by one bit position. The behavior is undefined unless dstIndex <= length()
.
|
inline |
Insert into this array at the specified dstIndex
the specified numBits
having the specified value
. All values with indices at or above dstIndex
in this array are shifted up by numBits
bit positions. The behavior is undefined unless dstIndex <= length()
.
|
inline |
Insert into this array, beginning at the specified dstIndex
, the values from the specified srcArray
. All values with indices at or above dstIndex
in this array are shifted up by srcArray.length()
bit positions. The behavior is undefined unless dstIndex <= length()
.
void bdlc::BitArray::insert | ( | bsl::size_t | dstIndex, |
const BitArray & | srcArray, | ||
bsl::size_t | srcIndex, | ||
bsl::size_t | numBits | ||
) |
Insert into this array, beginning at the specified dstIndex
, the specified numBits
from the specified srcArray
beginning at the specified srcIndex
. All values with initial indices at or above dstIndex
are shifted up by numBits
positions. The behavior is undefined unless dstIndex <= length()
and srcIndex + numBits <= srcArray.length()
.
|
inline |
Return true
if the value of any bit in this array is 0, and false
otherwise.
|
inline |
Return true
if the value of any bit in this array is 1, and false
otherwise.
|
inline |
Return true
if the length of this bit array is 0, and false
otherwise.
|
inline |
|
inlinestatic |
Return the most current BDEX streaming version number supported by this class.
|
inlinestatic |
Return the maximum valid BDEX format version, as indicated by the specified versionSelector
, to be passed to the bdexStreamOut
method. Note that it is highly recommended that versionSelector
be formatted as "YYYYMMDD", a date representation. Also note that versionSelector
should be a compile-time-chosen value that selects a format version supported by both externalizer and unexternalizer. See the bslx
package-level documentation for more information on BDEX streaming of value-semantic types and containers.
|
inline |
Bitwise MINUS (subtract) from the specified numBits
in this array, beginning at the specified dstIndex
, values from the specified srcArray
beginning at the specified srcIndex
(retaining the results). The behavior is undefined unless dstIndex + numBits <= length()
and srcIndex + numBits <= srcArray.length()
. Note that the logical difference A - B
is defined to be A & !B
.
|
inline |
MINUS (subtract) from the bit at the specified index
in this array the specified value
(retaining the result). The behavior is undefined unless index < length()
. Note that the logical difference A - B
is defined to be A & !B
.
|
inline |
|
inline |
Bitwise AND the value of the specified rhs
array with the value of this array (retaining the results), and return a non-const
reference to this object. The length of the result will be the maximum of the lengths of this object and rhs
, where any most-significant bits that are represented in one of the two but not the other will be set to 0. Note that a &= b;
will result in the same value of a
as a = a & b;
.
Bitwise MINUS the value of the specified rhs
array from the value of this array (retaining the results), and return a non-const
reference to this object. The length of the result will be the maximum of the lengths of this object and rhs
. If length() > rhs.length()
, the unmatched most-significant bits in this array are left unchanged; otherwise, any high-order bits of the result that were not present in this object prior to the operation will be set to 0. Note that a -= b;
will result in the same value of a
as a = a - b;
and if a
and b
are the same length, a -= b;
will result in the same value of a
as a &= ~b;
or a = a & ~b;
.
|
inline |
Shift the bits in this array LEFT by the specified numBits
, filling lower-order bits with zeros (retaining the results), and return a non-const
reference to this object. The behavior is undefined unless numBits <= length()
. Note that the length of this array is unchanged and the highest-order numBits
are discarded.
Assign to this array the value of the specified rhs
array, and return a non-const
reference to this array.
|
inline |
Shift the bits in this array RIGHT by the specified numBits
, filling higher-order bits with zeros and discarding low-order bits, and return a non-const
reference to this object. The behavior is undefined unless numBits <= length()
. Note that the length of this array is unchanged.
|
inline |
Return the value of the bit at the specified index
in this array. The behavior is undefined unless index < length()
.
Bitwise XOR the value of the specified rhs
array with the value of this array (retaining the results), and return a non-const
reference to this object. If length() > rhs.length()
, the unmatched most-significant bits in this array are left unchanged; otherwise, any unmatched most-significant bits in rhs
are propagated to the result without modification. Note that a ^= b;
will result in the same value of a
as a = a ^ b;
.
Bitwise OR the value of the specified rhs
array with the value of this array (retaining the results), and return a non-const
reference to this object. If length() > rhs.length()
, the unmatched most-significant bits in this array are left unchanged; otherwise, any unmatched most-significant bits in rhs
are propagated to the result without modification. Note that a |= b;
will result in the same value of a
as a = a | b;
.
|
inline |
Bitwise OR the specified numBits
in this array, beginning at the specified dstIndex
, with values from the specified srcArray
beginning at the specified srcIndex
(retaining the results). The behavior is undefined unless dstIndex + numBits <= length()
and srcIndex + numBits <= srcArray.length()
.
|
inline |
OR the bit at the specified index
in this array with the specified value
(retaining the result). The behavior is undefined unless index < length()
.
bsl::ostream & bdlc::BitArray::print | ( | bsl::ostream & | stream, |
int | level = 0 , |
||
int | spacesPerLevel = 4 |
||
) | const |
Format this object to the specified output stream
at the optionally specified indentation level
and return a non-const
reference to stream
. If level
is specified, optionally specify spacesPerLevel
, the number of spaces per indentation level for this and all of its nested objects. Each line is indented by the absolute value of level * spacesPerLevel
. If level
is negative, suppress indentation of the first line. If spacesPerLevel
is negative, suppress line breaks and format the entire output on one line. If stream
is initially invalid, this operation has no effect. Note that a trailing newline is provided in multiline mode only.
|
inline |
Remove from this array the bit at the specified index
. All values at indices above index
in this array are shifted down by one bit position. The length of this array is reduced by 1. The behavior is undefined unless index < length()
.
|
inline |
Remove from this array the specified numBits
, beginning at the specified index
. All values at indices above index
in this array are shifted down by numBits
positions. The length of this array is reduced by numBits
. The behavior is undefined unless index + numBits <= length()
.
|
inline |
Remove all of the bits in this array, leaving the length 0, but having no effect on capacity.
|
inline |
Reserve sufficient internal capacity to accommodate a length of at least the specified numBits
without reallocation. If an exception is thrown during this reallocation attempt (i.e., by the memory allocator indicated at construction) the value of this array is guaranteed to be unchanged.
void bdlc::BitArray::rotateLeft | ( | bsl::size_t | numBits | ) |
Shift the values in this array to the left by the specified numBits
positions, with the high-order values "rotating" into the low-order bits. The behavior is undefined unless numBits <= length()
. Note that the length of this array remains unchanged.
void bdlc::BitArray::rotateRight | ( | bsl::size_t | numBits | ) |
Shift the values in this array to the right by the specified numBits
positions, with the low-order values "rotating" into the high-order bits. The behavior is undefined unless numBits <= length()
. Note that the length of this array remains unchanged.
void bdlc::BitArray::setLength | ( | bsl::size_t | newLength, |
bool | value = false |
||
) |
Set the number of bits in this array to the specified newLength
. If newLength < length()
, bits at index positions at or above newLength
are removed; otherwise, any new bits (at or above the current length) are initialized to the optionally specified value
, or to 0 if value
is not specified.
|
inline |
Efficiently exchange the value of this object with the value of the specified other
object. This method provides the no-throw exception-safety guarantee. The behavior is undefined unless this object was created with the same allocator as other
.
|
inline |
|
inline |
Complement the value of the bit at the specified index
in this array. The behavior is undefined unless index < length()
.
|
inline |
Complement the values of each of the specified numBits
in this array, beginning at the specified index
. The behavior is undefined unless index + numBits <= length()
.
|
inline |
Complement the value of every bit in this array. Note that the behavior is analogous to applying the ~
operator to an object of fundamental type unsigned int
.
|
inline |
Bitwise XOR the specified numBits
in this array, beginning at the specified dstIndex
, with values from the specified srcArray
beginning at the specified srcIndex
(retaining the results). The behavior is undefined unless dstIndex + numBits <= length()
and srcIndex + numBits <= srcArray.length()
.
|
inline |
XOR the bit at the specified index
in this array with the specified value
(retaining the result). The behavior is undefined unless index < length()
.
Return true
if the specified lhs
and rhs
arrays have the same value, and false
otherwise. Two arrays have the same value if they have the same length, and corresponding bits at each bit position have the same value.
|
static |