1 //===- llvm/ADT/BitVector.h - 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 BitVector class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_BITVECTOR_H
15 #define LLVM_ADT_BITVECTOR_H
17 #include "llvm/Support/MathExtras.h"
26 typedef unsigned long BitWord;
28 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * 8 };
30 BitWord *Bits; // Actual bits.
31 unsigned Size; // Size of bitvector in bits.
32 unsigned Capacity; // Size of allocated memory in BitWord.
35 // Encapsulation of a single bit.
37 friend class BitVector;
42 reference(); // Undefined
45 reference(BitVector &b, unsigned Idx) {
46 WordRef = &b.Bits[Idx / BITWORD_SIZE];
47 BitPos = Idx % BITWORD_SIZE;
52 reference& operator=(bool t) {
54 *WordRef |= 1L << BitPos;
56 *WordRef &= ~(1L << BitPos);
60 operator bool() const {
61 return ((*WordRef) & (1L << BitPos)) ? true : false;
66 /// BitVector default ctor - Creates an empty bitvector.
67 BitVector() : Size(0), Capacity(0) {
71 /// BitVector ctor - Creates a bitvector of specified number of bits. All
72 /// bits are initialized to the specified value.
73 explicit BitVector(unsigned s, bool t = false) : Size(s) {
74 Capacity = NumBitWords(s);
75 Bits = new BitWord[Capacity];
76 init_words(Bits, Capacity, t);
81 /// BitVector copy ctor.
82 BitVector(const BitVector &RHS) : Size(RHS.size()) {
89 Capacity = NumBitWords(RHS.size());
90 Bits = new BitWord[Capacity];
91 std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
98 /// size - Returns the number of bits in this bitvector.
99 unsigned size() const { return Size; }
101 /// count - Returns the number of bits which are set.
102 unsigned count() const {
103 unsigned NumBits = 0;
104 for (unsigned i = 0; i < NumBitWords(size()); ++i)
105 if (sizeof(BitWord) == 4)
106 NumBits += CountPopulation_32((uint32_t)Bits[i]);
107 else if (sizeof(BitWord) == 8)
108 NumBits += CountPopulation_64(Bits[i]);
110 assert(0 && "Unsupported!");
114 /// any - Returns true if any bit is set.
116 for (unsigned i = 0; i < NumBitWords(size()); ++i)
122 /// none - Returns true if none of the bits are set.
127 /// find_first - Returns the index of the first set bit, -1 if none
128 /// of the bits are set.
129 int find_first() const {
130 for (unsigned i = 0; i < NumBitWords(size()); ++i)
132 if (sizeof(BitWord) == 4)
133 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
134 else if (sizeof(BitWord) == 8)
135 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
137 assert(0 && "Unsupported!");
142 /// find_next - Returns the index of the next set bit following the
143 /// "Prev" bit. Returns -1 if the next set bit is not found.
144 int find_next(unsigned Prev) const {
149 unsigned WordPos = Prev / BITWORD_SIZE;
150 unsigned BitPos = Prev % BITWORD_SIZE;
151 BitWord Copy = Bits[WordPos];
152 // Mask off previous bits.
153 Copy &= ~0L << BitPos;
156 if (sizeof(BitWord) == 4)
157 return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy);
158 else if (sizeof(BitWord) == 8)
159 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
161 assert(0 && "Unsupported!");
164 // Check subsequent words.
165 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
167 if (sizeof(BitWord) == 4)
168 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
169 else if (sizeof(BitWord) == 8)
170 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
172 assert(0 && "Unsupported!");
177 /// clear - Clear all bits.
182 /// resize - Grow or shrink the bitvector.
183 void resize(unsigned N, bool t = false) {
184 if (N > Capacity * BITWORD_SIZE) {
185 unsigned OldCapacity = Capacity;
187 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
190 // Set any old unused bits that are now included in the BitVector. This
191 // may set bits that are not included in the new vector, but we will clear
192 // them back out below.
196 // Update the size, and clear out any bits that are now unused
197 unsigned OldSize = Size;
199 if (t || N < OldSize)
203 void reserve(unsigned N) {
204 if (N > Capacity * BITWORD_SIZE)
210 init_words(Bits, Capacity, true);
215 BitVector &set(unsigned Idx) {
216 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
221 init_words(Bits, Capacity, false);
225 BitVector &reset(unsigned Idx) {
226 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
231 for (unsigned i = 0; i < NumBitWords(size()); ++i)
237 BitVector &flip(unsigned Idx) {
238 Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
243 BitVector operator~() const {
244 return BitVector(*this).flip();
248 reference operator[](unsigned Idx) {
249 assert (Idx < Size && "Out-of-bounds Bit access.");
250 return reference(*this, Idx);
253 bool operator[](unsigned Idx) const {
254 assert (Idx < Size && "Out-of-bounds Bit access.");
255 BitWord Mask = 1L << (Idx % BITWORD_SIZE);
256 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
259 bool test(unsigned Idx) const {
263 // Comparison operators.
264 bool operator==(const BitVector &RHS) const {
265 unsigned ThisWords = NumBitWords(size());
266 unsigned RHSWords = NumBitWords(RHS.size());
268 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
269 if (Bits[i] != RHS.Bits[i])
272 // Verify that any extra words are all zeros.
273 if (i != ThisWords) {
274 for (; i != ThisWords; ++i)
277 } else if (i != RHSWords) {
278 for (; i != RHSWords; ++i)
285 bool operator!=(const BitVector &RHS) const {
286 return !(*this == RHS);
289 // Intersection, union, disjoint union.
290 BitVector operator&=(const BitVector &RHS) {
291 unsigned ThisWords = NumBitWords(size());
292 unsigned RHSWords = NumBitWords(RHS.size());
294 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
295 Bits[i] &= RHS.Bits[i];
297 // Any bits that are just in this bitvector become zero, because they aren't
298 // in the RHS bit vector. Any words only in RHS are ignored because they
299 // are already zero in the LHS.
300 for (; i != ThisWords; ++i)
306 BitVector operator|=(const BitVector &RHS) {
307 assert(Size == RHS.Size && "Illegal operation!");
308 for (unsigned i = 0; i < NumBitWords(size()); ++i)
309 Bits[i] |= RHS.Bits[i];
313 BitVector operator^=(const BitVector &RHS) {
314 assert(Size == RHS.Size && "Illegal operation!");
315 for (unsigned i = 0; i < NumBitWords(size()); ++i)
316 Bits[i] ^= RHS.Bits[i];
320 // Assignment operator.
321 const BitVector &operator=(const BitVector &RHS) {
322 if (this == &RHS) return *this;
325 unsigned RHSWords = NumBitWords(Size);
326 if (Size <= Capacity * BITWORD_SIZE) {
327 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
332 // Grow the bitvector to have enough elements.
334 BitWord *NewBits = new BitWord[Capacity];
335 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
337 // Destroy the old bits.
345 unsigned NumBitWords(unsigned S) const {
346 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
349 // Set the unused bits in the high words.
350 void set_unused_bits(bool t = true) {
351 // Set high words first.
352 unsigned UsedWords = NumBitWords(Size);
353 if (Capacity > UsedWords)
354 init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
356 // Then set any stray high bits of the last used word.
357 unsigned ExtraBits = Size % BITWORD_SIZE;
359 Bits[UsedWords-1] &= ~(~0L << ExtraBits);
360 Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits;
364 // Clear the unused bits in the high words.
365 void clear_unused_bits() {
366 set_unused_bits(false);
369 void grow(unsigned NewSize) {
370 unsigned OldCapacity = Capacity;
371 Capacity = NumBitWords(NewSize);
372 BitWord *NewBits = new BitWord[Capacity];
374 // Copy the old bits over.
375 if (OldCapacity != 0)
376 std::copy(Bits, &Bits[OldCapacity], NewBits);
378 // Destroy the old bits.
385 void init_words(BitWord *B, unsigned NumWords, bool t) {
386 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
390 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
391 BitVector Result(LHS);
396 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
397 BitVector Result(LHS);
402 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
403 BitVector Result(LHS);
408 } // End llvm namespace