1 //===- llvm/ADT/SparseBitVector.h - Efficient Sparse BitVector -*- C++ -*- ===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Daniel Berlin and is distributed under
6 // the University of Illinois Open Source 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
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/ADT/ilist"
27 /// SparseBitVector is an implementation of a bitvector that is sparse by only
28 /// storing the elements that have non-zero bits set. In order to make this
29 /// fast for the most common cases, SparseBitVector is implemented as a linked
30 /// list of SparseBitVectorElements. We maintain a pointer to the last
31 /// SparseBitVectorElement accessed (in the form of a list iterator), in order
32 /// to make multiple in-order test/set constant time after the first one is
33 /// executed. Note that using vectors to store SparseBitVectorElement's does
34 /// not work out very well because it causes insertion in the middle to take
35 /// enormous amounts of time with a large amount of bits. Other structures that
36 /// have better worst cases for insertion in the middle (various balanced trees,
37 /// etc) do not perform as well in practice as a linked list with this iterator
38 /// kept up to date. They are also significantly more memory intensive.
41 template <unsigned ElementSize = 128>
42 struct SparseBitVectorElement {
44 typedef unsigned long BitWord;
46 BITWORD_SIZE = sizeof(BitWord) * 8,
47 BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
48 BITS_PER_ELEMENT = ElementSize
51 SparseBitVectorElement<ElementSize> *getNext() const {
54 SparseBitVectorElement<ElementSize> *getPrev() const {
58 void setNext(SparseBitVectorElement<ElementSize> *RHS) {
61 void setPrev(SparseBitVectorElement<ElementSize> *RHS) {
66 SparseBitVectorElement<ElementSize> *Next;
67 SparseBitVectorElement<ElementSize> *Prev;
68 // Index of Element in terms of where first bit starts.
69 unsigned ElementIndex;
70 BitWord Bits[BITWORDS_PER_ELEMENT];
71 // Needed for sentinels
72 SparseBitVectorElement() {
74 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
77 friend struct ilist_traits<SparseBitVectorElement<ElementSize> >;
80 explicit SparseBitVectorElement(unsigned Idx) {
82 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
85 ~SparseBitVectorElement() {
89 SparseBitVectorElement(const SparseBitVectorElement &RHS) {
90 ElementIndex = RHS.ElementIndex;
91 std::copy(&RHS.Bits[0], &RHS.Bits[BITWORDS_PER_ELEMENT], Bits);
95 bool operator==(const SparseBitVectorElement &RHS) const {
96 if (ElementIndex != RHS.ElementIndex)
98 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
99 if (Bits[i] != RHS.Bits[i])
104 bool operator!=(const SparseBitVectorElement &RHS) const {
105 return !(*this == RHS);
108 // Return the bits that make up word Idx in our element.
109 BitWord word(unsigned Idx) const {
110 assert (Idx < BITWORDS_PER_ELEMENT);
114 unsigned index() const {
119 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
125 void set(unsigned Idx) {
126 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
129 bool test_and_set (unsigned Idx) {
130 bool old = test(Idx);
136 void reset(unsigned Idx) {
137 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
140 bool test(unsigned Idx) const {
141 return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
144 unsigned count() const {
145 unsigned NumBits = 0;
146 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
147 if (sizeof(BitWord) == 4)
148 NumBits += CountPopulation_32(Bits[i]);
149 else if (sizeof(BitWord) == 8)
150 NumBits += CountPopulation_64(Bits[i]);
152 assert(0 && "Unsupported!");
156 /// find_first - Returns the index of the first set bit.
157 int find_first() const {
158 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
160 if (sizeof(BitWord) == 4)
161 return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
162 else if (sizeof(BitWord) == 8)
163 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
165 assert(0 && "Unsupported!");
167 assert(0 && "Illegal empty element");
170 /// find_next - Returns the index of the next set bit following the
171 /// "Prev" bit. Returns -1 if the next set bit is not found.
172 int find_next(unsigned Prev) const {
174 if (Prev >= BITS_PER_ELEMENT)
177 unsigned WordPos = Prev / BITWORD_SIZE;
178 unsigned BitPos = Prev % BITWORD_SIZE;
179 BitWord Copy = Bits[WordPos];
180 assert (WordPos <= BITWORDS_PER_ELEMENT
181 && "Word Position outside of element");
183 // Mask off previous bits.
184 Copy &= ~0L << BitPos;
187 if (sizeof(BitWord) == 4)
188 return WordPos * BITWORD_SIZE + CountTrailingZeros_32(Copy);
189 else if (sizeof(BitWord) == 8)
190 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
192 assert(0 && "Unsupported!");
195 // Check subsequent words.
196 for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
198 if (sizeof(BitWord) == 4)
199 return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
200 else if (sizeof(BitWord) == 8)
201 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
203 assert(0 && "Unsupported!");
208 // Union this element with RHS and return true if this one changed.
209 bool unionWith(const SparseBitVectorElement &RHS) {
210 bool changed = false;
211 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
212 BitWord old = changed ? 0 : Bits[i];
214 Bits[i] |= RHS.Bits[i];
215 if (!changed && old != Bits[i])
221 // Return true if we have any bits in common with RHS
222 bool intersects(const SparseBitVectorElement &RHS) const {
223 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
224 if (RHS.Bits[i] & Bits[i])
230 // Intersect this Element with RHS and return true if this one changed.
231 // BecameZero is set to true if this element became all-zero bits.
232 bool intersectWith(const SparseBitVectorElement &RHS,
234 bool changed = false;
238 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
239 BitWord old = changed ? 0 : Bits[i];
241 Bits[i] &= RHS.Bits[i];
245 if (!changed && old != Bits[i])
248 BecameZero = allzero;
251 // Intersect this Element with the complement of RHS and return true if this
252 // one changed. BecameZero is set to true if this element became all-zero
254 bool intersectWithComplement(const SparseBitVectorElement &RHS,
256 bool changed = false;
260 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
261 BitWord old = changed ? 0 : Bits[i];
263 Bits[i] &= ~RHS.Bits[i];
267 if (!changed && old != Bits[i])
270 BecameZero = allzero;
273 // Three argument version of intersectWithComplement that intersects
274 // RHS1 & ~RHS2 into this element
275 void intersectWithComplement(const SparseBitVectorElement &RHS1,
276 const SparseBitVectorElement &RHS2,
281 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
282 Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
286 BecameZero = allzero;
290 template <unsigned ElementSize = 128>
291 class SparseBitVector {
292 typedef ilist<SparseBitVectorElement<ElementSize> > ElementList;
293 typedef typename ElementList::iterator ElementListIter;
294 typedef typename ElementList::const_iterator ElementListConstIter;
296 BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
299 // Pointer to our current Element.
300 ElementListIter CurrElementIter;
301 ElementList Elements;
303 // This is like std::lower_bound, except we do linear searching from the
305 ElementListIter FindLowerBound(unsigned ElementIndex) {
307 if (Elements.empty()) {
308 CurrElementIter = Elements.begin();
309 return Elements.begin();
312 // Make sure our current iterator is valid.
313 if (CurrElementIter == Elements.end())
316 // Search from our current iterator, either backwards or forwards,
317 // depending on what element we are looking for.
318 ElementListIter ElementIter = CurrElementIter;
319 if (CurrElementIter->index() == ElementIndex) {
321 } else if (CurrElementIter->index() > ElementIndex) {
322 while (ElementIter != Elements.begin()
323 && ElementIter->index() > ElementIndex)
326 while (ElementIter != Elements.end() &&
327 ElementIter->index() <= ElementIndex)
331 CurrElementIter = ElementIter;
335 // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
336 // than it would be, in order to be efficient.
337 class SparseBitVectorIterator {
341 const SparseBitVector<ElementSize> *BitVector;
343 // Current element inside of bitmap.
344 ElementListConstIter Iter;
346 // Current bit number inside of our bitmap.
349 // Current word number inside of our element.
352 // Current bits from the element.
353 typename SparseBitVectorElement<ElementSize>::BitWord Bits;
355 // Move our iterator to the first non-zero bit in the bitmap.
356 void AdvanceToFirstNonZero() {
359 if (BitVector->Elements.empty()) {
363 Iter = BitVector->Elements.begin();
364 BitNumber = Iter->index() * ElementSize;
365 unsigned BitPos = Iter->find_first();
367 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
368 Bits = Iter->word(WordNumber);
369 Bits >>= BitPos % BITWORD_SIZE;
372 // Move our iterator to the next non-zero bit.
373 void AdvanceToNextNonZero() {
377 while (Bits && !(Bits & 1)) {
382 // See if we ran out of Bits in this word.
384 int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
385 // If we ran out of set bits in this element, move to next element.
386 if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
390 // We may run out of elements in the bitmap.
391 if (Iter == BitVector->Elements.end()) {
395 // Set up for next non zero word in bitmap.
396 BitNumber = Iter->index() * ElementSize;
397 NextSetBitNumber = Iter->find_first();
398 BitNumber += NextSetBitNumber;
399 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
400 Bits = Iter->word(WordNumber);
401 Bits >>= NextSetBitNumber % BITWORD_SIZE;
403 WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
404 Bits = Iter->word(WordNumber);
405 Bits >>= NextSetBitNumber % BITWORD_SIZE;
411 inline SparseBitVectorIterator& operator++() {
414 AdvanceToNextNonZero();
419 inline SparseBitVectorIterator operator++(int) {
420 SparseBitVectorIterator tmp = *this;
425 // Return the current set bit number.
426 unsigned operator*() const {
430 bool operator==(const SparseBitVectorIterator &RHS) const {
431 // If they are both at the end, ignore the rest of the fields.
432 if (AtEnd == RHS.AtEnd)
434 // Otherwise they are the same if they have the same bit number and
436 return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
438 bool operator!=(const SparseBitVectorIterator &RHS) const {
439 return !(*this == RHS);
441 SparseBitVectorIterator(): BitVector(NULL) {
445 SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
446 bool end = false):BitVector(RHS) {
447 Iter = BitVector->Elements.begin();
452 AdvanceToFirstNonZero();
456 typedef SparseBitVectorIterator iterator;
459 CurrElementIter = Elements.begin ();
465 // SparseBitVector copy ctor.
466 SparseBitVector(const SparseBitVector &RHS) {
467 ElementListConstIter ElementIter = RHS.Elements.begin();
468 while (ElementIter != RHS.Elements.end()) {
469 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
473 CurrElementIter = Elements.begin ();
476 // Test, Reset, and Set a bit in the bitmap.
477 bool test(unsigned Idx) {
478 if (Elements.empty())
481 unsigned ElementIndex = Idx / ElementSize;
482 ElementListIter ElementIter = FindLowerBound(ElementIndex);
484 // If we can't find an element that is supposed to contain this bit, there
485 // is nothing more to do.
486 if (ElementIter == Elements.end() ||
487 ElementIter->index() != ElementIndex)
489 return ElementIter->test(Idx % ElementSize);
492 void reset(unsigned Idx) {
493 if (Elements.empty())
496 unsigned ElementIndex = Idx / ElementSize;
497 ElementListIter ElementIter = FindLowerBound(ElementIndex);
499 // If we can't find an element that is supposed to contain this bit, there
500 // is nothing more to do.
501 if (ElementIter == Elements.end() ||
502 ElementIter->index() != ElementIndex)
504 ElementIter->reset(Idx % ElementSize);
506 // When the element is zeroed out, delete it.
507 if (ElementIter->empty()) {
509 Elements.erase(ElementIter);
513 void set(unsigned Idx) {
514 unsigned ElementIndex = Idx / ElementSize;
515 SparseBitVectorElement<ElementSize> *Element;
516 ElementListIter ElementIter;
517 if (Elements.empty()) {
518 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
519 ElementIter = Elements.insert(Elements.end(), Element);
522 ElementIter = FindLowerBound(ElementIndex);
524 if (ElementIter == Elements.end() ||
525 ElementIter->index() != ElementIndex) {
526 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
527 // Insert does insert before, and lower bound gives the one before.
528 ElementIter = Elements.insert(++ElementIter, Element);
531 ElementIter->set(Idx % ElementSize);
534 bool test_and_set (unsigned Idx) {
535 bool old = test(Idx);
541 // Union our bitmap with the RHS and return true if we changed.
542 bool operator|=(const SparseBitVector &RHS) {
543 bool changed = false;
544 ElementListIter Iter1 = Elements.begin();
545 ElementListConstIter Iter2 = RHS.Elements.begin();
547 // Check if both bitmaps are empty
548 if (Elements.empty() && RHS.Elements.empty())
551 while (Iter2 != RHS.Elements.end()) {
552 if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
553 Elements.insert(Iter1,
554 new SparseBitVectorElement<ElementSize>(*Iter2));
557 } else if (Iter1->index() == Iter2->index()) {
558 changed |= Iter1->unionWith(*Iter2);
565 CurrElementIter = Elements.begin();
569 // Intersect our bitmap with the RHS and return true if ours changed.
570 bool operator&=(const SparseBitVector &RHS) {
571 bool changed = false;
572 ElementListIter Iter1 = Elements.begin();
573 ElementListConstIter Iter2 = RHS.Elements.begin();
575 // Check if both bitmaps are empty.
576 if (Elements.empty() && RHS.Elements.empty())
579 // Loop through, intersecting as we go, erasing elements when necessary.
580 while (Iter2 != RHS.Elements.end()) {
581 if (Iter1 == Elements.end())
584 if (Iter1->index() > Iter2->index()) {
586 } else if (Iter1->index() == Iter2->index()) {
588 changed |= Iter1->intersectWith(*Iter2, BecameZero);
590 ElementListIter IterTmp = Iter1;
592 Elements.erase(IterTmp);
598 ElementListIter IterTmp = Iter1;
600 Elements.erase(IterTmp);
603 Elements.erase(Iter1, Elements.end());
604 CurrElementIter = Elements.begin();
608 // Intersect our bitmap with the complement of the RHS and return true if ours
610 bool intersectWithComplement(const SparseBitVector &RHS) {
611 bool changed = false;
612 ElementListIter Iter1 = Elements.begin();
613 ElementListConstIter Iter2 = RHS.Elements.begin();
615 // Check if they are both empty
616 if (Elements.empty() && RHS.Elements.empty())
619 // Loop through, intersecting as we go, erasing elements when necessary.
620 while (Iter2 != RHS.Elements.end()) {
621 if (Iter1 == Elements.end())
624 if (Iter1->index() > Iter2->index()) {
626 } else if (Iter1->index() == Iter2->index()) {
628 changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
630 ElementListIter IterTmp = Iter1;
632 Elements.erase(IterTmp);
638 ElementListIter IterTmp = Iter1;
640 Elements.erase(IterTmp);
643 CurrElementIter = Elements.begin();
647 bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
648 return intersectWithComplement(*RHS);
652 // Three argument version of intersectWithComplement. Result of RHS1 & ~RHS2
653 // is stored into this bitmap.
654 void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
655 const SparseBitVector<ElementSize> &RHS2)
658 ElementListConstIter Iter1 = RHS1.Elements.begin();
659 ElementListConstIter Iter2 = RHS2.Elements.begin();
661 // Check if they are both empty.
662 if (RHS1.empty() && RHS2.empty())
665 // Loop through, intersecting as we go, erasing elements when necessary.
666 while (Iter2 != RHS2.Elements.end()) {
667 if (Iter1 == RHS1.Elements.end())
670 if (Iter1->index() > Iter2->index()) {
672 } else if (Iter1->index() == Iter2->index()) {
673 bool BecameZero = false;
674 SparseBitVectorElement<ElementSize> *NewElement =
675 new SparseBitVectorElement<ElementSize>(Iter1->index());
676 NewElement->intersectWithComplement(*Iter1, *Iter2, BecameZero);
678 Elements.push_back(NewElement);
689 // copy the remaining elements
690 while (Iter1 != RHS1.Elements.end()) {
691 SparseBitVectorElement<ElementSize> *NewElement =
692 new SparseBitVectorElement<ElementSize>(*Iter1);
693 Elements.push_back(NewElement);
697 CurrElementIter = Elements.begin();
701 void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
702 const SparseBitVector<ElementSize> *RHS2) {
703 intersectWithComplement(*RHS1, *RHS2);
706 bool intersects(const SparseBitVector<ElementSize> *RHS) const {
707 return intersects(*RHS);
710 // Return true if we share any bits in common with RHS
711 bool intersects(const SparseBitVector<ElementSize> &RHS) const {
712 ElementListConstIter Iter1 = Elements.begin();
713 ElementListConstIter Iter2 = RHS.Elements.begin();
715 // Check if both bitmaps are empty.
716 if (Elements.empty() && RHS.Elements.empty())
719 // Loop through, intersecting stopping when we hit bits in common.
720 while (Iter2 != RHS.Elements.end()) {
721 if (Iter1 == Elements.end())
724 if (Iter1->index() > Iter2->index()) {
726 } else if (Iter1->index() == Iter2->index()) {
727 if (Iter1->intersects(*Iter2))
738 // Return the first set bit in the bitmap. Return -1 if no bits are set.
739 int find_first() const {
740 if (Elements.empty())
742 const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
743 return (First.index() * ElementSize) + First.find_first();
746 // Return true if the SparseBitVector is empty
748 return Elements.empty();
751 unsigned count() const {
752 unsigned BitCount = 0;
753 for (ElementListConstIter Iter = Elements.begin();
754 Iter != Elements.end();
756 BitCount += Iter->count();
760 iterator begin() const {
761 return iterator(this);
764 iterator end() const {
765 return iterator(this, ~0);
770 // Convenience functions to allow Or and And without dereferencing in the user
773 template <unsigned ElementSize>
774 inline bool operator |=(SparseBitVector<ElementSize> &LHS,
775 const SparseBitVector<ElementSize> *RHS) {
779 template <unsigned ElementSize>
780 inline bool operator |=(SparseBitVector<ElementSize> *LHS,
781 const SparseBitVector<ElementSize> &RHS) {
782 return LHS->operator|=(RHS);
785 template <unsigned ElementSize>
786 inline bool operator &=(SparseBitVector<ElementSize> *LHS,
787 const SparseBitVector<ElementSize> &RHS) {
788 return LHS->operator&=(RHS);
791 template <unsigned ElementSize>
792 inline bool operator &=(SparseBitVector<ElementSize> &LHS,
793 const SparseBitVector<ElementSize> *RHS) {
794 return LHS &= (*RHS);
798 // Dump a SparseBitVector to a stream
799 template <unsigned ElementSize>
800 void dump(const SparseBitVector<ElementSize> &LHS, llvm::OStream &out) {
803 typename SparseBitVector<ElementSize>::iterator bi;
804 for (bi = LHS.begin(); bi != LHS.end(); ++bi) {