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);
92 /// BitVector copy ctor.
93 BitVector(const BitVector &RHS) : Size(RHS.size()) {
94 Capacity = NumBitWords(RHS.size());
95 Bits = new BitWord[Capacity];
96 std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits);
99 /// size - Returns the number of bits in this bitvector.
100 unsigned size() const { return Size; }
102 /// count - Returns the number of bits which are set.
103 unsigned count() const {
104 unsigned NumBits = 0;
105 for (unsigned i = 0; i < NumBitWords(size()); ++i)
106 NumBits = CountPopulation_32(Bits[i]);
110 /// any - Returns true if any bit is set.
112 for (unsigned i = 0; i < NumBitWords(size()); ++i)
118 /// none - Returns true if none of the bits are set.
123 /// find_first - Returns the index of the first set bit, -1 if none
124 /// of the bits are set.
125 int find_first() const {
126 for (unsigned i = 0; i < NumBitWords(size()); ++i)
128 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
132 /// find_next - Returns the index of the next set bit following the
133 /// "Prev" bit. Returns -1 if the next set bit is not found.
134 int find_next(unsigned Prev) const {
139 unsigned WordPos = Prev / BITS_PER_WORD;
140 unsigned BitPos = Prev % BITS_PER_WORD;
141 BitWord Copy = Bits[WordPos];
142 // Mask off previous bits.
143 Copy &= ~0 << BitPos;
146 return WordPos * BITS_PER_WORD + CountTrailingZeros_32(Copy);
148 // Check subsequent words.
149 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
151 return i * BITS_PER_WORD + CountTrailingZeros_32(Bits[i]);
155 /// clear - Clear all bits.
158 Bits = new BitWord[0];
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)
189 init_words(Bits, Capacity, true);
194 BitVector &set(unsigned Idx) {
195 Bits[Idx / BITS_PER_WORD] |= 1 << (Idx % BITS_PER_WORD);
200 init_words(Bits, Capacity, false);
204 BitVector &reset(unsigned Idx) {
205 Bits[Idx / BITS_PER_WORD] &= ~(1 << (Idx % BITS_PER_WORD));
210 for (unsigned i = 0; i < NumBitWords(size()); ++i)
216 BitVector &flip(unsigned Idx) {
217 Bits[Idx / BITS_PER_WORD] ^= 1 << (Idx % BITS_PER_WORD);
222 BitVector operator~() const {
223 return BitVector(*this).flip();
227 reference operator[](unsigned Idx) {
228 return reference(*this, Idx);
231 bool operator[](unsigned Idx) const {
232 BitWord Mask = 1 << (Idx % BITS_PER_WORD);
233 return (Bits[Idx / BITS_PER_WORD] & Mask) != 0;
236 bool test(unsigned Idx) const {
240 // Comparison operators.
241 bool operator==(const BitVector &RHS) const {
242 assert(Size == RHS.Size && "Illegal operation!");
243 for (unsigned i = 0; i < NumBitWords(size()); ++i)
244 if (Bits[i] != RHS.Bits[i])
249 bool operator!=(const BitVector &RHS) const {
250 return !(*this == RHS);
253 // Intersection, union, disjoint union.
254 BitVector operator&=(const BitVector &RHS) {
255 assert(Size == RHS.Size && "Illegal operation!");
256 for (unsigned i = 0; i < NumBitWords(size()); ++i)
257 Bits[i] &= RHS.Bits[i];
261 BitVector operator|=(const BitVector &RHS) {
262 assert(Size == RHS.Size && "Illegal operation!");
263 for (unsigned i = 0; i < NumBitWords(size()); ++i)
264 Bits[i] |= RHS.Bits[i];
268 BitVector operator^=(const BitVector &RHS) {
269 assert(Size == RHS.Size && "Illegal operation!");
270 for (unsigned i = 0; i < NumBitWords(size()); ++i)
271 Bits[i] ^= RHS.Bits[i];
275 // Assignment operator.
276 const BitVector &operator=(const BitVector &RHS) {
277 if (this == &RHS) return *this;
280 unsigned RHSWords = NumBitWords(Size);
281 if (Size > Capacity * BITS_PER_WORD) {
282 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
287 // Grow the bitvector to have enough elements.
288 Capacity = NumBitWords(Size);
289 BitWord *NewBits = new BitWord[Capacity];
290 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
292 // Destroy the old bits.
300 unsigned NumBitWords(unsigned S) const {
301 return (S + BITS_PER_WORD-1) / BITS_PER_WORD;
304 // Clear the unused top bits in the high word.
305 void clear_unused_bits() {
306 unsigned ExtraBits = Size % BITS_PER_WORD;
307 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.
324 void init_words(BitWord *B, unsigned NumWords, bool t) {
325 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
329 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
330 BitVector Result(LHS);
335 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
336 BitVector Result(LHS);
341 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
342 BitVector Result(LHS);
347 } // End llvm namespace