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()) {
84 Capacity = NumBitWords(RHS.size());
85 Bits = new BitWord[Capacity];
86 std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
89 /// size - Returns the number of bits in this bitvector.
90 unsigned size() const { return Size; }
92 /// count - Returns the number of bits which are set.
93 unsigned count() const {
95 for (unsigned i = 0; i < NumBitWords(size()); ++i)
96 if (sizeof(BitWord) == 4)
97 NumBits += CountPopulation_32(Bits[i]);
98 else if (sizeof(BitWord) == 8)
99 NumBits += CountPopulation_64(Bits[i]);
101 assert(0 && "Unsupported!")
105 /// any - Returns true if any bit is set.
107 for (unsigned i = 0; i < NumBitWords(size()); ++i)
113 /// none - Returns true if none of the bits are set.
118 /// find_first - Returns the index of the first set bit, -1 if none
119 /// of the bits are set.
120 int find_first() const {
121 for (unsigned i = 0; i < NumBitWords(size()); ++i)
123 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
127 /// find_next - Returns the index of the next set bit following the
128 /// "Prev" bit. Returns -1 if the next set bit is not found.
129 int find_next(unsigned Prev) const {
134 unsigned WordPos = Prev / BITS_PER_WORD;
135 unsigned BitPos = Prev % BITS_PER_WORD;
136 BitWord Copy = Bits[WordPos];
137 // Mask off previous bits.
138 Copy &= ~0 << BitPos;
141 return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
143 // Check subsequent words.
144 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
146 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
150 /// clear - Clear all bits.
155 /// resize - Grow or shrink the bitvector.
156 void resize(unsigned N, bool t = false) {
157 if (N > Capacity * BITS_PER_WORD) {
158 unsigned OldCapacity = Capacity;
160 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 Bits[Size / BITS_PER_WORD] &= ~(~0 << ExtraBits);
299 void grow(unsigned NewSize) {
300 unsigned OldCapacity = Capacity;
301 Capacity = NumBitWords(NewSize);
302 BitWord *NewBits = new BitWord[Capacity];
304 // Copy the old bits over.
305 if (OldCapacity != 0)
306 std::copy(Bits, &Bits[OldCapacity], NewBits);
308 // Destroy the old bits.
313 void init_words(BitWord *B, unsigned NumWords, bool t) {
314 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
318 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
319 BitVector Result(LHS);
324 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
325 BitVector Result(LHS);
330 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
331 BitVector Result(LHS);
336 } // End llvm namespace