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"
24 typedef unsigned long BitWord;
26 enum { BITS_PER_WORD = sizeof(BitWord) * 8 };
28 BitWord *Bits; // Actual bits.
29 unsigned Size; // Size of bitvector in bits.
30 unsigned Capacity; // Size of allocated memory in BitWord.
33 // Encapsulation of a single bit.
35 friend class BitVector;
40 reference(); // Undefined
43 reference(BitVector &b, unsigned Idx) {
44 WordRef = &b.Bits[Idx / BITS_PER_WORD];
45 BitPos = Idx % BITS_PER_WORD;
50 reference& operator=(bool t) {
52 *WordRef |= 1L << BitPos;
54 *WordRef &= ~(1L << BitPos);
58 operator bool() const {
59 return (*WordRef) & (1L << BitPos);
64 /// BitVector default ctor - Creates an empty bitvector.
65 BitVector() : Size(0), Capacity(0) {
69 /// BitVector ctor - Creates a bitvector of specified number of bits. All
70 /// bits are initialized to the specified value.
71 explicit BitVector(unsigned s, bool t = false) : Size(s) {
72 Capacity = NumBitWords(s);
73 Bits = new BitWord[Capacity];
74 init_words(Bits, Capacity, t);
79 /// BitVector copy ctor.
80 BitVector(const BitVector &RHS) : Size(RHS.size()) {
87 Capacity = NumBitWords(RHS.size());
88 Bits = new BitWord[Capacity];
89 std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
96 /// size - Returns the number of bits in this bitvector.
97 unsigned size() const { return Size; }
99 /// count - Returns the number of bits which are set.
100 unsigned count() const {
101 unsigned NumBits = 0;
102 for (unsigned i = 0; i < NumBitWords(size()); ++i)
103 if (sizeof(BitWord) == 4)
104 NumBits += CountPopulation_32(Bits[i]);
105 else if (sizeof(BitWord) == 8)
106 NumBits += CountPopulation_64(Bits[i]);
108 assert(0 && "Unsupported!");
112 /// any - Returns true if any bit is set.
114 for (unsigned i = 0; i < NumBitWords(size()); ++i)
120 /// none - Returns true if none of the bits are set.
125 /// find_first - Returns the index of the first set bit, -1 if none
126 /// of the bits are set.
127 int find_first() const {
128 for (unsigned i = 0; i < NumBitWords(size()); ++i)
130 if (sizeof(BitWord) == 4)
131 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
132 else if (sizeof(BitWord) == 8)
133 return i * BITS_PER_WORD + CountTrailingZeros_64(Bits[i]);
135 assert(0 && "Unsupported!");
140 /// find_next - Returns the index of the next set bit following the
141 /// "Prev" bit. Returns -1 if the next set bit is not found.
142 int find_next(unsigned Prev) const {
147 unsigned WordPos = Prev / BITS_PER_WORD;
148 unsigned BitPos = Prev % BITS_PER_WORD;
149 BitWord Copy = Bits[WordPos];
150 // Mask off previous bits.
151 Copy &= ~0L << BitPos;
154 if (sizeof(BitWord) == 4)
155 return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
156 else if (sizeof(BitWord) == 8)
157 return WordPos * BITS_PER_WORD + CountTrailingZeros_64(Copy);
159 assert(0 && "Unsupported!");
162 // Check subsequent words.
163 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
165 if (sizeof(BitWord) == 4)
166 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
167 else if (sizeof(BitWord) == 8)
168 return i * BITS_PER_WORD + CountTrailingZeros_64(Bits[i]);
170 assert(0 && "Unsupported!");
175 /// clear - Clear all bits.
180 /// resize - Grow or shrink the bitvector.
181 void resize(unsigned N, bool t = false) {
182 if (N > Capacity * BITS_PER_WORD) {
183 unsigned OldCapacity = Capacity;
185 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
191 void reserve(unsigned N) {
192 if (N > Capacity * BITS_PER_WORD)
198 init_words(Bits, Capacity, true);
203 BitVector &set(unsigned Idx) {
204 Bits[Idx / BITS_PER_WORD] |= 1L << (Idx % BITS_PER_WORD);
209 init_words(Bits, Capacity, false);
213 BitVector &reset(unsigned Idx) {
214 Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
219 for (unsigned i = 0; i < NumBitWords(size()); ++i)
225 BitVector &flip(unsigned Idx) {
226 Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
231 BitVector operator~() const {
232 return BitVector(*this).flip();
236 reference operator[](unsigned Idx) {
237 return reference(*this, Idx);
240 bool operator[](unsigned Idx) const {
241 BitWord Mask = 1L << (Idx % BITS_PER_WORD);
242 return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
245 bool test(unsigned Idx) const {
249 // Comparison operators.
250 bool operator==(const BitVector &RHS) const {
251 if (Size != RHS.Size)
254 for (unsigned i = 0; i < NumBitWords(size()); ++i)
255 if (Bits[i] != RHS.Bits[i])
260 bool operator!=(const BitVector &RHS) const {
261 return !(*this == RHS);
264 // Intersection, union, disjoint union.
265 BitVector operator&=(const BitVector &RHS) {
266 assert(Size == RHS.Size && "Illegal operation!");
267 for (unsigned i = 0; i < NumBitWords(size()); ++i)
268 Bits[i] &= RHS.Bits[i];
272 BitVector operator|=(const BitVector &RHS) {
273 assert(Size == RHS.Size && "Illegal operation!");
274 for (unsigned i = 0; i < NumBitWords(size()); ++i)
275 Bits[i] |= RHS.Bits[i];
279 BitVector operator^=(const BitVector &RHS) {
280 assert(Size == RHS.Size && "Illegal operation!");
281 for (unsigned i = 0; i < NumBitWords(size()); ++i)
282 Bits[i] ^= RHS.Bits[i];
286 // Assignment operator.
287 const BitVector &operator=(const BitVector &RHS) {
288 if (this == &RHS) return *this;
291 unsigned RHSWords = NumBitWords(Size);
292 if (Size <= Capacity * BITS_PER_WORD) {
293 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
298 // Grow the bitvector to have enough elements.
299 Capacity = NumBitWords(Size);
300 BitWord *NewBits = new BitWord[Capacity];
301 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
303 // Destroy the old bits.
311 unsigned NumBitWords(unsigned S) const {
312 return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
315 // Clear the unused top bits in the high word.
316 void clear_unused_bits() {
317 unsigned ExtraBits = Size % BITS_PER_WORD;
319 unsigned index = Size / BITS_PER_WORD;
320 Bits[index] &= ~(~0L << ExtraBits);
324 void grow(unsigned NewSize) {
325 unsigned OldCapacity = Capacity;
326 Capacity = NumBitWords(NewSize);
327 BitWord *NewBits = new BitWord[Capacity];
329 // Copy the old bits over.
330 if (OldCapacity != 0)
331 std::copy(Bits, &Bits[OldCapacity], NewBits);
333 // Destroy the old bits.
338 void init_words(BitWord *B, unsigned NumWords, bool t) {
339 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
343 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
344 BitVector Result(LHS);
349 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
350 BitVector Result(LHS);
355 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
356 BitVector Result(LHS);
361 } // End llvm namespace