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/Compiler.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/MathExtras.h"
28 typedef unsigned long BitWord;
30 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
32 static_assert(BITWORD_SIZE == 64 || BITWORD_SIZE == 32,
33 "Unsupported word size");
35 BitWord *Bits; // Actual bits.
36 unsigned Size; // Size of bitvector in bits.
37 unsigned Capacity; // Size of allocated memory in BitWord.
40 typedef unsigned size_type;
41 // Encapsulation of a single bit.
43 friend class BitVector;
48 reference(); // Undefined
51 reference(BitVector &b, unsigned Idx) {
52 WordRef = &b.Bits[Idx / BITWORD_SIZE];
53 BitPos = Idx % BITWORD_SIZE;
58 reference &operator=(reference t) {
63 reference& operator=(bool t) {
65 *WordRef |= BitWord(1) << BitPos;
67 *WordRef &= ~(BitWord(1) << BitPos);
71 operator bool() const {
72 return ((*WordRef) & (BitWord(1) << BitPos)) ? true : false;
77 /// BitVector default ctor - Creates an empty bitvector.
78 BitVector() : Size(0), Capacity(0) {
82 /// BitVector ctor - Creates a bitvector of specified number of bits. All
83 /// bits are initialized to the specified value.
84 explicit BitVector(unsigned s, bool t = false) : Size(s) {
85 Capacity = NumBitWords(s);
86 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
87 init_words(Bits, Capacity, t);
92 /// BitVector copy ctor.
93 BitVector(const BitVector &RHS) : Size(RHS.size()) {
100 Capacity = NumBitWords(RHS.size());
101 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
102 std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
105 BitVector(BitVector &&RHS)
106 : Bits(RHS.Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
114 /// empty - Tests whether there are no bits in this bitvector.
115 bool empty() const { return Size == 0; }
117 /// size - Returns the number of bits in this bitvector.
118 size_type size() const { return Size; }
120 /// count - Returns the number of bits which are set.
121 size_type count() const {
122 unsigned NumBits = 0;
123 for (unsigned i = 0; i < NumBitWords(size()); ++i)
124 NumBits += countPopulation(Bits[i]);
128 /// any - Returns true if any bit is set.
130 for (unsigned i = 0; i < NumBitWords(size()); ++i)
136 /// all - Returns true if all bits are set.
138 for (unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
142 // If bits remain check that they are ones. The unused bits are always zero.
143 if (unsigned Remainder = Size % BITWORD_SIZE)
144 return Bits[Size / BITWORD_SIZE] == (1UL << Remainder) - 1;
149 /// none - Returns true if none of the bits are set.
154 /// find_first - Returns the index of the first set bit, -1 if none
155 /// of the bits are set.
156 int find_first() const {
157 for (unsigned i = 0; i < NumBitWords(size()); ++i)
159 return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
163 /// find_next - Returns the index of the next set bit following the
164 /// "Prev" bit. Returns -1 if the next set bit is not found.
165 int find_next(unsigned Prev) const {
170 unsigned WordPos = Prev / BITWORD_SIZE;
171 unsigned BitPos = Prev % BITWORD_SIZE;
172 BitWord Copy = Bits[WordPos];
173 // Mask off previous bits.
174 Copy &= ~0UL << BitPos;
177 return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
179 // Check subsequent words.
180 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
182 return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
186 /// clear - Clear all bits.
191 /// resize - Grow or shrink the bitvector.
192 void resize(unsigned N, bool t = false) {
193 if (N > Capacity * BITWORD_SIZE) {
194 unsigned OldCapacity = Capacity;
196 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
199 // Set any old unused bits that are now included in the BitVector. This
200 // may set bits that are not included in the new vector, but we will clear
201 // them back out below.
205 // Update the size, and clear out any bits that are now unused
206 unsigned OldSize = Size;
208 if (t || N < OldSize)
212 void reserve(unsigned N) {
213 if (N > Capacity * BITWORD_SIZE)
219 init_words(Bits, Capacity, true);
224 BitVector &set(unsigned Idx) {
225 assert(Bits && "Bits never allocated");
226 Bits[Idx / BITWORD_SIZE] |= BitWord(1) << (Idx % BITWORD_SIZE);
230 /// set - Efficiently set a range of bits in [I, E)
231 BitVector &set(unsigned I, unsigned E) {
232 assert(I <= E && "Attempted to set backwards range!");
233 assert(E <= size() && "Attempted to set out-of-bounds range!");
235 if (I == E) return *this;
237 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
238 BitWord EMask = 1UL << (E % BITWORD_SIZE);
239 BitWord IMask = 1UL << (I % BITWORD_SIZE);
240 BitWord Mask = EMask - IMask;
241 Bits[I / BITWORD_SIZE] |= Mask;
245 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
246 Bits[I / BITWORD_SIZE] |= PrefixMask;
247 I = RoundUpToAlignment(I, BITWORD_SIZE);
249 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
250 Bits[I / BITWORD_SIZE] = ~0UL;
252 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
254 Bits[I / BITWORD_SIZE] |= PostfixMask;
260 init_words(Bits, Capacity, false);
264 BitVector &reset(unsigned Idx) {
265 Bits[Idx / BITWORD_SIZE] &= ~(BitWord(1) << (Idx % BITWORD_SIZE));
269 /// reset - Efficiently reset a range of bits in [I, E)
270 BitVector &reset(unsigned I, unsigned E) {
271 assert(I <= E && "Attempted to reset backwards range!");
272 assert(E <= size() && "Attempted to reset out-of-bounds range!");
274 if (I == E) return *this;
276 if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
277 BitWord EMask = 1UL << (E % BITWORD_SIZE);
278 BitWord IMask = 1UL << (I % BITWORD_SIZE);
279 BitWord Mask = EMask - IMask;
280 Bits[I / BITWORD_SIZE] &= ~Mask;
284 BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
285 Bits[I / BITWORD_SIZE] &= ~PrefixMask;
286 I = RoundUpToAlignment(I, BITWORD_SIZE);
288 for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
289 Bits[I / BITWORD_SIZE] = 0UL;
291 BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
293 Bits[I / BITWORD_SIZE] &= ~PostfixMask;
299 for (unsigned i = 0; i < NumBitWords(size()); ++i)
305 BitVector &flip(unsigned Idx) {
306 Bits[Idx / BITWORD_SIZE] ^= BitWord(1) << (Idx % BITWORD_SIZE);
311 reference operator[](unsigned Idx) {
312 assert (Idx < Size && "Out-of-bounds Bit access.");
313 return reference(*this, Idx);
316 bool operator[](unsigned Idx) const {
317 assert (Idx < Size && "Out-of-bounds Bit access.");
318 BitWord Mask = BitWord(1) << (Idx % BITWORD_SIZE);
319 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
322 bool test(unsigned Idx) const {
326 /// Test if any common bits are set.
327 bool anyCommon(const BitVector &RHS) const {
328 unsigned ThisWords = NumBitWords(size());
329 unsigned RHSWords = NumBitWords(RHS.size());
330 for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
331 if (Bits[i] & RHS.Bits[i])
336 // Comparison operators.
337 bool operator==(const BitVector &RHS) const {
338 unsigned ThisWords = NumBitWords(size());
339 unsigned RHSWords = NumBitWords(RHS.size());
341 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
342 if (Bits[i] != RHS.Bits[i])
345 // Verify that any extra words are all zeros.
346 if (i != ThisWords) {
347 for (; i != ThisWords; ++i)
350 } else if (i != RHSWords) {
351 for (; i != RHSWords; ++i)
358 bool operator!=(const BitVector &RHS) const {
359 return !(*this == RHS);
362 /// Intersection, union, disjoint union.
363 BitVector &operator&=(const BitVector &RHS) {
364 unsigned ThisWords = NumBitWords(size());
365 unsigned RHSWords = NumBitWords(RHS.size());
367 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
368 Bits[i] &= RHS.Bits[i];
370 // Any bits that are just in this bitvector become zero, because they aren't
371 // in the RHS bit vector. Any words only in RHS are ignored because they
372 // are already zero in the LHS.
373 for (; i != ThisWords; ++i)
379 /// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
380 BitVector &reset(const BitVector &RHS) {
381 unsigned ThisWords = NumBitWords(size());
382 unsigned RHSWords = NumBitWords(RHS.size());
384 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
385 Bits[i] &= ~RHS.Bits[i];
389 /// test - Check if (This - RHS) is zero.
390 /// This is the same as reset(RHS) and any().
391 bool test(const BitVector &RHS) const {
392 unsigned ThisWords = NumBitWords(size());
393 unsigned RHSWords = NumBitWords(RHS.size());
395 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
396 if ((Bits[i] & ~RHS.Bits[i]) != 0)
399 for (; i != ThisWords ; ++i)
406 BitVector &operator|=(const BitVector &RHS) {
407 if (size() < RHS.size())
409 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
410 Bits[i] |= RHS.Bits[i];
414 BitVector &operator^=(const BitVector &RHS) {
415 if (size() < RHS.size())
417 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
418 Bits[i] ^= RHS.Bits[i];
422 // Assignment operator.
423 const BitVector &operator=(const BitVector &RHS) {
424 if (this == &RHS) return *this;
427 unsigned RHSWords = NumBitWords(Size);
428 if (Size <= Capacity * BITWORD_SIZE) {
430 std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
435 // Grow the bitvector to have enough elements.
437 assert(Capacity > 0 && "negative capacity?");
438 BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
439 std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
441 // Destroy the old bits.
448 const BitVector &operator=(BitVector &&RHS) {
449 if (this == &RHS) return *this;
454 Capacity = RHS.Capacity;
461 void swap(BitVector &RHS) {
462 std::swap(Bits, RHS.Bits);
463 std::swap(Size, RHS.Size);
464 std::swap(Capacity, RHS.Capacity);
467 //===--------------------------------------------------------------------===//
468 // Portable bit mask operations.
469 //===--------------------------------------------------------------------===//
471 // These methods all operate on arrays of uint32_t, each holding 32 bits. The
472 // fixed word size makes it easier to work with literal bit vector constants
475 // The LSB in each word is the lowest numbered bit. The size of a portable
476 // bit mask is always a whole multiple of 32 bits. If no bit mask size is
477 // given, the bit mask is assumed to cover the entire BitVector.
479 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
480 /// This computes "*this |= Mask".
481 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
482 applyMask<true, false>(Mask, MaskWords);
485 /// clearBitsInMask - Clear any bits in this vector that are set in Mask.
486 /// Don't resize. This computes "*this &= ~Mask".
487 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
488 applyMask<false, false>(Mask, MaskWords);
491 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
492 /// Don't resize. This computes "*this |= ~Mask".
493 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
494 applyMask<true, true>(Mask, MaskWords);
497 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
498 /// Don't resize. This computes "*this &= Mask".
499 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
500 applyMask<false, true>(Mask, MaskWords);
504 unsigned NumBitWords(unsigned S) const {
505 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
508 // Set the unused bits in the high words.
509 void set_unused_bits(bool t = true) {
510 // Set high words first.
511 unsigned UsedWords = NumBitWords(Size);
512 if (Capacity > UsedWords)
513 init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
515 // Then set any stray high bits of the last used word.
516 unsigned ExtraBits = Size % BITWORD_SIZE;
518 BitWord ExtraBitMask = ~0UL << ExtraBits;
520 Bits[UsedWords-1] |= ExtraBitMask;
522 Bits[UsedWords-1] &= ~ExtraBitMask;
526 // Clear the unused bits in the high words.
527 void clear_unused_bits() {
528 set_unused_bits(false);
531 void grow(unsigned NewSize) {
532 Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
533 assert(Capacity > 0 && "realloc-ing zero space");
534 Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
539 void init_words(BitWord *B, unsigned NumWords, bool t) {
540 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
543 template<bool AddBits, bool InvertMask>
544 void applyMask(const uint32_t *Mask, unsigned MaskWords) {
545 static_assert(BITWORD_SIZE % 32 == 0, "Unsupported BitWord size.");
546 MaskWords = std::min(MaskWords, (size() + 31) / 32);
547 const unsigned Scale = BITWORD_SIZE / 32;
549 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
550 BitWord BW = Bits[i];
551 // This inner loop should unroll completely when BITWORD_SIZE > 32.
552 for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
553 uint32_t M = *Mask++;
554 if (InvertMask) M = ~M;
555 if (AddBits) BW |= BitWord(M) << b;
556 else BW &= ~(BitWord(M) << b);
560 for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
561 uint32_t M = *Mask++;
562 if (InvertMask) M = ~M;
563 if (AddBits) Bits[i] |= BitWord(M) << b;
564 else Bits[i] &= ~(BitWord(M) << b);
571 } // End llvm namespace
574 /// Implement std::swap in terms of BitVector swap.
576 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {