llvm.org GIT mirror llvm / 0e2cf76
Several fixes: (1) Applied patch from Casey to implement iterator::operator= correctly: it should use a pointer, not a reference. (2) Added operators == and !=, and method all(). (3) Important bug fix: excess bits need to be ignored in operations like ==, count(), and all(). We do this by ensuring excess bits in the last bitset are always 0. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4837 91177308-0d34-0410-b5e6-96231b3b80d8 Vikram S. Adve 17 years ago
2 changed file(s) with 96 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
3535
3636
3737 class BitSetVector {
38 // Types used internal to the representation
3839 typedef std::bitset bitword;
3940 typedef bitword::reference reference;
4041 class iterator;
4142
43 // Data used in the representation
4244 std::vector bitsetVec;
4345 unsigned maxSize;
4446
47 private:
48 // Utility functions for the representation
4549 static unsigned NumWords(unsigned Size) { return (Size+WORDSIZE-1)/WORDSIZE;}
50 static unsigned LastWordSize(unsigned Size) { return Size % WORDSIZE; }
51
52 // Clear the unused bits in the last word.
53 // The unused bits are the high (WORDSIZE - LastWordSize()) bits
54 void ClearUnusedBits() {
55 unsigned long usedBits = (1U << LastWordSize(size())) - 1;
56 bitsetVec.back() &= bitword(usedBits);
57 }
4658
4759 const bitword& getWord(unsigned i) const { return bitsetVec[i]; }
4860 bitword& getWord(unsigned i) { return bitsetVec[i]; }
6779 /// Modifier methods: reset, set for entire set, operator[] for one element.
6880 ///
6981 void reset() {
70 for(std::vector::iterator I=bitsetVec.begin(), E=bitsetVec.end();
71 I != E; ++I)
72 I->reset();
82 for (unsigned i=0, N = bitsetVec.size(); i < N; ++i)
83 bitsetVec[i].reset();
7384 }
7485 void set() {
75 for(std::vector::iterator I=bitsetVec.begin(), E=bitsetVec.end();
76 I != E; ++I)
77 I->set();
78 }
79 std::bitset<32>::reference operator[](unsigned n) {
86 for (unsigned i=0, N = bitsetVec.size(); i < N; ++i) // skip last word
87 bitsetVec[i].set();
88 ClearUnusedBits();
89 }
90 reference operator[](unsigned n) {
91 assert(n < size() && "BitSetVector: Bit number out of range");
8092 unsigned ndiv = n / WORDSIZE, nmod = n % WORDSIZE;
81 assert(ndiv < bitsetVec.size() && "BitSetVector: Bit number out of range");
8293 return bitsetVec[ndiv][nmod];
8394 }
8495 iterator begin() { return iterator::begin(*this); }
8596 iterator end() { return iterator::end(*this); }
8697
8798 ///
99 /// Comparison operations: equal, not equal
100 ///
101 bool operator == (const BitSetVector& set2) const {
102 assert(maxSize == set2.maxSize && "Illegal == comparison");
103 for (unsigned i = 0; i < bitsetVec.size(); ++i)
104 if (getWord(i) != set2.getWord(i))
105 return false;
106 return true;
107 }
108 bool operator != (const BitSetVector& set2) const {
109 return ! (*this == set2);
110 }
111
112 ///
88113 /// Set membership operations: single element, any, none, count
89114 ///
90115 bool test(unsigned n) const {
116 assert(n < size() && "BitSetVector: Bit number out of range");
91117 unsigned ndiv = n / WORDSIZE, nmod = n % WORDSIZE;
92 assert(ndiv < bitsetVec.size() && "BitSetVector: Bit number out of range");
93118 return bitsetVec[ndiv].test(nmod);
94119 }
95120 bool any() const {
107132 n += bitsetVec[i].count();
108133 return n;
109134 }
135 bool all() const {
136 return (count() == size());
137 }
110138
111139 ///
112140 /// Set operations: intersection, union, disjoint union, complement.
136164 BitSetVector result(maxSize);
137165 for (unsigned i = 0; i < bitsetVec.size(); ++i)
138166 (result.getWord(i) = getWord(i)).flip();
167 result.ClearUnusedBits();
139168 return result;
140169 }
141170
155184 class iterator {
156185 unsigned currentBit;
157186 unsigned currentWord;
158 BitSetVector& bitvec;
159 iterator(unsigned B, unsigned W, BitSetVector _bitvec)
160 : currentBit(B), currentWord(W), bitvec(_bitvec) { }
187 BitSetVector* bitvec;
188 iterator(unsigned B, unsigned W, BitSetVector& _bitvec)
189 : currentBit(B), currentWord(W), bitvec(&_bitvec) { }
161190 public:
162191 iterator(BitSetVector& _bitvec)
163 : currentBit(0), currentWord(0), bitvec(_bitvec) { }
192 : currentBit(0), currentWord(0), bitvec(&_bitvec) { }
164193 iterator(const iterator& I)
165194 : currentBit(I.currentBit),currentWord(I.currentWord),bitvec(I.bitvec) { }
166195 iterator& operator=(const iterator& I) {
173202 // Increment and decrement operators (pre and post)
174203 iterator& operator++() {
175204 if (++currentBit == WORDSIZE)
176 { currentBit = 0; if (currentWord < bitvec.maxSize) ++currentWord; }
205 { currentBit = 0; if (currentWord < bitvec->maxSize) ++currentWord; }
177206 return *this;
178207 }
179208 iterator& operator--() {
180209 if (currentBit == 0) {
181210 currentBit = WORDSIZE-1;
182 currentWord = (currentWord == 0)? bitvec.maxSize : --currentWord;
211 currentWord = (currentWord == 0)? bitvec->maxSize : --currentWord;
183212 }
184213 else
185214 --currentBit;
190219
191220 // Dereferencing operators
192221 reference operator*() {
193 assert(currentWord < bitvec.maxSize &&
222 assert(currentWord < bitvec->maxSize &&
194223 "Dereferencing iterator past the end of a BitSetVector");
195 return bitvec.getWord(currentWord)[currentBit];
224 return bitvec->getWord(currentWord)[currentBit];
196225 }
197226
198227 // Comparison operator
199228 bool operator==(const iterator& I) {
200 return (&I.bitvec == &bitvec &&
229 return (I.bitvec == bitvec &&
201230 I.currentWord == currentWord && I.currentBit == currentBit);
202231 }
203232
3535
3636
3737 class BitSetVector {
38 // Types used internal to the representation
3839 typedef std::bitset bitword;
3940 typedef bitword::reference reference;
4041 class iterator;
4142
43 // Data used in the representation
4244 std::vector bitsetVec;
4345 unsigned maxSize;
4446
47 private:
48 // Utility functions for the representation
4549 static unsigned NumWords(unsigned Size) { return (Size+WORDSIZE-1)/WORDSIZE;}
50 static unsigned LastWordSize(unsigned Size) { return Size % WORDSIZE; }
51
52 // Clear the unused bits in the last word.
53 // The unused bits are the high (WORDSIZE - LastWordSize()) bits
54 void ClearUnusedBits() {
55 unsigned long usedBits = (1U << LastWordSize(size())) - 1;
56 bitsetVec.back() &= bitword(usedBits);
57 }
4658
4759 const bitword& getWord(unsigned i) const { return bitsetVec[i]; }
4860 bitword& getWord(unsigned i) { return bitsetVec[i]; }
6779 /// Modifier methods: reset, set for entire set, operator[] for one element.
6880 ///
6981 void reset() {
70 for(std::vector::iterator I=bitsetVec.begin(), E=bitsetVec.end();
71 I != E; ++I)
72 I->reset();
82 for (unsigned i=0, N = bitsetVec.size(); i < N; ++i)
83 bitsetVec[i].reset();
7384 }
7485 void set() {
75 for(std::vector::iterator I=bitsetVec.begin(), E=bitsetVec.end();
76 I != E; ++I)
77 I->set();
78 }
79 std::bitset<32>::reference operator[](unsigned n) {
86 for (unsigned i=0, N = bitsetVec.size(); i < N; ++i) // skip last word
87 bitsetVec[i].set();
88 ClearUnusedBits();
89 }
90 reference operator[](unsigned n) {
91 assert(n < size() && "BitSetVector: Bit number out of range");
8092 unsigned ndiv = n / WORDSIZE, nmod = n % WORDSIZE;
81 assert(ndiv < bitsetVec.size() && "BitSetVector: Bit number out of range");
8293 return bitsetVec[ndiv][nmod];
8394 }
8495 iterator begin() { return iterator::begin(*this); }
8596 iterator end() { return iterator::end(*this); }
8697
8798 ///
99 /// Comparison operations: equal, not equal
100 ///
101 bool operator == (const BitSetVector& set2) const {
102 assert(maxSize == set2.maxSize && "Illegal == comparison");
103 for (unsigned i = 0; i < bitsetVec.size(); ++i)
104 if (getWord(i) != set2.getWord(i))
105 return false;
106 return true;
107 }
108 bool operator != (const BitSetVector& set2) const {
109 return ! (*this == set2);
110 }
111
112 ///
88113 /// Set membership operations: single element, any, none, count
89114 ///
90115 bool test(unsigned n) const {
116 assert(n < size() && "BitSetVector: Bit number out of range");
91117 unsigned ndiv = n / WORDSIZE, nmod = n % WORDSIZE;
92 assert(ndiv < bitsetVec.size() && "BitSetVector: Bit number out of range");
93118 return bitsetVec[ndiv].test(nmod);
94119 }
95120 bool any() const {
107132 n += bitsetVec[i].count();
108133 return n;
109134 }
135 bool all() const {
136 return (count() == size());
137 }
110138
111139 ///
112140 /// Set operations: intersection, union, disjoint union, complement.
136164 BitSetVector result(maxSize);
137165 for (unsigned i = 0; i < bitsetVec.size(); ++i)
138166 (result.getWord(i) = getWord(i)).flip();
167 result.ClearUnusedBits();
139168 return result;
140169 }
141170
155184 class iterator {
156185 unsigned currentBit;
157186 unsigned currentWord;
158 BitSetVector& bitvec;
159 iterator(unsigned B, unsigned W, BitSetVector _bitvec)
160 : currentBit(B), currentWord(W), bitvec(_bitvec) { }
187 BitSetVector* bitvec;
188 iterator(unsigned B, unsigned W, BitSetVector& _bitvec)
189 : currentBit(B), currentWord(W), bitvec(&_bitvec) { }
161190 public:
162191 iterator(BitSetVector& _bitvec)
163 : currentBit(0), currentWord(0), bitvec(_bitvec) { }
192 : currentBit(0), currentWord(0), bitvec(&_bitvec) { }
164193 iterator(const iterator& I)
165194 : currentBit(I.currentBit),currentWord(I.currentWord),bitvec(I.bitvec) { }
166195 iterator& operator=(const iterator& I) {
173202 // Increment and decrement operators (pre and post)
174203 iterator& operator++() {
175204 if (++currentBit == WORDSIZE)
176 { currentBit = 0; if (currentWord < bitvec.maxSize) ++currentWord; }
205 { currentBit = 0; if (currentWord < bitvec->maxSize) ++currentWord; }
177206 return *this;
178207 }
179208 iterator& operator--() {
180209 if (currentBit == 0) {
181210 currentBit = WORDSIZE-1;
182 currentWord = (currentWord == 0)? bitvec.maxSize : --currentWord;
211 currentWord = (currentWord == 0)? bitvec->maxSize : --currentWord;
183212 }
184213 else
185214 --currentBit;
190219
191220 // Dereferencing operators
192221 reference operator*() {
193 assert(currentWord < bitvec.maxSize &&
222 assert(currentWord < bitvec->maxSize &&
194223 "Dereferencing iterator past the end of a BitSetVector");
195 return bitvec.getWord(currentWord)[currentBit];
224 return bitvec->getWord(currentWord)[currentBit];
196225 }
197226
198227 // Comparison operator
199228 bool operator==(const iterator& I) {
200 return (&I.bitvec == &bitvec &&
229 return (I.bitvec == bitvec &&
201230 I.currentWord == currentWord && I.currentBit == currentBit);
202231 }
203232