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.
43 template <unsigned ElementSize = 128>
44 struct SparseBitVectorElement
45 : public ilist_node<SparseBitVectorElement<ElementSize> > {
47 typedef unsigned long BitWord;
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 unsigned count() const {
124 unsigned NumBits = 0;
125 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
126 if (sizeof(BitWord) == 4)
127 NumBits += CountPopulation_32(Bits[i]);
128 else if (sizeof(BitWord) == 8)
129 NumBits += CountPopulation_64(Bits[i]);
131 llvm_unreachable("Unsupported!");
135 /// find_first - Returns the index of the first set bit.
136 int find_first() const {
137 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
139 if (sizeof(BitWord) == 4)
140 return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
141 if (sizeof(BitWord) == 8)
142 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
143 llvm_unreachable("Unsupported!");
145 llvm_unreachable("Illegal empty element");
148 /// find_next - Returns the index of the next set bit starting from the
149 /// "Curr" bit. Returns -1 if the next set bit is not found.
150 int find_next(unsigned Curr) const {
151 if (Curr >= BITS_PER_ELEMENT)
154 unsigned WordPos = Curr / BITWORD_SIZE;
155 unsigned BitPos = Curr % BITWORD_SIZE;
156 BitWord Copy = Bits[WordPos];
157 assert (WordPos <= BITWORDS_PER_ELEMENT
158 && "Word Position outside of element");
160 // Mask off previous bits.
161 Copy &= ~0L << BitPos;
164 if (sizeof(BitWord) == 4)
165 return WordPos * BITWORD_SIZE + CountTrailingZeros_32(Copy);
166 if (sizeof(BitWord) == 8)
167 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
168 llvm_unreachable("Unsupported!");
171 // Check subsequent words.
172 for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
174 if (sizeof(BitWord) == 4)
175 return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
176 if (sizeof(BitWord) == 8)
177 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
178 llvm_unreachable("Unsupported!");
183 // Union this element with RHS and return true if this one changed.
184 bool unionWith(const SparseBitVectorElement &RHS) {
185 bool changed = false;
186 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
187 BitWord old = changed ? 0 : Bits[i];
189 Bits[i] |= RHS.Bits[i];
190 if (!changed && old != Bits[i])
196 // Return true if we have any bits in common with RHS
197 bool intersects(const SparseBitVectorElement &RHS) const {
198 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
199 if (RHS.Bits[i] & Bits[i])
205 // Intersect this Element with RHS and return true if this one changed.
206 // BecameZero is set to true if this element became all-zero bits.
207 bool intersectWith(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;
226 // Intersect this Element with the complement of RHS and return true if this
227 // one changed. BecameZero is set to true if this element became all-zero
229 bool intersectWithComplement(const SparseBitVectorElement &RHS,
231 bool changed = false;
235 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
236 BitWord old = changed ? 0 : Bits[i];
238 Bits[i] &= ~RHS.Bits[i];
242 if (!changed && old != Bits[i])
245 BecameZero = allzero;
248 // Three argument version of intersectWithComplement that intersects
249 // RHS1 & ~RHS2 into this element
250 void intersectWithComplement(const SparseBitVectorElement &RHS1,
251 const SparseBitVectorElement &RHS2,
256 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
257 Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
261 BecameZero = allzero;
265 template <unsigned ElementSize = 128>
266 class SparseBitVector {
267 typedef ilist<SparseBitVectorElement<ElementSize> > ElementList;
268 typedef typename ElementList::iterator ElementListIter;
269 typedef typename ElementList::const_iterator ElementListConstIter;
271 BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
274 // Pointer to our current Element.
275 ElementListIter CurrElementIter;
276 ElementList Elements;
278 // This is like std::lower_bound, except we do linear searching from the
280 ElementListIter FindLowerBound(unsigned ElementIndex) {
282 if (Elements.empty()) {
283 CurrElementIter = Elements.begin();
284 return Elements.begin();
287 // Make sure our current iterator is valid.
288 if (CurrElementIter == Elements.end())
291 // Search from our current iterator, either backwards or forwards,
292 // depending on what element we are looking for.
293 ElementListIter ElementIter = CurrElementIter;
294 if (CurrElementIter->index() == ElementIndex) {
296 } else if (CurrElementIter->index() > ElementIndex) {
297 while (ElementIter != Elements.begin()
298 && ElementIter->index() > ElementIndex)
301 while (ElementIter != Elements.end() &&
302 ElementIter->index() < ElementIndex)
305 CurrElementIter = ElementIter;
309 // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
310 // than it would be, in order to be efficient.
311 class SparseBitVectorIterator {
315 const SparseBitVector<ElementSize> *BitVector;
317 // Current element inside of bitmap.
318 ElementListConstIter Iter;
320 // Current bit number inside of our bitmap.
323 // Current word number inside of our element.
326 // Current bits from the element.
327 typename SparseBitVectorElement<ElementSize>::BitWord Bits;
329 // Move our iterator to the first non-zero bit in the bitmap.
330 void AdvanceToFirstNonZero() {
333 if (BitVector->Elements.empty()) {
337 Iter = BitVector->Elements.begin();
338 BitNumber = Iter->index() * ElementSize;
339 unsigned BitPos = Iter->find_first();
341 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
342 Bits = Iter->word(WordNumber);
343 Bits >>= BitPos % BITWORD_SIZE;
346 // Move our iterator to the next non-zero bit.
347 void AdvanceToNextNonZero() {
351 while (Bits && !(Bits & 1)) {
356 // See if we ran out of Bits in this word.
358 int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
359 // If we ran out of set bits in this element, move to next element.
360 if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
364 // We may run out of elements in the bitmap.
365 if (Iter == BitVector->Elements.end()) {
369 // Set up for next non zero word in bitmap.
370 BitNumber = Iter->index() * ElementSize;
371 NextSetBitNumber = Iter->find_first();
372 BitNumber += NextSetBitNumber;
373 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
374 Bits = Iter->word(WordNumber);
375 Bits >>= NextSetBitNumber % BITWORD_SIZE;
377 WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
378 Bits = Iter->word(WordNumber);
379 Bits >>= NextSetBitNumber % BITWORD_SIZE;
380 BitNumber = Iter->index() * ElementSize;
381 BitNumber += NextSetBitNumber;
387 inline SparseBitVectorIterator& operator++() {
390 AdvanceToNextNonZero();
395 inline SparseBitVectorIterator operator++(int) {
396 SparseBitVectorIterator tmp = *this;
401 // Return the current set bit number.
402 unsigned operator*() const {
406 bool operator==(const SparseBitVectorIterator &RHS) const {
407 // If they are both at the end, ignore the rest of the fields.
408 if (AtEnd && RHS.AtEnd)
410 // Otherwise they are the same if they have the same bit number and
412 return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
414 bool operator!=(const SparseBitVectorIterator &RHS) const {
415 return !(*this == RHS);
417 SparseBitVectorIterator(): BitVector(NULL) {
421 SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
422 bool end = false):BitVector(RHS) {
423 Iter = BitVector->Elements.begin();
428 AdvanceToFirstNonZero();
432 typedef SparseBitVectorIterator iterator;
435 CurrElementIter = Elements.begin ();
441 // SparseBitVector copy ctor.
442 SparseBitVector(const SparseBitVector &RHS) {
443 ElementListConstIter ElementIter = RHS.Elements.begin();
444 while (ElementIter != RHS.Elements.end()) {
445 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
449 CurrElementIter = Elements.begin ();
458 SparseBitVector& operator=(const SparseBitVector& RHS) {
461 ElementListConstIter ElementIter = RHS.Elements.begin();
462 while (ElementIter != RHS.Elements.end()) {
463 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
467 CurrElementIter = Elements.begin ();
472 // Test, Reset, and Set a bit in the bitmap.
473 bool test(unsigned Idx) {
474 if (Elements.empty())
477 unsigned ElementIndex = Idx / ElementSize;
478 ElementListIter ElementIter = FindLowerBound(ElementIndex);
480 // If we can't find an element that is supposed to contain this bit, there
481 // is nothing more to do.
482 if (ElementIter == Elements.end() ||
483 ElementIter->index() != ElementIndex)
485 return ElementIter->test(Idx % ElementSize);
488 void reset(unsigned Idx) {
489 if (Elements.empty())
492 unsigned ElementIndex = Idx / ElementSize;
493 ElementListIter ElementIter = FindLowerBound(ElementIndex);
495 // If we can't find an element that is supposed to contain this bit, there
496 // is nothing more to do.
497 if (ElementIter == Elements.end() ||
498 ElementIter->index() != ElementIndex)
500 ElementIter->reset(Idx % ElementSize);
502 // When the element is zeroed out, delete it.
503 if (ElementIter->empty()) {
505 Elements.erase(ElementIter);
509 void set(unsigned Idx) {
510 unsigned ElementIndex = Idx / ElementSize;
511 SparseBitVectorElement<ElementSize> *Element;
512 ElementListIter ElementIter;
513 if (Elements.empty()) {
514 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
515 ElementIter = Elements.insert(Elements.end(), Element);
518 ElementIter = FindLowerBound(ElementIndex);
520 if (ElementIter == Elements.end() ||
521 ElementIter->index() != ElementIndex) {
522 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
523 // We may have hit the beginning of our SparseBitVector, in which case,
524 // we may need to insert right after this element, which requires moving
525 // the current iterator forward one, because insert does insert before.
526 if (ElementIter != Elements.end() &&
527 ElementIter->index() < ElementIndex)
528 ElementIter = Elements.insert(++ElementIter, Element);
530 ElementIter = Elements.insert(ElementIter, Element);
533 CurrElementIter = ElementIter;
535 ElementIter->set(Idx % ElementSize);
538 bool test_and_set (unsigned Idx) {
539 bool old = test(Idx);
547 bool operator!=(const SparseBitVector &RHS) const {
548 return !(*this == RHS);
551 bool operator==(const SparseBitVector &RHS) const {
552 ElementListConstIter Iter1 = Elements.begin();
553 ElementListConstIter Iter2 = RHS.Elements.begin();
555 for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
557 if (*Iter1 != *Iter2)
560 return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
563 // Union our bitmap with the RHS and return true if we changed.
564 bool operator|=(const SparseBitVector &RHS) {
565 bool changed = false;
566 ElementListIter Iter1 = Elements.begin();
567 ElementListConstIter Iter2 = RHS.Elements.begin();
569 // If RHS is empty, we are done
570 if (RHS.Elements.empty())
573 while (Iter2 != RHS.Elements.end()) {
574 if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
575 Elements.insert(Iter1,
576 new SparseBitVectorElement<ElementSize>(*Iter2));
579 } else if (Iter1->index() == Iter2->index()) {
580 changed |= Iter1->unionWith(*Iter2);
587 CurrElementIter = Elements.begin();
591 // Intersect our bitmap with the RHS and return true if ours changed.
592 bool operator&=(const SparseBitVector &RHS) {
593 bool changed = false;
594 ElementListIter Iter1 = Elements.begin();
595 ElementListConstIter Iter2 = RHS.Elements.begin();
597 // Check if both bitmaps are empty.
598 if (Elements.empty() && RHS.Elements.empty())
601 // Loop through, intersecting as we go, erasing elements when necessary.
602 while (Iter2 != RHS.Elements.end()) {
603 if (Iter1 == Elements.end()) {
604 CurrElementIter = Elements.begin();
608 if (Iter1->index() > Iter2->index()) {
610 } else if (Iter1->index() == Iter2->index()) {
612 changed |= Iter1->intersectWith(*Iter2, BecameZero);
614 ElementListIter IterTmp = Iter1;
616 Elements.erase(IterTmp);
622 ElementListIter IterTmp = Iter1;
624 Elements.erase(IterTmp);
627 Elements.erase(Iter1, Elements.end());
628 CurrElementIter = Elements.begin();
632 // Intersect our bitmap with the complement of the RHS and return true
634 bool intersectWithComplement(const SparseBitVector &RHS) {
635 bool changed = false;
636 ElementListIter Iter1 = Elements.begin();
637 ElementListConstIter Iter2 = RHS.Elements.begin();
639 // If either our bitmap or RHS is empty, we are done
640 if (Elements.empty() || RHS.Elements.empty())
643 // Loop through, intersecting as we go, erasing elements when necessary.
644 while (Iter2 != RHS.Elements.end()) {
645 if (Iter1 == Elements.end()) {
646 CurrElementIter = Elements.begin();
650 if (Iter1->index() > Iter2->index()) {
652 } else if (Iter1->index() == Iter2->index()) {
654 changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
656 ElementListIter IterTmp = Iter1;
658 Elements.erase(IterTmp);
667 CurrElementIter = Elements.begin();
671 bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
672 return intersectWithComplement(*RHS);
676 // Three argument version of intersectWithComplement.
677 // Result of RHS1 & ~RHS2 is stored into this bitmap.
678 void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
679 const SparseBitVector<ElementSize> &RHS2)
682 CurrElementIter = Elements.begin();
683 ElementListConstIter Iter1 = RHS1.Elements.begin();
684 ElementListConstIter Iter2 = RHS2.Elements.begin();
686 // If RHS1 is empty, we are done
687 // If RHS2 is empty, we still have to copy RHS1
688 if (RHS1.Elements.empty())
691 // Loop through, intersecting as we go, erasing elements when necessary.
692 while (Iter2 != RHS2.Elements.end()) {
693 if (Iter1 == RHS1.Elements.end())
696 if (Iter1->index() > Iter2->index()) {
698 } else if (Iter1->index() == Iter2->index()) {
699 bool BecameZero = false;
700 SparseBitVectorElement<ElementSize> *NewElement =
701 new SparseBitVectorElement<ElementSize>(Iter1->index());
702 NewElement->intersectWithComplement(*Iter1, *Iter2, BecameZero);
704 Elements.push_back(NewElement);
711 SparseBitVectorElement<ElementSize> *NewElement =
712 new SparseBitVectorElement<ElementSize>(*Iter1);
713 Elements.push_back(NewElement);
718 // copy the remaining elements
719 while (Iter1 != RHS1.Elements.end()) {
720 SparseBitVectorElement<ElementSize> *NewElement =
721 new SparseBitVectorElement<ElementSize>(*Iter1);
722 Elements.push_back(NewElement);
729 void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
730 const SparseBitVector<ElementSize> *RHS2) {
731 intersectWithComplement(*RHS1, *RHS2);
734 bool intersects(const SparseBitVector<ElementSize> *RHS) const {
735 return intersects(*RHS);
738 // Return true if we share any bits in common with RHS
739 bool intersects(const SparseBitVector<ElementSize> &RHS) const {
740 ElementListConstIter Iter1 = Elements.begin();
741 ElementListConstIter Iter2 = RHS.Elements.begin();
743 // Check if both bitmaps are empty.
744 if (Elements.empty() && RHS.Elements.empty())
747 // Loop through, intersecting stopping when we hit bits in common.
748 while (Iter2 != RHS.Elements.end()) {
749 if (Iter1 == Elements.end())
752 if (Iter1->index() > Iter2->index()) {
754 } else if (Iter1->index() == Iter2->index()) {
755 if (Iter1->intersects(*Iter2))
766 // Return true iff all bits set in this SparseBitVector are
768 bool contains(const SparseBitVector<ElementSize> &RHS) const {
769 SparseBitVector<ElementSize> Result(*this);
771 return (Result == RHS);
774 // Return the first set bit in the bitmap. Return -1 if no bits are set.
775 int find_first() const {
776 if (Elements.empty())
778 const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
779 return (First.index() * ElementSize) + First.find_first();
782 // Return true if the SparseBitVector is empty
784 return Elements.empty();
787 unsigned count() const {
788 unsigned BitCount = 0;
789 for (ElementListConstIter Iter = Elements.begin();
790 Iter != Elements.end();
792 BitCount += Iter->count();
796 iterator begin() const {
797 return iterator(this);
800 iterator end() const {
801 return iterator(this, true);
805 // Convenience functions to allow Or and And without dereferencing in the user
808 template <unsigned ElementSize>
809 inline bool operator |=(SparseBitVector<ElementSize> &LHS,
810 const SparseBitVector<ElementSize> *RHS) {
814 template <unsigned ElementSize>
815 inline bool operator |=(SparseBitVector<ElementSize> *LHS,
816 const SparseBitVector<ElementSize> &RHS) {
817 return LHS->operator|=(RHS);
820 template <unsigned ElementSize>
821 inline bool operator &=(SparseBitVector<ElementSize> *LHS,
822 const SparseBitVector<ElementSize> &RHS) {
823 return LHS->operator&=(RHS);
826 template <unsigned ElementSize>
827 inline bool operator &=(SparseBitVector<ElementSize> &LHS,
828 const SparseBitVector<ElementSize> *RHS) {
832 // Convenience functions for infix union, intersection, difference operators.
834 template <unsigned ElementSize>
835 inline SparseBitVector<ElementSize>
836 operator|(const SparseBitVector<ElementSize> &LHS,
837 const SparseBitVector<ElementSize> &RHS) {
838 SparseBitVector<ElementSize> Result(LHS);
843 template <unsigned ElementSize>
844 inline SparseBitVector<ElementSize>
845 operator&(const SparseBitVector<ElementSize> &LHS,
846 const SparseBitVector<ElementSize> &RHS) {
847 SparseBitVector<ElementSize> Result(LHS);
852 template <unsigned ElementSize>
853 inline SparseBitVector<ElementSize>
854 operator-(const SparseBitVector<ElementSize> &LHS,
855 const SparseBitVector<ElementSize> &RHS) {
856 SparseBitVector<ElementSize> Result;
857 Result.intersectWithComplement(LHS, RHS);
864 // Dump a SparseBitVector to a stream
865 template <unsigned ElementSize>
866 void dump(const SparseBitVector<ElementSize> &LHS, raw_ostream &out) {
869 typename SparseBitVector<ElementSize>::iterator bi = LHS.begin(),
873 for (++bi; bi != be; ++bi) {
879 } // end namespace llvm