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
55 bool isSmall() const {
56 return X & uintptr_t(1);
59 BitVector *getPointer() const {
61 return reinterpret_cast<BitVector *>(X);
64 void switchToSmall(uintptr_t NewSmallBits, size_t NewSize) {
66 setSmallSize(NewSize);
67 setSmallBits(NewSmallBits);
70 void switchToLarge(BitVector *BV) {
71 X = reinterpret_cast<uintptr_t>(BV);
72 assert(!isSmall() && "Tried to use an unaligned pointer");
75 // Return all the bits used for the "small" representation; this includes
76 // bits for the size as well as the element bits.
77 uintptr_t getSmallRawBits() const {
82 void setSmallRawBits(uintptr_t NewRawBits) {
84 X = NewRawBits << 1 | uintptr_t(1);
88 size_t getSmallSize() const {
89 return getSmallRawBits() >> SmallNumDataBits;
92 void setSmallSize(size_t Size) {
93 setSmallRawBits(getSmallBits() | (Size << SmallNumDataBits));
96 // Return the element bits.
97 uintptr_t getSmallBits() const {
98 return getSmallRawBits() & ~(~uintptr_t(0) << getSmallSize());
101 void setSmallBits(uintptr_t NewBits) {
102 setSmallRawBits(NewBits & ~(~uintptr_t(0) << getSmallSize()) |
103 (getSmallSize() << SmallNumDataBits));
107 /// SmallBitVector default ctor - Creates an empty bitvector.
108 SmallBitVector() : X(1) {}
110 /// SmallBitVector ctor - Creates a bitvector of specified number of bits. All
111 /// bits are initialized to the specified value.
112 explicit SmallBitVector(unsigned s, bool t = false) {
113 if (s <= SmallNumDataBits)
114 switchToSmall(t ? ~uintptr_t(0) : 0, s);
116 switchToLarge(new BitVector(s, t));
119 /// SmallBitVector copy ctor.
120 SmallBitVector(const SmallBitVector &RHS) {
124 switchToLarge(new BitVector(*RHS.getPointer()));
132 /// empty - Tests whether there are no bits in this bitvector.
134 return isSmall() ? getSmallSize() == 0 : getPointer()->empty();
137 /// size - Returns the number of bits in this bitvector.
138 size_t size() const {
139 return isSmall() ? getSmallSize() : getPointer()->size();
142 /// count - Returns the number of bits which are set.
143 unsigned count() const {
145 uintptr_t Bits = getSmallBits();
146 if (sizeof(uintptr_t) * CHAR_BIT == 32)
147 return CountPopulation_32(Bits);
148 if (sizeof(uintptr_t) * CHAR_BIT == 64)
149 return CountPopulation_64(Bits);
150 assert(0 && "Unsupported!");
152 return getPointer()->count();
155 /// any - Returns true if any bit is set.
158 return getSmallBits() != 0;
159 return getPointer()->any();
162 /// none - Returns true if none of the bits are set.
165 return getSmallBits() == 0;
166 return getPointer()->none();
169 /// find_first - Returns the index of the first set bit, -1 if none
170 /// of the bits are set.
171 int find_first() const {
173 uintptr_t Bits = getSmallBits();
174 if (sizeof(uintptr_t) * CHAR_BIT == 32) {
175 size_t FirstBit = CountTrailingZeros_32(Bits);
176 return FirstBit == 32 ? -1 : FirstBit;
177 } else if (sizeof(uintptr_t) * CHAR_BIT == 64) {
178 size_t FirstBit = CountTrailingZeros_64(Bits);
179 return FirstBit == 64 ? -1 : FirstBit;
181 assert(0 && "Unsupported!");
183 return getPointer()->find_first();
186 /// find_next - Returns the index of the next set bit following the
187 /// "Prev" bit. Returns -1 if the next set bit is not found.
188 int find_next(unsigned Prev) const {
190 uintptr_t Bits = getSmallBits();
191 // Mask off previous bits.
192 Bits &= ~uintptr_t(0) << (Prev + 1);
193 if (sizeof(uintptr_t) * CHAR_BIT == 32) {
194 size_t FirstBit = CountTrailingZeros_32(Bits);
195 return FirstBit == 32 ? -1 : FirstBit;
196 } else if (sizeof(uintptr_t) * CHAR_BIT == 64) {
197 size_t FirstBit = CountTrailingZeros_64(Bits);
198 return FirstBit == 64 ? -1 : FirstBit;
200 assert(0 && "Unsupported!");
202 return getPointer()->find_next(Prev);
205 /// clear - Clear all bits.
212 /// resize - Grow or shrink the bitvector.
213 void resize(unsigned N, bool t = false) {
215 getPointer()->resize(N, t);
216 } else if (SmallNumDataBits >= N) {
217 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
219 setSmallBits(NewBits | getSmallBits());
221 BitVector *BV = new BitVector(N, t);
222 uintptr_t OldBits = getSmallBits();
223 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
224 (*BV)[i] = (OldBits >> i) & 1;
229 void reserve(unsigned N) {
231 if (N > SmallNumDataBits) {
232 uintptr_t OldBits = getSmallRawBits();
233 size_t SmallSize = getSmallSize();
234 BitVector *BV = new BitVector(SmallSize);
235 for (size_t i = 0; i < SmallSize; ++i)
236 if ((OldBits >> i) & 1)
242 getPointer()->reserve(N);
247 SmallBitVector &set() {
249 setSmallBits(~uintptr_t(0));
255 SmallBitVector &set(unsigned Idx) {
257 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
259 getPointer()->set(Idx);
263 SmallBitVector &reset() {
267 getPointer()->reset();
271 SmallBitVector &reset(unsigned Idx) {
273 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
275 getPointer()->reset(Idx);
279 SmallBitVector &flip() {
281 setSmallBits(~getSmallBits());
283 getPointer()->flip();
287 SmallBitVector &flip(unsigned Idx) {
289 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
291 getPointer()->flip(Idx);
296 SmallBitVector operator~() const {
297 return SmallBitVector(*this).flip();
301 // TODO: Add an index operator which returns a "reference" (proxy class).
302 bool operator[](unsigned Idx) const {
303 assert(Idx < size() && "Out-of-bounds Bit access.");
305 return ((getSmallBits() >> Idx) & 1) != 0;
306 return getPointer()->operator[](Idx);
309 bool test(unsigned Idx) const {
313 // Comparison operators.
314 bool operator==(const SmallBitVector &RHS) const {
315 if (size() != RHS.size())
318 return getSmallBits() == RHS.getSmallBits();
320 return *getPointer() == *RHS.getPointer();
323 bool operator!=(const SmallBitVector &RHS) const {
324 return !(*this == RHS);
327 // Intersection, union, disjoint union.
328 SmallBitVector &operator&=(const SmallBitVector &RHS) {
329 resize(std::max(size(), RHS.size()));
331 setSmallBits(getSmallBits() & RHS.getSmallBits());
332 else if (!RHS.isSmall())
333 getPointer()->operator&=(*RHS.getPointer());
335 SmallBitVector Copy = RHS;
337 getPointer()->operator&=(*Copy.getPointer());
342 SmallBitVector &operator|=(const SmallBitVector &RHS) {
343 resize(std::max(size(), RHS.size()));
345 setSmallBits(getSmallBits() | RHS.getSmallBits());
346 else if (!RHS.isSmall())
347 getPointer()->operator|=(*RHS.getPointer());
349 SmallBitVector Copy = RHS;
351 getPointer()->operator|=(*Copy.getPointer());
356 SmallBitVector &operator^=(const SmallBitVector &RHS) {
357 resize(std::max(size(), RHS.size()));
359 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
360 else if (!RHS.isSmall())
361 getPointer()->operator^=(*RHS.getPointer());
363 SmallBitVector Copy = RHS;
365 getPointer()->operator^=(*Copy.getPointer());
370 // Assignment operator.
371 const SmallBitVector &operator=(const SmallBitVector &RHS) {
376 switchToLarge(new BitVector(*RHS.getPointer()));
379 *getPointer() = *RHS.getPointer();
388 void swap(SmallBitVector &RHS) {
393 inline SmallBitVector
394 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
395 SmallBitVector Result(LHS);
400 inline SmallBitVector
401 operator|(const SmallBitVector &LHS, const SmallBitVector &RHS) {
402 SmallBitVector Result(LHS);
407 inline SmallBitVector
408 operator^(const SmallBitVector &LHS, const SmallBitVector &RHS) {
409 SmallBitVector Result(LHS);
414 } // End llvm namespace
417 /// Implement std::swap in terms of BitVector swap.
419 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {