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