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/ADT/ilist.h"
19 #include "llvm/ADT/ilist_node.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/MathExtras.h"
23 #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>
43 struct SparseBitVectorElement
44 : public ilist_node<SparseBitVectorElement<ElementSize> > {
46 typedef unsigned long BitWord;
47 typedef unsigned size_type;
49 BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT,
50 BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
51 BITS_PER_ELEMENT = ElementSize
55 // Index of Element in terms of where first bit starts.
56 unsigned ElementIndex;
57 BitWord Bits[BITWORDS_PER_ELEMENT];
58 // Needed for sentinels
59 friend struct ilist_sentinel_traits<SparseBitVectorElement>;
60 SparseBitVectorElement() {
62 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
66 explicit SparseBitVectorElement(unsigned Idx) {
68 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
72 bool operator==(const SparseBitVectorElement &RHS) const {
73 if (ElementIndex != RHS.ElementIndex)
75 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
76 if (Bits[i] != RHS.Bits[i])
81 bool operator!=(const SparseBitVectorElement &RHS) const {
82 return !(*this == RHS);
85 // Return the bits that make up word Idx in our element.
86 BitWord word(unsigned Idx) const {
87 assert (Idx < BITWORDS_PER_ELEMENT);
91 unsigned index() const {
96 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
102 void set(unsigned Idx) {
103 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
106 bool test_and_set (unsigned Idx) {
107 bool old = test(Idx);
115 void reset(unsigned Idx) {
116 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
119 bool test(unsigned Idx) const {
120 return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
123 size_type count() const {
124 unsigned NumBits = 0;
125 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
126 NumBits += countPopulation(Bits[i]);
130 /// find_first - Returns the index of the first set bit.
131 int find_first() const {
132 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
134 return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
135 llvm_unreachable("Illegal empty element");
138 /// find_next - Returns the index of the next set bit starting from the
139 /// "Curr" bit. Returns -1 if the next set bit is not found.
140 int find_next(unsigned Curr) const {
141 if (Curr >= BITS_PER_ELEMENT)
144 unsigned WordPos = Curr / BITWORD_SIZE;
145 unsigned BitPos = Curr % BITWORD_SIZE;
146 BitWord Copy = Bits[WordPos];
147 assert (WordPos <= BITWORDS_PER_ELEMENT
148 && "Word Position outside of element");
150 // Mask off previous bits.
151 Copy &= ~0UL << BitPos;
154 return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
156 // Check subsequent words.
157 for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
159 return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
163 // Union this element with RHS and return true if this one changed.
164 bool unionWith(const SparseBitVectorElement &RHS) {
165 bool changed = false;
166 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
167 BitWord old = changed ? 0 : Bits[i];
169 Bits[i] |= RHS.Bits[i];
170 if (!changed && old != Bits[i])
176 // Return true if we have any bits in common with RHS
177 bool intersects(const SparseBitVectorElement &RHS) const {
178 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
179 if (RHS.Bits[i] & Bits[i])
185 // Intersect this Element with RHS and return true if this one changed.
186 // BecameZero is set to true if this element became all-zero bits.
187 bool intersectWith(const SparseBitVectorElement &RHS,
189 bool changed = false;
193 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
194 BitWord old = changed ? 0 : Bits[i];
196 Bits[i] &= RHS.Bits[i];
200 if (!changed && old != Bits[i])
203 BecameZero = allzero;
207 // Intersect this Element with the complement of RHS and return true if this
208 // one changed. BecameZero is set to true if this element became all-zero
210 bool intersectWithComplement(const SparseBitVectorElement &RHS,
212 bool changed = false;
216 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
217 BitWord old = changed ? 0 : Bits[i];
219 Bits[i] &= ~RHS.Bits[i];
223 if (!changed && old != Bits[i])
226 BecameZero = allzero;
230 // Three argument version of intersectWithComplement that intersects
231 // RHS1 & ~RHS2 into this element
232 void intersectWithComplement(const SparseBitVectorElement &RHS1,
233 const SparseBitVectorElement &RHS2,
238 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
239 Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
243 BecameZero = allzero;
247 template <unsigned ElementSize>
248 struct ilist_traits<SparseBitVectorElement<ElementSize> >
249 : public ilist_default_traits<SparseBitVectorElement<ElementSize> > {
250 typedef SparseBitVectorElement<ElementSize> Element;
252 Element *createSentinel() const { return static_cast<Element *>(&Sentinel); }
253 static void destroySentinel(Element *) {}
255 Element *provideInitialHead() const { return createSentinel(); }
256 Element *ensureHead(Element *) const { return createSentinel(); }
257 static void noteHead(Element *, Element *) {}
260 mutable ilist_half_node<Element> Sentinel;
263 template <unsigned ElementSize = 128>
264 class SparseBitVector {
265 typedef ilist<SparseBitVectorElement<ElementSize> > ElementList;
266 typedef typename ElementList::iterator ElementListIter;
267 typedef typename ElementList::const_iterator ElementListConstIter;
269 BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
272 // Pointer to our current Element.
273 ElementListIter CurrElementIter;
274 ElementList Elements;
276 // This is like std::lower_bound, except we do linear searching from the
278 ElementListIter FindLowerBound(unsigned ElementIndex) {
280 if (Elements.empty()) {
281 CurrElementIter = Elements.begin();
282 return Elements.begin();
285 // Make sure our current iterator is valid.
286 if (CurrElementIter == Elements.end())
289 // Search from our current iterator, either backwards or forwards,
290 // depending on what element we are looking for.
291 ElementListIter ElementIter = CurrElementIter;
292 if (CurrElementIter->index() == ElementIndex) {
294 } else if (CurrElementIter->index() > ElementIndex) {
295 while (ElementIter != Elements.begin()
296 && ElementIter->index() > ElementIndex)
299 while (ElementIter != Elements.end() &&
300 ElementIter->index() < ElementIndex)
303 CurrElementIter = ElementIter;
307 // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
308 // than it would be, in order to be efficient.
309 class SparseBitVectorIterator {
313 const SparseBitVector<ElementSize> *BitVector;
315 // Current element inside of bitmap.
316 ElementListConstIter Iter;
318 // Current bit number inside of our bitmap.
321 // Current word number inside of our element.
324 // Current bits from the element.
325 typename SparseBitVectorElement<ElementSize>::BitWord Bits;
327 // Move our iterator to the first non-zero bit in the bitmap.
328 void AdvanceToFirstNonZero() {
331 if (BitVector->Elements.empty()) {
335 Iter = BitVector->Elements.begin();
336 BitNumber = Iter->index() * ElementSize;
337 unsigned BitPos = Iter->find_first();
339 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
340 Bits = Iter->word(WordNumber);
341 Bits >>= BitPos % BITWORD_SIZE;
344 // Move our iterator to the next non-zero bit.
345 void AdvanceToNextNonZero() {
349 while (Bits && !(Bits & 1)) {
354 // See if we ran out of Bits in this word.
356 int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
357 // If we ran out of set bits in this element, move to next element.
358 if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
362 // We may run out of elements in the bitmap.
363 if (Iter == BitVector->Elements.end()) {
367 // Set up for next non-zero word in bitmap.
368 BitNumber = Iter->index() * ElementSize;
369 NextSetBitNumber = Iter->find_first();
370 BitNumber += NextSetBitNumber;
371 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
372 Bits = Iter->word(WordNumber);
373 Bits >>= NextSetBitNumber % BITWORD_SIZE;
375 WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
376 Bits = Iter->word(WordNumber);
377 Bits >>= NextSetBitNumber % BITWORD_SIZE;
378 BitNumber = Iter->index() * ElementSize;
379 BitNumber += NextSetBitNumber;
385 inline SparseBitVectorIterator& operator++() {
388 AdvanceToNextNonZero();
393 inline SparseBitVectorIterator operator++(int) {
394 SparseBitVectorIterator tmp = *this;
399 // Return the current set bit number.
400 unsigned operator*() const {
404 bool operator==(const SparseBitVectorIterator &RHS) const {
405 // If they are both at the end, ignore the rest of the fields.
406 if (AtEnd && RHS.AtEnd)
408 // Otherwise they are the same if they have the same bit number and
410 return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
413 bool operator!=(const SparseBitVectorIterator &RHS) const {
414 return !(*this == RHS);
417 SparseBitVectorIterator(): BitVector(nullptr) {
420 SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
421 bool end = false):BitVector(RHS) {
422 Iter = BitVector->Elements.begin();
427 AdvanceToFirstNonZero();
431 typedef SparseBitVectorIterator iterator;
434 CurrElementIter = Elements.begin ();
440 // SparseBitVector copy ctor.
441 SparseBitVector(const SparseBitVector &RHS) {
442 ElementListConstIter ElementIter = RHS.Elements.begin();
443 while (ElementIter != RHS.Elements.end()) {
444 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
448 CurrElementIter = Elements.begin ();
457 SparseBitVector& operator=(const SparseBitVector& RHS) {
463 ElementListConstIter ElementIter = RHS.Elements.begin();
464 while (ElementIter != RHS.Elements.end()) {
465 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
469 CurrElementIter = Elements.begin ();
474 // Test, Reset, and Set a bit in the bitmap.
475 bool test(unsigned Idx) {
476 if (Elements.empty())
479 unsigned ElementIndex = Idx / ElementSize;
480 ElementListIter ElementIter = FindLowerBound(ElementIndex);
482 // If we can't find an element that is supposed to contain this bit, there
483 // is nothing more to do.
484 if (ElementIter == Elements.end() ||
485 ElementIter->index() != ElementIndex)
487 return ElementIter->test(Idx % ElementSize);
490 void reset(unsigned Idx) {
491 if (Elements.empty())
494 unsigned ElementIndex = Idx / ElementSize;
495 ElementListIter ElementIter = FindLowerBound(ElementIndex);
497 // If we can't find an element that is supposed to contain this bit, there
498 // is nothing more to do.
499 if (ElementIter == Elements.end() ||
500 ElementIter->index() != ElementIndex)
502 ElementIter->reset(Idx % ElementSize);
504 // When the element is zeroed out, delete it.
505 if (ElementIter->empty()) {
507 Elements.erase(ElementIter);
511 void set(unsigned Idx) {
512 unsigned ElementIndex = Idx / ElementSize;
513 SparseBitVectorElement<ElementSize> *Element;
514 ElementListIter ElementIter;
515 if (Elements.empty()) {
516 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
517 ElementIter = Elements.insert(Elements.end(), Element);
520 ElementIter = FindLowerBound(ElementIndex);
522 if (ElementIter == Elements.end() ||
523 ElementIter->index() != ElementIndex) {
524 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
525 // We may have hit the beginning of our SparseBitVector, in which case,
526 // we may need to insert right after this element, which requires moving
527 // the current iterator forward one, because insert does insert before.
528 if (ElementIter != Elements.end() &&
529 ElementIter->index() < ElementIndex)
530 ElementIter = Elements.insert(++ElementIter, Element);
532 ElementIter = Elements.insert(ElementIter, Element);
535 CurrElementIter = ElementIter;
537 ElementIter->set(Idx % ElementSize);
540 bool test_and_set (unsigned Idx) {
541 bool old = test(Idx);
549 bool operator!=(const SparseBitVector &RHS) const {
550 return !(*this == RHS);
553 bool operator==(const SparseBitVector &RHS) const {
554 ElementListConstIter Iter1 = Elements.begin();
555 ElementListConstIter Iter2 = RHS.Elements.begin();
557 for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
559 if (*Iter1 != *Iter2)
562 return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
565 // Union our bitmap with the RHS and return true if we changed.
566 bool operator|=(const SparseBitVector &RHS) {
570 bool changed = false;
571 ElementListIter Iter1 = Elements.begin();
572 ElementListConstIter Iter2 = RHS.Elements.begin();
574 // If RHS is empty, we are done
575 if (RHS.Elements.empty())
578 while (Iter2 != RHS.Elements.end()) {
579 if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
580 Elements.insert(Iter1,
581 new SparseBitVectorElement<ElementSize>(*Iter2));
584 } else if (Iter1->index() == Iter2->index()) {
585 changed |= Iter1->unionWith(*Iter2);
592 CurrElementIter = Elements.begin();
596 // Intersect our bitmap with the RHS and return true if ours changed.
597 bool operator&=(const SparseBitVector &RHS) {
601 bool changed = false;
602 ElementListIter Iter1 = Elements.begin();
603 ElementListConstIter Iter2 = RHS.Elements.begin();
605 // Check if both bitmaps are empty.
606 if (Elements.empty() && RHS.Elements.empty())
609 // Loop through, intersecting as we go, erasing elements when necessary.
610 while (Iter2 != RHS.Elements.end()) {
611 if (Iter1 == Elements.end()) {
612 CurrElementIter = Elements.begin();
616 if (Iter1->index() > Iter2->index()) {
618 } else if (Iter1->index() == Iter2->index()) {
620 changed |= Iter1->intersectWith(*Iter2, BecameZero);
622 ElementListIter IterTmp = Iter1;
624 Elements.erase(IterTmp);
630 ElementListIter IterTmp = Iter1;
632 Elements.erase(IterTmp);
636 if (Iter1 != Elements.end()) {
637 Elements.erase(Iter1, Elements.end());
640 CurrElementIter = Elements.begin();
644 // Intersect our bitmap with the complement of the RHS and return true
646 bool intersectWithComplement(const SparseBitVector &RHS) {
655 bool changed = false;
656 ElementListIter Iter1 = Elements.begin();
657 ElementListConstIter Iter2 = RHS.Elements.begin();
659 // If either our bitmap or RHS is empty, we are done
660 if (Elements.empty() || RHS.Elements.empty())
663 // Loop through, intersecting as we go, erasing elements when necessary.
664 while (Iter2 != RHS.Elements.end()) {
665 if (Iter1 == Elements.end()) {
666 CurrElementIter = Elements.begin();
670 if (Iter1->index() > Iter2->index()) {
672 } else if (Iter1->index() == Iter2->index()) {
674 changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
676 ElementListIter IterTmp = Iter1;
678 Elements.erase(IterTmp);
687 CurrElementIter = Elements.begin();
691 bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
692 return intersectWithComplement(*RHS);
695 // Three argument version of intersectWithComplement.
696 // Result of RHS1 & ~RHS2 is stored into this bitmap.
697 void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
698 const SparseBitVector<ElementSize> &RHS2)
701 intersectWithComplement(RHS2);
703 } else if (this == &RHS2) {
704 SparseBitVector RHS2Copy(RHS2);
705 intersectWithComplement(RHS1, RHS2Copy);
710 CurrElementIter = Elements.begin();
711 ElementListConstIter Iter1 = RHS1.Elements.begin();
712 ElementListConstIter Iter2 = RHS2.Elements.begin();
714 // If RHS1 is empty, we are done
715 // If RHS2 is empty, we still have to copy RHS1
716 if (RHS1.Elements.empty())
719 // Loop through, intersecting as we go, erasing elements when necessary.
720 while (Iter2 != RHS2.Elements.end()) {
721 if (Iter1 == RHS1.Elements.end())
724 if (Iter1->index() > Iter2->index()) {
726 } else if (Iter1->index() == Iter2->index()) {
727 bool BecameZero = false;
728 SparseBitVectorElement<ElementSize> *NewElement =
729 new SparseBitVectorElement<ElementSize>(Iter1->index());
730 NewElement->intersectWithComplement(*Iter1, *Iter2, BecameZero);
732 Elements.push_back(NewElement);
739 SparseBitVectorElement<ElementSize> *NewElement =
740 new SparseBitVectorElement<ElementSize>(*Iter1);
741 Elements.push_back(NewElement);
746 // copy the remaining elements
747 while (Iter1 != RHS1.Elements.end()) {
748 SparseBitVectorElement<ElementSize> *NewElement =
749 new SparseBitVectorElement<ElementSize>(*Iter1);
750 Elements.push_back(NewElement);
755 void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
756 const SparseBitVector<ElementSize> *RHS2) {
757 intersectWithComplement(*RHS1, *RHS2);
760 bool intersects(const SparseBitVector<ElementSize> *RHS) const {
761 return intersects(*RHS);
764 // Return true if we share any bits in common with RHS
765 bool intersects(const SparseBitVector<ElementSize> &RHS) const {
766 ElementListConstIter Iter1 = Elements.begin();
767 ElementListConstIter Iter2 = RHS.Elements.begin();
769 // Check if both bitmaps are empty.
770 if (Elements.empty() && RHS.Elements.empty())
773 // Loop through, intersecting stopping when we hit bits in common.
774 while (Iter2 != RHS.Elements.end()) {
775 if (Iter1 == Elements.end())
778 if (Iter1->index() > Iter2->index()) {
780 } else if (Iter1->index() == Iter2->index()) {
781 if (Iter1->intersects(*Iter2))
792 // Return true iff all bits set in this SparseBitVector are
794 bool contains(const SparseBitVector<ElementSize> &RHS) const {
795 SparseBitVector<ElementSize> Result(*this);
797 return (Result == RHS);
800 // Return the first set bit in the bitmap. Return -1 if no bits are set.
801 int find_first() const {
802 if (Elements.empty())
804 const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
805 return (First.index() * ElementSize) + First.find_first();
808 // Return true if the SparseBitVector is empty
810 return Elements.empty();
813 unsigned count() const {
814 unsigned BitCount = 0;
815 for (ElementListConstIter Iter = Elements.begin();
816 Iter != Elements.end();
818 BitCount += Iter->count();
822 iterator begin() const {
823 return iterator(this);
826 iterator end() const {
827 return iterator(this, true);
831 // Convenience functions to allow Or and And without dereferencing in the user
834 template <unsigned ElementSize>
835 inline bool operator |=(SparseBitVector<ElementSize> &LHS,
836 const SparseBitVector<ElementSize> *RHS) {
840 template <unsigned ElementSize>
841 inline bool operator |=(SparseBitVector<ElementSize> *LHS,
842 const SparseBitVector<ElementSize> &RHS) {
843 return LHS->operator|=(RHS);
846 template <unsigned ElementSize>
847 inline bool operator &=(SparseBitVector<ElementSize> *LHS,
848 const SparseBitVector<ElementSize> &RHS) {
849 return LHS->operator&=(RHS);
852 template <unsigned ElementSize>
853 inline bool operator &=(SparseBitVector<ElementSize> &LHS,
854 const SparseBitVector<ElementSize> *RHS) {
858 // Convenience functions for infix union, intersection, difference operators.
860 template <unsigned ElementSize>
861 inline SparseBitVector<ElementSize>
862 operator|(const SparseBitVector<ElementSize> &LHS,
863 const SparseBitVector<ElementSize> &RHS) {
864 SparseBitVector<ElementSize> Result(LHS);
869 template <unsigned ElementSize>
870 inline SparseBitVector<ElementSize>
871 operator&(const SparseBitVector<ElementSize> &LHS,
872 const SparseBitVector<ElementSize> &RHS) {
873 SparseBitVector<ElementSize> Result(LHS);
878 template <unsigned ElementSize>
879 inline SparseBitVector<ElementSize>
880 operator-(const SparseBitVector<ElementSize> &LHS,
881 const SparseBitVector<ElementSize> &RHS) {
882 SparseBitVector<ElementSize> Result;
883 Result.intersectWithComplement(LHS, RHS);
887 // Dump a SparseBitVector to a stream
888 template <unsigned ElementSize>
889 void dump(const SparseBitVector<ElementSize> &LHS, raw_ostream &out) {
892 typename SparseBitVector<ElementSize>::iterator bi = LHS.begin(),
896 for (++bi; bi != be; ++bi) {
902 } // end namespace llvm
904 #endif // LLVM_ADT_SPARSEBITVECTOR_H