[llvm] db0d636 - [ADT][BitVector][NFC] Merge find_first_in() / find_first_unset_in()
Stefanos Baziotis via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 28 18:51:59 PDT 2020
Author: Stefanos Baziotis
Date: 2020-07-29T04:51:22+03:00
New Revision: db0d636e52c163196bc8abeb62b649b7de700569
URL: https://github.com/llvm/llvm-project/commit/db0d636e52c163196bc8abeb62b649b7de700569
DIFF: https://github.com/llvm/llvm-project/commit/db0d636e52c163196bc8abeb62b649b7de700569.diff
LOG: [ADT][BitVector][NFC] Merge find_first_in() / find_first_unset_in()
We can implement find_first_unset_in() in the same function
if every BitWord we use is first flipped.
Differential Revision: https://reviews.llvm.org/D84717
Added:
Modified:
llvm/include/llvm/ADT/BitVector.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/BitVector.h b/llvm/include/llvm/ADT/BitVector.h
index a8d0f07af94a..2a857786f454 100644
--- a/llvm/include/llvm/ADT/BitVector.h
+++ b/llvm/include/llvm/ADT/BitVector.h
@@ -203,9 +203,10 @@ class BitVector {
return !any();
}
- /// find_first_in - Returns the index of the first set bit in the range
- /// [Begin, End). Returns -1 if all bits in the range are unset.
- int find_first_in(unsigned Begin, unsigned End) const {
+ /// find_first_in - Returns the index of the first set / unset bit,
+ /// depending on \p Set, in the range [Begin, End).
+ /// Returns -1 if all bits in the range are unset / set.
+ int find_first_in(unsigned Begin, unsigned End, bool Set = true) const {
assert(Begin <= End && End <= Size);
if (Begin == End)
return -1;
@@ -214,8 +215,14 @@ class BitVector {
unsigned LastWord = (End - 1) / BITWORD_SIZE;
// Check subsequent words.
+ // The code below is based on search for the first _set_ bit. If
+ // we're searching for the first _unset_, we just take the
+ // complement of each word before we use it and apply
+ // the same method.
for (unsigned i = FirstWord; i <= LastWord; ++i) {
BitWord Copy = Bits[i];
+ if (!Set)
+ Copy = ~Copy;
if (i == FirstWord) {
unsigned FirstBit = Begin % BITWORD_SIZE;
@@ -266,32 +273,7 @@ class BitVector {
/// find_first_unset_in - Returns the index of the first unset bit in the
/// range [Begin, End). Returns -1 if all bits in the range are set.
int find_first_unset_in(unsigned Begin, unsigned End) const {
- assert(Begin <= End && End <= Size);
- if (Begin == End)
- return -1;
-
- unsigned FirstWord = Begin / BITWORD_SIZE;
- unsigned LastWord = (End - 1) / BITWORD_SIZE;
-
- // Check subsequent words.
- for (unsigned i = FirstWord; i <= LastWord; ++i) {
- BitWord Copy = Bits[i];
-
- if (i == FirstWord) {
- unsigned FirstBit = Begin % BITWORD_SIZE;
- Copy |= maskTrailingOnes<BitWord>(FirstBit);
- }
-
- if (i == LastWord) {
- unsigned LastBit = (End - 1) % BITWORD_SIZE;
- Copy |= maskTrailingZeros<BitWord>(LastBit + 1);
- }
- if (Copy != ~BitWord(0)) {
- unsigned Result = i * BITWORD_SIZE + countTrailingOnes(Copy);
- return Result < size() ? Result : -1;
- }
- }
- return -1;
+ return find_first_in(Begin, End, /* Set = */ false);
}
/// find_last_unset_in - Returns the index of the last unset bit in the
More information about the llvm-commits
mailing list