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 NumBits = CountPopulation_32(Bits[i]);
108 /// any - Returns true if any bit is set.
110 for (unsigned i = 0; i < NumBitWords(size()); ++i)
116 /// none - Returns true if none of the bits are set.
121 /// find_first - Returns the index of the first set bit, -1 if none
122 /// of the bits are set.
123 int find_first() const {
124 for (unsigned i = 0; i < NumBitWords(size()); ++i)
126 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
130 /// find_next - Returns the index of the next set bit following the
131 /// "Prev" bit. Returns -1 if the next set bit is not found.
132 int find_next(unsigned Prev) const {
137 unsigned WordPos = Prev / BITS_PER_WORD;
138 unsigned BitPos = Prev % BITS_PER_WORD;
139 BitWord Copy = Bits[WordPos];
140 // Mask off previous bits.
141 Copy &= ~0 << BitPos;
144 return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
146 // Check subsequent words.
147 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
149 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
153 /// clear - Clear all bits.
162 /// resize - Grow or shrink the bitvector.
163 void resize(unsigned N) {
164 if (N > Capacity * BITS_PER_WORD) {
165 unsigned OldCapacity = Capacity;
167 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), false);
172 void resize(unsigned N, bool t) {
173 if (N > Capacity * BITS_PER_WORD) {
174 unsigned OldCapacity = Capacity;
176 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
182 void reserve(unsigned N) {
183 if (N > Capacity * BITS_PER_WORD)
190 init_words(Bits, Capacity, true);
196 BitVector &set(unsigned Idx) {
197 Bits[Idx / BITS_PER_WORD] |= 1L << (Idx % BITS_PER_WORD);
203 init_words(Bits, Capacity, false);
207 BitVector &reset(unsigned Idx) {
208 Bits[Idx / BITS_PER_WORD] &= ~(1L << (Idx % BITS_PER_WORD));
213 for (unsigned i = 0; i < NumBitWords(size()); ++i)
219 BitVector &flip(unsigned Idx) {
220 Bits[Idx / BITS_PER_WORD] ^= 1L << (Idx % BITS_PER_WORD);
225 BitVector operator~() const {
226 return BitVector(*this).flip();
230 reference operator[](unsigned Idx) {
231 return reference(*this, Idx);
234 bool operator[](unsigned Idx) const {
235 BitWord Mask = 1L << (Idx % BITS_PER_WORD);
236 return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
239 bool test(unsigned Idx) const {
243 // Comparison operators.
244 bool operator==(const BitVector &RHS) const {
245 assert(Size == RHS.Size && "Illegal operation!");
246 for (unsigned i = 0; i < NumBitWords(size()); ++i)
247 if (Bits[i] != RHS.Bits[i])
252 bool operator!=(const BitVector &RHS) const {
253 return !(*this == RHS);
256 // Intersection, union, disjoint union.
257 BitVector operator&=(const BitVector &RHS) {
258 assert(Size == RHS.Size && "Illegal operation!");
259 for (unsigned i = 0; i < NumBitWords(size()); ++i)
260 Bits[i] &= RHS.Bits[i];
264 BitVector operator|=(const BitVector &RHS) {
265 assert(Size == RHS.Size && "Illegal operation!");
266 for (unsigned i = 0; i < NumBitWords(size()); ++i)
267 Bits[i] |= RHS.Bits[i];
271 BitVector operator^=(const BitVector &RHS) {
272 assert(Size == RHS.Size && "Illegal operation!");
273 for (unsigned i = 0; i < NumBitWords(size()); ++i)
274 Bits[i] ^= RHS.Bits[i];
278 // Assignment operator.
279 const BitVector &operator=(const BitVector &RHS) {
280 if (this == &RHS) return *this;
283 unsigned RHSWords = NumBitWords(Size);
284 if (Size <= Capacity * BITS_PER_WORD) {
285 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
290 // Grow the bitvector to have enough elements.
291 Capacity = NumBitWords(Size);
292 BitWord *NewBits = new BitWord[Capacity];
293 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
295 // Destroy the old bits.
303 unsigned NumBitWords(unsigned S) const {
304 return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
307 // Clear the unused top bits in the high word.
308 void clear_unused_bits() {
310 unsigned ExtraBits = Size % BITS_PER_WORD;
311 Bits[Size / BITS_PER_WORD] &= ~(~0 << ExtraBits);
315 void grow(unsigned NewSize) {
316 unsigned OldCapacity = Capacity;
317 Capacity = NumBitWords(NewSize);
318 BitWord *NewBits = new BitWord[Capacity];
320 // Copy the old bits over.
321 if (OldCapacity != 0)
322 std::copy(Bits, &Bits[OldCapacity], NewBits);
324 // Destroy the old bits.
330 void init_words(BitWord *B, unsigned NumWords, bool t) {
332 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
336 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
337 BitVector Result(LHS);
342 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
343 BitVector Result(LHS);
348 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
349 BitVector Result(LHS);
354 } // End llvm namespace