|
JASSv2
|
Long bitstrings. More...
#include <bitstring.h>
Public Member Functions | |
| bitstring () | |
| Constructor. | |
| virtual | ~bitstring () |
| Destructor. | |
| void | resize (size_t length_in_bits) |
| Set the length of the bitstring to length_in_bits. The new valid range is 0.. length_in_bits - 1. More... | |
| size_t | size (void) const |
| Return the length (in bits) of the bitstring. More... | |
| void | unsafe_setbit (size_t position) |
| Set the bit at position to 1. More... | |
| void | unsafe_unsetbit (size_t position) |
| Set the bit at position to 0. More... | |
| bool | unsafe_getbit (size_t position) const |
| Return the state (0 or 1) of the bit at the given position. More... | |
| void | setbit (size_t position) |
| Set the bit at position to 1. More... | |
| void | unsetbit (size_t position) |
| Set the bit at position to 0. More... | |
| bool | getbit (size_t position) const |
| Return the state (0 or 1) of the bit at the given position. More... | |
| void | bit_or (bitstring &answer, bitstring &with) |
| answer = this | with More... | |
| void | bit_xor (bitstring &answer, bitstring &with) |
| answer = this ^ with More... | |
| void | bit_and (bitstring &answer, bitstring &with) |
| answer = this & with More... | |
| void | bit_and_not (bitstring &answer, bitstring &with) |
| answer = this & ~with More... | |
| size_t | popcount (void) const |
| Return the number of set bits in the bitstring. This uses popcount(bitstring_word) to do the count. More... | |
| size_t | index (size_t which) |
| Returns the bit position of the which-th set bit. More... | |
| void | zero (void) |
| Set the bitstring to all zeros. | |
| void | one (void) |
| Set the bitstring to all ones. | |
| uint8_t * | serialise (size_t &length) |
| Return a serialised version of this object as an array of uint8_t. More... | |
Static Public Member Functions | |
| static size_t | popcount (uint64_t value) |
| Count the number of bits set in value. This uses the parallel algorithm from here: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel. More... | |
| static size_t | popcount (uint32_t value) |
| static size_t | popcount (uint16_t value) |
| static size_t | popcount (uint8_t value) |
| static void | unittest (void) |
| Unit test this class. | |
Protected Types | |
| enum | action { OR, XOR, AND, AND_NOT } |
| The action enum is used to pass a Boolean operation to operation(). More... | |
| typedef uint64_t | bitstring_word |
| bitstring_word is used as the underlying type for Boolean operations and for popcount() | |
Protected Member Functions | |
| virtual void | operation (action op, bitstring &a, bitstring &b, bitstring &c) |
| operation More... | |
Protected Attributes | |
| std::vector< uint8_t > | bits |
| The underlying storage for the long bitstring. | |
| size_t | bits_long |
| The length of the bitstring in bits. | |
Long bitstrings.
The internal storage is in bytes, but they are manipulated in chunks of bitstring::bitstring_word (which were intially uint64_t). The memory used to store the bits is guaranteed to be contigious. The set, unset, and get methods are impemented as function calls because an overloaded operator[] would need to return something that can be assigned to - which would be an inter mediate object that needs to know which bit is being referred to, and that would result in an object creation which is undesirable.
|
protected |
The action enum is used to pass a Boolean operation to operation().
| Enumerator | |
|---|---|
| OR | OR two bitstrings together. |
| XOR | XOR two bitstrings together. |
| AND | AND two bitstrings together. |
| AND_NOT | a AND ~ b |
answer = this & with
OR this and with, returning the result in answer. This method is used to avoid object copies and allocation that would come from using operator overloading in C++. With is [in, out] because this method sets the length of all three bitstrings to the length of the longest.
| answer | [out] The answer or this AND with |
| with | [in, out] the bitstring to AND this with |
answer = this & ~with
OR this and with, returning the result in answer. This method is used to avoid object copies and allocation that would come from using operator overloading in C++. With is [in, out] because this method sets the length of all three bitstrings to the length of the longest.
| answer | [out] The answer or this AND NOT with |
| with | [in, out] the bitstring to AND NOT this with |
answer = this | with
OR this and with, returning the result in answer. This method is used to avoid object copies and allocation that would come from using operator overloading in C++. With is [in, out] because this method sets the length of all three bitstrings to the length of the longest.
| answer | [out] The answer or this OR with |
| with | [in, out] the bitstring to OR this with |
answer = this ^ with
OR this and with, returning the result in answer. This method is used to avoid object copies and allocation that would come from using operator overloading in C++. With is [in, out] because this method sets the length of all three bitstrings to the length of the longest.
| answer | [out] The answer or this XOR with |
| with | [in, out] the bitstring to XOR this with |
|
inline |
Return the state (0 or 1) of the bit at the given position.
This method checks for overflow and returns false in the case of overflow.
| position | in] Which bit to check. |
| size_t JASS::bitstring::index | ( | size_t | which | ) |
Returns the bit position of the which-th set bit.
| [in] | which | set bit we're looking for. |
|
protectedvirtual |
operation
| op | [in] the operation we're going to perform. |
| a | [out] the answer (b op c) |
| b | [in] the first parameter |
| c | in] the second parameter |
|
inlinestatic |
Count the number of bits set in value. This uses the parallel algorithm from here: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel.
| value | [in] The value whose set bits are counted. |
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| size_t JASS::bitstring::popcount | ( | void | ) | const |
Return the number of set bits in the bitstring. This uses popcount(bitstring_word) to do the count.
| void JASS::bitstring::resize | ( | size_t | length_in_bits | ) |
Set the length of the bitstring to length_in_bits. The new valid range is 0.. length_in_bits - 1.
| length_in_bits | [in] the new length of bitstring |
|
inline |
Return a serialised version of this object as an array of uint8_t.
One frequent (and the initial use) of this class is to convert a bit-string into a byte-string. This method does exactly that. It returns a pointer to the internal byte-string and a length. The returned string is contigious. It is valid until the length of the bitstring changes.
| length | [out] The length of the bytestring (measured in bytes). |
|
inline |
Set the bit at position to 1.
This method checks for overflow and has no effect if overflow is detected.
| position | in] Which bit to set to 1. |
|
inline |
Return the length (in bits) of the bitstring.
|
inline |
Return the state (0 or 1) of the bit at the given position.
This method does not check for overflow and may result in unpredictable behaviour if a position outside the range [0..size()-1] is passed.
| position | in] Which bit to check. |
|
inline |
Set the bit at position to 1.
This method does not check for overflow and may result in unpredictable behaviour if a position outside the range [0..size()-1] is passed.
| position | in] Which bit to set to 1. |
|
inline |
Set the bit at position to 0.
This method does not check for overflow and may result in unpredictable behaviour if a position outside the range [0..size()-1] is passed.
| position | in] Which bit to set to 0. |
|
inline |
Set the bit at position to 0.
This method checks for overflow and has no effect if overflow is detected.
| position | in] Which bit to set to 0. |
1.8.13