Add isPositive for symmetry with isNegative.
[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       Capacity = 0;
82       return;
83     }
84
85     Capacity = NumBitWords(RHS.size());
86     Bits = new BitWord[Capacity];
87     std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
88   }
89
90   /// size - Returns the number of bits in this bitvector.
91   unsigned size() const { return Size; }
92
93   /// count - Returns the number of bits which are set.
94   unsigned count() const {
95     unsigned NumBits = 0;
96     for (unsigned i = 0; i < NumBitWords(size()); ++i)
97       if (sizeof(BitWord) == 4)
98         NumBits += CountPopulation_32(Bits[i]);
99       else if (sizeof(BitWord) == 8)
100         NumBits += CountPopulation_64(Bits[i]);
101       else
102         assert(0 && "Unsupported!");
103     return NumBits;
104   }
105
106   /// any - Returns true if any bit is set.
107   bool any() const {
108     for (unsigned i = 0; i < NumBitWords(size()); ++i)
109       if (Bits[i] != 0)
110         return true;
111     return false;
112   }
113
114   /// none - Returns true if none of the bits are set.
115   bool none() const {
116     return !any();
117   }
118
119   /// find_first - Returns the index of the first set bit, -1 if none
120   /// of the bits are set.
121   int find_first() const {
122     for (unsigned i = 0; i < NumBitWords(size()); ++i)
123       if (Bits[i] != 0) {
124         if (sizeof(BitWord) == 4)
125           return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
126         else if (sizeof(BitWord) == 8)
127           return i * BITS_PER_WORD + CountTrailingZeros_64(Bits[i]);
128         else
129           assert(0 && "Unsupported!");
130       }
131     return -1;
132   }
133
134   /// find_next - Returns the index of the next set bit following the
135   /// "Prev" bit. Returns -1 if the next set bit is not found.
136   int find_next(unsigned Prev) const {
137     ++Prev;
138     if (Prev >= Size)
139       return -1;
140
141     unsigned WordPos = Prev / BITS_PER_WORD;
142     unsigned BitPos = Prev % BITS_PER_WORD;
143     BitWord Copy = Bits[WordPos];
144     // Mask off previous bits.
145     Copy &= ~0L << BitPos;
146
147     if (Copy != 0) {
148       if (sizeof(BitWord) == 4)
149         return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
150       else if (sizeof(BitWord) == 8)
151         return WordPos * BITS_PER_WORD + CountTrailingZeros_64(Copy);
152       else
153         assert(0 && "Unsupported!");
154     }
155
156     // Check subsequent words.
157     for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
158       if (Bits[i] != 0) {
159         if (sizeof(BitWord) == 4)
160           return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
161         else if (sizeof(BitWord) == 8)
162           return i * BITS_PER_WORD + CountTrailingZeros_64(Bits[i]);
163         else
164           assert(0 && "Unsupported!");
165       }
166     return -1;
167   }
168
169   /// clear - Clear all bits.
170   void clear() {
171     Size = 0;
172   }
173
174   /// resize - Grow or shrink the bitvector.
175   void resize(unsigned N, bool t = false) {
176     if (N > Capacity * BITS_PER_WORD) {
177       unsigned OldCapacity = Capacity;
178       grow(N);
179       init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
180     }
181     Size = N;
182     clear_unused_bits();
183   }
184
185   void reserve(unsigned N) {
186     if (N > Capacity * BITS_PER_WORD)
187       grow(N);
188   }
189
190   // Set, reset, flip
191   BitVector &set() {
192     init_words(Bits, Capacity, true);
193     clear_unused_bits();
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     init_words(Bits, Capacity, false);
204     return *this;
205   }
206
207   BitVector &reset(unsigned Idx) {
208     Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
209     return *this;
210   }
211
212   BitVector &flip() {
213     for (unsigned i = 0; i < NumBitWords(size()); ++i)
214       Bits[i] = ~Bits[i];
215     clear_unused_bits();
216     return *this;
217   }
218
219   BitVector &flip(unsigned Idx) {
220     Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
221     return *this;
222   }
223
224   // No argument flip.
225   BitVector operator~() const {
226     return BitVector(*this).flip();
227   }
228
229   // Indexing.
230   reference operator[](unsigned Idx) {
231     return reference(*this, Idx);
232   }
233
234   bool operator[](unsigned Idx) const {
235     BitWord Mask = 1L << (Idx % BITS_PER_WORD);
236     return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
237   }
238
239   bool test(unsigned Idx) const {
240     return (*this)[Idx];
241   }
242
243   // Comparison operators.
244   bool operator==(const BitVector &RHS) const {
245     if (Size != RHS.Size)
246       return false;
247
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     unsigned ExtraBits = Size % BITS_PER_WORD;
312     if (ExtraBits) {
313       unsigned index = Size / BITS_PER_WORD;
314       Bits[index] &= ~(~0L << ExtraBits);
315     }
316   }
317
318   void grow(unsigned NewSize) {
319     unsigned OldCapacity = Capacity;
320     Capacity = NumBitWords(NewSize);
321     BitWord *NewBits = new BitWord[Capacity];
322
323     // Copy the old bits over.
324     if (OldCapacity != 0)
325       std::copy(Bits, &Bits[OldCapacity], NewBits);
326
327     // Destroy the old bits.
328     delete[] Bits;
329     Bits = NewBits;
330   }
331
332   void init_words(BitWord *B, unsigned NumWords, bool t) {
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