1 //===-- BitVectorSet.h - A bit-vector representation of sets ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is an implementation of the bit-vector representation of sets. Unlike
11 // vector<bool>, this allows much more efficient parallel set operations on
12 // bits, by using the bitset template. The bitset template unfortunately can
13 // only represent sets with a size chosen at compile-time. We therefore use a
14 // vector of bitsets. The maxmimum size of our sets (i.e., the size of the
15 // universal set) can be chosen at creation time.
17 // External functions:
19 // bool Disjoint(const BitSetVector& set1, const BitSetVector& set2):
20 // Tests if two sets have an empty intersection.
21 // This is more efficient than !(set1 & set2).any().
23 //===----------------------------------------------------------------------===//
25 #ifndef SUPPORT_BITSETVECTOR_H
26 #define SUPPORT_BITSETVECTOR_H
36 enum { BITSET_WORDSIZE = sizeof(long)*8 };
38 // Types used internal to the representation
39 typedef std::bitset<BITSET_WORDSIZE> bitword;
40 typedef bitword::reference reference;
42 // Data used in the representation
43 std::vector<bitword> bitsetVec;
47 // Utility functions for the representation
48 static unsigned NumWords(unsigned Size) {
49 return (Size+BITSET_WORDSIZE-1)/BITSET_WORDSIZE;
51 static unsigned LastWordSize(unsigned Size) { return Size % BITSET_WORDSIZE; }
53 // Clear the unused bits in the last word.
54 // The unused bits are the high (BITSET_WORDSIZE - LastWordSize()) bits
55 void ClearUnusedBits() {
56 unsigned long usedBits = (1U << LastWordSize(size())) - 1;
57 bitsetVec.back() &= bitword(usedBits);
60 const bitword& getWord(unsigned i) const { return bitsetVec[i]; }
61 bitword& getWord(unsigned i) { return bitsetVec[i]; }
63 friend bool Disjoint(const BitSetVector& set1,
64 const BitSetVector& set2);
66 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 / BITSET_WORDSIZE, nmod = n % BITSET_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 / BITSET_WORDSIZE, nmod = n % BITSET_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 == BITSET_WORDSIZE)
207 { currentBit = 0; if (currentWord < bitvec->size()) ++currentWord; }
210 iterator& operator--() {
211 if (currentBit == 0) {
212 currentBit = BITSET_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())
271 } // End llvm namespace