000cdd3d67e34656e9ceec0c8c878b5177018471
[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 #include <algorithm>
19 #include <cstdlib>
20 #include <cassert>
21
22 namespace llvm {
23
24 class BitVector {
25   typedef unsigned long BitWord;
26
27   enum { BITWORD_SIZE = sizeof(BitWord) * 8 };
28
29   BitWord  *Bits;        // Actual bits. 
30   unsigned Size;         // Size of bitvector in bits.
31   unsigned Capacity;     // Size of allocated memory in BitWord.
32
33 public:
34   // Encapsulation of a single bit.
35   class reference {
36     friend class BitVector;
37
38     BitWord *WordRef;
39     unsigned BitPos;
40
41     reference();  // Undefined
42
43   public:
44     reference(BitVector &b, unsigned Idx) {
45       WordRef = &b.Bits[Idx / BITWORD_SIZE];
46       BitPos = Idx % BITWORD_SIZE;
47     }
48
49     ~reference() {}
50
51     reference& operator=(bool t) {
52       if (t)
53         *WordRef |= 1L << BitPos;
54       else
55         *WordRef &= ~(1L << BitPos);
56       return *this;
57     }
58
59     operator bool() const {
60       return (*WordRef) & (1L << BitPos);
61     }
62   };
63
64
65   /// BitVector default ctor - Creates an empty bitvector.
66   BitVector() : Size(0), Capacity(0) {
67     Bits = NULL;
68   }
69
70   /// BitVector ctor - Creates a bitvector of specified number of bits. All
71   /// bits are initialized to the specified value.
72   explicit BitVector(unsigned s, bool t = false) : Size(s) {
73     Capacity = NumBitWords(s);
74     Bits = new BitWord[Capacity];
75     init_words(Bits, Capacity, t);
76     if (t)
77       clear_unused_bits();
78   }
79
80   /// BitVector copy ctor.
81   BitVector(const BitVector &RHS) : Size(RHS.size()) {
82     if (Size == 0) {
83       Bits = NULL;
84       Capacity = 0;
85       return;
86     }
87
88     Capacity = NumBitWords(RHS.size());
89     Bits = new BitWord[Capacity];
90     std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
91   }
92   
93   ~BitVector() {
94     delete[] 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         if (sizeof(BitWord) == 4)
132           return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
133         else if (sizeof(BitWord) == 8)
134           return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
135         else
136           assert(0 && "Unsupported!");
137       }
138     return -1;
139   }
140
141   /// find_next - Returns the index of the next set bit following the
142   /// "Prev" bit. Returns -1 if the next set bit is not found.
143   int find_next(unsigned Prev) const {
144     ++Prev;
145     if (Prev >= Size)
146       return -1;
147
148     unsigned WordPos = Prev / BITWORD_SIZE;
149     unsigned BitPos = Prev % BITWORD_SIZE;
150     BitWord Copy = Bits[WordPos];
151     // Mask off previous bits.
152     Copy &= ~0L << BitPos;
153
154     if (Copy != 0) {
155       if (sizeof(BitWord) == 4)
156         return WordPos * BITWORD_SIZE + CountTrailingZeros_32(Copy);
157       else if (sizeof(BitWord) == 8)
158         return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
159       else
160         assert(0 && "Unsupported!");
161     }
162
163     // Check subsequent words.
164     for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
165       if (Bits[i] != 0) {
166         if (sizeof(BitWord) == 4)
167           return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
168         else if (sizeof(BitWord) == 8)
169           return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
170         else
171           assert(0 && "Unsupported!");
172       }
173     return -1;
174   }
175
176   /// clear - Clear all bits.
177   void clear() {
178     Size = 0;
179   }
180
181   /// resize - Grow or shrink the bitvector.
182   void resize(unsigned N, bool t = false) {
183     if (N > Capacity * BITWORD_SIZE) {
184       unsigned OldCapacity = Capacity;
185       grow(N);
186       init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
187     }
188     
189     // Set any old unused bits that are now included in the BitVector. This 
190     // may set bits that are not included in the new vector, but we will clear 
191     // them back out below.
192     if (N > Size)
193       set_unused_bits(t);
194     
195     // Update the size, and clear out any bits that are now unused
196     unsigned OldSize = Size;
197     Size = N;
198     if (t || N < OldSize)
199       clear_unused_bits();
200   }
201
202   void reserve(unsigned N) {
203     if (N > Capacity * BITWORD_SIZE)
204       grow(N);
205   }
206
207   // Set, reset, flip
208   BitVector &set() {
209     init_words(Bits, Capacity, true);
210     clear_unused_bits();
211     return *this;
212   }
213
214   BitVector &set(unsigned Idx) {
215     Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
216     return *this;
217   }
218
219   BitVector &reset() {
220     init_words(Bits, Capacity, false);
221     return *this;
222   }
223
224   BitVector &reset(unsigned Idx) {
225     Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
226     return *this;
227   }
228
229   BitVector &flip() {
230     for (unsigned i = 0; i < NumBitWords(size()); ++i)
231       Bits[i] = ~Bits[i];
232     clear_unused_bits();
233     return *this;
234   }
235
236   BitVector &flip(unsigned Idx) {
237     Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
238     return *this;
239   }
240
241   // No argument flip.
242   BitVector operator~() const {
243     return BitVector(*this).flip();
244   }
245
246   // Indexing.
247   reference operator[](unsigned Idx) {
248     return reference(*this, Idx);
249   }
250
251   bool operator[](unsigned Idx) const {
252     BitWord Mask = 1L << (Idx % BITWORD_SIZE);
253     return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
254   }
255
256   bool test(unsigned Idx) const {
257     return (*this)[Idx];
258   }
259
260   // Comparison operators.
261   bool operator==(const BitVector &RHS) const {
262     unsigned ThisWords = NumBitWords(size());
263     unsigned RHSWords  = NumBitWords(RHS.size());
264     unsigned i;
265     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
266       if (Bits[i] != RHS.Bits[i])
267         return false;
268     
269     // Verify that any extra words are all zeros.
270     if (i != ThisWords) {
271       for (; i != ThisWords; ++i)
272         if (Bits[i])
273           return false;
274     } else if (i != RHSWords) {
275       for (; i != RHSWords; ++i)
276         if (RHS.Bits[i])
277           return false;
278     }
279     return true;
280   }
281
282   bool operator!=(const BitVector &RHS) const {
283     return !(*this == RHS);
284   }
285
286   // Intersection, union, disjoint union.
287   BitVector operator&=(const BitVector &RHS) {
288     unsigned ThisWords = NumBitWords(size());
289     unsigned RHSWords  = NumBitWords(RHS.size());
290     unsigned i;
291     for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
292       Bits[i] &= RHS.Bits[i];
293     
294     // Any bits that are just in this bitvector become zero, because they aren't
295     // in the RHS bit vector.  Any words only in RHS are ignored because they
296     // are already zero in the LHS.
297     for (; i != ThisWords; ++i)
298       Bits[i] = 0;
299     
300     return *this;
301   }
302
303   BitVector operator|=(const BitVector &RHS) {
304     assert(Size == RHS.Size && "Illegal operation!");
305     for (unsigned i = 0; i < NumBitWords(size()); ++i)
306       Bits[i] |= RHS.Bits[i];
307     return *this;
308   }
309
310   BitVector operator^=(const BitVector &RHS) {
311     assert(Size == RHS.Size && "Illegal operation!");
312     for (unsigned i = 0; i < NumBitWords(size()); ++i)
313       Bits[i] ^= RHS.Bits[i];
314     return *this;
315   }
316   
317   // Assignment operator.
318   const BitVector &operator=(const BitVector &RHS) {
319     if (this == &RHS) return *this;
320
321     Size = RHS.size();
322     unsigned RHSWords = NumBitWords(Size);
323     if (Size <= Capacity * BITWORD_SIZE) {
324       std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
325       clear_unused_bits();
326       return *this;
327     }
328   
329     // Grow the bitvector to have enough elements.
330     Capacity = RHSWords;
331     BitWord *NewBits = new BitWord[Capacity];
332     std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
333
334     // Destroy the old bits.
335     delete[] Bits;
336     Bits = NewBits;
337
338     return *this;
339   }
340
341 private:
342   unsigned NumBitWords(unsigned S) const {
343     return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
344   }
345   
346   // Set the unused bits in the high words.
347   void set_unused_bits(bool t = true) {
348     //  Set high words first.
349     unsigned UsedWords = NumBitWords(Size);
350     if (Capacity > UsedWords)
351       init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
352     
353     //  Then set any stray high bits of the last used word.
354     unsigned ExtraBits = Size % BITWORD_SIZE;
355     if (ExtraBits) {
356       Bits[UsedWords-1] &= ~(~0L << ExtraBits);
357       Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits;
358     }
359   }
360
361   // Clear the unused bits in the high words.
362   void clear_unused_bits() {
363     set_unused_bits(false);
364   }
365
366   void grow(unsigned NewSize) {
367     unsigned OldCapacity = Capacity;
368     Capacity = NumBitWords(NewSize);
369     BitWord *NewBits = new BitWord[Capacity];
370
371     // Copy the old bits over.
372     if (OldCapacity != 0)
373       std::copy(Bits, &Bits[OldCapacity], NewBits);
374
375     // Destroy the old bits.
376     delete[] Bits;
377     Bits = NewBits;
378   }
379
380   void init_words(BitWord *B, unsigned NumWords, bool t) {
381     memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
382   } 
383 };
384
385 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
386   BitVector Result(LHS);
387   Result &= RHS;
388   return Result;
389 }
390
391 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
392   BitVector Result(LHS);
393   Result |= RHS;
394   return Result;
395 }
396
397 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
398   BitVector Result(LHS);
399   Result ^= RHS;
400   return Result;
401 }
402  
403 } // End llvm namespace
404 #endif