llvm.org GIT mirror llvm / 6320260
Convert to use ilist and non-pointer lists for extra goodness git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41855 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 13 years ago
1 changed file(s) with 112 addition(s) and 97 deletion(s). Raw diff Collapse all Expand all
1616
1717 #include
1818 #include
19 #include
2019 #include
2120 #include "llvm/Support/DataTypes.h"
2221 #include "llvm/ADT/STLExtras.h"
2322 #include "llvm/Support/MathExtras.h"
24
23 #include "llvm/ADT/ilist"
2524 namespace llvm {
2625
2726 /// SparseBitVector is an implementation of a bitvector that is sparse by only
4746 BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
4847 BITS_PER_ELEMENT = ElementSize
4948 };
49
50 SparseBitVectorElement *getNext() const {
51 return Next;
52 }
53 SparseBitVectorElement *getPrev() const {
54 return Prev;
55 }
56
57 void setNext(SparseBitVectorElement *RHS) {
58 Next = RHS;
59 }
60 void setPrev(SparseBitVectorElement *RHS) {
61 Prev = RHS;
62 }
63
5064 private:
65 SparseBitVectorElement *Next;
66 SparseBitVectorElement *Prev;
5167 // Index of Element in terms of where first bit starts.
5268 unsigned ElementIndex;
5369 BitWord Bits[BITWORDS_PER_ELEMENT];
54 SparseBitVectorElement();
70 // Needed for sentinels
71 SparseBitVectorElement() {
72 ElementIndex = ~0UL;
73 memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
74 }
75
76 friend struct ilist_traits >;
77
5578 public:
5679 explicit SparseBitVectorElement(unsigned Idx) {
5780 ElementIndex = Idx;
261284 }
262285 BecameZero = !allzero;
263286 }
264
265287 };
266288
267289 template
268290 class SparseBitVector {
269 typedef std::list *> ElementList;
291 typedef ilist > ElementList;
270292 typedef typename ElementList::iterator ElementListIter;
271293 typedef typename ElementList::const_iterator ElementListConstIter;
272294 enum {
293315 // Search from our current iterator, either backwards or forwards,
294316 // depending on what element we are looking for.
295317 ElementListIter ElementIter = CurrElementIter;
296 if ((*CurrElementIter)->index() == ElementIndex) {
318 if (CurrElementIter->index() == ElementIndex) {
297319 return ElementIter;
298 } else if ((*CurrElementIter)->index() > ElementIndex) {
320 } else if (CurrElementIter->index() > ElementIndex) {
299321 while (ElementIter != Elements.begin()
300 && (*ElementIter)->index() > ElementIndex)
322 && ElementIter->index() > ElementIndex)
301323 --ElementIter;
302324 } else {
303325 while (ElementIter != Elements.end() &&
304 (*ElementIter)->index() <= ElementIndex)
326 ElementIter->index() <= ElementIndex)
305327 ++ElementIter;
306328 --ElementIter;
307329 }
338360 return;
339361 }
340362 Iter = BitVector->Elements.begin();
341 BitNumber = (*Iter)->index() * ElementSize;
342 unsigned BitPos = (*Iter)->find_first();
363 BitNumber = Iter->index() * ElementSize;
364 unsigned BitPos = Iter->find_first();
343365 BitNumber += BitPos;
344366 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
345 Bits = (*Iter)->word(WordNumber);
367 Bits = Iter->word(WordNumber);
346368 Bits >>= BitPos % BITWORD_SIZE;
347369 }
348370
358380
359381 // See if we ran out of Bits in this word.
360382 if (!Bits) {
361 int NextSetBitNumber = (*Iter)->find_next(BitNumber % ElementSize) ;
383 int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
362384 // If we ran out of set bits in this element, move to next element.
363385 if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
364386 ++Iter;
370392 return;
371393 }
372394 // Set up for next non zero word in bitmap.
373 BitNumber = (*Iter)->index() * ElementSize;
374 NextSetBitNumber = (*Iter)->find_first();
395 BitNumber = Iter->index() * ElementSize;
396 NextSetBitNumber = Iter->find_first();
375397 BitNumber += NextSetBitNumber;
376398 WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
377 Bits = (*Iter)->word(WordNumber);
399 Bits = Iter->word(WordNumber);
378400 Bits >>= NextSetBitNumber % BITWORD_SIZE;
379401 } else {
380402 WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
381 Bits = (*Iter)->word(WordNumber);
403 Bits = Iter->word(WordNumber);
382404 Bits >>= NextSetBitNumber % BITWORD_SIZE;
383405 }
384406 }
437459 }
438460
439461 ~SparseBitVector() {
440 for_each(Elements.begin(), Elements.end(),
441 deleter >);
442462 }
443463
444464 // SparseBitVector copy ctor.
445465 SparseBitVector(const SparseBitVector &RHS) {
446466 ElementListConstIter ElementIter = RHS.Elements.begin();
447467 while (ElementIter != RHS.Elements.end()) {
448 SparseBitVectorElement *ElementCopy;
449 ElementCopy = new SparseBitVectorElement(*(*ElementIter));
450 Elements.push_back(ElementCopy);
468 Elements.push_back(SparseBitVectorElement(*ElementIter));
469 ++ElementIter;
451470 }
452471
453472 CurrElementIter = Elements.begin ();
464483 // If we can't find an element that is supposed to contain this bit, there
465484 // is nothing more to do.
466485 if (ElementIter == Elements.end() ||
467 (*ElementIter)->index() != ElementIndex)
486 ElementIter->index() != ElementIndex)
468487 return false;
469 return (*ElementIter)->test(Idx % ElementSize);
488 return ElementIter->test(Idx % ElementSize);
470489 }
471490
472491 void reset(unsigned Idx) {
479498 // If we can't find an element that is supposed to contain this bit, there
480499 // is nothing more to do.
481500 if (ElementIter == Elements.end() ||
482 (*ElementIter)->index() != ElementIndex)
501 ElementIter->index() != ElementIndex)
483502 return;
484 (*ElementIter)->reset(Idx % ElementSize);
503 ElementIter->reset(Idx % ElementSize);
485504
486505 // When the element is zeroed out, delete it.
487 if ((*ElementIter)->empty()) {
488 delete (*ElementIter);
506 if (ElementIter->empty()) {
489507 ++CurrElementIter;
490508 Elements.erase(ElementIter);
491509 }
492510 }
493511
494512 void set(unsigned Idx) {
513 unsigned ElementIndex = Idx / ElementSize;
495514 SparseBitVectorElement *Element;
496 unsigned ElementIndex = Idx / ElementSize;
497
515 ElementListIter ElementIter;
498516 if (Elements.empty()) {
499517 Element = new SparseBitVectorElement(ElementIndex);
500 Elements.push_back(Element);
518 ElementIter = Elements.insert(Elements.end(), Element);
519
501520 } else {
502 ElementListIter ElementIter = FindLowerBound(ElementIndex);
503
504 if (ElementIter != Elements.end() &&
505 (*ElementIter)->index() == ElementIndex)
506 Element = *ElementIter;
507 else {
521 ElementIter = FindLowerBound(ElementIndex);
522
523 if (ElementIter == Elements.end() ||
524 ElementIter->index() != ElementIndex) {
508525 Element = new SparseBitVectorElement(ElementIndex);
509526 // Insert does insert before, and lower bound gives the one before.
510 Elements.insert(++ElementIter, Element);
511 }
512 }
513 Element->set(Idx % ElementSize);
527 ElementIter = Elements.insert(++ElementIter, Element);
528 }
529 }
530 ElementIter->set(Idx % ElementSize);
514531 }
515532
516533 bool test_and_set (unsigned Idx) {
526543 ElementListIter Iter1 = Elements.begin();
527544 ElementListConstIter Iter2 = RHS.Elements.begin();
528545
529 // IE They may both be end
530 if (Iter1 == Iter2)
546 // Check if both bitmaps are empty
547 if (Elements.empty() && RHS.Elements.empty())
531548 return false;
532549
533550 // See if the first bitmap element is the same in both. This is only
537554 return false;
538555
539556 while (Iter2 != RHS.Elements.end()) {
540 if (Iter1 == Elements.end() || (*Iter1)->index() > (*Iter2)->index()) {
541 SparseBitVectorElement *NewElem;
542
543 NewElem = new SparseBitVectorElement(*(*Iter2));
544 Elements.insert(Iter1, NewElem);
557 if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
558 Elements.insert(Iter1,
559 new SparseBitVectorElement(*Iter2));
545560 ++Iter2;
546561 changed = true;
547 } else if ((*Iter1)->index() == (*Iter2)->index()) {
548 changed |= (*Iter1)->unionWith(*(*Iter2));
562 } else if (Iter1->index() == Iter2->index()) {
563 changed |= Iter1->unionWith(*Iter2);
549564 ++Iter1;
550565 ++Iter2;
551566 } else {
562577 ElementListIter Iter1 = Elements.begin();
563578 ElementListConstIter Iter2 = RHS.Elements.begin();
564579
565 // IE They may both be end.
566 if (Iter1 == Iter2)
580 // Check if both bitmaps are empty.
581 if (Elements.empty() && RHS.Elements.empty())
567582 return false;
568583
569584 // See if the first bitmap element is the same in both. This is only
577592 if (Iter1 == Elements.end())
578593 return changed;
579594
580 if ((*Iter1)->index() > (*Iter2)->index()) {
581 ++Iter2;
582 } else if ((*Iter1)->index() == (*Iter2)->index()) {
595 if (Iter1->index() > Iter2->index()) {
596 ++Iter2;
597 } else if (Iter1->index() == Iter2->index()) {
583598 bool BecameZero;
584 changed |= (*Iter1)->intersectWith(*(*Iter2), BecameZero);
599 changed |= Iter1->intersectWith(*Iter2, BecameZero);
585600 if (BecameZero) {
586601 ElementListIter IterTmp = Iter1;
587 delete *IterTmp;
588602 Elements.erase(IterTmp);
589603 }
590604 ++Iter1;
592606 } else {
593607 ElementListIter IterTmp = Iter1;
594608 ++Iter1;
595 delete *IterTmp;
596609 Elements.erase(IterTmp);
597610 }
598611 }
599 for_each(Iter1, Elements.end(),
600 deleter >);
601612 Elements.erase(Iter1, Elements.end());
602613 CurrElementIter = Elements.begin();
603614 return changed;
610621 ElementListIter Iter1 = Elements.begin();
611622 ElementListConstIter Iter2 = RHS.Elements.begin();
612623
613 // IE They may both be end.
614 if (Iter1 == Iter2)
624 // Check if they are both empty
625 if (Elements.empty() && RHS.Elements.empty())
615626 return false;
616627
617628 // See if the first bitmap element is the same in both. This is only
618629 // possible if they are the same bitmap.
619630 if (Iter1 != Elements.end() && Iter2 != RHS.Elements.end())
620631 if (*Iter1 == *Iter2) {
621 for_each(Elements.begin(), Elements.end(),
622 deleter >);
623632 Elements.clear();
624633 return true;
625634 }
629638 if (Iter1 == Elements.end())
630639 return changed;
631640
632 if ((*Iter1)->index() > (*Iter2)->index()) {
633 ++Iter2;
634 } else if ((*Iter1)->index() == (*Iter2)->index()) {
641 if (Iter1->index() > Iter2->index()) {
642 ++Iter2;
643 } else if (Iter1->index() == Iter2->index()) {
635644 bool BecameZero;
636 changed |= (*Iter1)->intersectWithComplement(*(*Iter2), BecameZero);
645 changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
637646 if (BecameZero) {
638647 ElementListIter IterTmp = Iter1;
639 delete *IterTmp;
640648 Elements.erase(IterTmp);
641649 }
642650 ++Iter1;
644652 } else {
645653 ElementListIter IterTmp = Iter1;
646654 ++Iter1;
647 delete *IterTmp;
648655 Elements.erase(IterTmp);
649656 }
650657 }
662669 void intersectWithComplement(const SparseBitVector &RHS1,
663670 const SparseBitVector &RHS2)
664671 {
665 for_each(Elements.begin(), Elements.end(),
666 deleter >);
667672 Elements.clear();
668
669673 ElementListConstIter Iter1 = RHS1.Elements.begin();
670674 ElementListConstIter Iter2 = RHS2.Elements.begin();
671675
672 // IE They may both be end.
673 if (Iter1 == Iter2)
676 // Check if they are both empty.
677 if (RHS1.empty() && RHS2.empty())
674678 return;
675679
676680 // See if the first bitmap element is the same in both. This is only
685689 if (Iter1 == RHS1.Elements.end())
686690 return;
687691
688 if ((*Iter1)->index() > (*Iter2)->index()) {
689 ++Iter2;
690 } else if ((*Iter1)->index() == (*Iter2)->index()) {
692 if (Iter1->index() > Iter2->index()) {
693 ++Iter2;
694 } else if (Iter1->index() == Iter2->index()) {
691695 bool BecameZero = false;
692696 SparseBitVectorElement *NewElement =
693 new SparseBitVectorElement((*Iter1)->index());
694
695 NewElement->intersectWithComplement(*(*Iter1), *(*Iter2), BecameZero);
696 if (BecameZero) {
697 delete NewElement;
698 } else {
697 new SparseBitVectorElement(Iter1->index());
698 NewElement->intersectWithComplement(*Iter1, *Iter2, BecameZero);
699 if (!BecameZero) {
699700 Elements.push_back(NewElement);
700701 }
702 else
703 delete NewElement;
701704
702705 ++Iter1;
703706 ++Iter2;
705708 ++Iter1;
706709 }
707710 }
711
708712 // copy the remaining elements
709
710713 while (Iter1 != RHS1.Elements.end()) {
711714 SparseBitVectorElement *NewElement =
712 new SparseBitVectorElement(*(*Iter1));
715 new SparseBitVectorElement(*Iter1);
713716 Elements.push_back(NewElement);
714 }
715
717 ++Iter1;
718 }
719
716720 CurrElementIter = Elements.begin();
717721 return;
718722 }
731735 ElementListConstIter Iter1 = Elements.begin();
732736 ElementListConstIter Iter2 = RHS.Elements.begin();
733737
734 // IE They may both be end.
735 if (Iter1 == Iter2)
738 // Check if both bitmaps are empty.
739 if (Elements.empty() && RHS.Elements.empty())
736740 return false;
737741
738742 // See if the first bitmap element is the same in both. This is only
747751 if (Iter1 == Elements.end())
748752 return false;
749753
750 if ((*Iter1)->index() > (*Iter2)->index()) {
751 ++Iter2;
752 } else if ((*Iter1)->index() == (*Iter2)->index()) {
753 if ((*Iter1)->intersects(*(*Iter2)))
754 if (Iter1->index() > Iter2->index()) {
755 ++Iter2;
756 } else if (Iter1->index() == Iter2->index()) {
757 if (Iter1->intersects(*Iter2))
754758 return true;
755759 ++Iter1;
756760 ++Iter2;
765769 int find_first() const {
766770 if (Elements.empty())
767771 return -1;
768 const SparseBitVectorElement *First = *(Elements.begin());
769 return (First->index() * ElementSize) + First->find_first();
772 const SparseBitVectorElement &First = *(Elements.begin());
773 return (First.index() * ElementSize) + First.find_first();
770774 }
771775
772776 // Return true if the SparseBitVector is empty
779783 for (ElementListConstIter Iter = Elements.begin();
780784 Iter != Elements.end();
781785 ++Iter)
782 BitCount += (*Iter)->count();
786 BitCount += Iter->count();
783787
784788 return BitCount;
785789 }
789793
790794 iterator end() const {
791795 return iterator(this, ~0);
796 }
797
798 // Dump our bits to stderr
799 void dump(llvm::OStream &out) const {
800 out << "[ ";
801 for (iterator bi = begin();
802 bi != end();
803 ++bi) {
804 out << *bi << " ";
805 }
806 out << std::endl;
792807 }
793808 };
794809