Debian Codegraph @

mfbt/BitSet.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef mozilla_BitSet_h
8 #define mozilla_BitSet_h
9
10 #include "mozilla/Array.h"
11 #include "mozilla/MathAlgorithms.h"
12 #include "mozilla/Span.h"
13
14 namespace mozilla {
15
16 enum MemoryOrdering : uint8_t;
17 template <typename T, MemoryOrdering Order, typename Enable>
18 class Atomic;
19
20 namespace detail {
21
22 template <typename T>
23 struct UnwrapMaybeAtomic {
24 using Type = T;
25 };
26 template <typename T, MemoryOrdering Order, typename Enable>
27 struct UnwrapMaybeAtomic<mozilla::Atomic<T, Order, Enable>> {
28 using Type = T;
29 };
30
31 } // namespace detail
32
33 /**
34 * An object like std::bitset but which provides access to the underlying
35 * storage.
36 *
37 * The type |StorageType| must be an unsigned integer or a mozilla::Atomic
38 * wrapping an unsigned integer. Use of atomic types makes word access atomic,
39 * but does not make operations that operate on the whole bitset atomic.
40 *
41 * The limited API is due to expedience only; feel free to flesh out any
42 * std::bitset-like members.
43 */
44 template <size_t N, typename StorageType = size_t>
45 class BitSet {
46 public:
47 using Word = typename detail::UnwrapMaybeAtomic<StorageType>::Type;
48 static_assert(sizeof(Word) == sizeof(StorageType));
49 static_assert(
50 std::is_unsigned_v<Word>,
51 "StorageType must be an unsigned integral type, or equivalent Atomic");
52 static_assert(N != 0);
53
54 private:
55 static constexpr size_t kBitsPerWord = 8 * sizeof(Word);
56 static constexpr size_t kNumWords = (N + kBitsPerWord - 1) / kBitsPerWord;
57 static constexpr size_t kPaddingBits = (kNumWords * kBitsPerWord) - N;
58 static constexpr Word kPaddingMask = Word(-1) >> kPaddingBits;
59
60 // The zeroth bit in the bitset is the least significant bit of mStorage[0].
61 Array<StorageType, kNumWords> mStorage;
62
63 constexpr void ResetPaddingBits() {
64 if constexpr (kPaddingBits != 0) {
65 mStorage[kNumWords - 1] &= kPaddingMask;
66 }
67 }
68
69 public:
70 class Reference {
71 public:
72 Reference(BitSet<N, StorageType>& aBitSet, size_t aPos)
73 : mBitSet(aBitSet), mPos(aPos) {}
74
75 Reference& operator=(bool aValue) {
76 auto bit = Word(1) << (mPos % kBitsPerWord);
77 auto& word = mBitSet.mStorage[mPos / kBitsPerWord];
78 if (aValue) {
79 word |= bit;
80 } else {
81 word &= ~bit;
82 }
83 return *this;
84 }
85
86 MOZ_IMPLICIT operator bool() const { return mBitSet.test(mPos); }
87
88 private:
89 BitSet<N, StorageType>& mBitSet;
90 size_t mPos;
91 };
92
93 constexpr BitSet() : mStorage() {}
94
95 BitSet(const BitSet& aOther) { *this = aOther; }
96
97 BitSet& operator=(const BitSet& aOther) {
98 for (size_t i = 0; i < std::size(mStorage); i++) {
99 mStorage[i] = Word(aOther.mStorage[i]);
100 }
101 return *this;
102 }
103
104 explicit BitSet(Span<StorageType, kNumWords> aStorage) {
105 for (size_t i = 0; i < std::size(mStorage); i++) {
106 mStorage[i] = Word(aStorage[i]);
107 }
108 }
109
110 static constexpr size_t size() { return N; }
111
112 constexpr bool test(size_t aPos) const {
113 MOZ_ASSERT(aPos < N);
114 return mStorage[aPos / kBitsPerWord] & (Word(1) << (aPos % kBitsPerWord));
115 }
116
117 constexpr bool IsEmpty() const {
118 for (const StorageType& word : mStorage) {
119 if (word) {
120 return false;
121 }
122 }
123 return true;
124 }
125
126 explicit constexpr operator bool() { return !IsEmpty(); }
127
128 constexpr bool operator[](size_t aPos) const { return test(aPos); }
129
130 Reference operator[](size_t aPos) {
131 MOZ_ASSERT(aPos < N);
132 return {*this, aPos};
133 }
134
135 BitSet operator|(const BitSet<N, StorageType>& aOther) {
136 BitSet result = *this;
137 result |= aOther;
138 return result;
139 }
140
141 BitSet& operator|=(const BitSet<N, StorageType>& aOther) {
142 for (size_t i = 0; i < std::size(mStorage); i++) {
143 mStorage[i] |= aOther.mStorage[i];
144 }
145 return *this;
146 }
147
148 BitSet operator~() const {
149 BitSet result = *this;
150 result.Flip();
151 return result;
152 }
153
154 BitSet& operator&=(const BitSet<N, StorageType>& aOther) {
155 for (size_t i = 0; i < std::size(mStorage); i++) {
156 mStorage[i] &= aOther.mStorage[i];
157 }
158 return *this;
159 }
160
161 BitSet operator&(const BitSet<N, StorageType>& aOther) const {
162 BitSet result = *this;
163 result &= aOther;
164 return result;
165 }
166
167 bool operator==(const BitSet<N, StorageType>& aOther) const {
168 return mStorage == aOther.mStorage;
169 }
170
171 size_t Count() const {
172 size_t count = 0;
173
174 for (const StorageType& word : mStorage) {
175 if constexpr (kBitsPerWord > 32) {
176 count += CountPopulation64(word);
177 } else {
178 count += CountPopulation32(word);
179 }
180 }
181
182 return count;
183 }
184
185 // Set all bits to false.
186 void ResetAll() {
187 for (StorageType& word : mStorage) {
188 word = Word(0);
189 }
190 }
191
192 // Set all bits to true.
193 void SetAll() {
194 for (StorageType& word : mStorage) {
195 word = ~Word(0);
196 }
197 ResetPaddingBits();
198 }
199
200 void Flip() {
201 for (StorageType& word : mStorage) {
202 word = ~word;
203 }
204
205 ResetPaddingBits();
206 }
207
208 // Return the position of the first bit set, or SIZE_MAX if none.
209 size_t FindFirst() const { return FindNext(0); }
210
211 // Return the position of the next bit set starting from |aFromPos| inclusive,
212 // or SIZE_MAX if none.
213 size_t FindNext(size_t aFromPos) const {
214 MOZ_ASSERT(aFromPos < N);
215 size_t wordIndex = aFromPos / kBitsPerWord;
216 size_t bitIndex = aFromPos % kBitsPerWord;
217
218 Word word = mStorage[wordIndex];
219 // Mask word containing |aFromPos|.
220 word &= (Word(-1) << bitIndex);
221 while (word == 0) {
222 wordIndex++;
223 if (wordIndex == kNumWords) {
224 return SIZE_MAX;
225 }
226 word = mStorage[wordIndex];
227 }
228
229 uint_fast8_t pos = CountTrailingZeroes(word);
230 return wordIndex * kBitsPerWord + pos;
231 }
232
233 size_t FindLast() const { return FindPrev(size() - 1); }
234
235 // Return the position of the previous bit set starting from |aFromPos|
236 // inclusive, or SIZE_MAX if none.
237 size_t FindPrev(size_t aFromPos) const {
238 MOZ_ASSERT(aFromPos < N);
239 size_t wordIndex = aFromPos / kBitsPerWord;
240 size_t bitIndex = aFromPos % kBitsPerWord;
241
242 Word word = mStorage[wordIndex];
243 // Mask word containing |aFromPos|.
244 word &= Word(-1) >> (kBitsPerWord - 1 - bitIndex);
245 while (word == 0) {
246 if (wordIndex == 0) {
247 return SIZE_MAX;
248 }
249 wordIndex--;
250 word = mStorage[wordIndex];
251 }
252
253 uint_fast8_t pos = FindMostSignificantBit(word);
254 return wordIndex * kBitsPerWord + pos;
255 }
256
257 Span<StorageType> Storage() { return mStorage; }
258
259 Span<const StorageType> Storage() const { return mStorage; }
260 };
261
262 } // namespace mozilla
263
264 #endif // mozilla_BitSet_h