A simple set of 32 bits that can be individually marked or cleared.
| 30 | |
| 31 | // A simple set of 32 bits that can be individually marked or cleared. |
| 32 | struct BitSet32 { |
| 33 | uint32_t value; |
| 34 | |
| 35 | inline BitSet32() : value(0UL) { } |
| 36 | explicit inline BitSet32(uint32_t value) : value(value) { } |
| 37 | |
| 38 | // Gets the value associated with a particular bit index. |
| 39 | static inline uint32_t valueForBit(uint32_t n) { return 0x80000000UL >> n; } |
| 40 | |
| 41 | // Clears the bit set. |
| 42 | inline void clear() { clear(value); } |
| 43 | |
| 44 | static inline void clear(uint32_t& value) { value = 0UL; } |
| 45 | |
| 46 | // Returns the number of marked bits in the set. |
| 47 | inline uint32_t count() const { return count(value); } |
| 48 | |
| 49 | static inline uint32_t count(uint32_t value) { return __builtin_popcountl(value); } |
| 50 | |
| 51 | // Returns true if the bit set does not contain any marked bits. |
| 52 | inline bool isEmpty() const { return isEmpty(value); } |
| 53 | |
| 54 | static inline bool isEmpty(uint32_t value) { return ! value; } |
| 55 | |
| 56 | // Returns true if the bit set does not contain any unmarked bits. |
| 57 | inline bool isFull() const { return isFull(value); } |
| 58 | |
| 59 | static inline bool isFull(uint32_t value) { return value == 0xffffffffUL; } |
| 60 | |
| 61 | // Returns true if the specified bit is marked. |
| 62 | inline bool hasBit(uint32_t n) const { return hasBit(value, n); } |
| 63 | |
| 64 | static inline bool hasBit(uint32_t value, uint32_t n) { return value & valueForBit(n); } |
| 65 | |
| 66 | // Marks the specified bit. |
| 67 | inline void markBit(uint32_t n) { markBit(value, n); } |
| 68 | |
| 69 | static inline void markBit (uint32_t& value, uint32_t n) { value |= valueForBit(n); } |
| 70 | |
| 71 | // Clears the specified bit. |
| 72 | inline void clearBit(uint32_t n) { clearBit(value, n); } |
| 73 | |
| 74 | static inline void clearBit(uint32_t& value, uint32_t n) { value &= ~ valueForBit(n); } |
| 75 | |
| 76 | // Finds the first marked bit in the set. |
| 77 | // Result is undefined if all bits are unmarked. |
| 78 | inline uint32_t firstMarkedBit() const { return firstMarkedBit(value); } |
| 79 | |
| 80 | static uint32_t firstMarkedBit(uint32_t value) { return clz_checked(value); } |
| 81 | |
| 82 | // Finds the first unmarked bit in the set. |
| 83 | // Result is undefined if all bits are marked. |
| 84 | inline uint32_t firstUnmarkedBit() const { return firstUnmarkedBit(value); } |
| 85 | |
| 86 | static inline uint32_t firstUnmarkedBit(uint32_t value) { return clz_checked(~ value); } |
| 87 | |
| 88 | // Finds the last marked bit in the set. |
| 89 | // Result is undefined if all bits are unmarked. |