1 //===-- BitVectorSet.h - A bit-vector representation of sets -----*- C++ -*--=//
3 // class BitVectorSet --
5 // An implementation of the bit-vector representation of sets.
6 // Unlike vector<bool>, this allows much more efficient parallel set
7 // operations on bits, by using the bitset template . The bitset template
8 // unfortunately can only represent sets with a size chosen at compile-time.
9 // We therefore use a vector of bitsets. The maxmimum size of our sets
10 // (i.e., the size of the universal set) can be chosen at creation time.
12 // The size of each Bitset is defined by the macro WORDSIZE.
14 // NOTE: The WORDSIZE macro should be made machine-dependent, in order to use
15 // 64-bit words or whatever gives most efficient Bitsets on each platform.
18 // External functions:
20 // bool Disjoint(const BitSetVector& set1, const BitSetVector& set2):
21 // Tests if two sets have an empty intersection.
22 // This is more efficient than !(set1 & set2).any().
24 //===----------------------------------------------------------------------===//
26 #ifndef LLVM_SUPPORT_BITVECTORSET_H
27 #define LLVM_SUPPORT_BITVECTORSET_H
36 #define WORDSIZE (32U)
40 // Types used internal to the representation
41 typedef std::bitset<WORDSIZE> bitword;
42 typedef bitword::reference reference;
45 // Data used in the representation
46 std::vector<bitword> bitsetVec;
50 // Utility functions for the representation
51 static unsigned NumWords(unsigned Size) { return (Size+WORDSIZE-1)/WORDSIZE;}
52 static unsigned LastWordSize(unsigned Size) { return Size % WORDSIZE; }
54 // Clear the unused bits in the last word.
55 // The unused bits are the high (WORDSIZE - LastWordSize()) bits
56 void ClearUnusedBits() {
57 unsigned long usedBits = (1U << LastWordSize(size())) - 1;
58 bitsetVec.back() &= bitword(usedBits);
61 const bitword& getWord(unsigned i) const { return bitsetVec[i]; }
62 bitword& getWord(unsigned i) { return bitsetVec[i]; }
64 friend bool Disjoint(const BitSetVector& set1,
65 const BitSetVector& set2);
67 BitSetVector(); // do not implement!
71 /// Constructor: create a set of the maximum size maxSetSize.
72 /// The set is initialized to empty.
74 BitSetVector(unsigned maxSetSize)
75 : bitsetVec(NumWords(maxSetSize)), maxSize(maxSetSize) { }
77 /// size - Return the number of bits tracked by this bit vector...
78 unsigned size() const { return maxSize; }
81 /// Modifier methods: reset, set for entire set, operator[] for one element.
84 for (unsigned i=0, N = bitsetVec.size(); i < N; ++i)
88 for (unsigned i=0, N = bitsetVec.size(); i < N; ++i) // skip last word
92 reference operator[](unsigned n) {
93 assert(n < size() && "BitSetVector: Bit number out of range");
94 unsigned ndiv = n / WORDSIZE, nmod = n % WORDSIZE;
95 return bitsetVec[ndiv][nmod];
97 iterator begin() { return iterator::begin(*this); }
98 iterator end() { return iterator::end(*this); }
101 /// Comparison operations: equal, not equal
103 bool operator == (const BitSetVector& set2) const {
104 assert(maxSize == set2.maxSize && "Illegal == comparison");
105 for (unsigned i = 0; i < bitsetVec.size(); ++i)
106 if (getWord(i) != set2.getWord(i))
110 bool operator != (const BitSetVector& set2) const {
111 return ! (*this == set2);
115 /// Set membership operations: single element, any, none, count
117 bool test(unsigned n) const {
118 assert(n < size() && "BitSetVector: Bit number out of range");
119 unsigned ndiv = n / WORDSIZE, nmod = n % WORDSIZE;
120 return bitsetVec[ndiv].test(nmod);
123 for (unsigned i = 0; i < bitsetVec.size(); ++i)
124 if (bitsetVec[i].any())
131 unsigned count() const {
133 for (unsigned i = 0; i < bitsetVec.size(); ++i)
134 n += bitsetVec[i].count();
138 return (count() == size());
142 /// Set operations: intersection, union, disjoint union, complement.
144 BitSetVector operator& (const BitSetVector& set2) const {
145 assert(maxSize == set2.maxSize && "Illegal intersection");
146 BitSetVector result(maxSize);
147 for (unsigned i = 0; i < bitsetVec.size(); ++i)
148 result.getWord(i) = getWord(i) & set2.getWord(i);
151 BitSetVector operator| (const BitSetVector& set2) const {
152 assert(maxSize == set2.maxSize && "Illegal intersection");
153 BitSetVector result(maxSize);
154 for (unsigned i = 0; i < bitsetVec.size(); ++i)
155 result.getWord(i) = getWord(i) | set2.getWord(i);
158 BitSetVector operator^ (const BitSetVector& set2) const {
159 assert(maxSize == set2.maxSize && "Illegal intersection");
160 BitSetVector result(maxSize);
161 for (unsigned i = 0; i < bitsetVec.size(); ++i)
162 result.getWord(i) = getWord(i) ^ set2.getWord(i);
165 BitSetVector operator~ () const {
166 BitSetVector result(maxSize);
167 for (unsigned i = 0; i < bitsetVec.size(); ++i)
168 (result.getWord(i) = getWord(i)).flip();
169 result.ClearUnusedBits();
174 /// Printing and debugging support
176 void print(std::ostream &O) const;
177 void dump() const { print(std::cerr); }
181 // An iterator to enumerate the bits in a BitSetVector.
182 // Eventually, this needs to inherit from bidirectional_iterator.
183 // But this iterator may not be as useful as I once thought and
188 unsigned currentWord;
189 BitSetVector* bitvec;
190 iterator(unsigned B, unsigned W, BitSetVector& _bitvec)
191 : currentBit(B), currentWord(W), bitvec(&_bitvec) { }
193 iterator(BitSetVector& _bitvec)
194 : currentBit(0), currentWord(0), bitvec(&_bitvec) { }
195 iterator(const iterator& I)
196 : currentBit(I.currentBit),currentWord(I.currentWord),bitvec(I.bitvec) { }
197 iterator& operator=(const iterator& I) {
198 currentWord = I.currentWord;
199 currentBit = I.currentBit;
204 // Increment and decrement operators (pre and post)
205 iterator& operator++() {
206 if (++currentBit == WORDSIZE)
207 { currentBit = 0; if (currentWord < bitvec->size()) ++currentWord; }
210 iterator& operator--() {
211 if (currentBit == 0) {
212 currentBit = WORDSIZE-1;
213 currentWord = (currentWord == 0)? bitvec->size() : --currentWord;
219 iterator operator++(int) { iterator copy(*this); ++*this; return copy; }
220 iterator operator--(int) { iterator copy(*this); --*this; return copy; }
222 // Dereferencing operators
223 reference operator*() {
224 assert(currentWord < bitvec->size() &&
225 "Dereferencing iterator past the end of a BitSetVector");
226 return bitvec->getWord(currentWord)[currentBit];
229 // Comparison operator
230 bool operator==(const iterator& I) {
231 return (I.bitvec == bitvec &&
232 I.currentWord == currentWord && I.currentBit == currentBit);
236 static iterator begin(BitSetVector& _bitvec) { return iterator(_bitvec); }
237 static iterator end(BitSetVector& _bitvec) { return iterator(0,
238 _bitvec.size(), _bitvec); }
239 friend class BitSetVector;
244 inline void BitSetVector::print(std::ostream& O) const
246 for (std::vector<bitword>::const_iterator
247 I=bitsetVec.begin(), E=bitsetVec.end(); I != E; ++I)
248 O << "<" << (*I) << ">" << (I+1 == E? "\n" : ", ");
251 inline std::ostream& operator<< (std::ostream& O, const BitSetVector& bset)
259 /// Optimized versions of fundamental comparison operations
261 inline bool Disjoint(const BitSetVector& set1,
262 const BitSetVector& set2)
264 assert(set1.size() == set2.size() && "Illegal intersection");
265 for (unsigned i = 0; i < set1.bitsetVec.size(); ++i)
266 if ((set1.getWord(i) & set2.getWord(i)).any())