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