BitVector::count() bugs.
[oota-llvm.git] / include / llvm / ADT / BitVector.h
1 //===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Evan Cheng and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the BitVector class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_BITVECTOR_H
15 #define LLVM_ADT_BITVECTOR_H
16
17 #include "llvm/Support/MathExtras.h"
18
19 namespace llvm {
20
21 class BitVector {
22   typedef unsigned long BitWord;
23
24   enum { BITS_PER_WORD = sizeof(BitWord) * 8 };
25
26   BitWord  *Bits;        // Actual bits. 
27   unsigned Size;         // Size of bitvector in bits.
28   unsigned Capacity;     // Size of allocated memory in BitWord.
29
30 public:
31   // Encapsulation of a single bit.
32   class reference {
33     friend class BitVector;
34
35     BitWord *WordRef;
36     unsigned BitPos;
37
38     reference();  // Undefined
39
40   public:
41     reference(BitVector &b, unsigned Idx) {
42       WordRef = &b.Bits[Idx / BITS_PER_WORD];
43       BitPos = Idx % BITS_PER_WORD;
44     }
45
46     ~reference() {}
47
48     reference& operator=(bool t) {
49       if (t)
50         *WordRef |= 1L << BitPos;
51       else
52         *WordRef &= ~(1L << BitPos);
53       return *this;
54     }
55
56     reference& operator=(const reference& rhs) {
57       if (*rhs.WordRef & (1 << rhs.BitPos))
58         *WordRef |= 1L << BitPos;
59       else
60         *WordRef &= ~(1L << BitPos);
61       return *this;
62     }
63
64     operator bool() const {
65       return (*WordRef) & (1L << BitPos);
66     }
67   };
68
69
70   /// BitVector default ctor - Creates an empty bitvector.
71   BitVector() : Size(0), Capacity(0) {
72     Bits = NULL;
73   }
74
75   /// BitVector ctor - Creates a bitvector of specified number of bits. All
76   /// bits are initialized to the specified value.
77   explicit BitVector(unsigned s, bool t = false) : Size(s) {
78     Capacity = NumBitWords(s);
79     Bits = new BitWord[Capacity];
80     init_words(Bits, Capacity, t);
81     if (t)
82       clear_unused_bits();
83   }
84
85   /// BitVector copy ctor.
86   BitVector(const BitVector &RHS) : Size(RHS.size()) {
87     if (Size == 0) {
88       Bits = NULL;
89       return;
90     }
91
92     Capacity = NumBitWords(RHS.size());
93     Bits = new BitWord[Capacity];
94     std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
95   }
96
97   /// size - Returns the number of bits in this bitvector.
98   unsigned size() const { return Size; }
99
100   /// count - Returns the number of bits which are set.
101   unsigned count() const {
102     unsigned NumBits = 0;
103     for (unsigned i = 0; i < NumBitWords(size()); ++i)
104       if (sizeof(BitWord) == 4)
105         NumBits += CountPopulation_32(Bits[i]);
106       else if (sizeof(BitWord) == 8)
107         NumBits += CountPopulation_64(Bits[i]);
108       else
109         assert(0 && "Unsupported!")
110     return NumBits;
111   }
112
113   /// any - Returns true if any bit is set.
114   bool any() const {
115     for (unsigned i = 0; i < NumBitWords(size()); ++i)
116       if (Bits[i] != 0)
117         return true;
118     return false;
119   }
120
121   /// none - Returns true if none of the bits are set.
122   bool none() const {
123     return !any();
124   }
125
126   /// find_first - Returns the index of the first set bit, -1 if none
127   /// of the bits are set.
128   int find_first() const {
129     for (unsigned i = 0; i < NumBitWords(size()); ++i)
130       if (Bits[i] != 0)
131         return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
132     return -1;
133   }
134
135   /// find_next - Returns the index of the next set bit following the
136   /// "Prev" bit. Returns -1 if the next set bit is not found.
137   int find_next(unsigned Prev) const {
138     ++Prev;
139     if (Prev >= Size)
140       return -1;
141
142     unsigned WordPos = Prev / BITS_PER_WORD;
143     unsigned BitPos = Prev % BITS_PER_WORD;
144     BitWord Copy = Bits[WordPos];
145     // Mask off previous bits.
146     Copy &= ~0 << BitPos;
147
148     if (Copy != 0)
149       return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
150
151     // Check subsequent words.
152     for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
153       if (Bits[i] != 0)
154         return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
155     return -1;
156   }
157
158   /// clear - Clear all bits.
159   void clear() {
160     if (Capacity > 0) {
161       delete[] Bits;
162       Bits = NULL;
163       Size = Capacity = 0;
164     }
165   }
166
167   /// resize - Grow or shrink the bitvector.
168   void resize(unsigned N) {
169     if (N > Capacity * BITS_PER_WORD) {
170       unsigned OldCapacity = Capacity;
171       grow(N);
172       init_words(&Bits[OldCapacity], (Capacity-OldCapacity), false);
173     }
174     Size = N;
175   }
176
177   void resize(unsigned N, bool t) {
178     if (N > Capacity * BITS_PER_WORD) {
179       unsigned OldCapacity = Capacity;
180       grow(N);
181       init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
182     }
183     Size = N;
184     clear_unused_bits();
185   }
186
187   void reserve(unsigned N) {
188     if (N > Capacity * BITS_PER_WORD)
189       grow(N);
190   }
191
192   // Set, reset, flip
193   BitVector &set() {
194     if (Bits) {
195       init_words(Bits, Capacity, true);
196       clear_unused_bits();
197     }
198     return *this;
199   }
200
201   BitVector &set(unsigned Idx) {
202     Bits[Idx / BITS_PER_WORD] |= 1L << (Idx % BITS_PER_WORD);
203     return *this;
204   }
205
206   BitVector &reset() {
207     if (Bits)
208       init_words(Bits, Capacity, false);
209     return *this;
210   }
211
212   BitVector &reset(unsigned Idx) {
213     Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
214     return *this;
215   }
216
217   BitVector &flip() {
218     for (unsigned i = 0; i < NumBitWords(size()); ++i)
219       Bits[i] = ~Bits[i];
220     clear_unused_bits();
221     return *this;
222   }
223
224   BitVector &flip(unsigned Idx) {
225     Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
226     return *this;
227   }
228
229   // No argument flip.
230   BitVector operator~() const {
231     return BitVector(*this).flip();
232   }
233
234   // Indexing.
235   reference operator[](unsigned Idx) {
236     return reference(*this, Idx);
237   }
238
239   bool operator[](unsigned Idx) const {
240     BitWord Mask = 1L << (Idx % BITS_PER_WORD);
241     return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
242   }
243
244   bool test(unsigned Idx) const {
245     return (*this)[Idx];
246   }
247
248   // Comparison operators.
249   bool operator==(const BitVector &RHS) const {
250     assert(Size == RHS.Size && "Illegal operation!");
251     for (unsigned i = 0; i < NumBitWords(size()); ++i)
252       if (Bits[i] != RHS.Bits[i])
253         return false;
254     return true;
255   }
256
257   bool operator!=(const BitVector &RHS) const {
258     return !(*this == RHS);
259   }
260
261   // Intersection, union, disjoint union.
262   BitVector operator&=(const BitVector &RHS) {
263     assert(Size == RHS.Size && "Illegal operation!");
264     for (unsigned i = 0; i < NumBitWords(size()); ++i)
265       Bits[i] &= RHS.Bits[i];
266     return *this;
267   }
268
269   BitVector operator|=(const BitVector &RHS) {
270     assert(Size == RHS.Size && "Illegal operation!");
271     for (unsigned i = 0; i < NumBitWords(size()); ++i)
272       Bits[i] |= RHS.Bits[i];
273     return *this;
274   }
275
276   BitVector operator^=(const BitVector &RHS) {
277     assert(Size == RHS.Size && "Illegal operation!");
278     for (unsigned i = 0; i < NumBitWords(size()); ++i)
279       Bits[i] ^= RHS.Bits[i];
280     return *this;
281   }
282   
283   // Assignment operator.
284   const BitVector &operator=(const BitVector &RHS) {
285     if (this == &RHS) return *this;
286
287     Size = RHS.size();
288     unsigned RHSWords = NumBitWords(Size);
289     if (Size <= Capacity * BITS_PER_WORD) {
290       std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
291       clear_unused_bits();
292       return *this;
293     }
294   
295     // Grow the bitvector to have enough elements.
296     Capacity = NumBitWords(Size);
297     BitWord *NewBits = new BitWord[Capacity];
298     std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
299
300     // Destroy the old bits.
301     delete[] Bits;
302     Bits = NewBits;
303
304     return *this;
305   }
306
307 private:
308   unsigned NumBitWords(unsigned S) const {
309     return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
310   }
311
312   // Clear the unused top bits in the high word.
313   void clear_unused_bits() {
314     if (Size) {
315       unsigned ExtraBits = Size % BITS_PER_WORD;
316       Bits[Size / BITS_PER_WORD] &= ~(~0 << ExtraBits);
317     }
318   }
319
320   void grow(unsigned NewSize) {
321     unsigned OldCapacity = Capacity;
322     Capacity = NumBitWords(NewSize);
323     BitWord *NewBits = new BitWord[Capacity];
324
325     // Copy the old bits over.
326     if (OldCapacity != 0)
327       std::copy(Bits, &Bits[OldCapacity], NewBits);
328
329     // Destroy the old bits.
330     if (Bits)
331       delete[] Bits;
332     Bits = NewBits;
333   }
334
335   void init_words(BitWord *B, unsigned NumWords, bool t) {
336     if (B)
337       memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
338   } 
339 };
340
341 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
342   BitVector Result(LHS);
343   Result &= RHS;
344   return Result;
345 }
346
347 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
348   BitVector Result(LHS);
349   Result |= RHS;
350   return Result;
351 }
352
353 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
354   BitVector Result(LHS);
355   Result ^= RHS;
356   return Result;
357 }
358  
359 } // End llvm namespace
360 #endif