summaryrefslogtreecommitdiff
path: root/include/llvm/ADT/BitVector.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/llvm/ADT/BitVector.h')
-rw-r--r--include/llvm/ADT/BitVector.h43
1 files changed, 42 insertions, 1 deletions
diff --git a/include/llvm/ADT/BitVector.h b/include/llvm/ADT/BitVector.h
index cf3756d0d9c1f..8240d01ae977c 100644
--- a/include/llvm/ADT/BitVector.h
+++ b/include/llvm/ADT/BitVector.h
@@ -161,6 +161,17 @@ public:
return -1;
}
+ /// find_first_unset - Returns the index of the first unset bit, -1 if all
+ /// of the bits are set.
+ int find_first_unset() const {
+ for (unsigned i = 0; i < NumBitWords(size()); ++i)
+ if (Bits[i] != ~0UL) {
+ unsigned Result = i * BITWORD_SIZE + countTrailingOnes(Bits[i]);
+ return Result < size() ? Result : -1;
+ }
+ return -1;
+ }
+
/// find_next - Returns the index of the next set bit following the
/// "Prev" bit. Returns -1 if the next set bit is not found.
int find_next(unsigned Prev) const {
@@ -184,6 +195,30 @@ public:
return -1;
}
+ /// find_next_unset - Returns the index of the next usnet bit following the
+ /// "Prev" bit. Returns -1 if all remaining bits are set.
+ int find_next_unset(unsigned Prev) const {
+ ++Prev;
+ if (Prev >= Size)
+ return -1;
+
+ unsigned WordPos = Prev / BITWORD_SIZE;
+ unsigned BitPos = Prev % BITWORD_SIZE;
+ BitWord Copy = Bits[WordPos];
+ // Mask in previous bits.
+ BitWord Mask = (1 << BitPos) - 1;
+ Copy |= Mask;
+
+ if (Copy != ~0UL)
+ return next_unset_in_word(WordPos, Copy);
+
+ // Check subsequent words.
+ for (unsigned i = WordPos + 1; i < NumBitWords(size()); ++i)
+ if (Bits[i] != ~0UL)
+ return next_unset_in_word(i, Bits[i]);
+ return -1;
+ }
+
/// clear - Clear all bits.
void clear() {
Size = 0;
@@ -503,6 +538,11 @@ public:
}
private:
+ int next_unset_in_word(int WordIndex, BitWord Word) const {
+ unsigned Result = WordIndex * BITWORD_SIZE + countTrailingOnes(Word);
+ return Result < size() ? Result : -1;
+ }
+
unsigned NumBitWords(unsigned S) const {
return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
}
@@ -539,7 +579,8 @@ private:
}
void init_words(BitWord *B, unsigned NumWords, bool t) {
- memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
+ if (NumWords > 0)
+ memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
}
template<bool AddBits, bool InvertMask>