937296b487a6b6a9e8ce50fcaba697818e20f7dd
[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     if (t)
164       clear_unused_bits();
165   }
166
167   void reserve(unsigned N) {
168     if (N > Capacity * BITS_PER_WORD)
169       grow(N);
170   }
171
172   // Set, reset, flip
173   BitVector &set() {
174     init_words(Bits, Capacity, true);
175     clear_unused_bits();
176     return *this;
177   }
178
179   BitVector &set(unsigned Idx) {
180     Bits[Idx / BITS_PER_WORD] |= 1L << (Idx % BITS_PER_WORD);
181     return *this;
182   }
183
184   BitVector &reset() {
185     init_words(Bits, Capacity, false);
186     return *this;
187   }
188
189   BitVector &reset(unsigned Idx) {
190     Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
191     return *this;
192   }
193
194   BitVector &flip() {
195     for (unsigned i = 0; i < NumBitWords(size()); ++i)
196       Bits[i] = ~Bits[i];
197     clear_unused_bits();
198     return *this;
199   }
200
201   BitVector &flip(unsigned Idx) {
202     Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
203     return *this;
204   }
205
206   // No argument flip.
207   BitVector operator~() const {
208     return BitVector(*this).flip();
209   }
210
211   // Indexing.
212   reference operator[](unsigned Idx) {
213     return reference(*this, Idx);
214   }
215
216   bool operator[](unsigned Idx) const {
217     BitWord Mask = 1L << (Idx % BITS_PER_WORD);
218     return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
219   }
220
221   bool test(unsigned Idx) const {
222     return (*this)[Idx];
223   }
224
225   // Comparison operators.
226   bool operator==(const BitVector &RHS) const {
227     if (Size != RHS.Size)
228       return false;
229
230     for (unsigned i = 0; i < NumBitWords(size()); ++i)
231       if (Bits[i] != RHS.Bits[i])
232         return false;
233     return true;
234   }
235
236   bool operator!=(const BitVector &RHS) const {
237     return !(*this == RHS);
238   }
239
240   // Intersection, union, disjoint union.
241   BitVector operator&=(const BitVector &RHS) {
242     assert(Size == RHS.Size && "Illegal operation!");
243     for (unsigned i = 0; i < NumBitWords(size()); ++i)
244       Bits[i] &= RHS.Bits[i];
245     return *this;
246   }
247
248   BitVector operator|=(const BitVector &RHS) {
249     assert(Size == RHS.Size && "Illegal operation!");
250     for (unsigned i = 0; i < NumBitWords(size()); ++i)
251       Bits[i] |= RHS.Bits[i];
252     return *this;
253   }
254
255   BitVector operator^=(const BitVector &RHS) {
256     assert(Size == RHS.Size && "Illegal operation!");
257     for (unsigned i = 0; i < NumBitWords(size()); ++i)
258       Bits[i] ^= RHS.Bits[i];
259     return *this;
260   }
261   
262   // Assignment operator.
263   const BitVector &operator=(const BitVector &RHS) {
264     if (this == &RHS) return *this;
265
266     Size = RHS.size();
267     unsigned RHSWords = NumBitWords(Size);
268     if (Size <= Capacity * BITS_PER_WORD) {
269       std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
270       clear_unused_bits();
271       return *this;
272     }
273   
274     // Grow the bitvector to have enough elements.
275     Capacity = NumBitWords(Size);
276     BitWord *NewBits = new BitWord[Capacity];
277     std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
278
279     // Destroy the old bits.
280     delete[] Bits;
281     Bits = NewBits;
282
283     return *this;
284   }
285
286 private:
287   unsigned NumBitWords(unsigned S) const {
288     return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
289   }
290
291   // Clear the unused top bits in the high word.
292   void clear_unused_bits() {
293     if (Size) {
294       unsigned ExtraBits = Size % BITS_PER_WORD;
295       Bits[Size / BITS_PER_WORD] &= ~(~0 << ExtraBits);
296     }
297   }
298
299   void grow(unsigned NewSize) {
300     unsigned OldCapacity = Capacity;
301     Capacity = NumBitWords(NewSize);
302     BitWord *NewBits = new BitWord[Capacity];
303
304     // Copy the old bits over.
305     if (OldCapacity != 0)
306       std::copy(Bits, &Bits[OldCapacity], NewBits);
307
308     // Destroy the old bits.
309     delete[] Bits;
310     Bits = NewBits;
311   }
312
313   void init_words(BitWord *B, unsigned NumWords, bool t) {
314     memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
315   } 
316 };
317
318 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
319   BitVector Result(LHS);
320   Result &= RHS;
321   return Result;
322 }
323
324 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
325   BitVector Result(LHS);
326   Result |= RHS;
327   return Result;
328 }
329
330 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
331   BitVector Result(LHS);
332   Result ^= RHS;
333   return Result;
334 }
335  
336 } // End llvm namespace
337 #endif