1 //===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Evan Cheng and is distributed under
6 // the University of Illinois Open Source 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"
25 typedef unsigned long BitWord;
27 enum { BITS_PER_WORD = sizeof(BitWord) * 8 };
29 BitWord *Bits; // Actual bits.
30 unsigned Size; // Size of bitvector in bits.
31 unsigned Capacity; // Size of allocated memory in BitWord.
34 // Encapsulation of a single bit.
36 friend class BitVector;
41 reference(); // Undefined
44 reference(BitVector &b, unsigned Idx) {
45 WordRef = &b.Bits[Idx / BITS_PER_WORD];
46 BitPos = Idx % BITS_PER_WORD;
51 reference& operator=(bool t) {
53 *WordRef |= 1L << BitPos;
55 *WordRef &= ~(1L << BitPos);
59 operator bool() const {
60 return (*WordRef) & (1L << BitPos);
65 /// BitVector default ctor - Creates an empty bitvector.
66 BitVector() : Size(0), Capacity(0) {
70 /// BitVector ctor - Creates a bitvector of specified number of bits. All
71 /// bits are initialized to the specified value.
72 explicit BitVector(unsigned s, bool t = false) : Size(s) {
73 Capacity = NumBitWords(s);
74 Bits = new BitWord[Capacity];
75 init_words(Bits, Capacity, t);
80 /// BitVector copy ctor.
81 BitVector(const BitVector &RHS) : Size(RHS.size()) {
88 Capacity = NumBitWords(RHS.size());
89 Bits = new BitWord[Capacity];
90 std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
97 /// size - Returns the number of bits in this bitvector.
98 unsigned size() const { return Size; }
100 /// count - Returns the number of bits which are set.
101 unsigned count() const {
102 unsigned NumBits = 0;
103 for (unsigned i = 0; i < NumBitWords(size()); ++i)
104 if (sizeof(BitWord) == 4)
105 NumBits += CountPopulation_32(Bits[i]);
106 else if (sizeof(BitWord) == 8)
107 NumBits += CountPopulation_64(Bits[i]);
109 assert(0 && "Unsupported!");
113 /// any - Returns true if any bit is set.
115 for (unsigned i = 0; i < NumBitWords(size()); ++i)
121 /// none - Returns true if none of the bits are set.
126 /// find_first - Returns the index of the first set bit, -1 if none
127 /// of the bits are set.
128 int find_first() const {
129 for (unsigned i = 0; i < NumBitWords(size()); ++i)
131 if (sizeof(BitWord) == 4)
132 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
133 else if (sizeof(BitWord) == 8)
134 return i * BITS_PER_WORD + CountTrailingZeros_64(Bits[i]);
136 assert(0 && "Unsupported!");
141 /// find_next - Returns the index of the next set bit following the
142 /// "Prev" bit. Returns -1 if the next set bit is not found.
143 int find_next(unsigned Prev) const {
148 unsigned WordPos = Prev / BITS_PER_WORD;
149 unsigned BitPos = Prev % BITS_PER_WORD;
150 BitWord Copy = Bits[WordPos];
151 // Mask off previous bits.
152 Copy &= ~0L << BitPos;
155 if (sizeof(BitWord) == 4)
156 return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
157 else if (sizeof(BitWord) == 8)
158 return WordPos * BITS_PER_WORD + CountTrailingZeros_64(Copy);
160 assert(0 && "Unsupported!");
163 // Check subsequent words.
164 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
166 if (sizeof(BitWord) == 4)
167 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
168 else if (sizeof(BitWord) == 8)
169 return i * BITS_PER_WORD + CountTrailingZeros_64(Bits[i]);
171 assert(0 && "Unsupported!");
176 /// clear - Clear all bits.
181 /// resize - Grow or shrink the bitvector.
182 void resize(unsigned N, bool t = false) {
183 if (N > Capacity * BITS_PER_WORD) {
184 unsigned OldCapacity = Capacity;
186 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
192 void reserve(unsigned N) {
193 if (N > Capacity * BITS_PER_WORD)
199 init_words(Bits, Capacity, true);
204 BitVector &set(unsigned Idx) {
205 Bits[Idx / BITS_PER_WORD] |= 1L << (Idx % BITS_PER_WORD);
210 init_words(Bits, Capacity, false);
214 BitVector &reset(unsigned Idx) {
215 Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
220 for (unsigned i = 0; i < NumBitWords(size()); ++i)
226 BitVector &flip(unsigned Idx) {
227 Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
232 BitVector operator~() const {
233 return BitVector(*this).flip();
237 reference operator[](unsigned Idx) {
238 return reference(*this, Idx);
241 bool operator[](unsigned Idx) const {
242 BitWord Mask = 1L << (Idx % BITS_PER_WORD);
243 return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
246 bool test(unsigned Idx) const {
250 // Comparison operators.
251 bool operator==(const BitVector &RHS) const {
252 if (Size != RHS.Size)
255 for (unsigned i = 0; i < NumBitWords(size()); ++i)
256 if (Bits[i] != RHS.Bits[i])
261 bool operator!=(const BitVector &RHS) const {
262 return !(*this == RHS);
265 // Intersection, union, disjoint union.
266 BitVector operator&=(const BitVector &RHS) {
267 assert(Size == RHS.Size && "Illegal operation!");
268 for (unsigned i = 0; i < NumBitWords(size()); ++i)
269 Bits[i] &= RHS.Bits[i];
273 BitVector operator|=(const BitVector &RHS) {
274 assert(Size == RHS.Size && "Illegal operation!");
275 for (unsigned i = 0; i < NumBitWords(size()); ++i)
276 Bits[i] |= RHS.Bits[i];
280 BitVector operator^=(const BitVector &RHS) {
281 assert(Size == RHS.Size && "Illegal operation!");
282 for (unsigned i = 0; i < NumBitWords(size()); ++i)
283 Bits[i] ^= RHS.Bits[i];
287 // Assignment operator.
288 const BitVector &operator=(const BitVector &RHS) {
289 if (this == &RHS) return *this;
292 unsigned RHSWords = NumBitWords(Size);
293 if (Size <= Capacity * BITS_PER_WORD) {
294 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
299 // Grow the bitvector to have enough elements.
300 Capacity = NumBitWords(Size);
301 BitWord *NewBits = new BitWord[Capacity];
302 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
304 // Destroy the old bits.
312 unsigned NumBitWords(unsigned S) const {
313 return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
316 // Clear the unused top bits in the high word.
317 void clear_unused_bits() {
318 unsigned ExtraBits = Size % BITS_PER_WORD;
320 unsigned index = Size / BITS_PER_WORD;
321 Bits[index] &= ~(~0L << ExtraBits);
325 void grow(unsigned NewSize) {
326 unsigned OldCapacity = Capacity;
327 Capacity = NumBitWords(NewSize);
328 BitWord *NewBits = new BitWord[Capacity];
330 // Copy the old bits over.
331 if (OldCapacity != 0)
332 std::copy(Bits, &Bits[OldCapacity], NewBits);
334 // Destroy the old bits.
339 void init_words(BitWord *B, unsigned NumWords, bool t) {
340 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
344 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
345 BitVector Result(LHS);
350 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
351 BitVector Result(LHS);
356 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
357 BitVector Result(LHS);
362 } // End llvm namespace