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) * CHAR_BIT };
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=(reference t) {
57 reference& operator=(bool t) {
59 *WordRef |= 1L << BitPos;
61 *WordRef &= ~(1L << BitPos);
65 operator bool() const {
66 return ((*WordRef) & (1L << BitPos)) ? true : false;
71 /// BitVector default ctor - Creates an empty bitvector.
72 BitVector() : Size(0), Capacity(0) {
76 /// BitVector ctor - Creates a bitvector of specified number of bits. All
77 /// bits are initialized to the specified value.
78 explicit BitVector(unsigned s, bool t = false) : Size(s) {
79 Capacity = NumBitWords(s);
80 Bits = new BitWord[Capacity];
81 init_words(Bits, Capacity, t);
86 /// BitVector copy ctor.
87 BitVector(const BitVector &RHS) : Size(RHS.size()) {
94 Capacity = NumBitWords(RHS.size());
95 Bits = new BitWord[Capacity];
96 std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
103 /// empty - Tests whether there are no bits in this bitvector.
104 bool empty() const { return Size == 0; }
106 /// size - Returns the number of bits in this bitvector.
107 unsigned size() const { return Size; }
109 /// count - Returns the number of bits which are set.
110 unsigned count() const {
111 unsigned NumBits = 0;
112 for (unsigned i = 0; i < NumBitWords(size()); ++i)
113 if (sizeof(BitWord) == 4)
114 NumBits += CountPopulation_32((uint32_t)Bits[i]);
115 else if (sizeof(BitWord) == 8)
116 NumBits += CountPopulation_64(Bits[i]);
118 assert(0 && "Unsupported!");
122 /// any - Returns true if any bit is set.
124 for (unsigned i = 0; i < NumBitWords(size()); ++i)
130 /// all - Returns true if all bits are set.
132 // TODO: Optimize this.
133 return count() == size();
136 /// none - Returns true if none of the bits are set.
141 /// find_first - Returns the index of the first set bit, -1 if none
142 /// of the bits are set.
143 int find_first() const {
144 for (unsigned i = 0; i < NumBitWords(size()); ++i)
146 if (sizeof(BitWord) == 4)
147 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
148 else if (sizeof(BitWord) == 8)
149 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
151 assert(0 && "Unsupported!");
156 /// find_next - Returns the index of the next set bit following the
157 /// "Prev" bit. Returns -1 if the next set bit is not found.
158 int find_next(unsigned Prev) const {
163 unsigned WordPos = Prev / BITWORD_SIZE;
164 unsigned BitPos = Prev % BITWORD_SIZE;
165 BitWord Copy = Bits[WordPos];
166 // Mask off previous bits.
167 Copy &= ~0L << BitPos;
170 if (sizeof(BitWord) == 4)
171 return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy);
172 else if (sizeof(BitWord) == 8)
173 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
175 assert(0 && "Unsupported!");
178 // Check subsequent words.
179 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
181 if (sizeof(BitWord) == 4)
182 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
183 else if (sizeof(BitWord) == 8)
184 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
186 assert(0 && "Unsupported!");
191 /// clear - Clear all bits.
196 /// resize - Grow or shrink the bitvector.
197 void resize(unsigned N, bool t = false) {
198 if (N > Capacity * BITWORD_SIZE) {
199 unsigned OldCapacity = Capacity;
201 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
204 // Set any old unused bits that are now included in the BitVector. This
205 // may set bits that are not included in the new vector, but we will clear
206 // them back out below.
210 // Update the size, and clear out any bits that are now unused
211 unsigned OldSize = Size;
213 if (t || N < OldSize)
217 void reserve(unsigned N) {
218 if (N > Capacity * BITWORD_SIZE)
224 init_words(Bits, Capacity, true);
229 BitVector &set(unsigned Idx) {
230 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
235 init_words(Bits, Capacity, false);
239 BitVector &reset(unsigned Idx) {
240 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
245 for (unsigned i = 0; i < NumBitWords(size()); ++i)
251 BitVector &flip(unsigned Idx) {
252 Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
257 BitVector operator~() const {
258 return BitVector(*this).flip();
262 reference operator[](unsigned Idx) {
263 assert (Idx < Size && "Out-of-bounds Bit access.");
264 return reference(*this, Idx);
267 bool operator[](unsigned Idx) const {
268 assert (Idx < Size && "Out-of-bounds Bit access.");
269 BitWord Mask = 1L << (Idx % BITWORD_SIZE);
270 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
273 bool test(unsigned Idx) const {
277 // Comparison operators.
278 bool operator==(const BitVector &RHS) const {
279 unsigned ThisWords = NumBitWords(size());
280 unsigned RHSWords = NumBitWords(RHS.size());
282 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
283 if (Bits[i] != RHS.Bits[i])
286 // Verify that any extra words are all zeros.
287 if (i != ThisWords) {
288 for (; i != ThisWords; ++i)
291 } else if (i != RHSWords) {
292 for (; i != RHSWords; ++i)
299 bool operator!=(const BitVector &RHS) const {
300 return !(*this == RHS);
303 // Intersection, union, disjoint union.
304 BitVector &operator&=(const BitVector &RHS) {
305 unsigned ThisWords = NumBitWords(size());
306 unsigned RHSWords = NumBitWords(RHS.size());
308 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
309 Bits[i] &= RHS.Bits[i];
311 // Any bits that are just in this bitvector become zero, because they aren't
312 // in the RHS bit vector. Any words only in RHS are ignored because they
313 // are already zero in the LHS.
314 for (; i != ThisWords; ++i)
320 BitVector &operator|=(const BitVector &RHS) {
321 if (size() < RHS.size())
323 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
324 Bits[i] |= RHS.Bits[i];
328 BitVector &operator^=(const BitVector &RHS) {
329 if (size() < RHS.size())
331 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
332 Bits[i] ^= RHS.Bits[i];
336 // Assignment operator.
337 const BitVector &operator=(const BitVector &RHS) {
338 if (this == &RHS) return *this;
341 unsigned RHSWords = NumBitWords(Size);
342 if (Size <= Capacity * BITWORD_SIZE) {
344 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
349 // Grow the bitvector to have enough elements.
351 BitWord *NewBits = new BitWord[Capacity];
352 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
354 // Destroy the old bits.
361 void swap(BitVector &RHS) {
362 std::swap(Bits, RHS.Bits);
363 std::swap(Size, RHS.Size);
364 std::swap(Capacity, RHS.Capacity);
368 unsigned NumBitWords(unsigned S) const {
369 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
372 // Set the unused bits in the high words.
373 void set_unused_bits(bool t = true) {
374 // Set high words first.
375 unsigned UsedWords = NumBitWords(Size);
376 if (Capacity > UsedWords)
377 init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
379 // Then set any stray high bits of the last used word.
380 unsigned ExtraBits = Size % BITWORD_SIZE;
382 Bits[UsedWords-1] &= ~(~0L << ExtraBits);
383 Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits;
387 // Clear the unused bits in the high words.
388 void clear_unused_bits() {
389 set_unused_bits(false);
392 void grow(unsigned NewSize) {
393 unsigned OldCapacity = Capacity;
394 Capacity = NumBitWords(NewSize);
395 BitWord *NewBits = new BitWord[Capacity];
397 // Copy the old bits over.
398 if (OldCapacity != 0)
399 std::copy(Bits, &Bits[OldCapacity], NewBits);
401 // Destroy the old bits.
408 void init_words(BitWord *B, unsigned NumWords, bool t) {
409 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
413 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
414 BitVector Result(LHS);
419 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
420 BitVector Result(LHS);
425 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
426 BitVector Result(LHS);
431 } // End llvm namespace
434 /// Implement std::swap in terms of BitVector swap.
436 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {