llvm.org GIT mirror llvm / 16ebc26
Fix bugs with &=, intersect with complement. Add three argument version of intersect with complement. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41832 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 13 years ago
1 changed file(s) with 137 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
198198 bool intersects(const SparseBitVectorElement &RHS) const {
199199 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
200200 if (RHS.Bits[i] & Bits[i])
201 return true;
201 return true;
202202 }
203203 return false;
204204 }
205
205
206206 // Intersect this Element with RHS and return true if this one changed.
207207 // BecameZero is set to true if this element became all-zero bits.
208208 bool intersectWith(const SparseBitVectorElement &RHS,
246246 BecameZero = !allzero;
247247 return changed;
248248 }
249 // Three argument version of intersectWithComplement that intersects
250 // RHS1 & ~RHS2 into this element
251 void intersectWithComplement(const SparseBitVectorElement &RHS1,
252 const SparseBitVectorElement &RHS2,
253 bool &BecameZero) {
254 bool allzero = true;
255
256 BecameZero = false;
257 for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
258 Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
259 if (Bits[i] != 0)
260 allzero = false;
261 }
262 BecameZero = !allzero;
263 }
264
249265 };
250266
251267 template
345361 int NextSetBitNumber = (*Iter)->find_next(BitNumber % ElementSize) ;
346362 // If we ran out of set bits in this element, move to next element.
347363 if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
348 Iter++;
364 ++Iter;
349365 WordNumber = 0;
350366
351367 // We may run out of elements in the bitmap.
370386 public:
371387 // Preincrement.
372388 inline SparseBitVectorIterator& operator++() {
373 BitNumber++;
389 ++BitNumber;
374390 Bits >>= 1;
375391 AdvanceToNextNonZero();
376392 return *this;
399415 bool operator!=(const SparseBitVectorIterator &RHS) const {
400416 return !(*this == RHS);
401417 }
402 SparseBitVectorIterator(): BitVector(NULL) {
403 }
404
405
418 SparseBitVectorIterator(): BitVector(NULL) {
419 }
420
421
406422 SparseBitVectorIterator(const SparseBitVector *RHS,
407423 bool end = false):BitVector(RHS) {
408424 Iter = BitVector->Elements.begin();
496512 }
497513 Element->set(Idx % ElementSize);
498514 }
499
515
500516 bool test_and_set (unsigned Idx) {
501517 bool old = test(Idx);
502518 if (!old)
526542
527543 NewElem = new SparseBitVectorElement(*(*Iter2));
528544 Elements.insert(Iter1, NewElem);
529 Iter2++;
545 ++Iter2;
530546 changed = true;
531547 } else if ((*Iter1)->index() == (*Iter2)->index()) {
532548 changed |= (*Iter1)->unionWith(*(*Iter2));
533 Iter1++;
534 Iter2++;
549 ++Iter1;
550 ++Iter2;
535551 } else {
536 Iter1++;
552 ++Iter1;
537553 }
538554 }
539555 CurrElementIter = Elements.begin();
562578 return changed;
563579
564580 if ((*Iter1)->index() > (*Iter2)->index()) {
565 Iter2++;
581 ++Iter2;
566582 } else if ((*Iter1)->index() == (*Iter2)->index()) {
567583 bool BecameZero;
568584 changed |= (*Iter1)->intersectWith(*(*Iter2), BecameZero);
570586 ElementListIter IterTmp = Iter1;
571587 delete *IterTmp;
572588 Elements.erase(IterTmp);
573 Iter1++;
574 } else {
575 Iter1++;
576589 }
577 Iter2++;
590 ++Iter1;
591 ++Iter2;
578592 } else {
579593 ElementListIter IterTmp = Iter1;
580 Iter1++;
594 ++Iter1;
581595 delete *IterTmp;
582596 Elements.erase(IterTmp);
583597 }
584598 }
599 for_each(Iter1, Elements.end(),
600 deleter >);
601 Elements.erase(Iter1, Elements.end());
585602 CurrElementIter = Elements.begin();
586603 return changed;
587604 }
601618 // possible if they are the same bitmap.
602619 if (Iter1 != Elements.end() && Iter2 != RHS.Elements.end())
603620 if (*Iter1 == *Iter2) {
621 for_each(Elements.begin(), Elements.end(),
622 deleter >);
604623 Elements.clear();
605624 return true;
606625 }
607
626
608627 // Loop through, intersecting as we go, erasing elements when necessary.
609628 while (Iter2 != RHS.Elements.end()) {
610629 if (Iter1 == Elements.end())
611630 return changed;
612631
613632 if ((*Iter1)->index() > (*Iter2)->index()) {
614 Iter2++;
633 ++Iter2;
615634 } else if ((*Iter1)->index() == (*Iter2)->index()) {
616635 bool BecameZero;
617636 changed |= (*Iter1)->intersectWithComplement(*(*Iter2), BecameZero);
619638 ElementListIter IterTmp = Iter1;
620639 delete *IterTmp;
621640 Elements.erase(IterTmp);
622 Iter1++;
623 } else {
624 Iter1++;
625641 }
626 Iter2++;
642 ++Iter1;
643 ++Iter2;
627644 } else {
628645 ElementListIter IterTmp = Iter1;
629 Iter1++;
646 ++Iter1;
630647 delete *IterTmp;
631648 Elements.erase(IterTmp);
632649 }
638655 bool intersectWithComplement(const SparseBitVector *RHS) const {
639656 return intersectWithComplement(*RHS);
640657 }
641
642
658
659
660 // Three argument version of intersectWithComplement. Result of RHS1 & ~RHS2
661 // is stored into this bitmap.
662 void intersectWithComplement(const SparseBitVector &RHS1,
663 const SparseBitVector &RHS2)
664 {
665 for_each(Elements.begin(), Elements.end(),
666 deleter >);
667 Elements.clear();
668
669 ElementListConstIter Iter1 = RHS1.Elements.begin();
670 ElementListConstIter Iter2 = RHS2.Elements.begin();
671
672 // IE They may both be end.
673 if (Iter1 == Iter2)
674 return;
675
676 // See if the first bitmap element is the same in both. This is only
677 // possible if they are the same bitmap.
678 if (Iter1 != RHS1.Elements.end() && Iter2 != RHS2.Elements.end())
679 if (*Iter1 == *Iter2) {
680 return;
681 }
682
683 // Loop through, intersecting as we go, erasing elements when necessary.
684 while (Iter2 != RHS2.Elements.end()) {
685 if (Iter1 == RHS1.Elements.end())
686 return;
687
688 if ((*Iter1)->index() > (*Iter2)->index()) {
689 ++Iter2;
690 } else if ((*Iter1)->index() == (*Iter2)->index()) {
691 bool BecameZero = false;
692 SparseBitVectorElement *NewElement =
693 new SparseBitVectorElement((*Iter1)->index());
694
695 NewElement->intersectWithComplement(*(*Iter1), *(*Iter2), BecameZero);
696 if (BecameZero) {
697 delete NewElement;
698 } else {
699 Elements.push_back(NewElement);
700 }
701
702 ++Iter1;
703 ++Iter2;
704 } else {
705 ++Iter1;
706 }
707 }
708 // copy the remaining elements
709
710 while (Iter1 != RHS1.Elements.end()) {
711 SparseBitVectorElement *NewElement =
712 new SparseBitVectorElement(*(*Iter1));
713 Elements.push_back(NewElement);
714 }
715
716 CurrElementIter = Elements.begin();
717 return;
718 }
719
720 void intersectWithComplement(const SparseBitVector *RHS1,
721 const SparseBitVector *RHS2) {
722 intersectWithComplement(*RHS1, *RHS2);
723 }
724
643725 bool intersects(const SparseBitVector *RHS) const {
644726 return intersects(*RHS);
645727 }
646
728
647729 // Return true if we share any bits in common with RHS
648730 bool intersects(const SparseBitVector &RHS) const {
649731 ElementListConstIter Iter1 = Elements.begin();
659741 if (*Iter1 == *Iter2) {
660742 return true;
661743 }
662
744
663745 // Loop through, intersecting stopping when we hit bits in common.
664746 while (Iter2 != RHS.Elements.end()) {
665747 if (Iter1 == Elements.end())
666748 return false;
667749
668750 if ((*Iter1)->index() > (*Iter2)->index()) {
669 Iter2++;
751 ++Iter2;
670752 } else if ((*Iter1)->index() == (*Iter2)->index()) {
671753 if ((*Iter1)->intersects(*(*Iter2)))
672754 return true;
673 Iter1++;
674 Iter2++;
755 ++Iter1;
756 ++Iter2;
675757 } else {
676 Iter1++;
758 ++Iter1;
677759 }
678760 }
679761 return false;
680762 }
681
763
682764 // Return the first set bit in the bitmap. Return -1 if no bits are set.
683765 int find_first() const {
684766 if (Elements.empty())
691773 bool empty() const {
692774 return Elements.empty();
693775 }
694
776
695777 unsigned count() const {
696778 unsigned BitCount = 0;
697779 for (ElementListConstIter Iter = Elements.begin();
698780 Iter != Elements.end();
699781 ++Iter)
700782 BitCount += (*Iter)->count();
701
783
702784 return BitCount;
703785 }
704786 iterator begin() const {
711793 };
712794
713795 // Convenience functions to allow Or and And without dereferencing in the user
714 // code.
796 // code.
797
715798 template
716 inline void operator |=(SparseBitVector *LHS,
799 inline bool operator |=(SparseBitVector &LHS,
800 const SparseBitVector *RHS) {
801 return LHS |= *RHS;
802 }
803
804 template
805 inline bool operator |=(SparseBitVector *LHS,
717806 const SparseBitVector &RHS) {
718 LHS->operator|=(RHS);
807 return LHS->operator|=(RHS);
719808 }
720809
721810 template
722 inline void operator |=(SparseBitVector *LHS,
811 inline bool operator &=(SparseBitVector *LHS,
812 const SparseBitVector &RHS) {
813 return LHS->operator&=(RHS);
814 }
815
816 template
817 inline bool operator &=(SparseBitVector &LHS,
723818 const SparseBitVector *RHS) {
724 LHS->operator|=(RHS);
819 return LHS &= (*RHS);
725820 }
726821
727 template
728 inline void operator &=(SparseBitVector *LHS,
729 const SparseBitVector &RHS) {
730 LHS->operator&=(RHS);
731822 }
732823
733 template
734 inline void operator &=(SparseBitVector *LHS,
735 const SparseBitVector *RHS) {
736 LHS->operator&=(RHS);
737 }
738
739 }
740
741824 #endif