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 typedef unsigned long BitWord;
45 typedef unsigned size_type;
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_next - Returns the index of the next set bit starting from the
136 /// "Curr" bit. Returns -1 if the next set bit is not found.
137 int find_next(unsigned Curr) const {
138 if (Curr >= BITS_PER_ELEMENT)
141 unsigned WordPos = Curr / BITWORD_SIZE;
142 unsigned BitPos = Curr % BITWORD_SIZE;
143 BitWord Copy = Bits[WordPos];
144 assert(WordPos <= BITWORDS_PER_ELEMENT
145 && "Word Position outside of element");
147 // Mask off previous bits.
148 Copy &= ~0UL << BitPos;
151 return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
153 // Check subsequent words.
154 for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
156 return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
160 // Union this element with RHS and return true if this one changed.
161 bool unionWith(const SparseBitVectorElement &RHS) {
162 bool changed = false;
163 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
164 BitWord old = changed ? 0 : Bits[i];
166 Bits[i] |= RHS.Bits[i];
167 if (!changed && old != Bits[i])
173 // Return true if we have any bits in common with RHS
174 bool intersects(const SparseBitVectorElement &RHS) const {
175 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
176 if (RHS.Bits[i] & Bits[i])
182 // Intersect this Element with RHS and return true if this one changed.
183 // BecameZero is set to true if this element became all-zero bits.
184 bool intersectWith(const SparseBitVectorElement &RHS,
186 bool changed = false;
190 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
191 BitWord old = changed ? 0 : Bits[i];
193 Bits[i] &= RHS.Bits[i];
197 if (!changed && old != Bits[i])
200 BecameZero = allzero;
204 // Intersect this Element with the complement of RHS and return true if this
205 // one changed. BecameZero is set to true if this element became all-zero
207 bool intersectWithComplement(const SparseBitVectorElement &RHS,
209 bool changed = false;
213 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
214 BitWord old = changed ? 0 : Bits[i];
216 Bits[i] &= ~RHS.Bits[i];
220 if (!changed && old != Bits[i])
223 BecameZero = allzero;
227 // Three argument version of intersectWithComplement that intersects
228 // RHS1 & ~RHS2 into this element
229 void intersectWithComplement(const SparseBitVectorElement &RHS1,
230 const SparseBitVectorElement &RHS2,
235 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
236 Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
240 BecameZero = allzero;
244 template <unsigned ElementSize = 128>
245 class SparseBitVector {
246 typedef std::list<SparseBitVectorElement<ElementSize>> ElementList;
247 typedef typename ElementList::iterator ElementListIter;
248 typedef typename ElementList::const_iterator ElementListConstIter;
250 BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
253 // Pointer to our current Element.
254 ElementListIter CurrElementIter;
255 ElementList Elements;
257 // This is like std::lower_bound, except we do linear searching from the
259 ElementListIter FindLowerBound(unsigned ElementIndex) {
261 if (Elements.empty()) {
262 CurrElementIter = Elements.begin();
263 return Elements.begin();
266 // Make sure our current iterator is valid.
267 if (CurrElementIter == Elements.end())
270 // Search from our current iterator, either backwards or forwards,
271 // depending on what element we are looking for.
272 ElementListIter ElementIter = CurrElementIter;
273 if (CurrElementIter->index() == ElementIndex) {
275 } else if (CurrElementIter->index() > ElementIndex) {
276 while (ElementIter != Elements.begin()
277 && ElementIter->index() > ElementIndex)
280 while (ElementIter != Elements.end() &&
281 ElementIter->index() < ElementIndex)
284 CurrElementIter = ElementIter;
288 // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
289 // than it would be, in order to be efficient.
290 class SparseBitVectorIterator {
294 const SparseBitVector<ElementSize> *BitVector = nullptr;
296 // Current element inside of bitmap.
297 ElementListConstIter Iter;
299 // Current bit number inside of our bitmap.
302 // Current word number inside of our element.
305 // Current bits from the element.
306 typename SparseBitVectorElement<ElementSize>::BitWord Bits;
308 // Move our iterator to the first non-zero bit in the bitmap.
309 void AdvanceToFirstNonZero() {
312 if (BitVector->Elements.empty()) {
316 Iter = BitVector->Elements.begin();
317 BitNumber = Iter->index() * ElementSize;
318 unsigned BitPos = Iter->find_first();
320 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
321 Bits = Iter->word(WordNumber);
322 Bits >>= BitPos % BITWORD_SIZE;
325 // Move our iterator to the next non-zero bit.
326 void AdvanceToNextNonZero() {
330 while (Bits && !(Bits & 1)) {
335 // See if we ran out of Bits in this word.
337 int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
338 // If we ran out of set bits in this element, move to next element.
339 if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
343 // We may run out of elements in the bitmap.
344 if (Iter == BitVector->Elements.end()) {
348 // Set up for next non-zero word in bitmap.
349 BitNumber = Iter->index() * ElementSize;
350 NextSetBitNumber = Iter->find_first();
351 BitNumber += NextSetBitNumber;
352 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
353 Bits = Iter->word(WordNumber);
354 Bits >>= NextSetBitNumber % BITWORD_SIZE;
356 WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
357 Bits = Iter->word(WordNumber);
358 Bits >>= NextSetBitNumber % BITWORD_SIZE;
359 BitNumber = Iter->index() * ElementSize;
360 BitNumber += NextSetBitNumber;
366 SparseBitVectorIterator() = default;
368 SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
369 bool end = false):BitVector(RHS) {
370 Iter = BitVector->Elements.begin();
375 AdvanceToFirstNonZero();
379 inline SparseBitVectorIterator& operator++() {
382 AdvanceToNextNonZero();
387 inline SparseBitVectorIterator operator++(int) {
388 SparseBitVectorIterator tmp = *this;
393 // Return the current set bit number.
394 unsigned operator*() const {
398 bool operator==(const SparseBitVectorIterator &RHS) const {
399 // If they are both at the end, ignore the rest of the fields.
400 if (AtEnd && RHS.AtEnd)
402 // Otherwise they are the same if they have the same bit number and
404 return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
407 bool operator!=(const SparseBitVectorIterator &RHS) const {
408 return !(*this == RHS);
413 typedef SparseBitVectorIterator iterator;
416 CurrElementIter = Elements.begin();
419 ~SparseBitVector() = default;
421 // SparseBitVector copy ctor.
422 SparseBitVector(const SparseBitVector &RHS) {
423 ElementListConstIter ElementIter = RHS.Elements.begin();
424 while (ElementIter != RHS.Elements.end()) {
425 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
429 CurrElementIter = Elements.begin ();
438 SparseBitVector& operator=(const SparseBitVector& RHS) {
444 ElementListConstIter ElementIter = RHS.Elements.begin();
445 while (ElementIter != RHS.Elements.end()) {
446 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
450 CurrElementIter = Elements.begin ();
455 // Test, Reset, and Set a bit in the bitmap.
456 bool test(unsigned Idx) {
457 if (Elements.empty())
460 unsigned ElementIndex = Idx / ElementSize;
461 ElementListIter ElementIter = FindLowerBound(ElementIndex);
463 // If we can't find an element that is supposed to contain this bit, there
464 // is nothing more to do.
465 if (ElementIter == Elements.end() ||
466 ElementIter->index() != ElementIndex)
468 return ElementIter->test(Idx % ElementSize);
471 void reset(unsigned Idx) {
472 if (Elements.empty())
475 unsigned ElementIndex = Idx / ElementSize;
476 ElementListIter ElementIter = FindLowerBound(ElementIndex);
478 // If we can't find an element that is supposed to contain this bit, there
479 // is nothing more to do.
480 if (ElementIter == Elements.end() ||
481 ElementIter->index() != ElementIndex)
483 ElementIter->reset(Idx % ElementSize);
485 // When the element is zeroed out, delete it.
486 if (ElementIter->empty()) {
488 Elements.erase(ElementIter);
492 void set(unsigned Idx) {
493 unsigned ElementIndex = Idx / ElementSize;
494 ElementListIter ElementIter;
495 if (Elements.empty()) {
496 ElementIter = Elements.emplace(Elements.end(), ElementIndex);
498 ElementIter = FindLowerBound(ElementIndex);
500 if (ElementIter == Elements.end() ||
501 ElementIter->index() != ElementIndex) {
502 // We may have hit the beginning of our SparseBitVector, in which case,
503 // we may need to insert right after this element, which requires moving
504 // the current iterator forward one, because insert does insert before.
505 if (ElementIter != Elements.end() &&
506 ElementIter->index() < ElementIndex)
508 ElementIter = Elements.emplace(ElementIter, ElementIndex);
511 CurrElementIter = ElementIter;
513 ElementIter->set(Idx % ElementSize);
516 bool test_and_set(unsigned Idx) {
517 bool old = test(Idx);
525 bool operator!=(const SparseBitVector &RHS) const {
526 return !(*this == RHS);
529 bool operator==(const SparseBitVector &RHS) const {
530 ElementListConstIter Iter1 = Elements.begin();
531 ElementListConstIter Iter2 = RHS.Elements.begin();
533 for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
535 if (*Iter1 != *Iter2)
538 return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
541 // Union our bitmap with the RHS and return true if we changed.
542 bool operator|=(const SparseBitVector &RHS) {
546 bool changed = false;
547 ElementListIter Iter1 = Elements.begin();
548 ElementListConstIter Iter2 = RHS.Elements.begin();
550 // If RHS is empty, we are done
551 if (RHS.Elements.empty())
554 while (Iter2 != RHS.Elements.end()) {
555 if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
556 Elements.insert(Iter1, *Iter2);
559 } else if (Iter1->index() == Iter2->index()) {
560 changed |= Iter1->unionWith(*Iter2);
567 CurrElementIter = Elements.begin();
571 // Intersect our bitmap with the RHS and return true if ours changed.
572 bool operator&=(const SparseBitVector &RHS) {
576 bool changed = false;
577 ElementListIter Iter1 = Elements.begin();
578 ElementListConstIter Iter2 = RHS.Elements.begin();
580 // Check if both bitmaps are empty.
581 if (Elements.empty() && RHS.Elements.empty())
584 // Loop through, intersecting as we go, erasing elements when necessary.
585 while (Iter2 != RHS.Elements.end()) {
586 if (Iter1 == Elements.end()) {
587 CurrElementIter = Elements.begin();
591 if (Iter1->index() > Iter2->index()) {
593 } else if (Iter1->index() == Iter2->index()) {
595 changed |= Iter1->intersectWith(*Iter2, BecameZero);
597 ElementListIter IterTmp = Iter1;
599 Elements.erase(IterTmp);
605 ElementListIter IterTmp = Iter1;
607 Elements.erase(IterTmp);
611 if (Iter1 != Elements.end()) {
612 Elements.erase(Iter1, Elements.end());
615 CurrElementIter = Elements.begin();
619 // Intersect our bitmap with the complement of the RHS and return true
621 bool intersectWithComplement(const SparseBitVector &RHS) {
630 bool changed = false;
631 ElementListIter Iter1 = Elements.begin();
632 ElementListConstIter Iter2 = RHS.Elements.begin();
634 // If either our bitmap or RHS is empty, we are done
635 if (Elements.empty() || RHS.Elements.empty())
638 // Loop through, intersecting as we go, erasing elements when necessary.
639 while (Iter2 != RHS.Elements.end()) {
640 if (Iter1 == Elements.end()) {
641 CurrElementIter = Elements.begin();
645 if (Iter1->index() > Iter2->index()) {
647 } else if (Iter1->index() == Iter2->index()) {
649 changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
651 ElementListIter IterTmp = Iter1;
653 Elements.erase(IterTmp);
662 CurrElementIter = Elements.begin();
666 bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
667 return intersectWithComplement(*RHS);
670 // Three argument version of intersectWithComplement.
671 // Result of RHS1 & ~RHS2 is stored into this bitmap.
672 void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
673 const SparseBitVector<ElementSize> &RHS2)
676 intersectWithComplement(RHS2);
678 } else if (this == &RHS2) {
679 SparseBitVector RHS2Copy(RHS2);
680 intersectWithComplement(RHS1, RHS2Copy);
685 CurrElementIter = Elements.begin();
686 ElementListConstIter Iter1 = RHS1.Elements.begin();
687 ElementListConstIter Iter2 = RHS2.Elements.begin();
689 // If RHS1 is empty, we are done
690 // If RHS2 is empty, we still have to copy RHS1
691 if (RHS1.Elements.empty())
694 // Loop through, intersecting as we go, erasing elements when necessary.
695 while (Iter2 != RHS2.Elements.end()) {
696 if (Iter1 == RHS1.Elements.end())
699 if (Iter1->index() > Iter2->index()) {
701 } else if (Iter1->index() == Iter2->index()) {
702 bool BecameZero = false;
703 Elements.emplace_back(Iter1->index());
704 Elements.back().intersectWithComplement(*Iter1, *Iter2, BecameZero);
710 Elements.push_back(*Iter1++);
714 // copy the remaining elements
715 std::copy(Iter1, RHS1.Elements.end(), std::back_inserter(Elements));
718 void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
719 const SparseBitVector<ElementSize> *RHS2) {
720 intersectWithComplement(*RHS1, *RHS2);
723 bool intersects(const SparseBitVector<ElementSize> *RHS) const {
724 return intersects(*RHS);
727 // Return true if we share any bits in common with RHS
728 bool intersects(const SparseBitVector<ElementSize> &RHS) const {
729 ElementListConstIter Iter1 = Elements.begin();
730 ElementListConstIter Iter2 = RHS.Elements.begin();
732 // Check if both bitmaps are empty.
733 if (Elements.empty() && RHS.Elements.empty())
736 // Loop through, intersecting stopping when we hit bits in common.
737 while (Iter2 != RHS.Elements.end()) {
738 if (Iter1 == Elements.end())
741 if (Iter1->index() > Iter2->index()) {
743 } else if (Iter1->index() == Iter2->index()) {
744 if (Iter1->intersects(*Iter2))
755 // Return true iff all bits set in this SparseBitVector are
757 bool contains(const SparseBitVector<ElementSize> &RHS) const {
758 SparseBitVector<ElementSize> Result(*this);
760 return (Result == RHS);
763 // Return the first set bit in the bitmap. Return -1 if no bits are set.
764 int find_first() const {
765 if (Elements.empty())
767 const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
768 return (First.index() * ElementSize) + First.find_first();
771 // Return true if the SparseBitVector is empty
773 return Elements.empty();
776 unsigned count() const {
777 unsigned BitCount = 0;
778 for (ElementListConstIter Iter = Elements.begin();
779 Iter != Elements.end();
781 BitCount += Iter->count();
786 iterator begin() const {
787 return iterator(this);
790 iterator end() const {
791 return iterator(this, true);
795 // Convenience functions to allow Or and And without dereferencing in the user
798 template <unsigned ElementSize>
799 inline bool operator |=(SparseBitVector<ElementSize> &LHS,
800 const SparseBitVector<ElementSize> *RHS) {
804 template <unsigned ElementSize>
805 inline bool operator |=(SparseBitVector<ElementSize> *LHS,
806 const SparseBitVector<ElementSize> &RHS) {
807 return LHS->operator|=(RHS);
810 template <unsigned ElementSize>
811 inline bool operator &=(SparseBitVector<ElementSize> *LHS,
812 const SparseBitVector<ElementSize> &RHS) {
813 return LHS->operator&=(RHS);
816 template <unsigned ElementSize>
817 inline bool operator &=(SparseBitVector<ElementSize> &LHS,
818 const SparseBitVector<ElementSize> *RHS) {
822 // Convenience functions for infix union, intersection, difference operators.
824 template <unsigned ElementSize>
825 inline SparseBitVector<ElementSize>
826 operator|(const SparseBitVector<ElementSize> &LHS,
827 const SparseBitVector<ElementSize> &RHS) {
828 SparseBitVector<ElementSize> Result(LHS);
833 template <unsigned ElementSize>
834 inline SparseBitVector<ElementSize>
835 operator&(const SparseBitVector<ElementSize> &LHS,
836 const SparseBitVector<ElementSize> &RHS) {
837 SparseBitVector<ElementSize> Result(LHS);
842 template <unsigned ElementSize>
843 inline SparseBitVector<ElementSize>
844 operator-(const SparseBitVector<ElementSize> &LHS,
845 const SparseBitVector<ElementSize> &RHS) {
846 SparseBitVector<ElementSize> Result;
847 Result.intersectWithComplement(LHS, RHS);
851 // Dump a SparseBitVector to a stream
852 template <unsigned ElementSize>
853 void dump(const SparseBitVector<ElementSize> &LHS, raw_ostream &out) {
856 typename SparseBitVector<ElementSize>::iterator bi = LHS.begin(),
860 for (++bi; bi != be; ++bi) {
867 } // end namespace llvm
869 #endif // LLVM_ADT_SPARSEBITVECTOR_H