Implement a read/write operator[] for SmallBitVector with a proxy class.
[oota-llvm.git] / include / llvm / ADT / SmallBitVector.h
1 //===- llvm/ADT/SmallBitVector.h - 'Normally small' bit vectors -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the SmallBitVector class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_SMALLBITVECTOR_H
15 #define LLVM_ADT_SMALLBITVECTOR_H
16
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/Support/MathExtras.h"
19 #include <cassert>
20
21 namespace llvm {
22
23 /// SmallBitVector - This is a 'bitvector' (really, a variable-sized bit array),
24 /// optimized for the case when the array is small.  It contains one
25 /// pointer-sized field, which is directly used as a plain collection of bits
26 /// when possible, or as a pointer to a larger heap-allocated array when
27 /// necessary.  This allows normal "small" cases to be fast without losing
28 /// generality for large inputs.
29 ///
30 class SmallBitVector {
31   // TODO: In "large" mode, a pointer to a BitVector is used, leading to an
32   // unnecessary level of indirection. It would be more efficient to use a
33   // pointer to memory containing size, allocation size, and the array of bits.
34   uintptr_t X;
35
36   enum {
37     // The number of bits in this class.
38     NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
39
40     // One bit is used to discriminate between small and large mode. The
41     // remaining bits are used for the small-mode representation.
42     SmallNumRawBits = NumBaseBits - 1,
43
44     // A few more bits are used to store the size of the bit set in small mode.
45     // Theoretically this is a ceil-log2. These bits are encoded in the most
46     // significant bits of the raw bits.
47     SmallNumSizeBits = (NumBaseBits == 32 ? 5 :
48                         NumBaseBits == 64 ? 6 :
49                         SmallNumRawBits),
50
51     // The remaining bits are used to store the actual set in small mode.
52     SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
53   };
54
55 public:
56   // Encapsulation of a single bit.
57   class reference {
58     SmallBitVector &TheVector;
59     unsigned BitPos;
60
61   public:
62     reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
63
64     reference& operator=(reference t) {
65       *this = bool(t);
66       return *this;
67     }
68
69     reference& operator=(bool t) {
70       if (t)
71         TheVector.set(BitPos);
72       else
73         TheVector.reset(BitPos);
74       return *this;
75     }
76
77     operator bool() const {
78       return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
79     }
80   };
81
82 private:
83   bool isSmall() const {
84     return X & uintptr_t(1);
85   }
86
87   BitVector *getPointer() const {
88     assert(!isSmall());
89     return reinterpret_cast<BitVector *>(X);
90   }
91
92   void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
93     X = 1;
94     setSmallSize(NewSize);
95     setSmallBits(NewSmallBits);
96   }
97
98   void switchToLarge(BitVector *BV) {
99     X = reinterpret_cast<uintptr_t>(BV);
100     assert(!isSmall() && "Tried to use an unaligned pointer");
101   }
102
103   // Return all the bits used for the "small" representation; this includes
104   // bits for the size as well as the element bits.
105   uintptr_t getSmallRawBits() const {
106     assert(isSmall());
107     return X >> 1;
108   }
109
110   void setSmallRawBits(uintptr_t NewRawBits) {
111     assert(isSmall());
112     X = (NewRawBits << 1) | uintptr_t(1);
113   }
114
115   // Return the size.
116   size_t getSmallSize() const {
117     return getSmallRawBits() >> SmallNumDataBits;
118   }
119
120   void setSmallSize(size_t Size) {
121     setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
122   }
123
124   // Return the element bits.
125   uintptr_t getSmallBits() const {
126     return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
127   }
128
129   void setSmallBits(uintptr_t NewBits) {
130     setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
131                     (getSmallSize() << SmallNumDataBits));
132   }
133
134 public:
135   /// SmallBitVector default ctor - Creates an empty bitvector.
136   SmallBitVector() : X(1) {}
137
138   /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
139   /// bits are initialized to the specified value.
140   explicit SmallBitVector(unsigned s, bool t = false) {
141     if (s <= SmallNumDataBits)
142       switchToSmall(t ? ~uintptr_t(0) : 0, s);
143     else
144       switchToLarge(new BitVector(s, t));
145   }
146
147   /// SmallBitVector copy ctor.
148   SmallBitVector(const SmallBitVector &RHS) {
149     if (RHS.isSmall())
150       X = RHS.X;
151     else
152       switchToLarge(new BitVector(*RHS.getPointer()));
153   }
154
155   ~SmallBitVector() {
156     if (!isSmall())
157       delete getPointer();
158   }
159
160   /// empty - Tests whether there are no bits in this bitvector.
161   bool empty() const {
162     return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
163   }
164
165   /// size - Returns the number of bits in this bitvector.
166   size_t size() const {
167     return isSmall() ? getSmallSize() : getPointer()->size();
168   }
169
170   /// count - Returns the number of bits which are set.
171   unsigned count() const {
172     if (isSmall()) {
173       uintptr_t Bits = getSmallBits();
174       if (sizeof(uintptr_t) * CHAR_BIT == 32)
175         return CountPopulation_32(Bits);
176       if (sizeof(uintptr_t) * CHAR_BIT == 64)
177         return CountPopulation_64(Bits);
178       assert(0 && "Unsupported!");
179     }
180     return getPointer()->count();
181   }
182
183   /// any - Returns true if any bit is set.
184   bool any() const {
185     if (isSmall())
186       return getSmallBits() != 0;
187     return getPointer()->any();
188   }
189
190   /// none - Returns true if none of the bits are set.
191   bool none() const {
192     if (isSmall())
193       return getSmallBits() == 0;
194     return getPointer()->none();
195   }
196
197   /// find_first - Returns the index of the first set bit, -1 if none
198   /// of the bits are set.
199   int find_first() const {
200     if (isSmall()) {
201       uintptr_t Bits = getSmallBits();
202       if (sizeof(uintptr_t) * CHAR_BIT == 32) {
203         size_t FirstBit = CountTrailingZeros_32(Bits);
204         return FirstBit == 32 ? -1 : FirstBit;
205       } else if (sizeof(uintptr_t) * CHAR_BIT == 64) {
206         size_t FirstBit = CountTrailingZeros_64(Bits);
207         return FirstBit == 64 ? -1 : FirstBit;
208       }
209       assert(0 && "Unsupported!");
210     }
211     return getPointer()->find_first();
212   }
213
214   /// find_next - Returns the index of the next set bit following the
215   /// "Prev" bit. Returns -1 if the next set bit is not found.
216   int find_next(unsigned Prev) const {
217     if (isSmall()) {
218       uintptr_t Bits = getSmallBits();
219       // Mask off previous bits.
220       Bits &= ~uintptr_t(0) << (Prev + 1);
221       if (sizeof(uintptr_t) * CHAR_BIT == 32) {
222         size_t FirstBit = CountTrailingZeros_32(Bits);
223         return FirstBit == 32 ? -1 : FirstBit;
224       } else if (sizeof(uintptr_t) * CHAR_BIT == 64) {
225         size_t FirstBit = CountTrailingZeros_64(Bits);
226         return FirstBit == 64 ? -1 : FirstBit;
227       }
228       assert(0 && "Unsupported!");
229     }
230     return getPointer()->find_next(Prev);
231   }
232
233   /// clear - Clear all bits.
234   void clear() {
235     if (!isSmall())
236       delete getPointer();
237     switchToSmall(0, 0);
238   }
239
240   /// resize - Grow or shrink the bitvector.
241   void resize(unsigned N, bool t = false) {
242     if (!isSmall()) {
243       getPointer()->resize(N, t);
244     } else if (SmallNumDataBits >= N) {
245       uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
246       setSmallSize(N);
247       setSmallBits(NewBits | getSmallBits());
248     } else {
249       BitVector *BV = new BitVector(N, t);
250       uintptr_t OldBits = getSmallBits();
251       for (size_t i = 0, e = getSmallSize(); i != e; ++i)
252         (*BV)[i] = (OldBits >> i) & 1;
253       switchToLarge(BV);
254     }
255   }
256
257   void reserve(unsigned N) {
258     if (isSmall()) {
259       if (N > SmallNumDataBits) {
260         uintptr_t OldBits = getSmallRawBits();
261         size_t SmallSize = getSmallSize();
262         BitVector *BV = new BitVector(SmallSize);
263         for (size_t i = 0; i < SmallSize; ++i)
264           if ((OldBits >> i) & 1)
265             BV->set(i);
266         BV->reserve(N);
267         switchToLarge(BV);
268       }
269     } else {
270       getPointer()->reserve(N);
271     }
272   }
273
274   // Set, reset, flip
275   SmallBitVector &set() {
276     if (isSmall())
277       setSmallBits(~uintptr_t(0));
278     else
279       getPointer()->set();
280     return *this;
281   }
282
283   SmallBitVector &set(unsigned Idx) {
284     if (isSmall())
285       setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
286     else
287       getPointer()->set(Idx);
288     return *this;
289   }
290
291   SmallBitVector &reset() {
292     if (isSmall())
293       setSmallBits(0);
294     else
295       getPointer()->reset();
296     return *this;
297   }
298
299   SmallBitVector &reset(unsigned Idx) {
300     if (isSmall())
301       setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
302     else
303       getPointer()->reset(Idx);
304     return *this;
305   }
306
307   SmallBitVector &flip() {
308     if (isSmall())
309       setSmallBits(~getSmallBits());
310     else
311       getPointer()->flip();
312     return *this;
313   }
314
315   SmallBitVector &flip(unsigned Idx) {
316     if (isSmall())
317       setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
318     else
319       getPointer()->flip(Idx);
320     return *this;
321   }
322
323   // No argument flip.
324   SmallBitVector operator~() const {
325     return SmallBitVector(*this).flip();
326   }
327
328   // Indexing.
329   reference operator[](unsigned Idx) {
330     assert(Idx < size() && "Out-of-bounds Bit access.");
331     return reference(*this, Idx);
332   }
333
334   bool operator[](unsigned Idx) const {
335     assert(Idx < size() && "Out-of-bounds Bit access.");
336     if (isSmall())
337       return ((getSmallBits() >> Idx) & 1) != 0;
338     return getPointer()->operator[](Idx);
339   }
340
341   bool test(unsigned Idx) const {
342     return (*this)[Idx];
343   }
344
345   // Comparison operators.
346   bool operator==(const SmallBitVector &RHS) const {
347     if (size() != RHS.size())
348       return false;
349     if (isSmall())
350       return getSmallBits() == RHS.getSmallBits();
351     else
352       return *getPointer() == *RHS.getPointer();
353   }
354
355   bool operator!=(const SmallBitVector &RHS) const {
356     return !(*this == RHS);
357   }
358
359   // Intersection, union, disjoint union.
360   SmallBitVector &operator&=(const SmallBitVector &RHS) {
361     resize(std::max(size(), RHS.size()));
362     if (isSmall())
363       setSmallBits(getSmallBits() & RHS.getSmallBits());
364     else if (!RHS.isSmall())
365       getPointer()->operator&=(*RHS.getPointer());
366     else {
367       SmallBitVector Copy = RHS;
368       Copy.resize(size());
369       getPointer()->operator&=(*Copy.getPointer());
370     }
371     return *this;
372   }
373
374   SmallBitVector &operator|=(const SmallBitVector &RHS) {
375     resize(std::max(size(), RHS.size()));
376     if (isSmall())
377       setSmallBits(getSmallBits() | RHS.getSmallBits());
378     else if (!RHS.isSmall())
379       getPointer()->operator|=(*RHS.getPointer());
380     else {
381       SmallBitVector Copy = RHS;
382       Copy.resize(size());
383       getPointer()->operator|=(*Copy.getPointer());
384     }
385     return *this;
386   }
387
388   SmallBitVector &operator^=(const SmallBitVector &RHS) {
389     resize(std::max(size(), RHS.size()));
390     if (isSmall())
391       setSmallBits(getSmallBits() ^ RHS.getSmallBits());
392     else if (!RHS.isSmall())
393       getPointer()->operator^=(*RHS.getPointer());
394     else {
395       SmallBitVector Copy = RHS;
396       Copy.resize(size());
397       getPointer()->operator^=(*Copy.getPointer());
398     }
399     return *this;
400   }
401
402   // Assignment operator.
403   const SmallBitVector &operator=(const SmallBitVector &RHS) {
404     if (isSmall()) {
405       if (RHS.isSmall())
406         X = RHS.X;
407       else
408         switchToLarge(new BitVector(*RHS.getPointer()));
409     } else {
410       if (!RHS.isSmall())
411         *getPointer() = *RHS.getPointer();
412       else {
413         delete getPointer();
414         X = RHS.X;
415       }
416     }
417     return *this;
418   }
419
420   void swap(SmallBitVector &RHS) {
421     std::swap(X, RHS.X);
422   }
423 };
424
425 inline SmallBitVector
426 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
427   SmallBitVector Result(LHS);
428   Result &= RHS;
429   return Result;
430 }
431
432 inline SmallBitVector
433 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
434   SmallBitVector Result(LHS);
435   Result |= RHS;
436   return Result;
437 }
438
439 inline SmallBitVector
440 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
441   SmallBitVector Result(LHS);
442   Result ^= RHS;
443   return Result;
444 }
445
446 } // End llvm namespace
447
448 namespace std {
449   /// Implement std::swap in terms of BitVector swap.
450   inline void
451   swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {
452     LHS.swap(RHS);
453   }
454 }
455
456 #endif