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 reference& operator=(const reference& rhs) {
57 if (*rhs.WordRef & (1 << rhs.BitPos))
58 *WordRef |= 1L << BitPos;
60 *WordRef &= ~(1L << BitPos);
64 operator bool() const {
65 return (*WordRef) & (1L << BitPos);
70 /// BitVector default ctor - Creates an empty bitvector.
71 BitVector() : Size(0), Capacity(0) {
75 /// BitVector ctor - Creates a bitvector of specified number of bits. All
76 /// bits are initialized to the specified value.
77 explicit BitVector(unsigned s, bool t = false) : Size(s) {
78 Capacity = NumBitWords(s);
79 Bits = new BitWord[Capacity];
80 init_words(Bits, Capacity, t);
85 /// BitVector copy ctor.
86 BitVector(const BitVector &RHS) : Size(RHS.size()) {
92 Capacity = NumBitWords(RHS.size());
93 Bits = new BitWord[Capacity];
94 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 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
135 /// find_next - Returns the index of the next set bit following the
136 /// "Prev" bit. Returns -1 if the next set bit is not found.
137 int find_next(unsigned Prev) const {
142 unsigned WordPos = Prev / BITS_PER_WORD;
143 unsigned BitPos = Prev % BITS_PER_WORD;
144 BitWord Copy = Bits[WordPos];
145 // Mask off previous bits.
146 Copy &= ~0 << BitPos;
149 return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
151 // Check subsequent words.
152 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
154 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
158 /// clear - Clear all bits.
163 /// resize - Grow or shrink the bitvector.
164 void resize(unsigned N, bool t = false) {
165 if (N > Capacity * BITS_PER_WORD) {
166 unsigned OldCapacity = Capacity;
168 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
175 void reserve(unsigned N) {
176 if (N > Capacity * BITS_PER_WORD)
183 init_words(Bits, Capacity, true);
189 BitVector &set(unsigned Idx) {
190 Bits[Idx / BITS_PER_WORD] |= 1L << (Idx % BITS_PER_WORD);
196 init_words(Bits, Capacity, false);
200 BitVector &reset(unsigned Idx) {
201 Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
206 for (unsigned i = 0; i < NumBitWords(size()); ++i)
212 BitVector &flip(unsigned Idx) {
213 Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
218 BitVector operator~() const {
219 return BitVector(*this).flip();
223 reference operator[](unsigned Idx) {
224 return reference(*this, Idx);
227 bool operator[](unsigned Idx) const {
228 BitWord Mask = 1L << (Idx % BITS_PER_WORD);
229 return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
232 bool test(unsigned Idx) const {
236 // Comparison operators.
237 bool operator==(const BitVector &RHS) const {
238 if (Size != RHS.Size)
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;
278 unsigned RHSWords = NumBitWords(Size);
279 if (Size <= Capacity * BITS_PER_WORD) {
280 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
285 // Grow the bitvector to have enough elements.
286 Capacity = NumBitWords(Size);
287 BitWord *NewBits = new BitWord[Capacity];
288 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
290 // Destroy the old bits.
298 unsigned NumBitWords(unsigned S) const {
299 return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
302 // Clear the unused top bits in the high word.
303 void clear_unused_bits() {
305 unsigned ExtraBits = Size % BITS_PER_WORD;
306 Bits[Size / BITS_PER_WORD] &= ~(~0 << ExtraBits);
310 void grow(unsigned NewSize) {
311 unsigned OldCapacity = Capacity;
312 Capacity = NumBitWords(NewSize);
313 BitWord *NewBits = new BitWord[Capacity];
315 // Copy the old bits over.
316 if (OldCapacity != 0)
317 std::copy(Bits, &Bits[OldCapacity], NewBits);
319 // Destroy the old bits.
325 void init_words(BitWord *B, unsigned NumWords, bool t) {
327 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
331 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
332 BitVector Result(LHS);
337 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
338 BitVector Result(LHS);
343 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
344 BitVector Result(LHS);
349 } // End llvm namespace