Nvwa
1.1
|
Class to represent a packed boolean array. More...
#include <bool_array.h>
Classes | |
class | _Element |
Class to represent a reference to an array element. More... | |
Public Types | |
typedef unsigned long | size_type |
Type of array indices. More... | |
typedef _Element< byte > | reference |
Type of reference. | |
typedef _Element< const byte > | const_reference |
Type of const reference. | |
Public Member Functions | |
bool_array () noexcept | |
Constructs an empty bool_array. | |
bool_array (size_type size) | |
Constructs a bool_array with a specific size. More... | |
bool_array (const void *ptr, size_type size) | |
Constructs a bool_array from a given bitmap. More... | |
~bool_array () | |
Destroys the bool_array and releases memory. | |
bool_array (const bool_array &rhs) | |
Copy-constructor. More... | |
bool_array & | operator= (const bool_array &rhs) |
Assignment operator. More... | |
bool | create (size_type size) noexcept |
Creates the packed boolean array with a specific size. More... | |
void | initialize (bool value) noexcept |
Initializes all array elements to a specific value optimally. More... | |
reference | operator[] (size_type pos) |
Creates a reference to an array element. More... | |
const_reference | operator[] (size_type pos) const |
Creates a const reference to an array element. More... | |
bool | at (size_type pos) const |
Reads the boolean value of an array element at a specified position. More... | |
void | reset (size_type pos) |
Resets an array element to false at a specified position. More... | |
void | set (size_type pos) |
Sets an array element to true at a specified position. More... | |
size_type | size () const noexcept |
Gets the size of the bool_array. More... | |
size_type | count () const noexcept |
Counts elements with a true value. More... | |
size_type | count (size_type begin, size_type end=npos) const |
Counts elements with a true value in a specified range. More... | |
size_type | find (bool value, size_type offset=0) const |
Searches for the specified boolean value. More... | |
size_type | find (bool value, size_type offset, size_type count) const |
Searches for the specified boolean value. More... | |
size_type | find_until (bool value, size_type begin, size_type end) const |
Searches for the specified boolean value. More... | |
void | flip () noexcept |
Changes all true elements to false , and false ones to true . | |
void | swap (bool_array &rhs) noexcept |
Exchanges the content of this bool_array with another. More... | |
void | merge_and (const bool_array &rhs, size_type begin=0, size_type end=npos, size_type offset=0) |
Merges elements of another bool_array with a logical AND. More... | |
void | merge_or (const bool_array &rhs, size_type begin=0, size_type end=npos, size_type offset=0) |
Merges elements of another bool_array with a logical OR. More... | |
void | copy_to_bitmap (void *dest, size_type begin=0, size_type end=npos) |
Copies the bool_array content as bitmap to a specified buffer. More... | |
Static Public Member Functions | |
static size_t | get_num_bytes_from_bits (size_type num_bits) |
Converts the number of bits to number of bytes. More... | |
Static Public Attributes | |
static const size_type | npos = (size_type)-1 |
Constant representing `not found'. More... | |
Private Types | |
typedef unsigned char | byte |
Private definition of byte to avoid polluting the global namespace. More... | |
Private Member Functions | |
byte | get_8bits (size_type offset, size_type end) const |
Retreive contiguous 8 bits from the bool_array. More... | |
Static Private Attributes | |
static byte | _S_bit_count [256] |
Array that contains pre-calculated values how many 1-bits there are in a given byte. | |
static byte | _S_bit_ordinal [256] |
Array that contains pre-calculated values which the first 1-bit is for a given byte. More... | |
Class to represent a packed boolean array.
This was first written in April 1995, before I knew of any existing implementation of this kind of classes. Of course, the C++ Standard Template Library now demands an implementation of packed boolean array as vector<bool>
, but the code here should still be useful for the following reasons:
bit_vector
either);vector<bool>
(and the normal boolean array) under MSVC versions 6/8/9 and GCC versions before 4.3 (while the vector<bool>
implementations of MSVC 7.1 and GCC 4.3 have performance similar to that of bool_array
).
|
private |
Private definition of byte to avoid polluting the global namespace.
typedef unsigned long nvwa::bool_array::size_type |
Type of array indices.
|
explicit |
Constructs a bool_array with a specific size.
size | size of the array |
out_of_range | size equals 0 |
bad_alloc | memory is insufficient |
nvwa::bool_array::bool_array | ( | const void * | ptr, |
size_type | size | ||
) |
Constructs a bool_array from a given bitmap.
ptr | pointer to a bitmap |
size | size of the array |
out_of_range | size equals 0 |
bad_alloc | memory is insufficient |
nvwa::bool_array::bool_array | ( | const bool_array & | rhs | ) |
Copy-constructor.
rhs | the bool_array to copy from |
bad_alloc | memory is insufficient |
|
inline |
Reads the boolean value of an array element at a specified position.
pos | position of the array element to access |
out_of_range | pos is greater than the size of the array |
Copies the bool_array content as bitmap to a specified buffer.
The caller needs to ensure the destination buffer is big enough.
dest | address of the destination buffer |
begin | beginning of the range |
end | end of the range (exclusive) |
out_of_range | bad range for the source or the destination |
|
noexcept |
Counts elements with a true
value.
true
elements bool_array::size_type nvwa::bool_array::count | ( | size_type | begin, |
size_type | end = npos |
||
) | const |
Counts elements with a true
value in a specified range.
begin | beginning of the range |
end | end of the range (exclusive) |
true
elements out_of_range | the range [begin, end) is invalid |
|
noexcept |
Creates the packed boolean array with a specific size.
size | size of the array |
false
if size equals 0
or is too big, or if memory is insufficient; true
if size has a suitable value and memory allocation is successful.
|
inline |
Searches for the specified boolean value.
This function seaches from the specified position (default to beginning) to the end.
offset | the position at which the search is to begin |
value | the boolean value to find |
npos
otherwise
|
inline |
Searches for the specified boolean value.
This function accepts a range expressed in {position, count}.
offset | the position at which the search is to begin |
count | the number of bits to search |
value | the boolean value to find |
npos
otherwise out_of_range | offset and/or count is too big |
bool_array::size_type nvwa::bool_array::find_until | ( | bool | value, |
size_type | begin, | ||
size_type | end | ||
) | const |
Searches for the specified boolean value.
This function accepts a range expressed in [begin, end).
begin | the position at which the search is to begin |
end | the end position (exclusive) to stop searching |
value | the boolean value to find |
npos
otherwise out_of_range | the range [begin, end) is invalid |
|
private |
Retreive contiguous 8 bits from the bool_array.
If fewer than 8 bits are available, the extra bits are undefined.
offset | beginning position to retrieve the bits |
end | end position beyond whose byte no bits will be taken |
|
inlinestatic |
Converts the number of bits to number of bytes.
num_bits | number of bits |
|
noexcept |
Initializes all array elements to a specific value optimally.
value | the boolean value to assign to all elements |
void nvwa::bool_array::merge_and | ( | const bool_array & | rhs, |
size_type | begin = 0 , |
||
size_type | end = npos , |
||
size_type | offset = 0 |
||
) |
Merges elements of another bool_array with a logical AND.
rhs | another bool_array to merge |
begin | beginning of the range in rhs |
end | end of the range (exclusive) in rhs |
offset | position to merge in this bool_array |
out_of_range | bad range for the source or the destination |
void nvwa::bool_array::merge_or | ( | const bool_array & | rhs, |
size_type | begin = 0 , |
||
size_type | end = npos , |
||
size_type | offset = 0 |
||
) |
Merges elements of another bool_array with a logical OR.
rhs | another bool_array to merge |
begin | beginning of the range in rhs |
end | end of the range (exclusive) in rhs |
offset | position to merge in this bool_array |
out_of_range | bad range for the source or the destination |
bool_array & nvwa::bool_array::operator= | ( | const bool_array & | rhs | ) |
Assignment operator.
rhs | the bool_array to copy from |
bad_alloc | memory is insufficient |
|
inline |
Creates a reference to an array element.
pos | position of the array element to access |
|
inline |
Creates a const reference to an array element.
pos | position of the array element to access |
|
inline |
Resets an array element to false
at a specified position.
pos | position of the array element to access |
out_of_range | pos is greater than the size of the array |
|
inline |
Sets an array element to true
at a specified position.
pos | position of the array element to access |
out_of_range | pos is greater than the size of the array |
|
inlinenoexcept |
Gets the size of the bool_array.
|
noexcept |
Exchanges the content of this bool_array with another.
rhs | another bool_array to exchange content with |
|
staticprivate |
Array that contains pre-calculated values which the first 1-bit is for a given byte.
The first element indicates an invalid value (there are only 0-bits).