|
| 1 | +#include "BitSet.h" |
| 2 | +#include <bit> |
| 3 | + |
| 4 | +BitSet::BitSet(Memory::Allocator* allocator, u32 numSets) |
| 5 | + : _allocator(allocator) |
| 6 | + , _bitSet(allocator, numSets, numSets) |
| 7 | +{ |
| 8 | + Reset(); |
| 9 | +} |
| 10 | + |
| 11 | +void BitSet::SetEquals(const BitSet& other) |
| 12 | +{ |
| 13 | + // Guard self assignment |
| 14 | + if (this == &other) |
| 15 | + return; |
| 16 | + |
| 17 | + memcpy(&_bitSet[0], &other._bitSet[0], sizeof(u64) * _bitSet.Count()); |
| 18 | +} |
| 19 | + |
| 20 | +void BitSet::Reset() |
| 21 | +{ |
| 22 | + memset(&_bitSet[0], 0, sizeof(u64) * _bitSet.Count()); |
| 23 | +} |
| 24 | + |
| 25 | +void BitSet::Set(u32 index) |
| 26 | +{ |
| 27 | + u32 setIndex = index / 64; |
| 28 | + u32 bitIndex = index % 64; |
| 29 | + |
| 30 | + Set(setIndex, bitIndex); |
| 31 | +} |
| 32 | + |
| 33 | +void BitSet::Set(u32 setIndex, u32 bitIndex) |
| 34 | +{ |
| 35 | + _bitSet[setIndex] |= 1ull << bitIndex; |
| 36 | +} |
| 37 | + |
| 38 | +void BitSet::Unset(u32 index) |
| 39 | +{ |
| 40 | + u32 setIndex = index / 64; |
| 41 | + u32 bitIndex = index % 64; |
| 42 | + |
| 43 | + Unset(setIndex, bitIndex); |
| 44 | +} |
| 45 | + |
| 46 | +void BitSet::Unset(u32 setIndex, u32 bitIndex) |
| 47 | +{ |
| 48 | + u64 inverseMask = ~(1ull << bitIndex); |
| 49 | + _bitSet[setIndex] &= inverseMask; |
| 50 | +} |
| 51 | + |
| 52 | +bool BitSet::Has(u32 index) const |
| 53 | +{ |
| 54 | + u32 setIndex = index / 64; |
| 55 | + u32 bitIndex = index % 64; |
| 56 | + return Has(setIndex, bitIndex); |
| 57 | +} |
| 58 | + |
| 59 | +bool BitSet::Has(u32 setIndex, u32 bitIndex) const |
| 60 | +{ |
| 61 | + return (_bitSet[setIndex] & (1ull << bitIndex)) != 0; |
| 62 | +} |
| 63 | + |
| 64 | +u64 BitSet::GetBitSet(u32 index) const |
| 65 | +{ |
| 66 | + return _bitSet[index]; |
| 67 | +} |
| 68 | + |
| 69 | +bool BitSet::IsSubsetOf(const BitSet& other) const |
| 70 | +{ |
| 71 | + u32 numBitSets = static_cast<u32>(_bitSet.Count()); |
| 72 | + |
| 73 | + // If they are different size, it is not a subset |
| 74 | + if (numBitSets != static_cast<u32>(other._bitSet.Count())) |
| 75 | + return false; |
| 76 | + |
| 77 | + for (u32 i = 0; i < numBitSets; i++) |
| 78 | + { |
| 79 | + u64 a = _bitSet[i]; |
| 80 | + u64 b = other._bitSet[i]; |
| 81 | + |
| 82 | + if ((a & b) != a) |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + return true; |
| 87 | +} |
| 88 | + |
| 89 | +void BitSet::BitwiseUnset(const BitSet& other) |
| 90 | +{ |
| 91 | + u32 numBitSets = static_cast<u32>(_bitSet.Count()); |
| 92 | + |
| 93 | + for (u32 i = 0; i < numBitSets; i++) |
| 94 | + { |
| 95 | + _bitSet[i] &= (~other._bitSet[i]); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void BitSet::BitwiseAND(const BitSet& other) |
| 100 | +{ |
| 101 | + u32 numBitSets = static_cast<u32>(_bitSet.Count()); |
| 102 | + |
| 103 | + for (u32 i = 0; i < numBitSets; i++) |
| 104 | + { |
| 105 | + _bitSet[i] &= other._bitSet[i]; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void BitSet::BitwiseOR(const BitSet& other) |
| 110 | +{ |
| 111 | + u32 numBitSets = static_cast<u32>(_bitSet.Count()); |
| 112 | + |
| 113 | + for (u32 i = 0; i < numBitSets; i++) |
| 114 | + { |
| 115 | + _bitSet[i] |= other._bitSet[i]; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void BitSet::BitwiseXOR(const BitSet& other) |
| 120 | +{ |
| 121 | + u32 numBitSets = static_cast<u32>(_bitSet.Count()); |
| 122 | + |
| 123 | + for (u32 i = 0; i < numBitSets; i++) |
| 124 | + { |
| 125 | + _bitSet[i] ^= other._bitSet[i]; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +BitSet* BitSet::NewBitwiseUnset(const BitSet& other) const |
| 130 | +{ |
| 131 | + u32 numBitSets = static_cast<u32>(_bitSet.Count()); |
| 132 | + |
| 133 | + BitSet* result = Memory::Allocator::New<BitSet>(_allocator, _allocator, numBitSets); |
| 134 | + *result = *this; |
| 135 | + |
| 136 | + result->BitwiseUnset(other); |
| 137 | + |
| 138 | + return result; |
| 139 | +} |
| 140 | + |
| 141 | +BitSet* BitSet::NewBitwiseAND(const BitSet& other) const |
| 142 | +{ |
| 143 | + u32 numBitSets = static_cast<u32>(_bitSet.Count()); |
| 144 | + |
| 145 | + BitSet* result = Memory::Allocator::New<BitSet>(_allocator, _allocator, numBitSets); |
| 146 | + *result = *this; |
| 147 | + |
| 148 | + result->BitwiseAND(other); |
| 149 | + |
| 150 | + return result; |
| 151 | +} |
| 152 | + |
| 153 | +BitSet* BitSet::NewBitwiseOR(const BitSet& other) const |
| 154 | +{ |
| 155 | + u32 numBitSets = static_cast<u32>(_bitSet.Count()); |
| 156 | + |
| 157 | + BitSet* result = Memory::Allocator::New<BitSet>(_allocator, _allocator, numBitSets); |
| 158 | + *result = *this; |
| 159 | + |
| 160 | + result->BitwiseOR(other); |
| 161 | + |
| 162 | + return result; |
| 163 | +} |
| 164 | + |
| 165 | +BitSet* BitSet::NewBitwiseXOR(const BitSet& other) const |
| 166 | +{ |
| 167 | + u32 numBitSets = static_cast<u32>(_bitSet.Count()); |
| 168 | + |
| 169 | + BitSet* result = Memory::Allocator::New<BitSet>(_allocator, _allocator, numBitSets); |
| 170 | + *result = *this; |
| 171 | + |
| 172 | + result->BitwiseXOR(other); |
| 173 | + |
| 174 | + return result; |
| 175 | +} |
| 176 | + |
| 177 | +u32 BitSet::NumBitSets() const |
| 178 | +{ |
| 179 | + return static_cast<u32>(_bitSet.Count()); |
| 180 | +} |
| 181 | + |
| 182 | +void BitSet::ForEachSetBit(std::function<void(u32 set, u32 bit)> callback) const |
| 183 | +{ |
| 184 | + u32 numBitSets = NumBitSets(); |
| 185 | + for (u32 i = 0; i < numBitSets; i++) |
| 186 | + { |
| 187 | + u64 bits = _bitSet[i]; |
| 188 | + while (bits != 0) |
| 189 | + { |
| 190 | + u32 bit = static_cast<u32>(std::countr_zero(bits)); |
| 191 | + callback(i, bit); |
| 192 | + bits &= bits - 1; // Clear lowest set bit |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments