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 /// none - Returns true if none of the bits are set.
193 return getSmallBits() == 0;
194 return getPointer()->none();
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 {
201 uintptr_t Bits = getSmallBits();
204 if (sizeof(uintptr_t) * CHAR_BIT == 32)
205 return CountTrailingZeros_32(Bits);
206 if (sizeof(uintptr_t) * CHAR_BIT == 64)
207 return CountTrailingZeros_64(Bits);
208 assert(0 && "Unsupported!");
210 return getPointer()->find_first();
213 /// find_next - Returns the index of the next set bit following the
214 /// "Prev" bit. Returns -1 if the next set bit is not found.
215 int find_next(unsigned Prev) const {
217 uintptr_t Bits = getSmallBits();
218 // Mask off previous bits.
219 Bits &= ~uintptr_t(0) << (Prev + 1);
220 if (Bits == 0 || Prev + 1 >= getSmallSize())
222 if (sizeof(uintptr_t) * CHAR_BIT == 32)
223 return CountTrailingZeros_32(Bits);
224 if (sizeof(uintptr_t) * CHAR_BIT == 64)
225 return CountTrailingZeros_64(Bits);
226 assert(0 && "Unsupported!");
228 return getPointer()->find_next(Prev);
231 /// clear - Clear all bits.
238 /// resize - Grow or shrink the bitvector.
239 void resize(unsigned N, bool t = false) {
241 getPointer()->resize(N, t);
242 } else if (SmallNumDataBits >= N) {
243 uintptr_t NewBits = t ? ~uintptr_t(0) << getSmallSize() : 0;
245 setSmallBits(NewBits | getSmallBits());
247 BitVector *BV = new BitVector(N, t);
248 uintptr_t OldBits = getSmallBits();
249 for (size_t i = 0, e = getSmallSize(); i != e; ++i)
250 (*BV)[i] = (OldBits >> i) & 1;
255 void reserve(unsigned N) {
257 if (N > SmallNumDataBits) {
258 uintptr_t OldBits = getSmallRawBits();
259 size_t SmallSize = getSmallSize();
260 BitVector *BV = new BitVector(SmallSize);
261 for (size_t i = 0; i < SmallSize; ++i)
262 if ((OldBits >> i) & 1)
268 getPointer()->reserve(N);
273 SmallBitVector &set() {
275 setSmallBits(~uintptr_t(0));
281 SmallBitVector &set(unsigned Idx) {
283 setSmallBits(getSmallBits() | (uintptr_t(1) << Idx));
285 getPointer()->set(Idx);
289 SmallBitVector &reset() {
293 getPointer()->reset();
297 SmallBitVector &reset(unsigned Idx) {
299 setSmallBits(getSmallBits() & ~(uintptr_t(1) << Idx));
301 getPointer()->reset(Idx);
305 SmallBitVector &flip() {
307 setSmallBits(~getSmallBits());
309 getPointer()->flip();
313 SmallBitVector &flip(unsigned Idx) {
315 setSmallBits(getSmallBits() ^ (uintptr_t(1) << Idx));
317 getPointer()->flip(Idx);
322 SmallBitVector operator~() const {
323 return SmallBitVector(*this).flip();
327 reference operator[](unsigned Idx) {
328 assert(Idx < size() && "Out-of-bounds Bit access.");
329 return reference(*this, Idx);
332 bool operator[](unsigned Idx) const {
333 assert(Idx < size() && "Out-of-bounds Bit access.");
335 return ((getSmallBits() >> Idx) & 1) != 0;
336 return getPointer()->operator[](Idx);
339 bool test(unsigned Idx) const {
343 // Comparison operators.
344 bool operator==(const SmallBitVector &RHS) const {
345 if (size() != RHS.size())
348 return getSmallBits() == RHS.getSmallBits();
350 return *getPointer() == *RHS.getPointer();
353 bool operator!=(const SmallBitVector &RHS) const {
354 return !(*this == RHS);
357 // Intersection, union, disjoint union.
358 SmallBitVector &operator&=(const SmallBitVector &RHS) {
359 resize(std::max(size(), RHS.size()));
361 setSmallBits(getSmallBits() & RHS.getSmallBits());
362 else if (!RHS.isSmall())
363 getPointer()->operator&=(*RHS.getPointer());
365 SmallBitVector Copy = RHS;
367 getPointer()->operator&=(*Copy.getPointer());
372 SmallBitVector &operator|=(const SmallBitVector &RHS) {
373 resize(std::max(size(), RHS.size()));
375 setSmallBits(getSmallBits() | RHS.getSmallBits());
376 else if (!RHS.isSmall())
377 getPointer()->operator|=(*RHS.getPointer());
379 SmallBitVector Copy = RHS;
381 getPointer()->operator|=(*Copy.getPointer());
386 SmallBitVector &operator^=(const SmallBitVector &RHS) {
387 resize(std::max(size(), RHS.size()));
389 setSmallBits(getSmallBits() ^ RHS.getSmallBits());
390 else if (!RHS.isSmall())
391 getPointer()->operator^=(*RHS.getPointer());
393 SmallBitVector Copy = RHS;
395 getPointer()->operator^=(*Copy.getPointer());
400 // Assignment operator.
401 const SmallBitVector &operator=(const SmallBitVector &RHS) {
406 switchToLarge(new BitVector(*RHS.getPointer()));
409 *getPointer() = *RHS.getPointer();
418 void swap(SmallBitVector &RHS) {
423 inline SmallBitVector
424 operator&(const SmallBitVector &LHS, const SmallBitVector &RHS) {
425 SmallBitVector Result(LHS);
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 } // End llvm namespace
447 /// Implement std::swap in terms of BitVector swap.
449 swap(llvm::SmallBitVector &LHS, llvm::SmallBitVector &RHS) {