Missing a ;
[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     operator bool() const {
57       return (*WordRef) & (1L << BitPos);
58     }
59   };
60
61
62   /// BitVector default ctor - Creates an empty bitvector.
63   BitVector() : Size(0), Capacity(0) {
64     Bits = NULL;
65   }
66
67   /// BitVector ctor - Creates a bitvector of specified number of bits. All
68   /// bits are initialized to the specified value.
69   explicit BitVector(unsigned s, bool t = false) : Size(s) {
70     Capacity = NumBitWords(s);
71     Bits = new BitWord[Capacity];
72     init_words(Bits, Capacity, t);
73     if (t)
74       clear_unused_bits();
75   }
76
77   /// BitVector copy ctor.
78   BitVector(const BitVector &RHS) : Size(RHS.size()) {
79     if (Size == 0) {
80       Bits = NULL;
81       return;
82     }
83
84     Capacity = NumBitWords(RHS.size());
85     Bits = new BitWord[Capacity];
86     std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
87   }
88
89   /// size - Returns the number of bits in this bitvector.
90   unsigned size() const { return Size; }
91
92   /// count - Returns the number of bits which are set.
93   unsigned count() const {
94     unsigned NumBits = 0;
95     for (unsigned i = 0; i < NumBitWords(size()); ++i)
96       if (sizeof(BitWord) == 4)
97         NumBits += CountPopulation_32(Bits[i]);
98       else if (sizeof(BitWord) == 8)
99         NumBits += CountPopulation_64(Bits[i]);
100       else
101         assert(0 && "Unsupported!");
102     return NumBits;
103   }
104
105   /// any - Returns true if any bit is set.
106   bool any() const {
107     for (unsigned i = 0; i < NumBitWords(size()); ++i)
108       if (Bits[i] != 0)
109         return true;
110     return false;
111   }
112
113   /// none - Returns true if none of the bits are set.
114   bool none() const {
115     return !any();
116   }
117
118   /// find_first - Returns the index of the first set bit, -1 if none
119   /// of the bits are set.
120   int find_first() const {
121     for (unsigned i = 0; i < NumBitWords(size()); ++i)
122       if (Bits[i] != 0)
123         return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
124     return -1;
125   }
126
127   /// find_next - Returns the index of the next set bit following the
128   /// "Prev" bit. Returns -1 if the next set bit is not found.
129   int find_next(unsigned Prev) const {
130     ++Prev;
131     if (Prev >= Size)
132       return -1;
133
134     unsigned WordPos = Prev / BITS_PER_WORD;
135     unsigned BitPos = Prev % BITS_PER_WORD;
136     BitWord Copy = Bits[WordPos];
137     // Mask off previous bits.
138     Copy &= ~0 << BitPos;
139
140     if (Copy != 0)
141       return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
142
143     // Check subsequent words.
144     for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
145       if (Bits[i] != 0)
146         return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
147     return -1;
148   }
149
150   /// clear - Clear all bits.
151   void clear() {
152     Size = 0;
153   }
154
155   /// resize - Grow or shrink the bitvector.
156   void resize(unsigned N, bool t = false) {
157     if (N > Capacity * BITS_PER_WORD) {
158       unsigned OldCapacity = Capacity;
159       grow(N);
160       init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
161     }
162     Size = N;
163     clear_unused_bits();
164   }
165
166   void reserve(unsigned N) {
167     if (N > Capacity * BITS_PER_WORD)
168       grow(N);
169   }
170
171   // Set, reset, flip
172   BitVector &set() {
173     init_words(Bits, Capacity, true);
174     clear_unused_bits();
175     return *this;
176   }
177
178   BitVector &set(unsigned Idx) {
179     Bits[Idx / BITS_PER_WORD] |= 1L << (Idx % BITS_PER_WORD);
180     return *this;
181   }
182
183   BitVector &reset() {
184     init_words(Bits, Capacity, false);
185     return *this;
186   }
187
188   BitVector &reset(unsigned Idx) {
189     Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
190     return *this;
191   }
192
193   BitVector &flip() {
194     for (unsigned i = 0; i < NumBitWords(size()); ++i)
195       Bits[i] = ~Bits[i];
196     clear_unused_bits();
197     return *this;
198   }
199
200   BitVector &flip(unsigned Idx) {
201     Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
202     return *this;
203   }
204
205   // No argument flip.
206   BitVector operator~() const {
207     return BitVector(*this).flip();
208   }
209
210   // Indexing.
211   reference operator[](unsigned Idx) {
212     return reference(*this, Idx);
213   }
214
215   bool operator[](unsigned Idx) const {
216     BitWord Mask = 1L << (Idx % BITS_PER_WORD);
217     return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
218   }
219
220   bool test(unsigned Idx) const {
221     return (*this)[Idx];
222   }
223
224   // Comparison operators.
225   bool operator==(const BitVector &RHS) const {
226     if (Size != RHS.Size)
227       return false;
228
229     for (unsigned i = 0; i < NumBitWords(size()); ++i)
230       if (Bits[i] != RHS.Bits[i])
231         return false;
232     return true;
233   }
234
235   bool operator!=(const BitVector &RHS) const {
236     return !(*this == RHS);
237   }
238
239   // Intersection, union, disjoint union.
240   BitVector operator&=(const BitVector &RHS) {
241     assert(Size == RHS.Size && "Illegal operation!");
242     for (unsigned i = 0; i < NumBitWords(size()); ++i)
243       Bits[i] &= RHS.Bits[i];
244     return *this;
245   }
246
247   BitVector operator|=(const BitVector &RHS) {
248     assert(Size == RHS.Size && "Illegal operation!");
249     for (unsigned i = 0; i < NumBitWords(size()); ++i)
250       Bits[i] |= RHS.Bits[i];
251     return *this;
252   }
253
254   BitVector operator^=(const BitVector &RHS) {
255     assert(Size == RHS.Size && "Illegal operation!");
256     for (unsigned i = 0; i < NumBitWords(size()); ++i)
257       Bits[i] ^= RHS.Bits[i];
258     return *this;
259   }
260   
261   // Assignment operator.
262   const BitVector &operator=(const BitVector &RHS) {
263     if (this == &RHS) return *this;
264
265     Size = RHS.size();
266     unsigned RHSWords = NumBitWords(Size);
267     if (Size <= Capacity * BITS_PER_WORD) {
268       std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
269       clear_unused_bits();
270       return *this;
271     }
272   
273     // Grow the bitvector to have enough elements.
274     Capacity = NumBitWords(Size);
275     BitWord *NewBits = new BitWord[Capacity];
276     std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
277
278     // Destroy the old bits.
279     delete[] Bits;
280     Bits = NewBits;
281
282     return *this;
283   }
284
285 private:
286   unsigned NumBitWords(unsigned S) const {
287     return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
288   }
289
290   // Clear the unused top bits in the high word.
291   void clear_unused_bits() {
292     if (Size) {
293       unsigned ExtraBits = Size % BITS_PER_WORD;
294       Bits[Size / BITS_PER_WORD] &= ~(~0 << ExtraBits);
295     }
296   }
297
298   void grow(unsigned NewSize) {
299     unsigned OldCapacity = Capacity;
300     Capacity = NumBitWords(NewSize);
301     BitWord *NewBits = new BitWord[Capacity];
302
303     // Copy the old bits over.
304     if (OldCapacity != 0)
305       std::copy(Bits, &Bits[OldCapacity], NewBits);
306
307     // Destroy the old bits.
308     delete[] Bits;
309     Bits = NewBits;
310   }
311
312   void init_words(BitWord *B, unsigned NumWords, bool t) {
313     memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
314   } 
315 };
316
317 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
318   BitVector Result(LHS);
319   Result &= RHS;
320   return Result;
321 }
322
323 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
324   BitVector Result(LHS);
325   Result |= RHS;
326   return Result;
327 }
328
329 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
330   BitVector Result(LHS);
331   Result ^= RHS;
332   return Result;
333 }
334  
335 } // End llvm namespace
336 #endif