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"
27 typedef unsigned long BitWord;
29 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
31 BitWord *Bits; // Actual bits.
32 unsigned Size; // Size of bitvector in bits.
33 unsigned Capacity; // Size of allocated memory in BitWord.
36 // Encapsulation of a single bit.
38 friend class BitVector;
43 reference(); // Undefined
46 reference(BitVector &b, unsigned Idx) {
47 WordRef = &b.Bits[Idx / BITWORD_SIZE];
48 BitPos = Idx % BITWORD_SIZE;
53 reference &operator=(reference t) {
58 reference& operator=(bool t) {
60 *WordRef |= 1L << BitPos;
62 *WordRef &= ~(1L << BitPos);
66 operator bool() const {
67 return ((*WordRef) & (1L << BitPos)) ? true : false;
72 /// BitVector default ctor - Creates an empty bitvector.
73 BitVector() : Size(0), Capacity(0) {
77 /// BitVector ctor - Creates a bitvector of specified number of bits. All
78 /// bits are initialized to the specified value.
79 explicit BitVector(unsigned s, bool t = false) : Size(s) {
80 Capacity = NumBitWords(s);
81 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
82 init_words(Bits, Capacity, t);
87 /// BitVector copy ctor.
88 BitVector(const BitVector &RHS) : Size(RHS.size()) {
95 Capacity = NumBitWords(RHS.size());
96 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
97 std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
104 /// empty - Tests whether there are no bits in this bitvector.
105 bool empty() const { return Size == 0; }
107 /// size - Returns the number of bits in this bitvector.
108 unsigned size() const { return Size; }
110 /// count - Returns the number of bits which are set.
111 unsigned count() const {
112 unsigned NumBits = 0;
113 for (unsigned i = 0; i < NumBitWords(size()); ++i)
114 if (sizeof(BitWord) == 4)
115 NumBits += CountPopulation_32((uint32_t)Bits[i]);
116 else if (sizeof(BitWord) == 8)
117 NumBits += CountPopulation_64(Bits[i]);
119 assert(0 && "Unsupported!");
123 /// any - Returns true if any bit is set.
125 for (unsigned i = 0; i < NumBitWords(size()); ++i)
131 /// all - Returns true if all bits are set.
133 // TODO: Optimize this.
134 return count() == size();
137 /// none - Returns true if none of the bits are set.
142 /// find_first - Returns the index of the first set bit, -1 if none
143 /// of the bits are set.
144 int find_first() const {
145 for (unsigned i = 0; i < NumBitWords(size()); ++i)
147 if (sizeof(BitWord) == 4)
148 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
149 else if (sizeof(BitWord) == 8)
150 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
152 assert(0 && "Unsupported!");
157 /// find_next - Returns the index of the next set bit following the
158 /// "Prev" bit. Returns -1 if the next set bit is not found.
159 int find_next(unsigned Prev) const {
164 unsigned WordPos = Prev / BITWORD_SIZE;
165 unsigned BitPos = Prev % BITWORD_SIZE;
166 BitWord Copy = Bits[WordPos];
167 // Mask off previous bits.
168 Copy &= ~0L << BitPos;
171 if (sizeof(BitWord) == 4)
172 return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy);
173 else if (sizeof(BitWord) == 8)
174 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
176 assert(0 && "Unsupported!");
179 // Check subsequent words.
180 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
182 if (sizeof(BitWord) == 4)
183 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
184 else if (sizeof(BitWord) == 8)
185 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
187 assert(0 && "Unsupported!");
192 /// clear - Clear all bits.
197 /// resize - Grow or shrink the bitvector.
198 void resize(unsigned N, bool t = false) {
199 if (N > Capacity * BITWORD_SIZE) {
200 unsigned OldCapacity = Capacity;
202 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
205 // Set any old unused bits that are now included in the BitVector. This
206 // may set bits that are not included in the new vector, but we will clear
207 // them back out below.
211 // Update the size, and clear out any bits that are now unused
212 unsigned OldSize = Size;
214 if (t || N < OldSize)
218 void reserve(unsigned N) {
219 if (N > Capacity * BITWORD_SIZE)
225 init_words(Bits, Capacity, true);
230 BitVector &set(unsigned Idx) {
231 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
236 init_words(Bits, Capacity, false);
240 BitVector &reset(unsigned Idx) {
241 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
246 for (unsigned i = 0; i < NumBitWords(size()); ++i)
252 BitVector &flip(unsigned Idx) {
253 Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
258 BitVector operator~() const {
259 return BitVector(*this).flip();
263 reference operator[](unsigned Idx) {
264 assert (Idx < Size && "Out-of-bounds Bit access.");
265 return reference(*this, Idx);
268 bool operator[](unsigned Idx) const {
269 assert (Idx < Size && "Out-of-bounds Bit access.");
270 BitWord Mask = 1L << (Idx % BITWORD_SIZE);
271 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
274 bool test(unsigned Idx) const {
278 // Comparison operators.
279 bool operator==(const BitVector &RHS) const {
280 unsigned ThisWords = NumBitWords(size());
281 unsigned RHSWords = NumBitWords(RHS.size());
283 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
284 if (Bits[i] != RHS.Bits[i])
287 // Verify that any extra words are all zeros.
288 if (i != ThisWords) {
289 for (; i != ThisWords; ++i)
292 } else if (i != RHSWords) {
293 for (; i != RHSWords; ++i)
300 bool operator!=(const BitVector &RHS) const {
301 return !(*this == RHS);
304 // Intersection, union, disjoint union.
305 BitVector &operator&=(const BitVector &RHS) {
306 unsigned ThisWords = NumBitWords(size());
307 unsigned RHSWords = NumBitWords(RHS.size());
309 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
310 Bits[i] &= RHS.Bits[i];
312 // Any bits that are just in this bitvector become zero, because they aren't
313 // in the RHS bit vector. Any words only in RHS are ignored because they
314 // are already zero in the LHS.
315 for (; i != ThisWords; ++i)
321 // reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
322 BitVector &reset(const BitVector &RHS) {
323 unsigned ThisWords = NumBitWords(size());
324 unsigned RHSWords = NumBitWords(RHS.size());
326 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
327 Bits[i] &= ~RHS.Bits[i];
331 BitVector &operator|=(const BitVector &RHS) {
332 if (size() < RHS.size())
334 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
335 Bits[i] |= RHS.Bits[i];
339 BitVector &operator^=(const BitVector &RHS) {
340 if (size() < RHS.size())
342 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
343 Bits[i] ^= RHS.Bits[i];
347 // Assignment operator.
348 const BitVector &operator=(const BitVector &RHS) {
349 if (this == &RHS) return *this;
352 unsigned RHSWords = NumBitWords(Size);
353 if (Size <= Capacity * BITWORD_SIZE) {
355 std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
360 // Grow the bitvector to have enough elements.
362 BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
363 std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
365 // Destroy the old bits.
372 void swap(BitVector &RHS) {
373 std::swap(Bits, RHS.Bits);
374 std::swap(Size, RHS.Size);
375 std::swap(Capacity, RHS.Capacity);
378 //===--------------------------------------------------------------------===//
379 // Portable bit mask operations.
380 //===--------------------------------------------------------------------===//
382 // These methods all operate on arrays of uint32_t, each holding 32 bits. The
383 // fixed word size makes it easier to work with literal bit vector constants
386 // The LSB in each word is the lowest numbered bit. The size of a portable
387 // bit mask is always a whole multiple of 32 bits. If no bit mask size is
388 // given, the bit mask is assumed to cover the entire BitVector.
390 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
391 /// This computes "*this |= Mask".
392 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
393 applyMask<true, false>(Mask, MaskWords);
396 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
397 /// Don't resize. This computes "*this &= ~Mask".
398 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
399 applyMask<false, false>(Mask, MaskWords);
402 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
403 /// Don't resize. This computes "*this |= ~Mask".
404 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
405 applyMask<true, true>(Mask, MaskWords);
408 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
409 /// Don't resize. This computes "*this &= Mask".
410 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
411 applyMask<false, true>(Mask, MaskWords);
415 unsigned NumBitWords(unsigned S) const {
416 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
419 // Set the unused bits in the high words.
420 void set_unused_bits(bool t = true) {
421 // Set high words first.
422 unsigned UsedWords = NumBitWords(Size);
423 if (Capacity > UsedWords)
424 init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
426 // Then set any stray high bits of the last used word.
427 unsigned ExtraBits = Size % BITWORD_SIZE;
429 Bits[UsedWords-1] &= ~(~0L << ExtraBits);
430 Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits;
434 // Clear the unused bits in the high words.
435 void clear_unused_bits() {
436 set_unused_bits(false);
439 void grow(unsigned NewSize) {
440 Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
441 Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
446 void init_words(BitWord *B, unsigned NumWords, bool t) {
447 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
450 template<bool AddBits, bool InvertMask>
451 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
452 assert(BITWORD_SIZE % 32 == 0 && "Unsupported BitWord size.");
453 MaskWords = std::min(MaskWords, (size() + 31) / 32);
454 const unsigned Scale = BITWORD_SIZE / 32;
456 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
457 BitWord BW = Bits[i];
458 // This inner loop should unroll completely when BITWORD_SIZE > 32.
459 for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
460 uint32_t M = *Mask++;
461 if (InvertMask) M = ~M;
462 if (AddBits) BW |= BitWord(M) << b;
463 else BW &= ~(BitWord(M) << b);
467 for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
468 uint32_t M = *Mask++;
469 if (InvertMask) M = ~M;
470 if (AddBits) Bits[i] |= BitWord(M) << b;
471 else Bits[i] &= ~(BitWord(M) << b);
478 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
479 BitVector Result(LHS);
484 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
485 BitVector Result(LHS);
490 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
491 BitVector Result(LHS);
496 } // End llvm namespace
499 /// Implement std::swap in terms of BitVector swap.
501 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {