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"
26
typedef unsigned long BitWord;
28
enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
30
BitWord *Bits; // Actual bits.
31
unsigned Size; // Size of bitvector in bits.
32
unsigned Capacity; // Size of allocated memory in BitWord.
35
// Encapsulation of a single bit.
37
friend class BitVector;
42
reference(); // Undefined
45
reference(BitVector &b, unsigned Idx) {
46
WordRef = &b.Bits[Idx / BITWORD_SIZE];
47
BitPos = Idx % BITWORD_SIZE;
52
reference &operator=(reference t) {
57
reference& operator=(bool t) {
59
*WordRef |= 1L << BitPos;
61
*WordRef &= ~(1L << BitPos);
65
operator bool() const {
66
return ((*WordRef) & (1L << BitPos)) ? true : false;
71
/// BitVector default ctor - Creates an empty bitvector.
72
BitVector() : Size(0), Capacity(0) {
76
/// BitVector ctor - Creates a bitvector of specified number of bits. All
77
/// bits are initialized to the specified value.
78
explicit BitVector(unsigned s, bool t = false) : Size(s) {
79
Capacity = NumBitWords(s);
80
Bits = new BitWord[Capacity];
81
init_words(Bits, Capacity, t);
86
/// BitVector copy ctor.
87
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);
103
/// empty - Tests whether there are no bits in this bitvector.
104
bool empty() const { return Size == 0; }
106
/// size - Returns the number of bits in this bitvector.
107
unsigned size() const { return Size; }
109
/// count - Returns the number of bits which are set.
110
unsigned count() const {
111
unsigned NumBits = 0;
112
for (unsigned i = 0; i < NumBitWords(size()); ++i)
113
if (sizeof(BitWord) == 4)
114
NumBits += CountPopulation_32((uint32_t)Bits[i]);
115
else if (sizeof(BitWord) == 8)
116
NumBits += CountPopulation_64(Bits[i]);
118
assert(0 && "Unsupported!");
122
/// any - Returns true if any bit is set.
124
for (unsigned i = 0; i < NumBitWords(size()); ++i)
130
/// none - Returns true if none of the bits are set.
135
/// find_first - Returns the index of the first set bit, -1 if none
136
/// of the bits are set.
137
int find_first() const {
138
for (unsigned i = 0; i < NumBitWords(size()); ++i)
140
if (sizeof(BitWord) == 4)
141
return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
142
else if (sizeof(BitWord) == 8)
143
return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
145
assert(0 && "Unsupported!");
150
/// find_next - Returns the index of the next set bit following the
151
/// "Prev" bit. Returns -1 if the next set bit is not found.
152
int find_next(unsigned Prev) const {
157
unsigned WordPos = Prev / BITWORD_SIZE;
158
unsigned BitPos = Prev % BITWORD_SIZE;
159
BitWord Copy = Bits[WordPos];
160
// Mask off previous bits.
161
Copy &= ~0L << BitPos;
164
if (sizeof(BitWord) == 4)
165
return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy);
166
else if (sizeof(BitWord) == 8)
167
return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
169
assert(0 && "Unsupported!");
172
// Check subsequent words.
173
for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
175
if (sizeof(BitWord) == 4)
176
return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]);
177
else if (sizeof(BitWord) == 8)
178
return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
180
assert(0 && "Unsupported!");
185
/// clear - Clear all bits.
190
/// resize - Grow or shrink the bitvector.
191
void resize(unsigned N, bool t = false) {
192
if (N > Capacity * BITWORD_SIZE) {
193
unsigned OldCapacity = Capacity;
195
init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
198
// Set any old unused bits that are now included in the BitVector. This
199
// may set bits that are not included in the new vector, but we will clear
200
// them back out below.
204
// Update the size, and clear out any bits that are now unused
205
unsigned OldSize = Size;
207
if (t || N < OldSize)
211
void reserve(unsigned N) {
212
if (N > Capacity * BITWORD_SIZE)
218
init_words(Bits, Capacity, true);
223
BitVector &set(unsigned Idx) {
224
Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
229
init_words(Bits, Capacity, false);
233
BitVector &reset(unsigned Idx) {
234
Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
239
for (unsigned i = 0; i < NumBitWords(size()); ++i)
245
BitVector &flip(unsigned Idx) {
246
Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
251
BitVector operator~() const {
252
return BitVector(*this).flip();
256
reference operator[](unsigned Idx) {
257
assert (Idx < Size && "Out-of-bounds Bit access.");
258
return reference(*this, Idx);
261
bool operator[](unsigned Idx) const {
262
assert (Idx < Size && "Out-of-bounds Bit access.");
263
BitWord Mask = 1L << (Idx % BITWORD_SIZE);
264
return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
267
bool test(unsigned Idx) const {
271
// Comparison operators.
272
bool operator==(const BitVector &RHS) const {
273
unsigned ThisWords = NumBitWords(size());
274
unsigned RHSWords = NumBitWords(RHS.size());
276
for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
277
if (Bits[i] != RHS.Bits[i])
280
// Verify that any extra words are all zeros.
281
if (i != ThisWords) {
282
for (; i != ThisWords; ++i)
285
} else if (i != RHSWords) {
286
for (; i != RHSWords; ++i)
293
bool operator!=(const BitVector &RHS) const {
294
return !(*this == RHS);
297
// Intersection, union, disjoint union.
298
BitVector &operator&=(const BitVector &RHS) {
299
unsigned ThisWords = NumBitWords(size());
300
unsigned RHSWords = NumBitWords(RHS.size());
302
for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
303
Bits[i] &= RHS.Bits[i];
305
// Any bits that are just in this bitvector become zero, because they aren't
306
// in the RHS bit vector. Any words only in RHS are ignored because they
307
// are already zero in the LHS.
308
for (; i != ThisWords; ++i)
314
BitVector &operator|=(const BitVector &RHS) {
315
if (size() < RHS.size())
317
for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
318
Bits[i] |= RHS.Bits[i];
322
BitVector &operator^=(const BitVector &RHS) {
323
if (size() < RHS.size())
325
for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
326
Bits[i] ^= RHS.Bits[i];
330
// Assignment operator.
331
const BitVector &operator=(const BitVector &RHS) {
332
if (this == &RHS) return *this;
335
unsigned RHSWords = NumBitWords(Size);
336
if (Size <= Capacity * BITWORD_SIZE) {
338
std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits);
343
// Grow the bitvector to have enough elements.
345
BitWord *NewBits = new BitWord[Capacity];
346
std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits);
348
// Destroy the old bits.
355
void swap(BitVector &RHS) {
356
std::swap(Bits, RHS.Bits);
357
std::swap(Size, RHS.Size);
358
std::swap(Capacity, RHS.Capacity);
362
unsigned NumBitWords(unsigned S) const {
363
return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
366
// Set the unused bits in the high words.
367
void set_unused_bits(bool t = true) {
368
// Set high words first.
369
unsigned UsedWords = NumBitWords(Size);
370
if (Capacity > UsedWords)
371
init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
373
// Then set any stray high bits of the last used word.
374
unsigned ExtraBits = Size % BITWORD_SIZE;
376
Bits[UsedWords-1] &= ~(~0L << ExtraBits);
377
Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits;
381
// Clear the unused bits in the high words.
382
void clear_unused_bits() {
383
set_unused_bits(false);
386
void grow(unsigned NewSize) {
387
unsigned OldCapacity = Capacity;
388
Capacity = NumBitWords(NewSize);
389
BitWord *NewBits = new BitWord[Capacity];
391
// Copy the old bits over.
392
if (OldCapacity != 0)
393
std::copy(Bits, &Bits[OldCapacity], NewBits);
395
// Destroy the old bits.
402
void init_words(BitWord *B, unsigned NumWords, bool t) {
403
memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
407
inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) {
408
BitVector Result(LHS);
413
inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) {
414
BitVector Result(LHS);
419
inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) {
420
BitVector Result(LHS);
425
} // End llvm namespace
428
/// Implement std::swap in terms of BitVector swap.
430
swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {