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=(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 /// empty - Tests whether there are no bits in this bitvector.
99 bool empty() const { return Size == 0; }
101 /// size - Returns the number of bits in this bitvector.
102 unsigned size() const { return Size; }
104 /// count - Returns the number of bits which are set.
105 unsigned count() const {
106 unsigned NumBits = 0;
107 for (unsigned i = 0; i < NumBitWords(size()); ++i)
108 if (sizeof(BitWord) == 4)
109 NumBits += CountPopulation_32((uint32_t)Bits[i]);
110 else if (sizeof(BitWord) == 8)
111 NumBits += CountPopulation_64(Bits[i]);
113 assert(0 && "Unsupported!");
117 /// any - Returns true if any bit is set.
119 for (unsigned i = 0; i < NumBitWords(size()); ++i)
125 /// none - Returns true if none of the bits are set.
130 /// find_first - Returns the index of the first set bit, -1 if none
131 /// of the bits are set.
132 int find_first() const {
133 for (unsigned i = 0; i < NumBitWords(size()); ++i)
135 if (sizeof(BitWord) == 4)
136 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
137 else if (sizeof(BitWord) == 8)
138 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
140 assert(0 && "Unsupported!");
145 /// find_next - Returns the index of the next set bit following the
146 /// "Prev" bit. Returns -1 if the next set bit is not found.
147 int find_next(unsigned Prev) const {
152 unsigned WordPos = Prev / BITWORD_SIZE;
153 unsigned BitPos = Prev % BITWORD_SIZE;
154 BitWord Copy = Bits[WordPos];
155 // Mask off previous bits.
156 Copy &= ~0L << BitPos;
159 if (sizeof(BitWord) == 4)
160 return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy);
161 else if (sizeof(BitWord) == 8)
162 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
164 assert(0 && "Unsupported!");
167 // Check subsequent words.
168 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
170 if (sizeof(BitWord) == 4)
171 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
172 else if (sizeof(BitWord) == 8)
173 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
175 assert(0 && "Unsupported!");
180 /// clear - Clear all bits.
185 /// resize - Grow or shrink the bitvector.
186 void resize(unsigned N, bool t = false) {
187 if (N > Capacity * BITWORD_SIZE) {
188 unsigned OldCapacity = Capacity;
190 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
193 // Set any old unused bits that are now included in the BitVector. This
194 // may set bits that are not included in the new vector, but we will clear
195 // them back out below.
199 // Update the size, and clear out any bits that are now unused
200 unsigned OldSize = Size;
202 if (t || N < OldSize)
206 void reserve(unsigned N) {
207 if (N > Capacity * BITWORD_SIZE)
213 init_words(Bits, Capacity, true);
218 BitVector &set(unsigned Idx) {
219 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
224 init_words(Bits, Capacity, false);
228 BitVector &reset(unsigned Idx) {
229 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
234 for (unsigned i = 0; i < NumBitWords(size()); ++i)
240 BitVector &flip(unsigned Idx) {
241 Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
246 BitVector operator~() const {
247 return BitVector(*this).flip();
251 reference operator[](unsigned Idx) {
252 assert (Idx < Size && "Out-of-bounds Bit access.");
253 return reference(*this, Idx);
256 bool operator[](unsigned Idx) const {
257 assert (Idx < Size && "Out-of-bounds Bit access.");
258 BitWord Mask = 1L << (Idx % BITWORD_SIZE);
259 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
262 bool test(unsigned Idx) const {
266 // Comparison operators.
267 bool operator==(const BitVector &RHS) const {
268 unsigned ThisWords = NumBitWords(size());
269 unsigned RHSWords = NumBitWords(RHS.size());
271 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
272 if (Bits[i] != RHS.Bits[i])
275 // Verify that any extra words are all zeros.
276 if (i != ThisWords) {
277 for (; i != ThisWords; ++i)
280 } else if (i != RHSWords) {
281 for (; i != RHSWords; ++i)
288 bool operator!=(const BitVector &RHS) const {
289 return !(*this == RHS);
292 // Intersection, union, disjoint union.
293 BitVector &operator&=(const BitVector &RHS) {
294 unsigned ThisWords = NumBitWords(size());
295 unsigned RHSWords = NumBitWords(RHS.size());
297 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
298 Bits[i] &= RHS.Bits[i];
300 // Any bits that are just in this bitvector become zero, because they aren't
301 // in the RHS bit vector. Any words only in RHS are ignored because they
302 // are already zero in the LHS.
303 for (; i != ThisWords; ++i)
309 BitVector &operator|=(const BitVector &RHS) {
310 if (size() < RHS.size())
312 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
313 Bits[i] |= RHS.Bits[i];
317 BitVector &operator^=(const BitVector &RHS) {
318 if (size() < RHS.size())
320 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
321 Bits[i] ^= RHS.Bits[i];
325 // Assignment operator.
326 const BitVector &operator=(const BitVector &RHS) {
327 if (this == &RHS) return *this;
330 unsigned RHSWords = NumBitWords(Size);
331 if (Size <= Capacity * BITWORD_SIZE) {
333 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
338 // Grow the bitvector to have enough elements.
340 BitWord *NewBits = new BitWord[Capacity];
341 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
343 // Destroy the old bits.
350 void swap(BitVector &RHS) {
351 std::swap(Bits, RHS.Bits);
352 std::swap(Size, RHS.Size);
353 std::swap(Capacity, RHS.Capacity);
357 unsigned NumBitWords(unsigned S) const {
358 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
361 // Set the unused bits in the high words.
362 void set_unused_bits(bool t = true) {
363 // Set high words first.
364 unsigned UsedWords = NumBitWords(Size);
365 if (Capacity > UsedWords)
366 init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
368 // Then set any stray high bits of the last used word.
369 unsigned ExtraBits = Size % BITWORD_SIZE;
371 Bits[UsedWords-1] &= ~(~0L << ExtraBits);
372 Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits;
376 // Clear the unused bits in the high words.
377 void clear_unused_bits() {
378 set_unused_bits(false);
381 void grow(unsigned NewSize) {
382 unsigned OldCapacity = Capacity;
383 Capacity = NumBitWords(NewSize);
384 BitWord *NewBits = new BitWord[Capacity];
386 // Copy the old bits over.
387 if (OldCapacity != 0)
388 std::copy(Bits, &Bits[OldCapacity], NewBits);
390 // Destroy the old bits.
397 void init_words(BitWord *B, unsigned NumWords, bool t) {
398 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
402 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
403 BitVector Result(LHS);
408 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
409 BitVector Result(LHS);
414 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
415 BitVector Result(LHS);
420 } // End llvm namespace
423 /// Implement std::swap in terms of BitVector swap.
425 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {