Fix small bug in operator== for iterators
[oota-llvm.git] / include / llvm / ADT / SparseBitVector.h
1 //===- llvm/ADT/SparseBitVector.h - Efficient Sparse BitVector -*- C++ -*- ===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the SparseBitVector class.  See the doxygen comment for
11 // SparseBitVector for more details on the algorithm used.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ADT_SPARSEBITVECTOR_H
16 #define LLVM_ADT_SPARSEBITVECTOR_H
17
18 #include <cassert>
19 #include <cstring>
20 #include <algorithm>
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/ADT/ilist"
25 namespace llvm {
26
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.
39
40
41 template <unsigned ElementSize = 128>
42 struct SparseBitVectorElement {
43 public:
44   typedef unsigned long BitWord;
45   enum {
46     BITWORD_SIZE = sizeof(BitWord) * 8,
47     BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
48     BITS_PER_ELEMENT = ElementSize
49   };
50
51   SparseBitVectorElement<ElementSize> *getNext() const {
52     return Next;
53   }
54   SparseBitVectorElement<ElementSize> *getPrev() const {
55     return Prev;
56   }
57
58   void setNext(SparseBitVectorElement<ElementSize> *RHS) {
59     Next = RHS;
60   }
61   void setPrev(SparseBitVectorElement<ElementSize> *RHS) {
62     Prev = RHS;
63   }
64
65 private:
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() {
73     ElementIndex = ~0UL;
74     memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
75   }
76
77   friend struct ilist_traits<SparseBitVectorElement<ElementSize> >;
78 public:
79   explicit SparseBitVectorElement(unsigned Idx) {
80     ElementIndex = Idx;
81     memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
82   }
83
84   ~SparseBitVectorElement() {
85   }
86
87   // Copy ctor.
88   SparseBitVectorElement(const SparseBitVectorElement &RHS) {
89     ElementIndex = RHS.ElementIndex;
90     std::copy(&RHS.Bits[0], &RHS.Bits[BITWORDS_PER_ELEMENT], Bits);
91   }
92
93   // Comparison.
94   bool operator==(const SparseBitVectorElement &RHS) const {
95     if (ElementIndex != RHS.ElementIndex)
96       return false;
97     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
98       if (Bits[i] != RHS.Bits[i])
99         return false;
100     return true;
101   }
102
103   bool operator!=(const SparseBitVectorElement &RHS) const {
104     return !(*this == RHS);
105   }
106
107   // Return the bits that make up word Idx in our element.
108   BitWord word(unsigned Idx) const {
109     assert (Idx < BITWORDS_PER_ELEMENT);
110     return Bits[Idx];
111   }
112
113   unsigned index() const {
114     return ElementIndex;
115   }
116
117   bool empty() const {
118     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
119       if (Bits[i])
120         return false;
121     return true;
122   }
123
124   void set(unsigned Idx) {
125     Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
126   }
127
128   bool test_and_set (unsigned Idx) {
129     bool old = test(Idx);
130     if (!old) {
131       set(Idx);
132       return true;
133     }
134     return false;
135   }
136
137   void reset(unsigned Idx) {
138     Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
139   }
140
141   bool test(unsigned Idx) const {
142     return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
143   }
144
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]);
152       else
153         assert(0 && "Unsupported!");
154     return NumBits;
155   }
156
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)
160       if (Bits[i] != 0) {
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]);
165         else
166           assert(0 && "Unsupported!");
167       }
168     assert(0 && "Illegal empty element");
169   }
170
171   /// find_next - Returns the index of the next set bit following the
172   /// "Prev" bit. Returns -1 if the next set bit is not found.
173   int find_next(unsigned Prev) const {
174     ++Prev;
175     if (Prev >= BITS_PER_ELEMENT)
176       return -1;
177
178     unsigned WordPos = Prev / BITWORD_SIZE;
179     unsigned BitPos = Prev % BITWORD_SIZE;
180     BitWord Copy = Bits[WordPos];
181     assert (WordPos <= BITWORDS_PER_ELEMENT
182             && "Word Position outside of element");
183
184     // Mask off previous bits.
185     Copy &= ~0L << BitPos;
186
187     if (Copy != 0) {
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);
192       else
193         assert(0 && "Unsupported!");
194     }
195
196     // Check subsequent words.
197     for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
198       if (Bits[i] != 0) {
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]);
203         else
204           assert(0 && "Unsupported!");
205       }
206     return -1;
207   }
208
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];
214
215       Bits[i] |= RHS.Bits[i];
216       if (!changed && old != Bits[i])
217         changed = true;
218     }
219     return changed;
220   }
221
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])
226         return true;
227     }
228     return false;
229   }
230
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,
234                      bool &BecameZero) {
235     bool changed = false;
236     bool allzero = true;
237
238     BecameZero = false;
239     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
240       BitWord old = changed ? 0 : Bits[i];
241
242       Bits[i] &= RHS.Bits[i];
243       if (Bits[i] != 0)
244         allzero = false;
245
246       if (!changed && old != Bits[i])
247         changed = true;
248     }
249     BecameZero = allzero;
250     return changed;
251   }
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
254   // bits.
255   bool intersectWithComplement(const SparseBitVectorElement &RHS,
256                                bool &BecameZero) {
257     bool changed = false;
258     bool allzero = true;
259
260     BecameZero = false;
261     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
262       BitWord old = changed ? 0 : Bits[i];
263
264       Bits[i] &= ~RHS.Bits[i];
265       if (Bits[i] != 0)
266         allzero = false;
267
268       if (!changed && old != Bits[i])
269         changed = true;
270     }
271     BecameZero = allzero;
272     return changed;
273   }
274   // Three argument version of intersectWithComplement that intersects
275   // RHS1 & ~RHS2 into this element
276   void intersectWithComplement(const SparseBitVectorElement &RHS1,
277                                const SparseBitVectorElement &RHS2,
278                                bool &BecameZero) {
279     bool allzero = true;
280
281     BecameZero = false;
282     for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
283       Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
284       if (Bits[i] != 0)
285         allzero = false;
286     }
287     BecameZero = allzero;
288   }
289
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) {
294       HashVal ^= Bits[i];
295     }
296     return HashVal;
297   }
298 };
299
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;
305   enum {
306     BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
307   };
308
309   // Pointer to our current Element.
310   ElementListIter CurrElementIter;
311   ElementList Elements;
312
313   // This is like std::lower_bound, except we do linear searching from the
314   // current position.
315   ElementListIter FindLowerBound(unsigned ElementIndex) {
316
317     if (Elements.empty()) {
318       CurrElementIter = Elements.begin();
319       return Elements.begin();
320     }
321
322     // Make sure our current iterator is valid.
323     if (CurrElementIter == Elements.end())
324       --CurrElementIter;
325
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) {
330       return ElementIter;
331     } else if (CurrElementIter->index() > ElementIndex) {
332       while (ElementIter != Elements.begin()
333              && ElementIter->index() > ElementIndex)
334         --ElementIter;
335     } else {
336       while (ElementIter != Elements.end() &&
337              ElementIter->index() <= ElementIndex)
338         ++ElementIter;
339       --ElementIter;
340     }
341     CurrElementIter = ElementIter;
342     return ElementIter;
343   }
344
345   // Iterator to walk set bits in the bitmap.  This iterator is a lot uglier
346   // than it would be, in order to be efficient.
347   class SparseBitVectorIterator {
348   private:
349     bool AtEnd;
350
351     const SparseBitVector<ElementSize> *BitVector;
352
353     // Current element inside of bitmap.
354     ElementListConstIter Iter;
355
356     // Current bit number inside of our bitmap.
357     unsigned BitNumber;
358
359     // Current word number inside of our element.
360     unsigned WordNumber;
361
362     // Current bits from the element.
363     typename SparseBitVectorElement<ElementSize>::BitWord Bits;
364
365     // Move our iterator to the first non-zero bit in the bitmap.
366     void AdvanceToFirstNonZero() {
367       if (AtEnd)
368         return;
369       if (BitVector->Elements.empty()) {
370         AtEnd = true;
371         return;
372       }
373       Iter = BitVector->Elements.begin();
374       BitNumber = Iter->index() * ElementSize;
375       unsigned BitPos = Iter->find_first();
376       BitNumber += BitPos;
377       WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
378       Bits = Iter->word(WordNumber);
379       Bits >>= BitPos % BITWORD_SIZE;
380     }
381
382     // Move our iterator to the next non-zero bit.
383     void AdvanceToNextNonZero() {
384       if (AtEnd)
385         return;
386
387       while (Bits && !(Bits & 1)) {
388         Bits >>= 1;
389         BitNumber += 1;
390       }
391
392       // See if we ran out of Bits in this word.
393       if (!Bits) {
394         int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
395         // If we ran out of set bits in this element, move to next element.
396         if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
397           ++Iter;
398           WordNumber = 0;
399
400           // We may run out of elements in the bitmap.
401           if (Iter == BitVector->Elements.end()) {
402             AtEnd = true;
403             return;
404           }
405           // Set up for next non zero word in bitmap.
406           BitNumber = Iter->index() * ElementSize;
407           NextSetBitNumber = Iter->find_first();
408           BitNumber += NextSetBitNumber;
409           WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
410           Bits = Iter->word(WordNumber);
411           Bits >>= NextSetBitNumber % BITWORD_SIZE;
412         } else {
413           WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
414           Bits = Iter->word(WordNumber);
415           Bits >>= NextSetBitNumber % BITWORD_SIZE;
416           BitNumber = Iter->index() * ElementSize;
417           BitNumber += NextSetBitNumber;
418         }
419       }
420     }
421   public:
422     // Preincrement.
423     inline SparseBitVectorIterator& operator++() {
424       ++BitNumber;
425       Bits >>= 1;
426       AdvanceToNextNonZero();
427       return *this;
428     }
429
430     // Postincrement.
431     inline SparseBitVectorIterator operator++(int) {
432       SparseBitVectorIterator tmp = *this;
433       ++*this;
434       return tmp;
435     }
436
437     // Return the current set bit number.
438     unsigned operator*() const {
439       return BitNumber;
440     }
441
442     bool operator==(const SparseBitVectorIterator &RHS) const {
443       // If they are both at the end, ignore the rest of the fields.
444       if (AtEnd && RHS.AtEnd)
445         return true;
446       // Otherwise they are the same if they have the same bit number and
447       // bitmap.
448       return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
449     }
450     bool operator!=(const SparseBitVectorIterator &RHS) const {
451       return !(*this == RHS);
452     }
453     SparseBitVectorIterator(): BitVector(NULL) {
454     }
455
456
457     SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
458                             bool end = false):BitVector(RHS) {
459       Iter = BitVector->Elements.begin();
460       BitNumber = 0;
461       Bits = 0;
462       WordNumber = ~0;
463       AtEnd = end;
464       AdvanceToFirstNonZero();
465     }
466   };
467 public:
468   typedef SparseBitVectorIterator iterator;
469
470   SparseBitVector () {
471     CurrElementIter = Elements.begin ();
472   }
473
474   ~SparseBitVector() {
475   }
476
477   // SparseBitVector copy ctor.
478   SparseBitVector(const SparseBitVector &RHS) {
479     ElementListConstIter ElementIter = RHS.Elements.begin();
480     while (ElementIter != RHS.Elements.end()) {
481       Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
482       ++ElementIter;
483     }
484
485     CurrElementIter = Elements.begin ();
486   }
487
488   // Test, Reset, and Set a bit in the bitmap.
489   bool test(unsigned Idx) {
490     if (Elements.empty())
491       return false;
492
493     unsigned ElementIndex = Idx / ElementSize;
494     ElementListIter ElementIter = FindLowerBound(ElementIndex);
495
496     // If we can't find an element that is supposed to contain this bit, there
497     // is nothing more to do.
498     if (ElementIter == Elements.end() ||
499         ElementIter->index() != ElementIndex)
500       return false;
501     return ElementIter->test(Idx % ElementSize);
502   }
503
504   void reset(unsigned Idx) {
505     if (Elements.empty())
506       return;
507
508     unsigned ElementIndex = Idx / ElementSize;
509     ElementListIter ElementIter = FindLowerBound(ElementIndex);
510
511     // If we can't find an element that is supposed to contain this bit, there
512     // is nothing more to do.
513     if (ElementIter == Elements.end() ||
514         ElementIter->index() != ElementIndex)
515       return;
516     ElementIter->reset(Idx % ElementSize);
517
518     // When the element is zeroed out, delete it.
519     if (ElementIter->empty()) {
520       ++CurrElementIter;
521       Elements.erase(ElementIter);
522     }
523   }
524
525   void set(unsigned Idx) {
526     unsigned ElementIndex = Idx / ElementSize;
527     SparseBitVectorElement<ElementSize> *Element;
528     ElementListIter ElementIter;
529     if (Elements.empty()) {
530       Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
531       ElementIter = Elements.insert(Elements.end(), Element);
532
533     } else {
534       ElementIter = FindLowerBound(ElementIndex);
535
536       if (ElementIter == Elements.end() ||
537           ElementIter->index() != ElementIndex) {
538         Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
539         // Insert does insert before, and lower bound gives the one before.
540         ElementIter = Elements.insert(++ElementIter, Element);
541       }
542     }
543     ElementIter->set(Idx % ElementSize);
544   }
545
546   bool test_and_set (unsigned Idx) {
547     bool old = test(Idx);
548     if (!old) {
549       set(Idx);
550       return true;
551     }
552     return false;
553   }
554
555   bool operator!=(const SparseBitVector &RHS) const {
556     return !(*this == RHS);
557   }
558
559   bool operator==(const SparseBitVector &RHS) const {
560     ElementListConstIter Iter1 = Elements.begin();
561     ElementListConstIter Iter2 = RHS.Elements.begin();
562
563     for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
564          ++Iter1, ++Iter2) {
565       if (*Iter1 != *Iter2)
566         return false;
567     }
568     return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
569   }
570
571   // Union our bitmap with the RHS and return true if we changed.
572   bool operator|=(const SparseBitVector &RHS) {
573     bool changed = false;
574     ElementListIter Iter1 = Elements.begin();
575     ElementListConstIter Iter2 = RHS.Elements.begin();
576
577     // Check if both bitmaps are empty
578     if (Elements.empty() && RHS.Elements.empty())
579       return false;
580
581     while (Iter2 != RHS.Elements.end()) {
582       if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
583         Elements.insert(Iter1,
584                         new SparseBitVectorElement<ElementSize>(*Iter2));
585         ++Iter2;
586         changed = true;
587       } else if (Iter1->index() == Iter2->index()) {
588         changed |= Iter1->unionWith(*Iter2);
589         ++Iter1;
590         ++Iter2;
591       } else {
592         ++Iter1;
593       }
594     }
595     CurrElementIter = Elements.begin();
596     return changed;
597   }
598
599   // Intersect our bitmap with the RHS and return true if ours changed.
600   bool operator&=(const SparseBitVector &RHS) {
601     bool changed = false;
602     ElementListIter Iter1 = Elements.begin();
603     ElementListConstIter Iter2 = RHS.Elements.begin();
604
605     // Check if both bitmaps are empty.
606     if (Elements.empty() && RHS.Elements.empty())
607       return false;
608
609     // Loop through, intersecting as we go, erasing elements when necessary.
610     while (Iter2 != RHS.Elements.end()) {
611       if (Iter1 == Elements.end())
612         return changed;
613
614       if (Iter1->index() > Iter2->index()) {
615         ++Iter2;
616       } else if (Iter1->index() == Iter2->index()) {
617         bool BecameZero;
618         changed |= Iter1->intersectWith(*Iter2, BecameZero);
619         if (BecameZero) {
620           ElementListIter IterTmp = Iter1;
621           ++Iter1;
622           Elements.erase(IterTmp);
623         } else {
624           ++Iter1;
625         }
626         ++Iter2;
627       } else {
628         ElementListIter IterTmp = Iter1;
629         ++Iter1;
630         Elements.erase(IterTmp);
631       }
632     }
633     Elements.erase(Iter1, Elements.end());
634     CurrElementIter = Elements.begin();
635     return changed;
636   }
637
638   // Intersect our bitmap with the complement of the RHS and return true if ours
639   // changed.
640   bool intersectWithComplement(const SparseBitVector &RHS) {
641     bool changed = false;
642     ElementListIter Iter1 = Elements.begin();
643     ElementListConstIter Iter2 = RHS.Elements.begin();
644
645     // Check if they are both empty
646     if (Elements.empty() && RHS.Elements.empty())
647       return false;
648
649     // Loop through, intersecting as we go, erasing elements when necessary.
650     while (Iter2 != RHS.Elements.end()) {
651       if (Iter1 == Elements.end())
652         return changed;
653
654       if (Iter1->index() > Iter2->index()) {
655         ++Iter2;
656       } else if (Iter1->index() == Iter2->index()) {
657         bool BecameZero;
658         changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
659         if (BecameZero) {
660           ElementListIter IterTmp = Iter1;
661           ++Iter1;
662           Elements.erase(IterTmp);
663         } else {
664           ++Iter1;
665         }
666         ++Iter2;
667       } else {
668         ElementListIter IterTmp = Iter1;
669         ++Iter1;
670         Elements.erase(IterTmp);
671       }
672     }
673     CurrElementIter = Elements.begin();
674     return changed;
675   }
676
677   bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
678     return intersectWithComplement(*RHS);
679   }
680
681
682   //  Three argument version of intersectWithComplement.  Result of RHS1 & ~RHS2
683   //  is stored into this bitmap.
684   void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
685                                const SparseBitVector<ElementSize> &RHS2)
686   {
687     Elements.clear();
688     ElementListConstIter Iter1 = RHS1.Elements.begin();
689     ElementListConstIter Iter2 = RHS2.Elements.begin();
690
691     // Check if they are both empty.
692     if (RHS1.empty() && RHS2.empty())
693       return;
694
695     // Loop through, intersecting as we go, erasing elements when necessary.
696     while (Iter2 != RHS2.Elements.end()) {
697       if (Iter1 == RHS1.Elements.end())
698         return;
699
700       if (Iter1->index() > Iter2->index()) {
701         ++Iter2;
702       } else if (Iter1->index() == Iter2->index()) {
703         bool BecameZero = false;
704         SparseBitVectorElement<ElementSize> *NewElement =
705           new SparseBitVectorElement<ElementSize>(Iter1->index());
706         NewElement->intersectWithComplement(*Iter1, *Iter2, BecameZero);
707         if (!BecameZero) {
708           Elements.push_back(NewElement);
709         }
710         else
711           delete NewElement;
712         ++Iter1;
713         ++Iter2;
714       } else {
715         ++Iter1;
716       }
717     }
718
719     // copy the remaining elements
720     while (Iter1 != RHS1.Elements.end()) {
721         SparseBitVectorElement<ElementSize> *NewElement =
722           new SparseBitVectorElement<ElementSize>(*Iter1);
723         Elements.push_back(NewElement);
724         ++Iter1;
725       }
726
727     CurrElementIter = Elements.begin();
728     return;
729   }
730
731   void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
732                                const SparseBitVector<ElementSize> *RHS2) {
733     intersectWithComplement(*RHS1, *RHS2);
734   }
735
736   bool intersects(const SparseBitVector<ElementSize> *RHS) const {
737     return intersects(*RHS);
738   }
739
740   // Return true if we share any bits in common with RHS
741   bool intersects(const SparseBitVector<ElementSize> &RHS) const {
742     ElementListConstIter Iter1 = Elements.begin();
743     ElementListConstIter Iter2 = RHS.Elements.begin();
744
745     // Check if both bitmaps are empty.
746     if (Elements.empty() && RHS.Elements.empty())
747       return false;
748
749     // Loop through, intersecting stopping when we hit bits in common.
750     while (Iter2 != RHS.Elements.end()) {
751       if (Iter1 == Elements.end())
752         return false;
753
754       if (Iter1->index() > Iter2->index()) {
755         ++Iter2;
756       } else if (Iter1->index() == Iter2->index()) {
757         if (Iter1->intersects(*Iter2))
758           return true;
759         ++Iter1;
760         ++Iter2;
761       } else {
762         ++Iter1;
763       }
764     }
765     return false;
766   }
767
768   // Return the first set bit in the bitmap.  Return -1 if no bits are set.
769   int find_first() const {
770     if (Elements.empty())
771       return -1;
772     const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
773     return (First.index() * ElementSize) + First.find_first();
774   }
775
776   // Return true if the SparseBitVector is empty
777   bool empty() const {
778     return Elements.empty();
779   }
780
781   unsigned count() const {
782     unsigned BitCount = 0;
783     for (ElementListConstIter Iter = Elements.begin();
784          Iter != Elements.end();
785          ++Iter)
786       BitCount += Iter->count();
787
788     return BitCount;
789   }
790   iterator begin() const {
791     return iterator(this);
792   }
793
794   iterator end() const {
795     return iterator(this, ~0);
796   }
797
798   // Get a hash value for this bitmap.
799   uint64_t getHashValue() const {
800     uint64_t HashVal = 0;
801     for (ElementListConstIter Iter = Elements.begin();
802          Iter != Elements.end();
803          ++Iter) {
804       HashVal ^= Iter->index();
805       HashVal ^= Iter->getHashValue();
806     }
807     return HashVal;
808   }
809 };
810
811 // Convenience functions to allow Or and And without dereferencing in the user
812 // code.
813
814 template <unsigned ElementSize>
815 inline bool operator |=(SparseBitVector<ElementSize> &LHS,
816                         const SparseBitVector<ElementSize> *RHS) {
817   return LHS |= *RHS;
818 }
819
820 template <unsigned ElementSize>
821 inline bool operator |=(SparseBitVector<ElementSize> *LHS,
822                         const SparseBitVector<ElementSize> &RHS) {
823   return LHS->operator|=(RHS);
824 }
825
826 template <unsigned ElementSize>
827 inline bool operator &=(SparseBitVector<ElementSize> *LHS,
828                         const SparseBitVector<ElementSize> &RHS) {
829   return LHS->operator&=(RHS);
830 }
831
832 template <unsigned ElementSize>
833 inline bool operator &=(SparseBitVector<ElementSize> &LHS,
834                         const SparseBitVector<ElementSize> *RHS) {
835   return LHS &= (*RHS);
836 }
837
838
839 // Dump a SparseBitVector to a stream
840 template <unsigned ElementSize>
841 void dump(const SparseBitVector<ElementSize> &LHS, llvm::OStream &out) {
842   out << "[ ";
843
844   typename SparseBitVector<ElementSize>::iterator bi;
845   for (bi = LHS.begin(); bi != LHS.end(); ++bi) {
846     out << *bi << " ";
847   }
848     out << " ]\n";
849 }
850 }
851
852
853
854 #endif