1 //===-- llvm/ADT/BitVectorSet.h - A bit-vector rep. 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 LLVM_ADT_BITSETVECTOR_H
26 #define LLVM_ADT_BITSETVECTOR_H
28 #include "llvm/Support/Streams.h"
35 enum { BITSET_WORDSIZE = sizeof(long)*8 };
37 // Types used internal to the representation
38 typedef std::bitset<BITSET_WORDSIZE> bitword;
39 typedef bitword::reference reference;
41 // Data used in the representation
42 std::vector<bitword> bitsetVec;
46 // Utility functions for the representation
47 static unsigned NumWords(unsigned Size) {
48 return (Size+BITSET_WORDSIZE-1)/BITSET_WORDSIZE;
50 static unsigned LastWordSize(unsigned Size) { return Size % BITSET_WORDSIZE; }
52 // Clear the unused bits in the last word.
53 // The unused bits are the high (BITSET_WORDSIZE - LastWordSize()) bits
54 void ClearUnusedBits() {
55 unsigned long usedBits = (1U << LastWordSize(size())) - 1;
56 bitsetVec.back() &= bitword(usedBits);
59 const bitword& getWord(unsigned i) const { return bitsetVec[i]; }
60 bitword& getWord(unsigned i) { return bitsetVec[i]; }
62 friend bool Disjoint(const BitSetVector& set1,
63 const BitSetVector& set2);
65 BitSetVector(); // do not implement!
70 /// Constructor: create a set of the maximum size maxSetSize.
71 /// The set is initialized to empty.
73 BitSetVector(unsigned maxSetSize)
74 : bitsetVec(NumWords(maxSetSize)), maxSize(maxSetSize) { }
76 /// size - Return the number of bits tracked by this bit vector...
77 unsigned size() const { return maxSize; }
80 /// Modifier methods: reset, set for entire set, operator[] for one element.
83 for (unsigned i=0, N = bitsetVec.size(); i < N; ++i)
87 for (unsigned i=0, N = bitsetVec.size(); i < N; ++i) // skip last word
91 reference operator[](unsigned n) {
92 assert(n < size() && "BitSetVector: Bit number out of range");
93 unsigned ndiv = n / BITSET_WORDSIZE, nmod = n % BITSET_WORDSIZE;
94 return bitsetVec[ndiv][nmod];
96 iterator begin() { return iterator::begin(*this); }
97 iterator end() { return iterator::end(*this); }
100 /// Comparison operations: equal, not equal
102 bool operator == (const BitSetVector& set2) const {
103 assert(maxSize == set2.maxSize && "Illegal == comparison");
104 for (unsigned i = 0; i < bitsetVec.size(); ++i)
105 if (getWord(i) != set2.getWord(i))
109 bool operator != (const BitSetVector& set2) const {
110 return ! (*this == set2);
114 /// Set membership operations: single element, any, none, count
116 bool test(unsigned n) const {
117 assert(n < size() && "BitSetVector: Bit number out of range");
118 unsigned ndiv = n / BITSET_WORDSIZE, nmod = n % BITSET_WORDSIZE;
119 return bitsetVec[ndiv].test(nmod);
122 for (unsigned i = 0; i < bitsetVec.size(); ++i)
123 if (bitsetVec[i].any())
130 unsigned count() const {
132 for (unsigned i = 0; i < bitsetVec.size(); ++i)
133 n += bitsetVec[i].count();
137 return (count() == size());
141 /// Set operations: intersection, union, disjoint union, complement.
143 BitSetVector operator& (const BitSetVector& set2) const {
144 assert(maxSize == set2.maxSize && "Illegal intersection");
145 BitSetVector result(maxSize);
146 for (unsigned i = 0; i < bitsetVec.size(); ++i)
147 result.getWord(i) = getWord(i) & set2.getWord(i);
150 BitSetVector operator| (const BitSetVector& set2) const {
151 assert(maxSize == set2.maxSize && "Illegal intersection");
152 BitSetVector result(maxSize);
153 for (unsigned i = 0; i < bitsetVec.size(); ++i)
154 result.getWord(i) = getWord(i) | set2.getWord(i);
157 BitSetVector operator^ (const BitSetVector& set2) const {
158 assert(maxSize == set2.maxSize && "Illegal intersection");
159 BitSetVector result(maxSize);
160 for (unsigned i = 0; i < bitsetVec.size(); ++i)
161 result.getWord(i) = getWord(i) ^ set2.getWord(i);
164 BitSetVector operator~ () const {
165 BitSetVector result(maxSize);
166 for (unsigned i = 0; i < bitsetVec.size(); ++i)
167 (result.getWord(i) = getWord(i)).flip();
168 result.ClearUnusedBits();
173 /// Printing and debugging support
175 void print(std::ostream &O) const;
176 void print(std::ostream *O) const { if (O) print(*O); }
177 void dump() const { print(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);
234 bool operator!=(const iterator& I) {
235 return !(*this == I);
239 static iterator begin(BitSetVector& _bitvec) { return iterator(_bitvec); }
240 static iterator end(BitSetVector& _bitvec) { return iterator(0,
241 _bitvec.size(), _bitvec); }
242 friend class BitSetVector;
247 inline void BitSetVector::print(std::ostream& O) const
249 for (std::vector<bitword>::const_iterator
250 I=bitsetVec.begin(), E=bitsetVec.end(); I != E; ++I)
251 O << "<" << (*I) << ">" << (I+1 == E? "\n" : ", ");
254 inline std::ostream& operator<<(std::ostream& O, const BitSetVector& bset) {
260 /// Optimized versions of fundamental comparison operations
262 inline bool Disjoint(const BitSetVector& set1,
263 const BitSetVector& set2)
265 assert(set1.size() == set2.size() && "Illegal intersection");
266 for (unsigned i = 0; i < set1.bitsetVec.size(); ++i)
267 if ((set1.getWord(i) & set2.getWord(i)).any())
272 } // End llvm namespace