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
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> >;
79 explicit SparseBitVectorElement(unsigned Idx) {
81 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
84 ~SparseBitVectorElement() {
88 SparseBitVectorElement(const SparseBitVectorElement &RHS) {
89 ElementIndex = RHS.ElementIndex;
90 std::copy(&RHS.Bits[0], &RHS.Bits[BITWORDS_PER_ELEMENT], Bits);
94 bool operator==(const SparseBitVectorElement &RHS) const {
95 if (ElementIndex != RHS.ElementIndex)
97 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
98 if (Bits[i] != RHS.Bits[i])
103 bool operator!=(const SparseBitVectorElement &RHS) const {
104 return !(*this == RHS);
107 // Return the bits that make up word Idx in our element.
108 BitWord word(unsigned Idx) const {
109 assert (Idx < BITWORDS_PER_ELEMENT);
113 unsigned index() const {
118 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
124 void set(unsigned Idx) {
125 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
128 bool test_and_set (unsigned Idx) {
129 bool old = test(Idx);
137 void reset(unsigned Idx) {
138 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
141 bool test(unsigned Idx) const {
142 return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
145 unsigned count() const {
146 unsigned NumBits = 0;
147 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
148 if (sizeof(BitWord) == 4)
149 NumBits += CountPopulation_32(Bits[i]);
150 else if (sizeof(BitWord) == 8)
151 NumBits += CountPopulation_64(Bits[i]);
153 assert(0 && "Unsupported!");
157 /// find_first - Returns the index of the first set bit.
158 int find_first() const {
159 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
161 if (sizeof(BitWord) == 4)
162 return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
163 else if (sizeof(BitWord) == 8)
164 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
166 assert(0 && "Unsupported!");
168 assert(0 && "Illegal empty element");
169 return 0; // Not reached
172 /// find_next - Returns the index of the next set bit starting from the
173 /// "Curr" bit. Returns -1 if the next set bit is not found.
174 int find_next(unsigned Curr) const {
175 if (Curr >= BITS_PER_ELEMENT)
178 unsigned WordPos = Curr / BITWORD_SIZE;
179 unsigned BitPos = Curr % BITWORD_SIZE;
180 BitWord Copy = Bits[WordPos];
181 assert (WordPos <= BITWORDS_PER_ELEMENT
182 && "Word Position outside of element");
184 // Mask off previous bits.
185 Copy &= ~0L << BitPos;
188 if (sizeof(BitWord) == 4)
189 return WordPos * BITWORD_SIZE + CountTrailingZeros_32(Copy);
190 else if (sizeof(BitWord) == 8)
191 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
193 assert(0 && "Unsupported!");
196 // Check subsequent words.
197 for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
199 if (sizeof(BitWord) == 4)
200 return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
201 else if (sizeof(BitWord) == 8)
202 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
204 assert(0 && "Unsupported!");
209 // Union this element with RHS and return true if this one changed.
210 bool unionWith(const SparseBitVectorElement &RHS) {
211 bool changed = false;
212 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
213 BitWord old = changed ? 0 : Bits[i];
215 Bits[i] |= RHS.Bits[i];
216 if (!changed && old != Bits[i])
222 // Return true if we have any bits in common with RHS
223 bool intersects(const SparseBitVectorElement &RHS) const {
224 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
225 if (RHS.Bits[i] & Bits[i])
231 // Intersect this Element with RHS and return true if this one changed.
232 // BecameZero is set to true if this element became all-zero bits.
233 bool intersectWith(const SparseBitVectorElement &RHS,
235 bool changed = false;
239 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
240 BitWord old = changed ? 0 : Bits[i];
242 Bits[i] &= RHS.Bits[i];
246 if (!changed && old != Bits[i])
249 BecameZero = allzero;
252 // Intersect this Element with the complement of RHS and return true if this
253 // one changed. BecameZero is set to true if this element became all-zero
255 bool intersectWithComplement(const SparseBitVectorElement &RHS,
257 bool changed = false;
261 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
262 BitWord old = changed ? 0 : Bits[i];
264 Bits[i] &= ~RHS.Bits[i];
268 if (!changed && old != Bits[i])
271 BecameZero = allzero;
274 // Three argument version of intersectWithComplement that intersects
275 // RHS1 & ~RHS2 into this element
276 void intersectWithComplement(const SparseBitVectorElement &RHS1,
277 const SparseBitVectorElement &RHS2,
282 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
283 Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
287 BecameZero = allzero;
290 // Get a hash value for this element;
291 uint64_t getHashValue() const {
292 uint64_t HashVal = 0;
293 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
300 template <unsigned ElementSize = 128>
301 class SparseBitVector {
302 typedef ilist<SparseBitVectorElement<ElementSize> > ElementList;
303 typedef typename ElementList::iterator ElementListIter;
304 typedef typename ElementList::const_iterator ElementListConstIter;
306 BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
309 // Pointer to our current Element.
310 ElementListIter CurrElementIter;
311 ElementList Elements;
313 // This is like std::lower_bound, except we do linear searching from the
315 ElementListIter FindLowerBound(unsigned ElementIndex) {
317 if (Elements.empty()) {
318 CurrElementIter = Elements.begin();
319 return Elements.begin();
322 // Make sure our current iterator is valid.
323 if (CurrElementIter == Elements.end())
326 // Search from our current iterator, either backwards or forwards,
327 // depending on what element we are looking for.
328 ElementListIter ElementIter = CurrElementIter;
329 if (CurrElementIter->index() == ElementIndex) {
331 } else if (CurrElementIter->index() > ElementIndex) {
332 while (ElementIter != Elements.begin()
333 && ElementIter->index() > ElementIndex)
336 while (ElementIter != Elements.end() &&
337 ElementIter->index() < ElementIndex)
340 CurrElementIter = ElementIter;
344 // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
345 // than it would be, in order to be efficient.
346 class SparseBitVectorIterator {
350 const SparseBitVector<ElementSize> *BitVector;
352 // Current element inside of bitmap.
353 ElementListConstIter Iter;
355 // Current bit number inside of our bitmap.
358 // Current word number inside of our element.
361 // Current bits from the element.
362 typename SparseBitVectorElement<ElementSize>::BitWord Bits;
364 // Move our iterator to the first non-zero bit in the bitmap.
365 void AdvanceToFirstNonZero() {
368 if (BitVector->Elements.empty()) {
372 Iter = BitVector->Elements.begin();
373 BitNumber = Iter->index() * ElementSize;
374 unsigned BitPos = Iter->find_first();
376 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
377 Bits = Iter->word(WordNumber);
378 Bits >>= BitPos % BITWORD_SIZE;
381 // Move our iterator to the next non-zero bit.
382 void AdvanceToNextNonZero() {
386 while (Bits && !(Bits & 1)) {
391 // See if we ran out of Bits in this word.
393 int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
394 // If we ran out of set bits in this element, move to next element.
395 if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
399 // We may run out of elements in the bitmap.
400 if (Iter == BitVector->Elements.end()) {
404 // Set up for next non zero word in bitmap.
405 BitNumber = Iter->index() * ElementSize;
406 NextSetBitNumber = Iter->find_first();
407 BitNumber += NextSetBitNumber;
408 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
409 Bits = Iter->word(WordNumber);
410 Bits >>= NextSetBitNumber % BITWORD_SIZE;
412 WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
413 Bits = Iter->word(WordNumber);
414 Bits >>= NextSetBitNumber % BITWORD_SIZE;
415 BitNumber = Iter->index() * ElementSize;
416 BitNumber += NextSetBitNumber;
422 inline SparseBitVectorIterator& operator++() {
425 AdvanceToNextNonZero();
430 inline SparseBitVectorIterator operator++(int) {
431 SparseBitVectorIterator tmp = *this;
436 // Return the current set bit number.
437 unsigned operator*() const {
441 bool operator==(const SparseBitVectorIterator &RHS) const {
442 // If they are both at the end, ignore the rest of the fields.
443 if (AtEnd && RHS.AtEnd)
445 // Otherwise they are the same if they have the same bit number and
447 return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
449 bool operator!=(const SparseBitVectorIterator &RHS) const {
450 return !(*this == RHS);
452 SparseBitVectorIterator(): BitVector(NULL) {
456 SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
457 bool end = false):BitVector(RHS) {
458 Iter = BitVector->Elements.begin();
463 AdvanceToFirstNonZero();
467 typedef SparseBitVectorIterator iterator;
470 CurrElementIter = Elements.begin ();
476 // SparseBitVector copy ctor.
477 SparseBitVector(const SparseBitVector &RHS) {
478 ElementListConstIter ElementIter = RHS.Elements.begin();
479 while (ElementIter != RHS.Elements.end()) {
480 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
484 CurrElementIter = Elements.begin ();
487 // Test, Reset, and Set a bit in the bitmap.
488 bool test(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 return ElementIter->test(Idx % ElementSize);
503 void reset(unsigned Idx) {
504 if (Elements.empty())
507 unsigned ElementIndex = Idx / ElementSize;
508 ElementListIter ElementIter = FindLowerBound(ElementIndex);
510 // If we can't find an element that is supposed to contain this bit, there
511 // is nothing more to do.
512 if (ElementIter == Elements.end() ||
513 ElementIter->index() != ElementIndex)
515 ElementIter->reset(Idx % ElementSize);
517 // When the element is zeroed out, delete it.
518 if (ElementIter->empty()) {
520 Elements.erase(ElementIter);
524 void set(unsigned Idx) {
525 unsigned ElementIndex = Idx / ElementSize;
526 SparseBitVectorElement<ElementSize> *Element;
527 ElementListIter ElementIter;
528 if (Elements.empty()) {
529 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
530 ElementIter = Elements.insert(Elements.end(), Element);
533 ElementIter = FindLowerBound(ElementIndex);
535 if (ElementIter == Elements.end() ||
536 ElementIter->index() != ElementIndex) {
537 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
538 // We may have hit the beginning of our SparseBitVector, in which case,
539 // we may need to insert right after this element, which requires moving
540 // the current iterator forward one, because insert does insert before.
541 if (ElementIter != Elements.end() &&
542 ElementIter->index() < ElementIndex)
543 ElementIter = Elements.insert(++ElementIter, Element);
545 ElementIter = Elements.insert(ElementIter, Element);
548 CurrElementIter = ElementIter;
550 ElementIter->set(Idx % ElementSize);
553 bool test_and_set (unsigned Idx) {
554 bool old = test(Idx);
562 bool operator!=(const SparseBitVector &RHS) const {
563 return !(*this == RHS);
566 bool operator==(const SparseBitVector &RHS) const {
567 ElementListConstIter Iter1 = Elements.begin();
568 ElementListConstIter Iter2 = RHS.Elements.begin();
570 for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
572 if (*Iter1 != *Iter2)
575 return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
578 // Union our bitmap with the RHS and return true if we changed.
579 bool operator|=(const SparseBitVector &RHS) {
580 bool changed = false;
581 ElementListIter Iter1 = Elements.begin();
582 ElementListConstIter Iter2 = RHS.Elements.begin();
584 // If RHS is empty, we are done
585 if (RHS.Elements.empty())
588 while (Iter2 != RHS.Elements.end()) {
589 if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
590 Elements.insert(Iter1,
591 new SparseBitVectorElement<ElementSize>(*Iter2));
594 } else if (Iter1->index() == Iter2->index()) {
595 changed |= Iter1->unionWith(*Iter2);
602 CurrElementIter = Elements.begin();
606 // Intersect our bitmap with the RHS and return true if ours changed.
607 bool operator&=(const SparseBitVector &RHS) {
608 bool changed = false;
609 ElementListIter Iter1 = Elements.begin();
610 ElementListConstIter Iter2 = RHS.Elements.begin();
612 // Check if both bitmaps are empty.
613 if (Elements.empty() && RHS.Elements.empty())
616 // Loop through, intersecting as we go, erasing elements when necessary.
617 while (Iter2 != RHS.Elements.end()) {
618 if (Iter1 == Elements.end()) {
619 CurrElementIter = Elements.begin();
623 if (Iter1->index() > Iter2->index()) {
625 } else if (Iter1->index() == Iter2->index()) {
627 changed |= Iter1->intersectWith(*Iter2, BecameZero);
629 ElementListIter IterTmp = Iter1;
631 Elements.erase(IterTmp);
637 ElementListIter IterTmp = Iter1;
639 Elements.erase(IterTmp);
642 Elements.erase(Iter1, Elements.end());
643 CurrElementIter = Elements.begin();
647 // Intersect our bitmap with the complement of the RHS and return true if ours
649 bool intersectWithComplement(const SparseBitVector &RHS) {
650 bool changed = false;
651 ElementListIter Iter1 = Elements.begin();
652 ElementListConstIter Iter2 = RHS.Elements.begin();
654 // If either our bitmap or RHS is empty, we are done
655 if (Elements.empty() || RHS.Elements.empty())
658 // Loop through, intersecting as we go, erasing elements when necessary.
659 while (Iter2 != RHS.Elements.end()) {
660 if (Iter1 == Elements.end()) {
661 CurrElementIter = Elements.begin();
665 if (Iter1->index() > Iter2->index()) {
667 } else if (Iter1->index() == Iter2->index()) {
669 changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
671 ElementListIter IterTmp = Iter1;
673 Elements.erase(IterTmp);
682 CurrElementIter = Elements.begin();
686 bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
687 return intersectWithComplement(*RHS);
691 // Three argument version of intersectWithComplement. Result of RHS1 & ~RHS2
692 // is stored into this bitmap.
693 void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
694 const SparseBitVector<ElementSize> &RHS2)
697 CurrElementIter = Elements.begin();
698 ElementListConstIter Iter1 = RHS1.Elements.begin();
699 ElementListConstIter Iter2 = RHS2.Elements.begin();
701 // If RHS1 is empty, we are done
702 // If RHS2 is empty, we still have to copy RHS1
703 if (RHS1.Elements.empty())
706 // Loop through, intersecting as we go, erasing elements when necessary.
707 while (Iter2 != RHS2.Elements.end()) {
708 if (Iter1 == RHS1.Elements.end())
711 if (Iter1->index() > Iter2->index()) {
713 } else if (Iter1->index() == Iter2->index()) {
714 bool BecameZero = false;
715 SparseBitVectorElement<ElementSize> *NewElement =
716 new SparseBitVectorElement<ElementSize>(Iter1->index());
717 NewElement->intersectWithComplement(*Iter1, *Iter2, BecameZero);
719 Elements.push_back(NewElement);
726 SparseBitVectorElement<ElementSize> *NewElement =
727 new SparseBitVectorElement<ElementSize>(*Iter1);
728 Elements.push_back(NewElement);
733 // copy the remaining elements
734 while (Iter1 != RHS1.Elements.end()) {
735 SparseBitVectorElement<ElementSize> *NewElement =
736 new SparseBitVectorElement<ElementSize>(*Iter1);
737 Elements.push_back(NewElement);
744 void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
745 const SparseBitVector<ElementSize> *RHS2) {
746 intersectWithComplement(*RHS1, *RHS2);
749 bool intersects(const SparseBitVector<ElementSize> *RHS) const {
750 return intersects(*RHS);
753 // Return true if we share any bits in common with RHS
754 bool intersects(const SparseBitVector<ElementSize> &RHS) const {
755 ElementListConstIter Iter1 = Elements.begin();
756 ElementListConstIter Iter2 = RHS.Elements.begin();
758 // Check if both bitmaps are empty.
759 if (Elements.empty() && RHS.Elements.empty())
762 // Loop through, intersecting stopping when we hit bits in common.
763 while (Iter2 != RHS.Elements.end()) {
764 if (Iter1 == Elements.end())
767 if (Iter1->index() > Iter2->index()) {
769 } else if (Iter1->index() == Iter2->index()) {
770 if (Iter1->intersects(*Iter2))
781 // Return the first set bit in the bitmap. Return -1 if no bits are set.
782 int find_first() const {
783 if (Elements.empty())
785 const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
786 return (First.index() * ElementSize) + First.find_first();
789 // Return true if the SparseBitVector is empty
791 return Elements.empty();
794 unsigned count() const {
795 unsigned BitCount = 0;
796 for (ElementListConstIter Iter = Elements.begin();
797 Iter != Elements.end();
799 BitCount += Iter->count();
803 iterator begin() const {
804 return iterator(this);
807 iterator end() const {
808 return iterator(this, true);
811 // Get a hash value for this bitmap.
812 uint64_t getHashValue() const {
813 uint64_t HashVal = 0;
814 for (ElementListConstIter Iter = Elements.begin();
815 Iter != Elements.end();
817 HashVal ^= Iter->index();
818 HashVal ^= Iter->getHashValue();
824 // Convenience functions to allow Or and And without dereferencing in the user
827 template <unsigned ElementSize>
828 inline bool operator |=(SparseBitVector<ElementSize> &LHS,
829 const SparseBitVector<ElementSize> *RHS) {
833 template <unsigned ElementSize>
834 inline bool operator |=(SparseBitVector<ElementSize> *LHS,
835 const SparseBitVector<ElementSize> &RHS) {
836 return LHS->operator|=(RHS);
839 template <unsigned ElementSize>
840 inline bool operator &=(SparseBitVector<ElementSize> *LHS,
841 const SparseBitVector<ElementSize> &RHS) {
842 return LHS->operator&=(RHS);
845 template <unsigned ElementSize>
846 inline bool operator &=(SparseBitVector<ElementSize> &LHS,
847 const SparseBitVector<ElementSize> *RHS) {
848 return LHS &= (*RHS);
852 // Dump a SparseBitVector to a stream
853 template <unsigned ElementSize>
854 void dump(const SparseBitVector<ElementSize> &LHS, llvm::OStream &out) {
857 typename SparseBitVector<ElementSize>::iterator bi;
858 for (bi = LHS.begin(); bi != LHS.end(); ++bi) {