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.h"
28 /// SparseBitVector is an implementation of a bitvector that is sparse by only
29 /// storing the elements that have non-zero bits set. In order to make this
30 /// fast for the most common cases, SparseBitVector is implemented as a linked
31 /// list of SparseBitVectorElements. We maintain a pointer to the last
32 /// SparseBitVectorElement accessed (in the form of a list iterator), in order
33 /// to make multiple in-order test/set constant time after the first one is
34 /// executed. Note that using vectors to store SparseBitVectorElement's does
35 /// not work out very well because it causes insertion in the middle to take
36 /// enormous amounts of time with a large amount of bits. Other structures that
37 /// have better worst cases for insertion in the middle (various balanced trees,
38 /// etc) do not perform as well in practice as a linked list with this iterator
39 /// kept up to date. They are also significantly more memory intensive.
42 template <unsigned ElementSize = 128>
43 struct SparseBitVectorElement
44 : ilist_node<SparseBitVectorElement<ElementSize> > {
46 typedef unsigned long BitWord;
48 BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT,
49 BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
50 BITS_PER_ELEMENT = ElementSize
54 // Index of Element in terms of where first bit starts.
55 unsigned ElementIndex;
56 BitWord Bits[BITWORDS_PER_ELEMENT];
57 // Needed for sentinels
58 friend struct ilist_sentinel_traits<SparseBitVectorElement>;
59 SparseBitVectorElement() {
61 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
65 explicit SparseBitVectorElement(unsigned Idx) {
67 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
71 bool operator==(const SparseBitVectorElement &RHS) const {
72 if (ElementIndex != RHS.ElementIndex)
74 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
75 if (Bits[i] != RHS.Bits[i])
80 bool operator!=(const SparseBitVectorElement &RHS) const {
81 return !(*this == RHS);
84 // Return the bits that make up word Idx in our element.
85 BitWord word(unsigned Idx) const {
86 assert (Idx < BITWORDS_PER_ELEMENT);
90 unsigned index() const {
95 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
101 void set(unsigned Idx) {
102 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
105 bool test_and_set (unsigned Idx) {
106 bool old = test(Idx);
114 void reset(unsigned Idx) {
115 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
118 bool test(unsigned Idx) const {
119 return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
122 unsigned count() const {
123 unsigned NumBits = 0;
124 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
125 if (sizeof(BitWord) == 4)
126 NumBits += CountPopulation_32(Bits[i]);
127 else if (sizeof(BitWord) == 8)
128 NumBits += CountPopulation_64(Bits[i]);
130 assert(0 && "Unsupported!");
134 /// find_first - Returns the index of the first set bit.
135 int find_first() const {
136 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
138 if (sizeof(BitWord) == 4)
139 return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
140 else if (sizeof(BitWord) == 8)
141 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
143 assert(0 && "Unsupported!");
145 assert(0 && "Illegal empty element");
146 return 0; // Not reached
149 /// find_next - Returns the index of the next set bit starting from the
150 /// "Curr" bit. Returns -1 if the next set bit is not found.
151 int find_next(unsigned Curr) const {
152 if (Curr >= BITS_PER_ELEMENT)
155 unsigned WordPos = Curr / BITWORD_SIZE;
156 unsigned BitPos = Curr % BITWORD_SIZE;
157 BitWord Copy = Bits[WordPos];
158 assert (WordPos <= BITWORDS_PER_ELEMENT
159 && "Word Position outside of element");
161 // Mask off previous bits.
162 Copy &= ~0L << BitPos;
165 if (sizeof(BitWord) == 4)
166 return WordPos * BITWORD_SIZE + CountTrailingZeros_32(Copy);
167 else if (sizeof(BitWord) == 8)
168 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
170 assert(0 && "Unsupported!");
173 // Check subsequent words.
174 for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
176 if (sizeof(BitWord) == 4)
177 return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
178 else if (sizeof(BitWord) == 8)
179 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
181 assert(0 && "Unsupported!");
186 // Union this element with RHS and return true if this one changed.
187 bool unionWith(const SparseBitVectorElement &RHS) {
188 bool changed = false;
189 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
190 BitWord old = changed ? 0 : Bits[i];
192 Bits[i] |= RHS.Bits[i];
193 if (!changed && old != Bits[i])
199 // Return true if we have any bits in common with RHS
200 bool intersects(const SparseBitVectorElement &RHS) const {
201 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
202 if (RHS.Bits[i] & Bits[i])
208 // Intersect this Element with RHS and return true if this one changed.
209 // BecameZero is set to true if this element became all-zero bits.
210 bool intersectWith(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;
229 // Intersect this Element with the complement of RHS and return true if this
230 // one changed. BecameZero is set to true if this element became all-zero
232 bool intersectWithComplement(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 // Three argument version of intersectWithComplement that intersects
252 // RHS1 & ~RHS2 into this element
253 void intersectWithComplement(const SparseBitVectorElement &RHS1,
254 const SparseBitVectorElement &RHS2,
259 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
260 Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
264 BecameZero = allzero;
267 // Get a hash value for this element;
268 uint64_t getHashValue() const {
269 uint64_t HashVal = 0;
270 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
277 template <unsigned ElementSize = 128>
278 class SparseBitVector {
279 typedef ilist<SparseBitVectorElement<ElementSize> > ElementList;
280 typedef typename ElementList::iterator ElementListIter;
281 typedef typename ElementList::const_iterator ElementListConstIter;
283 BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
286 // Pointer to our current Element.
287 ElementListIter CurrElementIter;
288 ElementList Elements;
290 // This is like std::lower_bound, except we do linear searching from the
292 ElementListIter FindLowerBound(unsigned ElementIndex) {
294 if (Elements.empty()) {
295 CurrElementIter = Elements.begin();
296 return Elements.begin();
299 // Make sure our current iterator is valid.
300 if (CurrElementIter == Elements.end())
303 // Search from our current iterator, either backwards or forwards,
304 // depending on what element we are looking for.
305 ElementListIter ElementIter = CurrElementIter;
306 if (CurrElementIter->index() == ElementIndex) {
308 } else if (CurrElementIter->index() > ElementIndex) {
309 while (ElementIter != Elements.begin()
310 && ElementIter->index() > ElementIndex)
313 while (ElementIter != Elements.end() &&
314 ElementIter->index() < ElementIndex)
317 CurrElementIter = ElementIter;
321 // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
322 // than it would be, in order to be efficient.
323 class SparseBitVectorIterator {
327 const SparseBitVector<ElementSize> *BitVector;
329 // Current element inside of bitmap.
330 ElementListConstIter Iter;
332 // Current bit number inside of our bitmap.
335 // Current word number inside of our element.
338 // Current bits from the element.
339 typename SparseBitVectorElement<ElementSize>::BitWord Bits;
341 // Move our iterator to the first non-zero bit in the bitmap.
342 void AdvanceToFirstNonZero() {
345 if (BitVector->Elements.empty()) {
349 Iter = BitVector->Elements.begin();
350 BitNumber = Iter->index() * ElementSize;
351 unsigned BitPos = Iter->find_first();
353 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
354 Bits = Iter->word(WordNumber);
355 Bits >>= BitPos % BITWORD_SIZE;
358 // Move our iterator to the next non-zero bit.
359 void AdvanceToNextNonZero() {
363 while (Bits && !(Bits & 1)) {
368 // See if we ran out of Bits in this word.
370 int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
371 // If we ran out of set bits in this element, move to next element.
372 if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
376 // We may run out of elements in the bitmap.
377 if (Iter == BitVector->Elements.end()) {
381 // Set up for next non zero word in bitmap.
382 BitNumber = Iter->index() * ElementSize;
383 NextSetBitNumber = Iter->find_first();
384 BitNumber += NextSetBitNumber;
385 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
386 Bits = Iter->word(WordNumber);
387 Bits >>= NextSetBitNumber % BITWORD_SIZE;
389 WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
390 Bits = Iter->word(WordNumber);
391 Bits >>= NextSetBitNumber % BITWORD_SIZE;
392 BitNumber = Iter->index() * ElementSize;
393 BitNumber += NextSetBitNumber;
399 inline SparseBitVectorIterator& operator++() {
402 AdvanceToNextNonZero();
407 inline SparseBitVectorIterator operator++(int) {
408 SparseBitVectorIterator tmp = *this;
413 // Return the current set bit number.
414 unsigned operator*() const {
418 bool operator==(const SparseBitVectorIterator &RHS) const {
419 // If they are both at the end, ignore the rest of the fields.
420 if (AtEnd && RHS.AtEnd)
422 // Otherwise they are the same if they have the same bit number and
424 return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
426 bool operator!=(const SparseBitVectorIterator &RHS) const {
427 return !(*this == RHS);
429 SparseBitVectorIterator(): BitVector(NULL) {
433 SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
434 bool end = false):BitVector(RHS) {
435 Iter = BitVector->Elements.begin();
440 AdvanceToFirstNonZero();
444 typedef SparseBitVectorIterator iterator;
447 CurrElementIter = Elements.begin ();
453 // SparseBitVector copy ctor.
454 SparseBitVector(const SparseBitVector &RHS) {
455 ElementListConstIter ElementIter = RHS.Elements.begin();
456 while (ElementIter != RHS.Elements.end()) {
457 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
461 CurrElementIter = Elements.begin ();
470 SparseBitVector& operator=(const SparseBitVector& RHS) {
473 ElementListConstIter ElementIter = RHS.Elements.begin();
474 while (ElementIter != RHS.Elements.end()) {
475 Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
479 CurrElementIter = Elements.begin ();
484 // Test, Reset, and Set a bit in the bitmap.
485 bool test(unsigned Idx) {
486 if (Elements.empty())
489 unsigned ElementIndex = Idx / ElementSize;
490 ElementListIter ElementIter = FindLowerBound(ElementIndex);
492 // If we can't find an element that is supposed to contain this bit, there
493 // is nothing more to do.
494 if (ElementIter == Elements.end() ||
495 ElementIter->index() != ElementIndex)
497 return ElementIter->test(Idx % ElementSize);
500 void reset(unsigned Idx) {
501 if (Elements.empty())
504 unsigned ElementIndex = Idx / ElementSize;
505 ElementListIter ElementIter = FindLowerBound(ElementIndex);
507 // If we can't find an element that is supposed to contain this bit, there
508 // is nothing more to do.
509 if (ElementIter == Elements.end() ||
510 ElementIter->index() != ElementIndex)
512 ElementIter->reset(Idx % ElementSize);
514 // When the element is zeroed out, delete it.
515 if (ElementIter->empty()) {
517 Elements.erase(ElementIter);
521 void set(unsigned Idx) {
522 unsigned ElementIndex = Idx / ElementSize;
523 SparseBitVectorElement<ElementSize> *Element;
524 ElementListIter ElementIter;
525 if (Elements.empty()) {
526 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
527 ElementIter = Elements.insert(Elements.end(), Element);
530 ElementIter = FindLowerBound(ElementIndex);
532 if (ElementIter == Elements.end() ||
533 ElementIter->index() != ElementIndex) {
534 Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
535 // We may have hit the beginning of our SparseBitVector, in which case,
536 // we may need to insert right after this element, which requires moving
537 // the current iterator forward one, because insert does insert before.
538 if (ElementIter != Elements.end() &&
539 ElementIter->index() < ElementIndex)
540 ElementIter = Elements.insert(++ElementIter, Element);
542 ElementIter = Elements.insert(ElementIter, Element);
545 CurrElementIter = ElementIter;
547 ElementIter->set(Idx % ElementSize);
550 bool test_and_set (unsigned Idx) {
551 bool old = test(Idx);
559 bool operator!=(const SparseBitVector &RHS) const {
560 return !(*this == RHS);
563 bool operator==(const SparseBitVector &RHS) const {
564 ElementListConstIter Iter1 = Elements.begin();
565 ElementListConstIter Iter2 = RHS.Elements.begin();
567 for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
569 if (*Iter1 != *Iter2)
572 return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
575 // Union our bitmap with the RHS and return true if we changed.
576 bool operator|=(const SparseBitVector &RHS) {
577 bool changed = false;
578 ElementListIter Iter1 = Elements.begin();
579 ElementListConstIter Iter2 = RHS.Elements.begin();
581 // If RHS is empty, we are done
582 if (RHS.Elements.empty())
585 while (Iter2 != RHS.Elements.end()) {
586 if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
587 Elements.insert(Iter1,
588 new SparseBitVectorElement<ElementSize>(*Iter2));
591 } else if (Iter1->index() == Iter2->index()) {
592 changed |= Iter1->unionWith(*Iter2);
599 CurrElementIter = Elements.begin();
603 // Intersect our bitmap with the RHS and return true if ours changed.
604 bool operator&=(const SparseBitVector &RHS) {
605 bool changed = false;
606 ElementListIter Iter1 = Elements.begin();
607 ElementListConstIter Iter2 = RHS.Elements.begin();
609 // Check if both bitmaps are empty.
610 if (Elements.empty() && RHS.Elements.empty())
613 // Loop through, intersecting as we go, erasing elements when necessary.
614 while (Iter2 != RHS.Elements.end()) {
615 if (Iter1 == Elements.end()) {
616 CurrElementIter = Elements.begin();
620 if (Iter1->index() > Iter2->index()) {
622 } else if (Iter1->index() == Iter2->index()) {
624 changed |= Iter1->intersectWith(*Iter2, BecameZero);
626 ElementListIter IterTmp = Iter1;
628 Elements.erase(IterTmp);
634 ElementListIter IterTmp = Iter1;
636 Elements.erase(IterTmp);
639 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) {
647 bool changed = false;
648 ElementListIter Iter1 = Elements.begin();
649 ElementListConstIter Iter2 = RHS.Elements.begin();
651 // If either our bitmap or RHS is empty, we are done
652 if (Elements.empty() || RHS.Elements.empty())
655 // Loop through, intersecting as we go, erasing elements when necessary.
656 while (Iter2 != RHS.Elements.end()) {
657 if (Iter1 == Elements.end()) {
658 CurrElementIter = Elements.begin();
662 if (Iter1->index() > Iter2->index()) {
664 } else if (Iter1->index() == Iter2->index()) {
666 changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
668 ElementListIter IterTmp = Iter1;
670 Elements.erase(IterTmp);
679 CurrElementIter = Elements.begin();
683 bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
684 return intersectWithComplement(*RHS);
688 // Three argument version of intersectWithComplement.
689 // Result of RHS1 & ~RHS2 is stored into this bitmap.
690 void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
691 const SparseBitVector<ElementSize> &RHS2)
694 CurrElementIter = Elements.begin();
695 ElementListConstIter Iter1 = RHS1.Elements.begin();
696 ElementListConstIter Iter2 = RHS2.Elements.begin();
698 // If RHS1 is empty, we are done
699 // If RHS2 is empty, we still have to copy RHS1
700 if (RHS1.Elements.empty())
703 // Loop through, intersecting as we go, erasing elements when necessary.
704 while (Iter2 != RHS2.Elements.end()) {
705 if (Iter1 == RHS1.Elements.end())
708 if (Iter1->index() > Iter2->index()) {
710 } else if (Iter1->index() == Iter2->index()) {
711 bool BecameZero = false;
712 SparseBitVectorElement<ElementSize> *NewElement =
713 new SparseBitVectorElement<ElementSize>(Iter1->index());
714 NewElement->intersectWithComplement(*Iter1, *Iter2, BecameZero);
716 Elements.push_back(NewElement);
723 SparseBitVectorElement<ElementSize> *NewElement =
724 new SparseBitVectorElement<ElementSize>(*Iter1);
725 Elements.push_back(NewElement);
730 // copy the remaining elements
731 while (Iter1 != RHS1.Elements.end()) {
732 SparseBitVectorElement<ElementSize> *NewElement =
733 new SparseBitVectorElement<ElementSize>(*Iter1);
734 Elements.push_back(NewElement);
741 void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
742 const SparseBitVector<ElementSize> *RHS2) {
743 intersectWithComplement(*RHS1, *RHS2);
746 bool intersects(const SparseBitVector<ElementSize> *RHS) const {
747 return intersects(*RHS);
750 // Return true if we share any bits in common with RHS
751 bool intersects(const SparseBitVector<ElementSize> &RHS) const {
752 ElementListConstIter Iter1 = Elements.begin();
753 ElementListConstIter Iter2 = RHS.Elements.begin();
755 // Check if both bitmaps are empty.
756 if (Elements.empty() && RHS.Elements.empty())
759 // Loop through, intersecting stopping when we hit bits in common.
760 while (Iter2 != RHS.Elements.end()) {
761 if (Iter1 == Elements.end())
764 if (Iter1->index() > Iter2->index()) {
766 } else if (Iter1->index() == Iter2->index()) {
767 if (Iter1->intersects(*Iter2))
778 // Return true iff all bits set in this SparseBitVector are
780 bool contains(const SparseBitVector<ElementSize> &RHS) const {
781 SparseBitVector<ElementSize> Result(*this);
783 return (Result == RHS);
786 // Return the first set bit in the bitmap. Return -1 if no bits are set.
787 int find_first() const {
788 if (Elements.empty())
790 const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
791 return (First.index() * ElementSize) + First.find_first();
794 // Return true if the SparseBitVector is empty
796 return Elements.empty();
799 unsigned count() const {
800 unsigned BitCount = 0;
801 for (ElementListConstIter Iter = Elements.begin();
802 Iter != Elements.end();
804 BitCount += Iter->count();
808 iterator begin() const {
809 return iterator(this);
812 iterator end() const {
813 return iterator(this, true);
816 // Get a hash value for this bitmap.
817 uint64_t getHashValue() const {
818 uint64_t HashVal = 0;
819 for (ElementListConstIter Iter = Elements.begin();
820 Iter != Elements.end();
822 HashVal ^= Iter->index();
823 HashVal ^= Iter->getHashValue();
829 // Convenience functions to allow Or and And without dereferencing in the user
832 template <unsigned ElementSize>
833 inline bool operator |=(SparseBitVector<ElementSize> &LHS,
834 const SparseBitVector<ElementSize> *RHS) {
838 template <unsigned ElementSize>
839 inline bool operator |=(SparseBitVector<ElementSize> *LHS,
840 const SparseBitVector<ElementSize> &RHS) {
841 return LHS->operator|=(RHS);
844 template <unsigned ElementSize>
845 inline bool operator &=(SparseBitVector<ElementSize> *LHS,
846 const SparseBitVector<ElementSize> &RHS) {
847 return LHS->operator&=(RHS);
850 template <unsigned ElementSize>
851 inline bool operator &=(SparseBitVector<ElementSize> &LHS,
852 const SparseBitVector<ElementSize> *RHS) {
856 // Convenience functions for infix union, intersection, difference operators.
858 template <unsigned ElementSize>
859 inline SparseBitVector<ElementSize>
860 operator|(const SparseBitVector<ElementSize> &LHS,
861 const SparseBitVector<ElementSize> &RHS) {
862 SparseBitVector<ElementSize> Result(LHS);
867 template <unsigned ElementSize>
868 inline SparseBitVector<ElementSize>
869 operator&(const SparseBitVector<ElementSize> &LHS,
870 const SparseBitVector<ElementSize> &RHS) {
871 SparseBitVector<ElementSize> Result(LHS);
876 template <unsigned ElementSize>
877 inline SparseBitVector<ElementSize>
878 operator-(const SparseBitVector<ElementSize> &LHS,
879 const SparseBitVector<ElementSize> &RHS) {
880 SparseBitVector<ElementSize> Result;
881 Result.intersectWithComplement(LHS, RHS);
888 // Dump a SparseBitVector to a stream
889 template <unsigned ElementSize>
890 void dump(const SparseBitVector<ElementSize> &LHS, llvm::OStream &out) {
893 typename SparseBitVector<ElementSize>::iterator bi;
894 for (bi = LHS.begin(); bi != LHS.end(); ++bi) {
899 } // end namespace llvm