/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/** * An object like std::bitset but which provides access to the underlying * storage. * * The limited API is due to expedience only; feel free to flesh out any * std::bitset-like members.
*/ template <size_t N, typename Word = size_t> class BitSet {
static_assert(std::is_unsigned_v<Word>, "The Word type must be an unsigned integral type");
static_assert(N != 0);
for (const Word& word : mStorage) { if constexpr (kBitsPerWord > 32) {
count += CountPopulation64(word);
} else {
count += CountPopulation32(word);
}
}
return count;
}
// Set all bits to false. void ResetAll() { PodArrayZero(mStorage); }
// Set all bits to true. void SetAll() {
memset(mStorage.begin(), 0xff, kNumWords * sizeof(Word));
ResetPaddingBits();
}
void Flip() { for (Word& word : mStorage) {
word = ~word;
}
ResetPaddingBits();
}
// Return the position of the first bit set, or SIZE_MAX if none.
size_t FindFirst() const { return FindNext(0); }
// Return the position of the next bit set starting from |aFromPos| inclusive, // or SIZE_MAX if none.
size_t FindNext(size_t aFromPos) const {
MOZ_ASSERT(aFromPos < N);
size_t wordIndex = aFromPos / kBitsPerWord;
size_t bitIndex = aFromPos % kBitsPerWord;
Word word = mStorage[wordIndex]; // Mask word containing |aFromPos|.
word &= (Word(-1) << bitIndex); while (word == 0) {
wordIndex++; if (wordIndex == kNumWords) { return SIZE_MAX;
}
word = mStorage[wordIndex];
}
// Return the position of the previous bit set starting from |aFromPos| // inclusive, or SIZE_MAX if none.
size_t FindPrev(size_t aFromPos) const {
MOZ_ASSERT(aFromPos < N);
size_t wordIndex = aFromPos / kBitsPerWord;
size_t bitIndex = aFromPos % kBitsPerWord;
Word word = mStorage[wordIndex]; // Mask word containing |aFromPos|.
word &= Word(-1) >> (kBitsPerWord - 1 - bitIndex); while (word == 0) { if (wordIndex == 0) { return SIZE_MAX;
}
wordIndex--;
word = mStorage[wordIndex];
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.