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