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