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 BitVector &operator|=(const BitVector &RHS) {
322 if (size() < RHS.size())
324 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
325 Bits[i] |= RHS.Bits[i];
329 BitVector &operator^=(const BitVector &RHS) {
330 if (size() < RHS.size())
332 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
333 Bits[i] ^= RHS.Bits[i];
337 // Assignment operator.
338 const BitVector &operator=(const BitVector &RHS) {
339 if (this == &RHS) return *this;
342 unsigned RHSWords = NumBitWords(Size);
343 if (Size <= Capacity * BITWORD_SIZE) {
345 std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
350 // Grow the bitvector to have enough elements.
352 BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
353 std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
355 // Destroy the old bits.
362 void swap(BitVector &RHS) {
363 std::swap(Bits, RHS.Bits);
364 std::swap(Size, RHS.Size);
365 std::swap(Capacity, RHS.Capacity);
369 unsigned NumBitWords(unsigned S) const {
370 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
373 // Set the unused bits in the high words.
374 void set_unused_bits(bool t = true) {
375 // Set high words first.
376 unsigned UsedWords = NumBitWords(Size);
377 if (Capacity > UsedWords)
378 init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
380 // Then set any stray high bits of the last used word.
381 unsigned ExtraBits = Size % BITWORD_SIZE;
383 Bits[UsedWords-1] &= ~(~0L << ExtraBits);
384 Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits;
388 // Clear the unused bits in the high words.
389 void clear_unused_bits() {
390 set_unused_bits(false);
393 void grow(unsigned NewSize) {
394 Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
395 Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
400 void init_words(BitWord *B, unsigned NumWords, bool t) {
401 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
405 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
406 BitVector Result(LHS);
411 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
412 BitVector Result(LHS);
417 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
418 BitVector Result(LHS);
423 } // End llvm namespace
426 /// Implement std::swap in terms of BitVector swap.
428 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {