1 //===- llvm/ADT/SparseBitVector.h - Efficient Sparse BitVector --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SparseBitVector class. See the doxygen comment for
11 // SparseBitVector for more details on the algorithm used.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ADT_SPARSEBITVECTOR_H
16 #define LLVM_ADT_SPARSEBITVECTOR_H
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Support/raw_ostream.h"
29 /// SparseBitVector is an implementation of a bitvector that is sparse by only
30 /// storing the elements that have non-zero bits set. In order to make this
31 /// fast for the most common cases, SparseBitVector is implemented as a linked
32 /// list of SparseBitVectorElements. We maintain a pointer to the last
33 /// SparseBitVectorElement accessed (in the form of a list iterator), in order
34 /// to make multiple in-order test/set constant time after the first one is
35 /// executed. Note that using vectors to store SparseBitVectorElement's does
36 /// not work out very well because it causes insertion in the middle to take
37 /// enormous amounts of time with a large amount of bits. Other structures that
38 /// have better worst cases for insertion in the middle (various balanced trees,
39 /// etc) do not perform as well in practice as a linked list with this iterator
40 /// kept up to date. They are also significantly more memory intensive.
42 template <unsigned ElementSize = 128> struct SparseBitVectorElement {
44 using BitWord = unsigned long;
45 using size_type = unsigned;
47 BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT,
48 BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
49 BITS_PER_ELEMENT = ElementSize
53 // Index of Element in terms of where first bit starts.
54 unsigned ElementIndex;
55 BitWord Bits[BITWORDS_PER_ELEMENT];
57 SparseBitVectorElement() {
59 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
63 explicit SparseBitVectorElement(unsigned Idx) {
65 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
69 bool operator==(const SparseBitVectorElement &RHS) const {
70 if (ElementIndex != RHS.ElementIndex)
72 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
73 if (Bits[i] != RHS.Bits[i])
78 bool operator!=(const SparseBitVectorElement &RHS) const {
79 return !(*this == RHS);
82 // Return the bits that make up word Idx in our element.
83 BitWord word(unsigned Idx) const {
84 assert(Idx < BITWORDS_PER_ELEMENT);
88 unsigned index() const {
93 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
99 void set(unsigned Idx) {
100 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
103 bool test_and_set(unsigned Idx) {
104 bool old = test(Idx);
112 void reset(unsigned Idx) {
113 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
116 bool test(unsigned Idx) const {
117 return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
120 size_type count() const {
121 unsigned NumBits = 0;
122 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
123 NumBits += countPopulation(Bits[i]);
127 /// find_first - Returns the index of the first set bit.
128 int find_first() const {
129 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
131 return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
132 llvm_unreachable("Illegal empty element");
135 /// find_last - Returns the index of the last set bit.
136 int find_last() const {
137 for (unsigned I = 0; I < BITWORDS_PER_ELEMENT; ++I) {
138 unsigned Idx = BITWORDS_PER_ELEMENT - I - 1;
140 return Idx * BITWORD_SIZE + BITWORD_SIZE -
141 countLeadingZeros(Bits[Idx]) - 1;
143 llvm_unreachable("Illegal empty element");
146 /// find_next - Returns the index of the next set bit starting from the
147 /// "Curr" bit. Returns -1 if the next set bit is not found.
148 int find_next(unsigned Curr) const {
149 if (Curr >= BITS_PER_ELEMENT)
152 unsigned WordPos = Curr / BITWORD_SIZE;
153 unsigned BitPos = Curr % BITWORD_SIZE;
154 BitWord Copy = Bits[WordPos];
155 assert(WordPos <= BITWORDS_PER_ELEMENT
156 && "Word Position outside of element");
158 // Mask off previous bits.
159 Copy &= ~0UL << BitPos;
162 return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
164 // Check subsequent words.
165 for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
167 return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
171 // Union this element with RHS and return true if this one changed.
172 bool unionWith(const SparseBitVectorElement &RHS) {
173 bool changed = false;
174 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
175 BitWord old = changed ? 0 : Bits[i];
177 Bits[i] |= RHS.Bits[i];
178 if (!changed && old != Bits[i])
184 // Return true if we have any bits in common with RHS
185 bool intersects(const SparseBitVectorElement &RHS) const {
186 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
187 if (RHS.Bits[i] & Bits[i])
193 // Intersect this Element with RHS and return true if this one changed.
194 // BecameZero is set to true if this element became all-zero bits.
195 bool intersectWith(const SparseBitVectorElement &RHS,
197 bool changed = false;
201 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
202 BitWord old = changed ? 0 : Bits[i];
204 Bits[i] &= RHS.Bits[i];
208 if (!changed && old != Bits[i])
211 BecameZero = allzero;
215 // Intersect this Element with the complement of RHS and return true if this
216 // one changed. BecameZero is set to true if this element became all-zero
218 bool intersectWithComplement(const SparseBitVectorElement &RHS,
220 bool changed = false;
224 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
225 BitWord old = changed ? 0 : Bits[i];
227 Bits[i] &= ~RHS.Bits[i];
231 if (!changed && old != Bits[i])
234 BecameZero = allzero;
238 // Three argument version of intersectWithComplement that intersects
239 // RHS1 & ~RHS2 into this element
240 void intersectWithComplement(const SparseBitVectorElement &RHS1,
241 const SparseBitVectorElement &RHS2,
246 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
247 Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
251 BecameZero = allzero;
255 template <unsigned ElementSize = 128>
256 class SparseBitVector {
257 using ElementList = std::list<SparseBitVectorElement<ElementSize>>;
258 using ElementListIter = typename ElementList::iterator;
259 using ElementListConstIter = typename ElementList::const_iterator;
261 BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
264 ElementList Elements;
265 // Pointer to our current Element. This has no visible effect on the external
266 // state of a SparseBitVector, it's just used to improve performance in the
267 // common case of testing/modifying bits with similar indices.
268 mutable ElementListIter CurrElementIter;
270 // This is like std::lower_bound, except we do linear searching from the
272 ElementListIter FindLowerBoundImpl(unsigned ElementIndex) const {
274 // We cache a non-const iterator so we're forced to resort to const_cast to
275 // get the begin/end in the case where 'this' is const. To avoid duplication
276 // of code with the only difference being whether the const cast is present
277 // 'this' is always const in this particular function and we sort out the
278 // difference in FindLowerBound and FindLowerBoundConst.
279 ElementListIter Begin =
280 const_cast<SparseBitVector<ElementSize> *>(this)->Elements.begin();
281 ElementListIter End =
282 const_cast<SparseBitVector<ElementSize> *>(this)->Elements.end();
284 if (Elements.empty()) {
285 CurrElementIter = Begin;
286 return CurrElementIter;
289 // Make sure our current iterator is valid.
290 if (CurrElementIter == End)
293 // Search from our current iterator, either backwards or forwards,
294 // depending on what element we are looking for.
295 ElementListIter ElementIter = CurrElementIter;
296 if (CurrElementIter->index() == ElementIndex) {
298 } else if (CurrElementIter->index() > ElementIndex) {
299 while (ElementIter != Begin
300 && ElementIter->index() > ElementIndex)
303 while (ElementIter != End &&
304 ElementIter->index() < ElementIndex)
307 CurrElementIter = ElementIter;
310 ElementListConstIter FindLowerBoundConst(unsigned ElementIndex) const {
311 return FindLowerBoundImpl(ElementIndex);
313 ElementListIter FindLowerBound(unsigned ElementIndex) {
314 return FindLowerBoundImpl(ElementIndex);
317 // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
318 // than it would be, in order to be efficient.
319 class SparseBitVectorIterator {
323 const SparseBitVector<ElementSize> *BitVector = nullptr;
325 // Current element inside of bitmap.
326 ElementListConstIter Iter;
328 // Current bit number inside of our bitmap.
331 // Current word number inside of our element.
334 // Current bits from the element.
335 typename SparseBitVectorElement<ElementSize>::BitWord Bits;
337 // Move our iterator to the first non-zero bit in the bitmap.
338 void AdvanceToFirstNonZero() {
341 if (BitVector->Elements.empty()) {
345 Iter = BitVector->Elements.begin();
346 BitNumber = Iter->index() * ElementSize;
347 unsigned BitPos = Iter->find_first();
349 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
350 Bits = Iter->word(WordNumber);
351 Bits >>= BitPos % BITWORD_SIZE;
354 // Move our iterator to the next non-zero bit.
355 void AdvanceToNextNonZero() {
359 while (Bits && !(Bits & 1)) {
364 // See if we ran out of Bits in this word.
366 int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
367 // If we ran out of set bits in this element, move to next element.
368 if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
372 // We may run out of elements in the bitmap.
373 if (Iter == BitVector->Elements.end()) {
377 // Set up for next non-zero word in bitmap.
378 BitNumber = Iter->index() * ElementSize;
379 NextSetBitNumber = Iter->find_first();
380 BitNumber += NextSetBitNumber;
381 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
382 Bits = Iter->word(WordNumber);
383 Bits >>= NextSetBitNumber % BITWORD_SIZE;
385 WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
386 Bits = Iter->word(WordNumber);
387 Bits >>= NextSetBitNumber % BITWORD_SIZE;
388 BitNumber = Iter->index() * ElementSize;
389 BitNumber += NextSetBitNumber;
395 SparseBitVectorIterator() = default;
397 SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
398 bool end = false):BitVector(RHS) {
399 Iter = BitVector->Elements.begin();
404 AdvanceToFirstNonZero();
408 inline SparseBitVectorIterator& operator++() {
411 AdvanceToNextNonZero();
416 inline SparseBitVectorIterator operator++(int) {
417 SparseBitVectorIterator tmp = *this;
422 // Return the current set bit number.
423 unsigned operator*() const {
427 bool operator==(const SparseBitVectorIterator &RHS) const {
428 // If they are both at the end, ignore the rest of the fields.
429 if (AtEnd && RHS.AtEnd)
431 // Otherwise they are the same if they have the same bit number and
433 return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
436 bool operator!=(const SparseBitVectorIterator &RHS) const {
437 return !(*this == RHS);
442 using iterator = SparseBitVectorIterator;
444 SparseBitVector() : Elements(), CurrElementIter(Elements.begin()) {}
446 SparseBitVector(const SparseBitVector &RHS)
447 : Elements(RHS.Elements), CurrElementIter(Elements.begin()) {}
448 SparseBitVector(SparseBitVector &&RHS)
449 : Elements(std::move(RHS.Elements)), CurrElementIter(Elements.begin()) {}
457 SparseBitVector& operator=(const SparseBitVector& RHS) {
461 Elements = RHS.Elements;
462 CurrElementIter = Elements.begin();
465 SparseBitVector &operator=(SparseBitVector &&RHS) {
466 Elements = std::move(RHS.Elements);
467 CurrElementIter = Elements.begin();
471 // Test, Reset, and Set a bit in the bitmap.
472 bool test(unsigned Idx) const {
473 if (Elements.empty())
476 unsigned ElementIndex = Idx / ElementSize;
477 ElementListConstIter ElementIter = FindLowerBoundConst(ElementIndex);
479 // If we can't find an element that is supposed to contain this bit, there
480 // is nothing more to do.
481 if (ElementIter == Elements.end() ||
482 ElementIter->index() != ElementIndex)
484 return ElementIter->test(Idx % ElementSize);
487 void reset(unsigned Idx) {
488 if (Elements.empty())
491 unsigned ElementIndex = Idx / ElementSize;
492 ElementListIter ElementIter = FindLowerBound(ElementIndex);
494 // If we can't find an element that is supposed to contain this bit, there
495 // is nothing more to do.
496 if (ElementIter == Elements.end() ||
497 ElementIter->index() != ElementIndex)
499 ElementIter->reset(Idx % ElementSize);
501 // When the element is zeroed out, delete it.
502 if (ElementIter->empty()) {
504 Elements.erase(ElementIter);
508 void set(unsigned Idx) {
509 unsigned ElementIndex = Idx / ElementSize;
510 ElementListIter ElementIter;
511 if (Elements.empty()) {
512 ElementIter = Elements.emplace(Elements.end(), ElementIndex);
514 ElementIter = FindLowerBound(ElementIndex);
516 if (ElementIter == Elements.end() ||
517 ElementIter->index() != ElementIndex) {
518 // We may have hit the beginning of our SparseBitVector, in which case,
519 // we may need to insert right after this element, which requires moving
520 // the current iterator forward one, because insert does insert before.
521 if (ElementIter != Elements.end() &&
522 ElementIter->index() < ElementIndex)
524 ElementIter = Elements.emplace(ElementIter, ElementIndex);
527 CurrElementIter = ElementIter;
529 ElementIter->set(Idx % ElementSize);
532 bool test_and_set(unsigned Idx) {
533 bool old = test(Idx);
541 bool operator!=(const SparseBitVector &RHS) const {
542 return !(*this == RHS);
545 bool operator==(const SparseBitVector &RHS) const {
546 ElementListConstIter Iter1 = Elements.begin();
547 ElementListConstIter Iter2 = RHS.Elements.begin();
549 for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
551 if (*Iter1 != *Iter2)
554 return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
557 // Union our bitmap with the RHS and return true if we changed.
558 bool operator|=(const SparseBitVector &RHS) {
562 bool changed = false;
563 ElementListIter Iter1 = Elements.begin();
564 ElementListConstIter Iter2 = RHS.Elements.begin();
566 // If RHS is empty, we are done
567 if (RHS.Elements.empty())
570 while (Iter2 != RHS.Elements.end()) {
571 if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
572 Elements.insert(Iter1, *Iter2);
575 } else if (Iter1->index() == Iter2->index()) {
576 changed |= Iter1->unionWith(*Iter2);
583 CurrElementIter = Elements.begin();
587 // Intersect our bitmap with the RHS and return true if ours changed.
588 bool operator&=(const SparseBitVector &RHS) {
592 bool changed = false;
593 ElementListIter Iter1 = Elements.begin();
594 ElementListConstIter Iter2 = RHS.Elements.begin();
596 // Check if both bitmaps are empty.
597 if (Elements.empty() && RHS.Elements.empty())
600 // Loop through, intersecting as we go, erasing elements when necessary.
601 while (Iter2 != RHS.Elements.end()) {
602 if (Iter1 == Elements.end()) {
603 CurrElementIter = Elements.begin();
607 if (Iter1->index() > Iter2->index()) {
609 } else if (Iter1->index() == Iter2->index()) {
611 changed |= Iter1->intersectWith(*Iter2, BecameZero);
613 ElementListIter IterTmp = Iter1;
615 Elements.erase(IterTmp);
621 ElementListIter IterTmp = Iter1;
623 Elements.erase(IterTmp);
627 if (Iter1 != Elements.end()) {
628 Elements.erase(Iter1, Elements.end());
631 CurrElementIter = Elements.begin();
635 // Intersect our bitmap with the complement of the RHS and return true
637 bool intersectWithComplement(const SparseBitVector &RHS) {
646 bool changed = false;
647 ElementListIter Iter1 = Elements.begin();
648 ElementListConstIter Iter2 = RHS.Elements.begin();
650 // If either our bitmap or RHS is empty, we are done
651 if (Elements.empty() || RHS.Elements.empty())
654 // Loop through, intersecting as we go, erasing elements when necessary.
655 while (Iter2 != RHS.Elements.end()) {
656 if (Iter1 == Elements.end()) {
657 CurrElementIter = Elements.begin();
661 if (Iter1->index() > Iter2->index()) {
663 } else if (Iter1->index() == Iter2->index()) {
665 changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
667 ElementListIter IterTmp = Iter1;
669 Elements.erase(IterTmp);
678 CurrElementIter = Elements.begin();
682 bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
683 return intersectWithComplement(*RHS);
686 // Three argument version of intersectWithComplement.
687 // Result of RHS1 & ~RHS2 is stored into this bitmap.
688 void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
689 const SparseBitVector<ElementSize> &RHS2)
692 intersectWithComplement(RHS2);
694 } else if (this == &RHS2) {
695 SparseBitVector RHS2Copy(RHS2);
696 intersectWithComplement(RHS1, RHS2Copy);
701 CurrElementIter = Elements.begin();
702 ElementListConstIter Iter1 = RHS1.Elements.begin();
703 ElementListConstIter Iter2 = RHS2.Elements.begin();
705 // If RHS1 is empty, we are done
706 // If RHS2 is empty, we still have to copy RHS1
707 if (RHS1.Elements.empty())
710 // Loop through, intersecting as we go, erasing elements when necessary.
711 while (Iter2 != RHS2.Elements.end()) {
712 if (Iter1 == RHS1.Elements.end())
715 if (Iter1->index() > Iter2->index()) {
717 } else if (Iter1->index() == Iter2->index()) {
718 bool BecameZero = false;
719 Elements.emplace_back(Iter1->index());
720 Elements.back().intersectWithComplement(*Iter1, *Iter2, BecameZero);
726 Elements.push_back(*Iter1++);
730 // copy the remaining elements
731 std::copy(Iter1, RHS1.Elements.end(), std::back_inserter(Elements));
734 void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
735 const SparseBitVector<ElementSize> *RHS2) {
736 intersectWithComplement(*RHS1, *RHS2);
739 bool intersects(const SparseBitVector<ElementSize> *RHS) const {
740 return intersects(*RHS);
743 // Return true if we share any bits in common with RHS
744 bool intersects(const SparseBitVector<ElementSize> &RHS) const {
745 ElementListConstIter Iter1 = Elements.begin();
746 ElementListConstIter Iter2 = RHS.Elements.begin();
748 // Check if both bitmaps are empty.
749 if (Elements.empty() && RHS.Elements.empty())
752 // Loop through, intersecting stopping when we hit bits in common.
753 while (Iter2 != RHS.Elements.end()) {
754 if (Iter1 == Elements.end())
757 if (Iter1->index() > Iter2->index()) {
759 } else if (Iter1->index() == Iter2->index()) {
760 if (Iter1->intersects(*Iter2))
771 // Return true iff all bits set in this SparseBitVector are
773 bool contains(const SparseBitVector<ElementSize> &RHS) const {
774 SparseBitVector<ElementSize> Result(*this);
776 return (Result == RHS);
779 // Return the first set bit in the bitmap. Return -1 if no bits are set.
780 int find_first() const {
781 if (Elements.empty())
783 const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
784 return (First.index() * ElementSize) + First.find_first();
787 // Return the last set bit in the bitmap. Return -1 if no bits are set.
788 int find_last() const {
789 if (Elements.empty())
791 const SparseBitVectorElement<ElementSize> &Last = *(Elements.rbegin());
792 return (Last.index() * ElementSize) + Last.find_last();
795 // Return true if the SparseBitVector is empty
797 return Elements.empty();
800 unsigned count() const {
801 unsigned BitCount = 0;
802 for (ElementListConstIter Iter = Elements.begin();
803 Iter != Elements.end();
805 BitCount += Iter->count();
810 iterator begin() const {
811 return iterator(this);
814 iterator end() const {
815 return iterator(this, true);
819 // Convenience functions to allow Or and And without dereferencing in the user
822 template <unsigned ElementSize>
823 inline bool operator |=(SparseBitVector<ElementSize> &LHS,
824 const SparseBitVector<ElementSize> *RHS) {
828 template <unsigned ElementSize>
829 inline bool operator |=(SparseBitVector<ElementSize> *LHS,
830 const SparseBitVector<ElementSize> &RHS) {
831 return LHS->operator|=(RHS);
834 template <unsigned ElementSize>
835 inline bool operator &=(SparseBitVector<ElementSize> *LHS,
836 const SparseBitVector<ElementSize> &RHS) {
837 return LHS->operator&=(RHS);
840 template <unsigned ElementSize>
841 inline bool operator &=(SparseBitVector<ElementSize> &LHS,
842 const SparseBitVector<ElementSize> *RHS) {
846 // Convenience functions for infix union, intersection, difference operators.
848 template <unsigned ElementSize>
849 inline SparseBitVector<ElementSize>
850 operator|(const SparseBitVector<ElementSize> &LHS,
851 const SparseBitVector<ElementSize> &RHS) {
852 SparseBitVector<ElementSize> Result(LHS);
857 template <unsigned ElementSize>
858 inline SparseBitVector<ElementSize>
859 operator&(const SparseBitVector<ElementSize> &LHS,
860 const SparseBitVector<ElementSize> &RHS) {
861 SparseBitVector<ElementSize> Result(LHS);
866 template <unsigned ElementSize>
867 inline SparseBitVector<ElementSize>
868 operator-(const SparseBitVector<ElementSize> &LHS,
869 const SparseBitVector<ElementSize> &RHS) {
870 SparseBitVector<ElementSize> Result;
871 Result.intersectWithComplement(LHS, RHS);
875 // Dump a SparseBitVector to a stream
876 template <unsigned ElementSize>
877 void dump(const SparseBitVector<ElementSize> &LHS, raw_ostream &out) {
880 typename SparseBitVector<ElementSize>::iterator bi = LHS.begin(),
884 for (++bi; bi != be; ++bi) {
891 } // end namespace llvm
893 #endif // LLVM_ADT_SPARSEBITVECTOR_H