Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 1 | #pragma once |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 2 | |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 3 | #include <stdint.h> |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 4 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 5 | namespace libhei |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 6 | { |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 7 | |
| 8 | class BitStringBuffer; |
| 9 | |
Zane Shelley | 72fd2e4 | 2022-11-12 12:14:53 -0600 | [diff] [blame^] | 10 | // ############################################################################## |
| 11 | // BitString class |
| 12 | // ############################################################################## |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * A BitString is general purpose class providing the ability to manipulate |
| 16 | * individual bits within an allocated section of contiguous memory. |
| 17 | * |
| 18 | * A BitString does not "own" the memory, it only accesses and manipulates the |
| 19 | * bits in the range specified. Users will need to ensure memory is allocated |
| 20 | * and deallocated appropriately. As an alternative, a BitStringBuffer is a |
| 21 | * BitString that will allocate and maintain its own memory. |
| 22 | * |
| 23 | * The length of a BitString is only limited by the amount of memory that |
| 24 | * contains the data buffer. |
| 25 | * |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 26 | * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 27 | * |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 28 | * - The bit positions are ordered 0 to n (left to right), where n is the bit |
| 29 | * length minus one. |
| 30 | * - The data stored in memory is assumed to be in big-endian byte format. |
| 31 | * |
| 32 | * So, for example: |
| 33 | * |
| 34 | * uint8_t a[2]; // 16 bits of memory |
| 35 | * BitString bs { 16, a }; // init BitString for a |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 36 | * bs.setFieldRight(0, 16, 0x1122); // set all 16 bits to 0x1122 |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 37 | * |
| 38 | * Results in: |
| 39 | * |
| 40 | * a[0] == bs.getFieldRight(0, 8) (i.e. 0x11) |
| 41 | * a[1] == bs.getFieldRight(8, 8) (i.e. 0x22) |
| 42 | * |
| 43 | * It is very important you do NOT do this: |
| 44 | * |
| 45 | * uint16_t x = 0x1122; // 16 bits of memory |
| 46 | * BitString bs { 16, &x }; // init BitString for x |
| 47 | * |
| 48 | * The results are undefined, or at least not portable. For example: |
| 49 | * |
| 50 | * Big-endian: |
| 51 | * x is stored in memory as |0x11|0x22|. |
| 52 | * Therefore, bs.getFieldRight(0, 8) returns 0x11. |
| 53 | * |
| 54 | * Little-endian: |
| 55 | * x is stored in memory as |0x22|0x11|. |
| 56 | * Therefore, bs.getFieldRight(0, 8) returns 0x22. |
| 57 | * |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 58 | */ |
| 59 | class BitString |
| 60 | { |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 61 | private: // constants |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 62 | static const uint64_t UINT64_BIT_LEN; |
| 63 | static const uint64_t UINT8_BIT_LEN; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 64 | |
| 65 | public: // functions |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 66 | /** |
| 67 | * @brief Constructor |
| 68 | * @param i_bitLen The number of bits in the bit string. |
| 69 | * @param i_bufAddr The starting address of the memory buffer. |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 70 | * @param i_offset By default, position 0 will be the first bit of the |
| 71 | * buffer's start address. However, this parameter can be |
| 72 | * used to indicate that position 0 actually starts |
| 73 | * somewhere in the middle of the buffer. |
| 74 | * @pre Use getMinBytes() to calulate the minimum number of bytes needed |
| 75 | * to allocate sufficient memory space for this bit string. |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 76 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 77 | BitString(uint64_t i_bitLen, void* i_bufAddr, uint64_t i_offset = 0) : |
Zane Shelley | 5a78fa8 | 2022-09-16 16:49:58 -0500 | [diff] [blame] | 78 | iv_bitLen(i_bitLen), iv_bufAddr(static_cast<uint8_t*>(i_bufAddr)), |
| 79 | iv_offset(i_offset) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 80 | {} |
| 81 | |
| 82 | /** @brief Destructor */ |
| 83 | virtual ~BitString() {} |
| 84 | |
| 85 | /** @return The number of bits in the bit string buffer. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 86 | uint64_t getBitLen() const |
| 87 | { |
| 88 | return iv_bitLen; |
| 89 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 90 | |
| 91 | /** @return The address of the bit string buffer. Note that this may |
| 92 | * return nullptr. */ |
Zane Shelley | 5a78fa8 | 2022-09-16 16:49:58 -0500 | [diff] [blame] | 93 | uint8_t* getBufAddr() const |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 94 | { |
| 95 | return iv_bufAddr; |
| 96 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * @param i_bitLen The number of bits for a bit string. |
| 100 | * @param i_offset Optional starting position of the bit string within the |
| 101 | * memory buffer. |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 102 | * @return The minimum number of bytes required to allocate sufficient |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 103 | * memory space for a bit string. |
| 104 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 105 | static uint64_t getMinBytes(uint64_t i_bitLen, uint64_t i_offset = 0) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 106 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 107 | return (i_bitLen + i_offset + UINT8_BIT_LEN - 1) / UINT8_BIT_LEN; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @brief Returns a left-justified value of the given length from the bit |
| 112 | * string starting at the given position. |
| 113 | * @param i_pos The starting position of the target range. |
| 114 | * @param i_len The number of bits of the target range. |
| 115 | * @return The value of the field range specified (left-justified). |
| 116 | * @pre nullptr != getBufAddr() |
| 117 | * @pre 0 < i_len |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 118 | * @pre i_len <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 119 | * @pre i_pos + i_len <= getBitLen() |
| 120 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 121 | uint64_t getFieldLeft(uint64_t i_pos, uint64_t i_len) const |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 122 | { |
| 123 | return getFieldRight(i_pos, i_len) << (UINT64_BIT_LEN - i_len); |
| 124 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * @brief Returns a right-justified value of the given length from the bit |
| 128 | * string starting at the given position. |
| 129 | * @param i_pos The starting position of the target range. |
| 130 | * @param i_len The number of bits of the target range. |
| 131 | * @return The value of the field range specified (right-justified). |
| 132 | * @pre nullptr != getBufAddr() |
| 133 | * @pre 0 < i_len |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 134 | * @pre i_len <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 135 | * @pre i_pos + i_len <= getBitLen() |
| 136 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 137 | uint64_t getFieldRight(uint64_t i_pos, uint64_t i_len) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * @brief Sets a left-justified value of the given length into the bit |
| 141 | * string starting at the given position. |
| 142 | * @param i_pos The starting position of the target range. |
| 143 | * @param i_len The number of bits of the target range. |
| 144 | * @param i_val The left-justified value to set. |
| 145 | * @pre nullptr != getBufAddr() |
| 146 | * @pre 0 < i_len |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 147 | * @pre i_len <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 148 | * @pre i_pos + i_len <= getBitLen() |
| 149 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 150 | void setFieldLeft(uint64_t i_pos, uint64_t i_len, uint64_t i_val); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * @brief Sets a right-justified value of the given length into the bit |
| 154 | * string starting at the given position. |
| 155 | * @param i_pos The starting position of the target range. |
| 156 | * @param i_len The number of bits of the target range. |
| 157 | * @param i_val The right-justified value to set. |
| 158 | * @pre nullptr != getBufAddr() |
| 159 | * @pre 0 < i_len |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 160 | * @pre i_len <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 161 | * @pre i_pos + i_len <= getBitLen() |
| 162 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 163 | void setFieldRight(uint64_t i_pos, uint64_t i_len, uint64_t i_val) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 164 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 165 | setFieldLeft(i_pos, i_len, i_val << (UINT64_BIT_LEN - i_len)); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param i_pos The target position. |
| 170 | * @return True if the bit at the given position is set(1), false otherwise. |
| 171 | * @pre i_pos < getBitLen(). |
| 172 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 173 | bool isBitSet(uint64_t i_pos) const |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 174 | { |
| 175 | return 0 != getFieldRight(i_pos, 1); |
| 176 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * @brief Sets the target position to 1. |
| 180 | * @param i_pos The target position. |
| 181 | * @pre i_pos < getBitLen(). |
| 182 | */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 183 | void setBit(uint64_t i_pos) |
| 184 | { |
| 185 | setFieldRight(i_pos, 1, 1); |
| 186 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 187 | |
| 188 | /** @brief Sets the entire bit string to 1's. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 189 | void setAll() |
| 190 | { |
| 191 | setPattern(UINT64_MAX); |
| 192 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * @brief Sets the target position to 0. |
| 196 | * @param i_pos The target position. |
| 197 | * @pre i_pos < getBitLen(). |
| 198 | */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 199 | void clearBit(uint64_t i_pos) |
| 200 | { |
| 201 | setFieldRight(i_pos, 1, 0); |
| 202 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 203 | |
| 204 | /** @brief Sets the entire bit string to 0's. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 205 | void clearAll() |
| 206 | { |
| 207 | setPattern(0); |
| 208 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * @brief Sets a range within the string based on the pattern and length |
| 212 | * provided. |
| 213 | * @param i_sPos Starting position of this string. |
| 214 | * @param i_sLen The length of the target range. |
| 215 | * @param i_pattern The pattern to set (right justified). |
| 216 | * @param i_pLen The length of the pattern. |
| 217 | * @pre nullptr != getBufAddr() |
| 218 | * @pre 0 < i_sLen |
| 219 | * @pre i_sPos + i_sLen <= getBitLen() |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 220 | * @pre 0 < i_pLen <= UINT64_BIT_LEN |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 221 | * @post The pattern is repeated/truncated as needed. |
| 222 | * |
| 223 | * Examples: i_sPos(0), i_sLen(10), i_pattern(0xA), i_pLen(4) |
| 224 | * Old String: 0000000000 |
| 225 | * New String: 1010101010 |
| 226 | * |
| 227 | * i_sPos(3), i_sLen(4), i_pattern(0x3), i_pLen(3) |
| 228 | * Old String: 0001001000 |
| 229 | * New String: 0000110000 |
| 230 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 231 | void setPattern(uint64_t i_sPos, uint64_t i_sLen, uint64_t i_pattern, |
| 232 | uint64_t i_pLen); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * @brief Sets entire string based on the pattern and length provided. |
| 236 | * @param i_pattern The pattern to set (right justified). |
| 237 | * @param i_pLen The length of the pattern. |
| 238 | * @note See definition above for prerequisites. |
| 239 | * @post The entire string is filled with the pattern. |
| 240 | * @post The pattern is repeated/truncated as needed. |
| 241 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 242 | void setPattern(uint64_t i_pattern, uint64_t i_pLen) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 243 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 244 | setPattern(0, getBitLen(), i_pattern, i_pLen); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 248 | * @brief Sets entire string based on the pattern provided. |
| 249 | * @param i_pattern The pattern to set (right justified). |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 250 | * @note See definition above for prerequisites. |
| 251 | * @post The entire string is filled with the pattern. |
| 252 | * @post The pattern is repeated/truncated as needed. |
| 253 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 254 | void setPattern(uint64_t i_pattern) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 255 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 256 | setPattern(i_pattern, sizeof(i_pattern) * 8); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * @brief Set bits in this string based on the given string. |
| 261 | * @param i_sStr The source string. |
| 262 | * @param i_sPos The starting position of the source string. |
| 263 | * @param i_sLen The number of bits to copy from the source string. |
| 264 | * @param i_dPos The starting position of the this string. |
| 265 | * @pre nullptr != getBufAddr() |
| 266 | * @pre nullptr != i_sStr.getBufAddr() |
| 267 | * @pre 0 < i_sLen |
| 268 | * @pre i_sPos + i_sLen <= i_sStr.getBitLen() |
| 269 | * @pre i_dPos < getBitLen() |
| 270 | * @post Source bits in given range are copied to this starting at i_dPos. |
| 271 | * @note If the length of the given string is greater than the length of |
| 272 | * this string, then the extra bits are ignored. |
| 273 | * @note If the length of the given string is less than the length of this |
| 274 | * string, then the extra bits in this string are not modified. |
| 275 | * @note This string and the source string may specify overlapping memory. |
| 276 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 277 | void setString(const BitString& i_sStr, uint64_t i_sPos, uint64_t i_sLen, |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 278 | uint64_t i_dPos = 0); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * @brief Set bits in this string based on the provided string. |
| 282 | * @param i_sStr The source string. |
| 283 | * @note This will try to copy as much of the source as possible to this |
| 284 | * string, starting with the first bit in each string. |
| 285 | * @note See the other definition of this function for details and |
| 286 | * restrictions. |
| 287 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 288 | void setString(const BitString& i_sStr) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 289 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 290 | setString(i_sStr, 0, i_sStr.getBitLen()); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @brief Masks (clears) any bits set in this string that correspond to bits |
| 295 | * set in the given string (this & ~mask). |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 296 | * @param i_mask The mask string (right justified). |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 297 | * @note If the length of the given string is greater than the length of |
| 298 | * this string, then the extra bits are ignored. |
| 299 | * @note If the length of the given string is less than the length of this |
| 300 | * string, then the extra bits in this string are not modified. |
| 301 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 302 | void maskString(const BitString& i_mask); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * @param i_str The string to compare. |
| 306 | * @return True if the strings are equivalent, false otherwise. |
| 307 | * @pre Both strings must be of equal length and have same values to be |
| 308 | * equal. |
| 309 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 310 | bool isEqual(const BitString& i_str) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 311 | |
| 312 | /** @return True if there are no bit set(1) in this bit string, false |
| 313 | * otherwise. */ |
| 314 | bool isZero() const; |
| 315 | |
| 316 | /** |
| 317 | * @param i_pos The starting position of the target range. |
| 318 | * @param i_len The length of the target range. |
| 319 | * @return The number of bits that are set(1) in given range of this string. |
| 320 | * @pre nullptr != getBufAddr() |
| 321 | * @pre i_pos + i_len <= getBitLen() |
| 322 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 323 | uint64_t getSetCount(uint64_t i_pos, uint64_t i_len) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 324 | |
| 325 | /** @return The number of bits that are set(1) in this string. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 326 | uint64_t getSetCount() const |
| 327 | { |
| 328 | return getSetCount(0, getBitLen()); |
| 329 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 330 | |
| 331 | /** @brief Comparison operator. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 332 | bool operator==(const BitString& i_str) const |
| 333 | { |
| 334 | return isEqual(i_str); |
| 335 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 336 | |
Zane Shelley | 6eb6190 | 2020-05-15 22:25:58 -0500 | [diff] [blame] | 337 | /** |
| 338 | * @brief Less-than operator. |
| 339 | * |
| 340 | * IMPORTANT: |
| 341 | * The purpose of this function is primarily for sorting these objects in |
| 342 | * data structures like map and vector. It does not guarantee a less than |
| 343 | * comparison of the bit strings because bit strings can vary in length and |
| 344 | * it is difficult to define that kind of comparison. |
| 345 | */ |
Zane Shelley | ec06f82 | 2019-12-05 22:23:19 -0600 | [diff] [blame] | 346 | bool operator<(const BitString& i_str) const; |
| 347 | |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 348 | /** @brief Bitwise NOT operator. */ |
| 349 | BitStringBuffer operator~() const; |
| 350 | |
| 351 | /** @brief Bitwise AND operator. */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 352 | BitStringBuffer operator&(const BitString& i_bs) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 353 | |
| 354 | /** @brief Bitwise OR operator. */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 355 | BitStringBuffer operator|(const BitString& i_bs) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 356 | |
| 357 | /** @brief Right shift operator. */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 358 | BitStringBuffer operator>>(uint64_t i_shift) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 359 | |
| 360 | /** @brief Left shift operator. */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 361 | BitStringBuffer operator<<(uint64_t i_shift) const; |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * @brief Explicitly disables copy from BitString. |
| 365 | * |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 366 | * Prevents assigning a BitString& to a BitString, which would strip |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 367 | * polymorphism. |
| 368 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 369 | BitString(const BitString& i_bs) = delete; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 370 | |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 371 | /** |
| 372 | * @brief Explicitly disables assignment from BitStringBuffer. |
| 373 | * |
| 374 | * Allowing this would be dangerous if the BitStringBuffer goes out of scope |
| 375 | * because the BitString would point to memory that is no longer in context. |
| 376 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 377 | BitString& operator=(const BitStringBuffer& i_bsb) = delete; |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 378 | |
| 379 | /** |
| 380 | * @brief Explicitly disables copy from BitStringBuffer. |
| 381 | * |
| 382 | * Allowing this would be dangerous if the BitStringBuffer goes out of scope |
| 383 | * because the BitString would point to memory that is no longer in context. |
| 384 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 385 | BitString(const BitStringBuffer& i_bsb) = delete; |
Ben Tyner | a8126fd | 2019-08-01 19:40:07 -0500 | [diff] [blame] | 386 | |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 387 | protected: // functions |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 388 | /** |
| 389 | * @param i_newBufAddr The starting address of the new bit string buffer. |
| 390 | * @pre Before calling this function, make sure you deallocate the old |
| 391 | * buffer to avoid memory leaks. |
| 392 | */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 393 | void setBufAddr(void* i_newBufAddr) |
| 394 | { |
Zane Shelley | 5a78fa8 | 2022-09-16 16:49:58 -0500 | [diff] [blame] | 395 | iv_bufAddr = static_cast<uint8_t*>(i_newBufAddr); |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 396 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 397 | |
| 398 | /** @param i_newBitLen The new bit length of this bit string buffer. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 399 | void setBitLen(uint64_t i_newBitLen) |
| 400 | { |
| 401 | iv_bitLen = i_newBitLen; |
| 402 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 403 | |
| 404 | private: // functions |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 405 | /** |
| 406 | * @brief Given a bit position within the bit string, this function returns |
| 407 | * the address that contains the bit position and the bit position |
| 408 | * relative to that address. |
| 409 | * @param o_relPos The returned relative position. |
| 410 | * @param i_absPos The inputted absolute position. |
| 411 | * @return The relative address. |
| 412 | * @pre nullptr != getBufAddr() |
| 413 | * @pre i_absPos < getBitLen() |
| 414 | */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 415 | uint8_t* getRelativePosition(uint64_t& o_relPos, uint64_t i_absPos) const; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 416 | |
Zane Shelley | 7c8faa1 | 2019-10-28 22:26:28 -0500 | [diff] [blame] | 417 | private: |
Zane Shelley | 5a78fa8 | 2022-09-16 16:49:58 -0500 | [diff] [blame] | 418 | uint64_t iv_bitLen; ///< The bit length of this buffer. |
| 419 | uint8_t* iv_bufAddr; ///< The beginning address of this buffer. |
| 420 | uint64_t iv_offset; ///< Start position offset |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 421 | }; |
| 422 | |
Zane Shelley | 72fd2e4 | 2022-11-12 12:14:53 -0600 | [diff] [blame^] | 423 | // ############################################################################## |
| 424 | // BitStringBuffer class |
| 425 | // ############################################################################## |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 426 | |
| 427 | /** A BitStringBuffer is a BitString that maintains its own buffer in memory. It |
| 428 | * guarantees that sufficient memory is allocated and deallocated in the |
| 429 | * constructor and destructor, respectively. In addition, the assignment |
| 430 | * operator will adjust the amount of memory needed, as necessary, for the |
| 431 | * assignment. */ |
| 432 | class BitStringBuffer : public BitString |
| 433 | { |
| 434 | public: // functions |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 435 | /** |
| 436 | * @brief Constructor |
| 437 | * @param i_bitLen Number of bits in the string. |
| 438 | */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 439 | explicit BitStringBuffer(uint64_t i_bitLen); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 440 | |
| 441 | /** @brief Destructor */ |
| 442 | ~BitStringBuffer(); |
| 443 | |
| 444 | /** @brief Copy constructor from BitString */ |
Zane Shelley | d065924 | 2020-05-15 23:02:29 -0500 | [diff] [blame] | 445 | explicit BitStringBuffer(const BitString& i_bs); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 446 | |
| 447 | /** @brief Copy constructor from BitStringBuffer */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 448 | BitStringBuffer(const BitStringBuffer& i_bsb); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 449 | |
| 450 | /** @brief Assignment from BitString */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 451 | BitStringBuffer& operator=(const BitString& i_bs); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 452 | |
| 453 | /** @brief Assignment from BitStringBuffer */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 454 | BitStringBuffer& operator=(const BitStringBuffer& i_bsb); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 455 | |
| 456 | private: // functions |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 457 | /** @brief Deallocates the old buffer, if needed, and initializes the new |
| 458 | * buffer. */ |
| 459 | void initBuffer(); |
| 460 | }; |
| 461 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 462 | } // end namespace libhei |