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 |= 1 << BitPos;
52 *WordRef &= ~(1 << BitPos);
56 reference& operator=(const reference& rhs) {
57 if (*rhs.WordRef & (1 << rhs.BitPos))
58 *WordRef |= 1 << BitPos;
60 *WordRef &= ~(1 << BitPos);
64 operator bool() const {
65 return (*WordRef) & (1 << BitPos);
70 /// BitVector default ctor - Creates an empty bitvector.
71 BitVector() : Size(0), Capacity(0) {
72 Bits = new BitWord[0];
75 /// BitVector ctor - Creates a bitvector of specified number of bits. All
76 /// bits are initialized to false;
77 BitVector(unsigned s) : Size(s) {
78 Capacity = NumBitWords(s);
79 Bits = new BitWord[Capacity];
80 init_words(Bits, Capacity, false);
83 /// BitVector ctor - Creates a bitvector of specified number of bits. All
84 /// bits are initialized to the specified value.
85 BitVector(unsigned s, bool t) : Size(s) {
86 Capacity = NumBitWords(s);
87 Bits = new BitWord[Capacity];
88 init_words(Bits, Capacity, t);
91 /// BitVector copy ctor.
92 BitVector(const BitVector &RHS) : Size(RHS.size()) {
93 Capacity = NumBitWords(RHS.size());
94 Bits = new BitWord[Capacity];
95 std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
98 /// size - Returns the number of bits in this bitvector.
99 unsigned size() const { return Size; }
101 /// count - Returns the number of bits which are set.
102 unsigned count() const {
103 unsigned NumBits = 0;
104 for (unsigned i = 0; i < NumBitWords(size()); ++i)
105 NumBits = CountPopulation_32(Bits[i]);
109 /// any - Returns true if any bit is set.
111 for (unsigned i = 0; i < NumBitWords(size()); ++i)
117 /// none - Returns true if none of the bits are set.
122 /// find_first - Returns the index of the first set bit, -1 if none
123 /// of the bits are set.
124 int find_first() const {
125 for (unsigned i = 0; i < NumBitWords(size()); ++i)
127 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
131 /// find_next - Returns the index of the next set bit following the
132 /// "Prev" bit. Returns -1 if the next set bit is not found.
133 int find_next(unsigned Prev) const {
138 unsigned WordPos = Prev / BITS_PER_WORD;
139 unsigned BitPos = Prev % BITS_PER_WORD;
140 BitWord Copy = Bits[WordPos];
141 // Mask off previous bits.
142 Copy &= ~0 << BitPos;
145 return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
147 // Check subsequent words.
148 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
150 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
154 /// clear - Clear all bits.
157 Bits = new BitWord[0];
161 /// resize - Grow or shrink the bitvector.
162 void resize(unsigned N) {
163 if (N > Capacity * BITS_PER_WORD) {
164 unsigned OldCapacity = Capacity;
166 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), false);
171 void resize(unsigned N, bool t) {
172 if (N > Capacity * BITS_PER_WORD) {
173 unsigned OldCapacity = Capacity;
175 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
180 void reserve(unsigned N) {
181 if (N > Capacity * BITS_PER_WORD)
187 init_words(Bits, Capacity, true);
192 BitVector &set(unsigned Idx) {
193 Bits[Idx / BITS_PER_WORD] |= 1 << (Idx % BITS_PER_WORD);
198 init_words(Bits, Capacity, false);
202 BitVector &reset(unsigned Idx) {
203 Bits[Idx / BITS_PER_WORD] &= ~(1 << (Idx % BITS_PER_WORD));
208 for (unsigned i = 0; i < NumBitWords(size()); ++i)
214 BitVector &flip(unsigned Idx) {
215 Bits[Idx / BITS_PER_WORD] ^= 1 << (Idx % BITS_PER_WORD);
220 BitVector operator~() const {
221 return BitVector(*this).flip();
225 reference operator[](unsigned Idx) {
226 return reference(*this, Idx);
229 bool operator[](unsigned Idx) const {
230 BitWord Mask = 1 << (Idx % BITS_PER_WORD);
231 return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
234 bool test(unsigned Idx) const {
238 // Comparison operators.
239 bool operator==(const BitVector &RHS) const {
240 assert(Size == RHS.Size && "Illegal operation!");
241 for (unsigned i = 0; i < NumBitWords(size()); ++i)
242 if (Bits[i] != RHS.Bits[i])
247 bool operator!=(const BitVector &RHS) const {
248 return !(*this == RHS);
251 // Intersection, union, disjoint union.
252 BitVector operator&=(const BitVector &RHS) {
253 assert(Size == RHS.Size && "Illegal operation!");
254 for (unsigned i = 0; i < NumBitWords(size()); ++i)
255 Bits[i] &= RHS.Bits[i];
259 BitVector operator|=(const BitVector &RHS) {
260 assert(Size == RHS.Size && "Illegal operation!");
261 for (unsigned i = 0; i < NumBitWords(size()); ++i)
262 Bits[i] |= RHS.Bits[i];
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 // Assignment operator.
274 const BitVector &operator=(const BitVector &RHS) {
275 if (this == &RHS) return *this;
277 unsigned RHSWords = NumBitWords(RHS.size());
278 unsigned NewSize = RHS.size();
279 if (NewSize <= Capacity * BITS_PER_WORD) {
280 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
286 // Grow the bitvector to have enough elements.
287 Capacity = NumBitWords(NewSize);
288 BitWord *NewBits = new BitWord[Capacity];
289 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
291 // Destroy the old bits.
299 unsigned NumBitWords(unsigned S) const {
300 return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
303 // Clear the unused top bits in the high word.
304 void clear_unused_bits() {
305 unsigned ExtraBits = Size % BITS_PER_WORD;
306 Bits[Size / BITS_PER_WORD] &= ~(~0 << ExtraBits);
309 void grow(unsigned NewSize) {
310 unsigned OldCapacity = Capacity;
311 Capacity = NumBitWords(NewSize);
312 BitWord *NewBits = new BitWord[Capacity];
314 // Copy the old bits over.
315 if (OldCapacity != 0)
316 std::copy(Bits, &Bits[OldCapacity], NewBits);
318 // Destroy the old bits.
323 void init_words(BitWord *B, unsigned NumWords, bool t) {
324 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
328 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
329 BitVector Result(LHS);
334 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
335 BitVector Result(LHS);
340 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
341 BitVector Result(LHS);
346 } // End llvm namespace