X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FADT%2FSparseBitVector.h;h=6230135131a7ccb9a0eabc07475aa64109b46e25;hb=104cf9e02b0ed94d4173869a598af6c6972a8660;hp=56db805647e6c7d7eb52df041ade618046dff640;hpb=9d03724c8c348dd31cc5b692b08b23c67fd4b0a0;p=oota-llvm.git diff --git a/include/llvm/ADT/SparseBitVector.h b/include/llvm/ADT/SparseBitVector.h index 56db805647e..6230135131a 100644 --- a/include/llvm/ADT/SparseBitVector.h +++ b/include/llvm/ADT/SparseBitVector.h @@ -2,8 +2,8 @@ // // The LLVM Compiler Infrastructure // -// This file was developed by Daniel Berlin and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -16,12 +16,13 @@ #define LLVM_ADT_SPARSEBITVECTOR_H #include +#include #include -#include #include "llvm/Support/DataTypes.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/MathExtras.h" -#include "llvm/ADT/ilist" +#include "llvm/ADT/ilist.h" + namespace llvm { /// SparseBitVector is an implementation of a bitvector that is sparse by only @@ -39,57 +40,33 @@ namespace llvm { template -struct SparseBitVectorElement { +struct SparseBitVectorElement + : ilist_node > { public: typedef unsigned long BitWord; enum { - BITWORD_SIZE = sizeof(BitWord) * 8, + BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT, BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE, BITS_PER_ELEMENT = ElementSize }; - SparseBitVectorElement *getNext() const { - return Next; - } - SparseBitVectorElement *getPrev() const { - return Prev; - } - - void setNext(SparseBitVectorElement *RHS) { - Next = RHS; - } - void setPrev(SparseBitVectorElement *RHS) { - Prev = RHS; - } - private: - SparseBitVectorElement *Next; - SparseBitVectorElement *Prev; // Index of Element in terms of where first bit starts. unsigned ElementIndex; BitWord Bits[BITWORDS_PER_ELEMENT]; // Needed for sentinels + friend struct ilist_sentinel_traits; SparseBitVectorElement() { - ElementIndex = ~0UL; + ElementIndex = ~0U; memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT); } - friend struct ilist_traits >; public: explicit SparseBitVectorElement(unsigned Idx) { ElementIndex = Idx; memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT); } - ~SparseBitVectorElement() { - } - - // Copy ctor. - SparseBitVectorElement(const SparseBitVectorElement &RHS) { - ElementIndex = RHS.ElementIndex; - std::copy(&RHS.Bits[0], &RHS.Bits[BITWORDS_PER_ELEMENT], Bits); - } - // Comparison. bool operator==(const SparseBitVectorElement &RHS) const { if (ElementIndex != RHS.ElementIndex) @@ -166,17 +143,17 @@ public: assert(0 && "Unsupported!"); } assert(0 && "Illegal empty element"); + return 0; // Not reached } - /// find_next - Returns the index of the next set bit following the - /// "Prev" bit. Returns -1 if the next set bit is not found. - int find_next(unsigned Prev) const { - ++Prev; - if (Prev >= BITS_PER_ELEMENT) + /// find_next - Returns the index of the next set bit starting from the + /// "Curr" bit. Returns -1 if the next set bit is not found. + int find_next(unsigned Curr) const { + if (Curr >= BITS_PER_ELEMENT) return -1; - unsigned WordPos = Prev / BITWORD_SIZE; - unsigned BitPos = Prev % BITWORD_SIZE; + unsigned WordPos = Curr / BITWORD_SIZE; + unsigned BitPos = Curr % BITWORD_SIZE; BitWord Copy = Bits[WordPos]; assert (WordPos <= BITWORDS_PER_ELEMENT && "Word Position outside of element"); @@ -390,7 +367,7 @@ class SparseBitVector { // See if we ran out of Bits in this word. if (!Bits) { - int NextSetBitNumber = Iter->find_next((BitNumber - 1) % ElementSize) ; + int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ; // If we ran out of set bits in this element, move to next element. if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) { ++Iter; @@ -484,6 +461,26 @@ public: CurrElementIter = Elements.begin (); } + // Clear. + void clear() { + Elements.clear(); + } + + // Assignment + SparseBitVector& operator=(const SparseBitVector& RHS) { + Elements.clear(); + + ElementListConstIter ElementIter = RHS.Elements.begin(); + while (ElementIter != RHS.Elements.end()) { + Elements.push_back(SparseBitVectorElement(*ElementIter)); + ++ElementIter; + } + + CurrElementIter = Elements.begin (); + + return *this; + } + // Test, Reset, and Set a bit in the bitmap. bool test(unsigned Idx) { if (Elements.empty()) @@ -546,7 +543,7 @@ public: } } CurrElementIter = ElementIter; - + ElementIter->set(Idx % ElementSize); } @@ -581,8 +578,8 @@ public: ElementListIter Iter1 = Elements.begin(); ElementListConstIter Iter2 = RHS.Elements.begin(); - // Check if both bitmaps are empty - if (Elements.empty() && RHS.Elements.empty()) + // If RHS is empty, we are done + if (RHS.Elements.empty()) return false; while (Iter2 != RHS.Elements.end()) { @@ -615,8 +612,10 @@ public: // Loop through, intersecting as we go, erasing elements when necessary. while (Iter2 != RHS.Elements.end()) { - if (Iter1 == Elements.end()) + if (Iter1 == Elements.end()) { + CurrElementIter = Elements.begin(); return changed; + } if (Iter1->index() > Iter2->index()) { ++Iter2; @@ -642,21 +641,23 @@ public: return changed; } - // Intersect our bitmap with the complement of the RHS and return true if ours - // changed. + // Intersect our bitmap with the complement of the RHS and return true + // if ours changed. bool intersectWithComplement(const SparseBitVector &RHS) { bool changed = false; ElementListIter Iter1 = Elements.begin(); ElementListConstIter Iter2 = RHS.Elements.begin(); - // Check if they are both empty - if (Elements.empty() && RHS.Elements.empty()) + // If either our bitmap or RHS is empty, we are done + if (Elements.empty() || RHS.Elements.empty()) return false; // Loop through, intersecting as we go, erasing elements when necessary. while (Iter2 != RHS.Elements.end()) { - if (Iter1 == Elements.end()) + if (Iter1 == Elements.end()) { + CurrElementIter = Elements.begin(); return changed; + } if (Iter1->index() > Iter2->index()) { ++Iter2; @@ -672,9 +673,7 @@ public: } ++Iter2; } else { - ElementListIter IterTmp = Iter1; ++Iter1; - Elements.erase(IterTmp); } } CurrElementIter = Elements.begin(); @@ -686,17 +685,19 @@ public: } - // Three argument version of intersectWithComplement. Result of RHS1 & ~RHS2 - // is stored into this bitmap. + // Three argument version of intersectWithComplement. + // Result of RHS1 & ~RHS2 is stored into this bitmap. void intersectWithComplement(const SparseBitVector &RHS1, const SparseBitVector &RHS2) { Elements.clear(); + CurrElementIter = Elements.begin(); ElementListConstIter Iter1 = RHS1.Elements.begin(); ElementListConstIter Iter2 = RHS2.Elements.begin(); - // Check if they are both empty. - if (RHS1.empty() && RHS2.empty()) + // If RHS1 is empty, we are done + // If RHS2 is empty, we still have to copy RHS1 + if (RHS1.Elements.empty()) return; // Loop through, intersecting as we go, erasing elements when necessary. @@ -719,6 +720,9 @@ public: ++Iter1; ++Iter2; } else { + SparseBitVectorElement *NewElement = + new SparseBitVectorElement(*Iter1); + Elements.push_back(NewElement); ++Iter1; } } @@ -731,7 +735,6 @@ public: ++Iter1; } - CurrElementIter = Elements.begin(); return; } @@ -772,6 +775,14 @@ public: return false; } + // Return true iff all bits set in this SparseBitVector are + // also set in RHS. + bool contains(const SparseBitVector &RHS) const { + SparseBitVector Result(*this); + Result &= RHS; + return (Result == RHS); + } + // Return the first set bit in the bitmap. Return -1 if no bits are set. int find_first() const { if (Elements.empty()) @@ -799,7 +810,7 @@ public: } iterator end() const { - return iterator(this, ~0); + return iterator(this, true); } // Get a hash value for this bitmap. @@ -839,10 +850,41 @@ inline bool operator &=(SparseBitVector *LHS, template inline bool operator &=(SparseBitVector &LHS, const SparseBitVector *RHS) { - return LHS &= (*RHS); + return LHS &= *RHS; +} + +// Convenience functions for infix union, intersection, difference operators. + +template +inline SparseBitVector +operator|(const SparseBitVector &LHS, + const SparseBitVector &RHS) { + SparseBitVector Result(LHS); + Result |= RHS; + return Result; +} + +template +inline SparseBitVector +operator&(const SparseBitVector &LHS, + const SparseBitVector &RHS) { + SparseBitVector Result(LHS); + Result &= RHS; + return Result; +} + +template +inline SparseBitVector +operator-(const SparseBitVector &LHS, + const SparseBitVector &RHS) { + SparseBitVector Result; + Result.intersectWithComplement(LHS, RHS); + return Result; } + + // Dump a SparseBitVector to a stream template void dump(const SparseBitVector &LHS, llvm::OStream &out) { @@ -852,10 +894,8 @@ void dump(const SparseBitVector &LHS, llvm::OStream &out) { for (bi = LHS.begin(); bi != LHS.end(); ++bi) { out << *bi << " "; } - out << " ]\n"; + out << " ]\n"; } -} - - +} // end namespace llvm #endif