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"
22 typedef unsigned long BitWord;
24 enum { BITS_PER_WORD = sizeof(BitWord) * 8 };
26 BitWord *Bits; // Actual bits.
27 unsigned Size; // Size of bitvector in bits.
28 unsigned Capacity; // Size of allocated memory in BitWord.
31 // Encapsulation of a single bit.
33 friend class BitVector;
38 reference(); // Undefined
41 reference(BitVector &b, unsigned Idx) {
42 WordRef = &b.Bits[Idx / BITS_PER_WORD];
43 BitPos = Idx % BITS_PER_WORD;
48 reference& operator=(bool t) {
50 *WordRef |= 1L << BitPos;
52 *WordRef &= ~(1L << BitPos);
56 operator bool() const {
57 return (*WordRef) & (1L << BitPos);
62 /// BitVector default ctor - Creates an empty bitvector.
63 BitVector() : Size(0), Capacity(0) {
67 /// BitVector ctor - Creates a bitvector of specified number of bits. All
68 /// bits are initialized to the specified value.
69 explicit BitVector(unsigned s, bool t = false) : Size(s) {
70 Capacity = NumBitWords(s);
71 Bits = new BitWord[Capacity];
72 init_words(Bits, Capacity, t);
77 /// BitVector copy ctor.
78 BitVector(const BitVector &RHS) : Size(RHS.size()) {
85 Capacity = NumBitWords(RHS.size());
86 Bits = new BitWord[Capacity];
87 std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
90 /// size - Returns the number of bits in this bitvector.
91 unsigned size() const { return Size; }
93 /// count - Returns the number of bits which are set.
94 unsigned count() const {
96 for (unsigned i = 0; i < NumBitWords(size()); ++i)
97 if (sizeof(BitWord) == 4)
98 NumBits += CountPopulation_32(Bits[i]);
99 else if (sizeof(BitWord) == 8)
100 NumBits += CountPopulation_64(Bits[i]);
102 assert(0 && "Unsupported!");
106 /// any - Returns true if any bit is set.
108 for (unsigned i = 0; i < NumBitWords(size()); ++i)
114 /// none - Returns true if none of the bits are set.
119 /// find_first - Returns the index of the first set bit, -1 if none
120 /// of the bits are set.
121 int find_first() const {
122 for (unsigned i = 0; i < NumBitWords(size()); ++i)
124 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
128 /// find_next - Returns the index of the next set bit following the
129 /// "Prev" bit. Returns -1 if the next set bit is not found.
130 int find_next(unsigned Prev) const {
135 unsigned WordPos = Prev / BITS_PER_WORD;
136 unsigned BitPos = Prev % BITS_PER_WORD;
137 BitWord Copy = Bits[WordPos];
138 // Mask off previous bits.
139 Copy &= ~0 << BitPos;
142 return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
144 // Check subsequent words.
145 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
147 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
151 /// clear - Clear all bits.
156 /// resize - Grow or shrink the bitvector.
157 void resize(unsigned N, bool t = false) {
158 if (N > Capacity * BITS_PER_WORD) {
159 unsigned OldCapacity = Capacity;
161 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
167 void reserve(unsigned N) {
168 if (N > Capacity * BITS_PER_WORD)
174 init_words(Bits, Capacity, true);
179 BitVector &set(unsigned Idx) {
180 Bits[Idx / BITS_PER_WORD] |= 1L << (Idx % BITS_PER_WORD);
185 init_words(Bits, Capacity, false);
189 BitVector &reset(unsigned Idx) {
190 Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
195 for (unsigned i = 0; i < NumBitWords(size()); ++i)
201 BitVector &flip(unsigned Idx) {
202 Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
207 BitVector operator~() const {
208 return BitVector(*this).flip();
212 reference operator[](unsigned Idx) {
213 return reference(*this, Idx);
216 bool operator[](unsigned Idx) const {
217 BitWord Mask = 1L << (Idx % BITS_PER_WORD);
218 return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
221 bool test(unsigned Idx) const {
225 // Comparison operators.
226 bool operator==(const BitVector &RHS) const {
227 if (Size != RHS.Size)
230 for (unsigned i = 0; i < NumBitWords(size()); ++i)
231 if (Bits[i] != RHS.Bits[i])
236 bool operator!=(const BitVector &RHS) const {
237 return !(*this == RHS);
240 // Intersection, union, disjoint union.
241 BitVector operator&=(const BitVector &RHS) {
242 assert(Size == RHS.Size && "Illegal operation!");
243 for (unsigned i = 0; i < NumBitWords(size()); ++i)
244 Bits[i] &= RHS.Bits[i];
248 BitVector operator|=(const BitVector &RHS) {
249 assert(Size == RHS.Size && "Illegal operation!");
250 for (unsigned i = 0; i < NumBitWords(size()); ++i)
251 Bits[i] |= RHS.Bits[i];
255 BitVector operator^=(const BitVector &RHS) {
256 assert(Size == RHS.Size && "Illegal operation!");
257 for (unsigned i = 0; i < NumBitWords(size()); ++i)
258 Bits[i] ^= RHS.Bits[i];
262 // Assignment operator.
263 const BitVector &operator=(const BitVector &RHS) {
264 if (this == &RHS) return *this;
267 unsigned RHSWords = NumBitWords(Size);
268 if (Size <= Capacity * BITS_PER_WORD) {
269 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
274 // Grow the bitvector to have enough elements.
275 Capacity = NumBitWords(Size);
276 BitWord *NewBits = new BitWord[Capacity];
277 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
279 // Destroy the old bits.
287 unsigned NumBitWords(unsigned S) const {
288 return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
291 // Clear the unused top bits in the high word.
292 void clear_unused_bits() {
294 unsigned ExtraBits = Size % BITS_PER_WORD;
295 unsigned index = Size / BITS_PER_WORD;
296 if (Size % BITS_PER_WORD == 0)
298 Bits[index] &= ~(~0 << ExtraBits);
302 void grow(unsigned NewSize) {
303 unsigned OldCapacity = Capacity;
304 Capacity = NumBitWords(NewSize);
305 BitWord *NewBits = new BitWord[Capacity];
307 // Copy the old bits over.
308 if (OldCapacity != 0)
309 std::copy(Bits, &Bits[OldCapacity], NewBits);
311 // Destroy the old bits.
316 void init_words(BitWord *B, unsigned NumWords, bool t) {
317 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
321 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
322 BitVector Result(LHS);
327 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
328 BitVector Result(LHS);
333 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
334 BitVector Result(LHS);
339 } // End llvm namespace