1 //===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
25 typedef unsigned long BitWord;
27 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * 8 };
29 BitWord *Bits; // Actual bits.
30 unsigned Size; // Size of bitvector in bits.
31 unsigned Capacity; // Size of allocated memory in BitWord.
34 // Encapsulation of a single bit.
36 friend class BitVector;
41 reference(); // Undefined
44 reference(BitVector &b, unsigned Idx) {
45 WordRef = &b.Bits[Idx / BITWORD_SIZE];
46 BitPos = Idx % BITWORD_SIZE;
51 reference& operator=(bool t) {
53 *WordRef |= 1L << BitPos;
55 *WordRef &= ~(1L << BitPos);
59 operator bool() const {
60 return ((*WordRef) & (1L << BitPos)) ? true : false;
65 /// BitVector default ctor - Creates an empty bitvector.
66 BitVector() : Size(0), Capacity(0) {
70 /// BitVector ctor - Creates a bitvector of specified number of bits. All
71 /// bits are initialized to the specified value.
72 explicit BitVector(unsigned s, bool t = false) : Size(s) {
73 Capacity = NumBitWords(s);
74 Bits = new BitWord[Capacity];
75 init_words(Bits, Capacity, t);
80 /// BitVector copy ctor.
81 BitVector(const BitVector &RHS) : Size(RHS.size()) {
88 Capacity = NumBitWords(RHS.size());
89 Bits = new BitWord[Capacity];
90 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((uint32_t)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 if (sizeof(BitWord) == 4)
132 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
133 else if (sizeof(BitWord) == 8)
134 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
136 assert(0 && "Unsupported!");
141 /// find_next - Returns the index of the next set bit following the
142 /// "Prev" bit. Returns -1 if the next set bit is not found.
143 int find_next(unsigned Prev) const {
148 unsigned WordPos = Prev / BITWORD_SIZE;
149 unsigned BitPos = Prev % BITWORD_SIZE;
150 BitWord Copy = Bits[WordPos];
151 // Mask off previous bits.
152 Copy &= ~0L << BitPos;
155 if (sizeof(BitWord) == 4)
156 return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy);
157 else if (sizeof(BitWord) == 8)
158 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
160 assert(0 && "Unsupported!");
163 // Check subsequent words.
164 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
166 if (sizeof(BitWord) == 4)
167 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
168 else if (sizeof(BitWord) == 8)
169 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
171 assert(0 && "Unsupported!");
176 /// clear - Clear all bits.
181 /// resize - Grow or shrink the bitvector.
182 void resize(unsigned N, bool t = false) {
183 if (N > Capacity * BITWORD_SIZE) {
184 unsigned OldCapacity = Capacity;
186 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
189 // Set any old unused bits that are now included in the BitVector. This
190 // may set bits that are not included in the new vector, but we will clear
191 // them back out below.
195 // Update the size, and clear out any bits that are now unused
196 unsigned OldSize = Size;
198 if (t || N < OldSize)
202 void reserve(unsigned N) {
203 if (N > Capacity * BITWORD_SIZE)
209 init_words(Bits, Capacity, true);
214 BitVector &set(unsigned Idx) {
215 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
220 init_words(Bits, Capacity, false);
224 BitVector &reset(unsigned Idx) {
225 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
230 for (unsigned i = 0; i < NumBitWords(size()); ++i)
236 BitVector &flip(unsigned Idx) {
237 Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
242 BitVector operator~() const {
243 return BitVector(*this).flip();
247 reference operator[](unsigned Idx) {
248 assert (Idx < Size && "Out-of-bounds Bit access.");
249 return reference(*this, Idx);
252 bool operator[](unsigned Idx) const {
253 assert (Idx < Size && "Out-of-bounds Bit access.");
254 BitWord Mask = 1L << (Idx % BITWORD_SIZE);
255 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
258 bool test(unsigned Idx) const {
262 // Comparison operators.
263 bool operator==(const BitVector &RHS) const {
264 unsigned ThisWords = NumBitWords(size());
265 unsigned RHSWords = NumBitWords(RHS.size());
267 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
268 if (Bits[i] != RHS.Bits[i])
271 // Verify that any extra words are all zeros.
272 if (i != ThisWords) {
273 for (; i != ThisWords; ++i)
276 } else if (i != RHSWords) {
277 for (; i != RHSWords; ++i)
284 bool operator!=(const BitVector &RHS) const {
285 return !(*this == RHS);
288 // Intersection, union, disjoint union.
289 BitVector operator&=(const BitVector &RHS) {
290 unsigned ThisWords = NumBitWords(size());
291 unsigned RHSWords = NumBitWords(RHS.size());
293 for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
294 Bits[i] &= RHS.Bits[i];
296 // Any bits that are just in this bitvector become zero, because they aren't
297 // in the RHS bit vector. Any words only in RHS are ignored because they
298 // are already zero in the LHS.
299 for (; i != ThisWords; ++i)
305 BitVector operator|=(const BitVector &RHS) {
306 assert(Size == RHS.Size && "Illegal operation!");
307 for (unsigned i = 0; i < NumBitWords(size()); ++i)
308 Bits[i] |= RHS.Bits[i];
312 BitVector operator^=(const BitVector &RHS) {
313 assert(Size == RHS.Size && "Illegal operation!");
314 for (unsigned i = 0; i < NumBitWords(size()); ++i)
315 Bits[i] ^= RHS.Bits[i];
319 // Assignment operator.
320 const BitVector &operator=(const BitVector &RHS) {
321 if (this == &RHS) return *this;
324 unsigned RHSWords = NumBitWords(Size);
325 if (Size <= Capacity * BITWORD_SIZE) {
326 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
331 // Grow the bitvector to have enough elements.
333 BitWord *NewBits = new BitWord[Capacity];
334 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
336 // Destroy the old bits.
344 unsigned NumBitWords(unsigned S) const {
345 return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
348 // Set the unused bits in the high words.
349 void set_unused_bits(bool t = true) {
350 // Set high words first.
351 unsigned UsedWords = NumBitWords(Size);
352 if (Capacity > UsedWords)
353 init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
355 // Then set any stray high bits of the last used word.
356 unsigned ExtraBits = Size % BITWORD_SIZE;
358 Bits[UsedWords-1] &= ~(~0L << ExtraBits);
359 Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits;
363 // Clear the unused bits in the high words.
364 void clear_unused_bits() {
365 set_unused_bits(false);
368 void grow(unsigned NewSize) {
369 unsigned OldCapacity = Capacity;
370 Capacity = NumBitWords(NewSize);
371 BitWord *NewBits = new BitWord[Capacity];
373 // Copy the old bits over.
374 if (OldCapacity != 0)
375 std::copy(Bits, &Bits[OldCapacity], NewBits);
377 // Destroy the old bits.
384 void init_words(BitWord *B, unsigned NumWords, bool t) {
385 memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
389 inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
390 BitVector Result(LHS);
395 inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
396 BitVector Result(LHS);
401 inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
402 BitVector Result(LHS);
407 } // End llvm namespace