llvm.org GIT mirror llvm / 1b6998e
Add remaining functions necessary for andersen's git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41830 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 13 years ago
1 changed file(s) with 194 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
194194 return changed;
195195 }
196196
197 // Return true if we have any bits in common with RHS
198 bool intersects(const SparseBitVectorElement &RHS) const {
199 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
200 if (RHS.Bits[i] & Bits[i])
201 return true;
202 }
203 return false;
204 }
205
197206 // Intersect this Element with RHS and return true if this one changed.
198207 // BecameZero is set to true if this element became all-zero bits.
199208 bool intersectWith(const SparseBitVectorElement &RHS,
206215 BitWord old = changed ? 0 : Bits[i];
207216
208217 Bits[i] &= RHS.Bits[i];
218 if (Bits[i] != 0)
219 allzero = false;
220
221 if (old != Bits[i])
222 changed = true;
223 }
224 BecameZero = !allzero;
225 return changed;
226 }
227 // Intersect this Element with the complement of RHS and return true if this
228 // one changed. BecameZero is set to true if this element became all-zero
229 // bits.
230 bool intersectWithComplement(const SparseBitVectorElement &RHS,
231 bool &BecameZero) {
232 bool changed = false;
233 bool allzero = true;
234
235 BecameZero = false;
236 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
237 BitWord old = changed ? 0 : Bits[i];
238
239 Bits[i] &= ~RHS.Bits[i];
209240 if (Bits[i] != 0)
210241 allzero = false;
211242
264295
265296 // Iterator to walk set bits in the bitmap. This iterator is a lot uglier
266297 // than it would be, in order to be efficient.
267 struct SparseBitVectorIterator {
298 class SparseBitVectorIterator {
268299 private:
269300 bool AtEnd;
270301
271 SparseBitVector &BitVector;
302 const SparseBitVector *BitVector;
272303
273304 // Current element inside of bitmap.
274305 ElementListConstIter Iter;
286317 void AdvanceToFirstNonZero() {
287318 if (AtEnd)
288319 return;
289 if (BitVector.Elements.empty()) {
320 if (BitVector->Elements.empty()) {
290321 AtEnd = true;
291322 return;
292323 }
293 Iter = BitVector.Elements.begin();
324 Iter = BitVector->Elements.begin();
294325 BitNumber = (*Iter)->index() * ElementSize;
295326 unsigned BitPos = (*Iter)->find_first();
296327 BitNumber += BitPos;
318349 WordNumber = 0;
319350
320351 // We may run out of elements in the bitmap.
321 if (Iter == BitVector.Elements.end()) {
352 if (Iter == BitVector->Elements.end()) {
322353 AtEnd = true;
323354 return;
324355 }
368399 bool operator!=(const SparseBitVectorIterator &RHS) const {
369400 return !(*this == RHS);
370401 }
371
372 explicit SparseBitVectorIterator(SparseBitVector &RHS,
373 bool end = false):BitVector(RHS) {
374 Iter = BitVector.Elements.begin();
402 SparseBitVectorIterator(): BitVector(NULL) {
403 }
404
405
406 SparseBitVectorIterator(const SparseBitVector *RHS,
407 bool end = false):BitVector(RHS) {
408 Iter = BitVector->Elements.begin();
375409 BitNumber = 0;
376410 Bits = 0;
377411 WordNumber = ~0;
381415 };
382416 public:
383417 typedef SparseBitVectorIterator iterator;
384 typedef const SparseBitVectorIterator const_iterator;
385418
386419 SparseBitVector () {
387420 CurrElementIter = Elements.begin ();
462495 }
463496 }
464497 Element->set(Idx % ElementSize);
498 }
499
500 bool test_and_set (unsigned Idx) {
501 bool old = test(Idx);
502 if (!old)
503 set(Idx);
504 return !old;
465505 }
466506
467507 // Union our bitmap with the RHS and return true if we changed.
546586 return changed;
547587 }
548588
589 // Intersect our bitmap with the complement of the RHS and return true if ours
590 // changed.
591 bool intersectWithComplement(const SparseBitVector &RHS) {
592 bool changed = false;
593 ElementListIter Iter1 = Elements.begin();
594 ElementListConstIter Iter2 = RHS.Elements.begin();
595
596 // IE They may both be end.
597 if (Iter1 == Iter2)
598 return false;
599
600 // See if the first bitmap element is the same in both. This is only
601 // possible if they are the same bitmap.
602 if (Iter1 != Elements.end() && Iter2 != RHS.Elements.end())
603 if (*Iter1 == *Iter2) {
604 Elements.clear();
605 return true;
606 }
607
608 // Loop through, intersecting as we go, erasing elements when necessary.
609 while (Iter2 != RHS.Elements.end()) {
610 if (Iter1 == Elements.end())
611 return changed;
612
613 if ((*Iter1)->index() > (*Iter2)->index()) {
614 Iter2++;
615 } else if ((*Iter1)->index() == (*Iter2)->index()) {
616 bool BecameZero;
617 changed |= (*Iter1)->intersectWithComplement(*(*Iter2), BecameZero);
618 if (BecameZero) {
619 ElementListIter IterTmp = Iter1;
620 delete *IterTmp;
621 Elements.erase(IterTmp);
622 Iter1++;
623 } else {
624 Iter1++;
625 }
626 Iter2++;
627 } else {
628 ElementListIter IterTmp = Iter1;
629 Iter1++;
630 delete *IterTmp;
631 Elements.erase(IterTmp);
632 }
633 }
634 CurrElementIter = Elements.begin();
635 return changed;
636 }
637
638 bool intersectWithComplement(const SparseBitVector *RHS) const {
639 return intersectWithComplement(*RHS);
640 }
641
642
643 bool intersects(const SparseBitVector *RHS) const {
644 return intersects(*RHS);
645 }
646
647 // Return true if we share any bits in common with RHS
648 bool intersects(const SparseBitVector &RHS) const {
649 ElementListConstIter Iter1 = Elements.begin();
650 ElementListConstIter Iter2 = RHS.Elements.begin();
651
652 // IE They may both be end.
653 if (Iter1 == Iter2)
654 return false;
655
656 // See if the first bitmap element is the same in both. This is only
657 // possible if they are the same bitmap.
658 if (Iter1 != Elements.end() && Iter2 != RHS.Elements.end())
659 if (*Iter1 == *Iter2) {
660 return true;
661 }
662
663 // Loop through, intersecting stopping when we hit bits in common.
664 while (Iter2 != RHS.Elements.end()) {
665 if (Iter1 == Elements.end())
666 return false;
667
668 if ((*Iter1)->index() > (*Iter2)->index()) {
669 Iter2++;
670 } else if ((*Iter1)->index() == (*Iter2)->index()) {
671 if ((*Iter1)->intersects(*(*Iter2)))
672 return true;
673 Iter1++;
674 Iter2++;
675 } else {
676 Iter1++;
677 }
678 }
679 return false;
680 }
681
682 // Return the first set bit in the bitmap. Return -1 if no bits are set.
683 int find_first() const {
684 if (Elements.empty())
685 return -1;
686 const SparseBitVectorElement *First = *(Elements.begin());
687 return (First->index() * ElementSize) + First->find_first();
688 }
689
690 // Return true if the SparseBitVector is empty
691 bool empty() const {
692 return Elements.empty();
693 }
694
695 unsigned count() const {
696 unsigned BitCount = 0;
697 for (ElementListConstIter Iter = Elements.begin();
698 Iter != Elements.end();
699 ++Iter)
700 BitCount += (*Iter)->count();
701
702 return BitCount;
703 }
549704 iterator begin() const {
550 return iterator(*this);
705 return iterator(this);
551706 }
552707
553708 iterator end() const {
554 return iterator(*this, ~0);
709 return iterator(this, ~0);
555710 }
556711 };
712
713 // Convenience functions to allow Or and And without dereferencing in the user
714 // code.
715 template
716 inline void operator |=(SparseBitVector *LHS,
717 const SparseBitVector &RHS) {
718 LHS->operator|=(RHS);
557719 }
558720
721 template
722 inline void operator |=(SparseBitVector *LHS,
723 const SparseBitVector *RHS) {
724 LHS->operator|=(RHS);
725 }
726
727 template
728 inline void operator &=(SparseBitVector *LHS,
729 const SparseBitVector &RHS) {
730 LHS->operator&=(RHS);
731 }
732
733 template
734 inline void operator &=(SparseBitVector *LHS,
735 const SparseBitVector *RHS) {
736 LHS->operator&=(RHS);
737 }
738
739 }
740
559741 #endif