1 //===- llvm/ADT/SmallBitVector.h - 'Normally small' bit vectors -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SmallBitVector class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_SMALLBITVECTOR_H
15 #define LLVM_ADT_SMALLBITVECTOR_H
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/Support/MathExtras.h"
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.
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.
37 // The number of bits in this class.
38 NumBaseBits = sizeof(uintptr_t) * CHAR_BIT,
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,
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 :
51 // The remaining bits are used to store the actual set in small mode.
52 SmallNumDataBits = SmallNumRawBits - SmallNumSizeBits
56 // Encapsulation of a single bit.
58 SmallBitVector &TheVector;
62 reference(SmallBitVector &b, unsigned Idx) : TheVector(b), BitPos(Idx) {}
64 reference& operator=(reference t) {
69 reference& operator=(bool t) {
71 TheVector.set(BitPos);
73 TheVector.reset(BitPos);
77 operator bool() const {
78 return const_cast<const SmallBitVector &>(TheVector).operator[](BitPos);
83 bool isSmall() const {
84 return X & uintptr_t(1);
87 BitVector *getPointer() const {
89 return reinterpret_cast<BitVector *>(X);
92 void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
94 setSmallSize(NewSize);
95 setSmallBits(NewSmallBits);
98 void switchToLarge(BitVector *BV) {
99 X = reinterpret_cast<uintptr_t>(BV);
100 assert(!isSmall() && "Tried to use an unaligned pointer");
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 {
110 void setSmallRawBits(uintptr_t NewRawBits) {
112 X = (NewRawBits << 1) | uintptr_t(1);
116 size_t getSmallSize() const {
117 return getSmallRawBits() >> SmallNumDataBits;
120 void setSmallSize(size_t Size) {
121 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
124 // Return the element bits.
125 uintptr_t getSmallBits() const {
126 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
129 void setSmallBits(uintptr_t NewBits) {
130 setSmallRawBits((NewBits & ~(~uintptr_t(0) << getSmallSize())) |
131 (getSmallSize() << SmallNumDataBits));
135 /// SmallBitVector default ctor - Creates an empty bitvector.
136 SmallBitVector() : X(1) {}
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);
144 switchToLarge(new BitVector(s, t));
147 /// SmallBitVector copy ctor.
148 SmallBitVector(const SmallBitVector &RHS) {
152 switchToLarge(new BitVector(*RHS.getPointer()));
160 /// empty - Tests whether there are no bits in this bitvector.
162 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
165 /// size - Returns the number of bits in this bitvector.
166 size_t size() const {
167 return isSmall() ? getSmallSize() : getPointer()->size();
170 /// count - Returns the number of bits which are set.
171 unsigned count() const {
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!");
180 return getPointer()->count();
183 /// any - Returns true if any bit is set.
186 return getSmallBits() != 0;
187 return getPointer()->any();
190 /// all - Returns true if all bits are set.
193 return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1;
194 return getPointer()->all();
197 /// none - Returns true if none of the bits are set.
200 return getSmallBits() == 0;
201 return getPointer()->none();
204 /// find_first - Returns the index of the first set bit, -1 if none
205 /// of the bits are set.
206 int find_first() const {
208 uintptr_t Bits = getSmallBits();
211 if (sizeof(uintptr_t) * CHAR_BIT == 32)
212 return CountTrailingZeros_32(Bits);
213 if (sizeof(uintptr_t) * CHAR_BIT == 64)
214 return CountTrailingZeros_64(Bits);
215 assert(0 && "Unsupported!");
217 return getPointer()->find_first();
220 /// find_next - Returns the index of the next set bit following the
221 /// "Prev" bit. Returns -1 if the next set bit is not found.
222 int find_next(unsigned Prev) const {
224 uintptr_t Bits = getSmallBits();
225 // Mask off previous bits.
226 Bits &= ~uintptr_t(0) << (Prev + 1);
227 if (Bits == 0 || Prev + 1 >= getSmallSize())
229 if (sizeof(uintptr_t) * CHAR_BIT == 32)
230 return CountTrailingZeros_32(Bits);
231 if (sizeof(uintptr_t) * CHAR_BIT == 64)
232 return CountTrailingZeros_64(Bits);
233 assert(0 && "Unsupported!");
235 return getPointer()->find_next(Prev);
238 /// clear - Clear all bits.
245 /// resize - Grow or shrink the bitvector.
246 void resize(unsigned N, bool t = false) {
248 getPointer()->resize(N, t);
249 } else if (SmallNumDataBits >= N) {
250 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
252 setSmallBits(NewBits | getSmallBits());
254 BitVector *BV = new BitVector(N, t);
255 uintptr_t OldBits = getSmallBits();
256 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
257 (*BV)[i] = (OldBits >> i) & 1;
262 void reserve(unsigned N) {
264 if (N > SmallNumDataBits) {
265 uintptr_t OldBits = getSmallRawBits();
266 size_t SmallSize = getSmallSize();
267 BitVector *BV = new BitVector(SmallSize);
268 for (size_t i = 0; i < SmallSize; ++i)
269 if ((OldBits >> i) & 1)
275 getPointer()->reserve(N);
280 SmallBitVector &set() {
282 setSmallBits(~uintptr_t(0));
288 SmallBitVector &set(unsigned Idx) {
290 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
292 getPointer()->set(Idx);
296 SmallBitVector &reset() {
300 getPointer()->reset();
304 SmallBitVector &reset(unsigned Idx) {
306 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
308 getPointer()->reset(Idx);
312 SmallBitVector &flip() {
314 setSmallBits(~getSmallBits());
316 getPointer()->flip();
320 SmallBitVector &flip(unsigned Idx) {
322 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
324 getPointer()->flip(Idx);
329 SmallBitVector operator~() const {
330 return SmallBitVector(*this).flip();
334 reference operator[](unsigned Idx) {
335 assert(Idx < size() && "Out-of-bounds Bit access.");
336 return reference(*this, Idx);
339 bool operator[](unsigned Idx) const {
340 assert(Idx < size() && "Out-of-bounds Bit access.");
342 return ((getSmallBits() >> Idx) & 1) != 0;
343 return getPointer()->operator[](Idx);
346 bool test(unsigned Idx) const {
350 // Comparison operators.
351 bool operator==(const SmallBitVector &RHS) const {
352 if (size() != RHS.size())
355 return getSmallBits() == RHS.getSmallBits();
357 return *getPointer() == *RHS.getPointer();
360 bool operator!=(const SmallBitVector &RHS) const {
361 return !(*this == RHS);
364 // Intersection, union, disjoint union.
365 SmallBitVector &operator&=(const SmallBitVector &RHS) {
366 resize(std::max(size(), RHS.size()));
368 setSmallBits(getSmallBits() & RHS.getSmallBits());
369 else if (!RHS.isSmall())
370 getPointer()->operator&=(*RHS.getPointer());
372 SmallBitVector Copy = RHS;
374 getPointer()->operator&=(*Copy.getPointer());
379 SmallBitVector &operator|=(const SmallBitVector &RHS) {
380 resize(std::max(size(), RHS.size()));
382 setSmallBits(getSmallBits() | RHS.getSmallBits());
383 else if (!RHS.isSmall())
384 getPointer()->operator|=(*RHS.getPointer());
386 SmallBitVector Copy = RHS;
388 getPointer()->operator|=(*Copy.getPointer());
393 SmallBitVector &operator^=(const SmallBitVector &RHS) {
394 resize(std::max(size(), RHS.size()));
396 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
397 else if (!RHS.isSmall())
398 getPointer()->operator^=(*RHS.getPointer());
400 SmallBitVector Copy = RHS;
402 getPointer()->operator^=(*Copy.getPointer());
407 // Assignment operator.
408 const SmallBitVector &operator=(const SmallBitVector &RHS) {
413 switchToLarge(new BitVector(*RHS.getPointer()));
416 *getPointer() = *RHS.getPointer();
425 void swap(SmallBitVector &RHS) {
430 inline SmallBitVector
431 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
432 SmallBitVector Result(LHS);
437 inline SmallBitVector
438 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
439 SmallBitVector Result(LHS);
444 inline SmallBitVector
445 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
446 SmallBitVector Result(LHS);
451 } // End llvm namespace
454 /// Implement std::swap in terms of BitVector swap.
456 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {