BitVector.h revision fab4c9e9df1aeb33b55cfcfa174fac8d61df96fd
1//===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the BitVector class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_ADT_BITVECTOR_H 15#define LLVM_ADT_BITVECTOR_H 16 17#include "llvm/Support/MathExtras.h" 18#include <algorithm> 19#include <cassert> 20#include <climits> 21#include <cstring> 22 23namespace llvm { 24 25class BitVector { 26 typedef unsigned long BitWord; 27 28 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT }; 29 30 BitWord *Bits; // Actual bits. 31 unsigned Size; // Size of bitvector in bits. 32 unsigned Capacity; // Size of allocated memory in BitWord. 33 34public: 35 // Encapsulation of a single bit. 36 class reference { 37 friend class BitVector; 38 39 BitWord *WordRef; 40 unsigned BitPos; 41 42 reference(); // Undefined 43 44 public: 45 reference(BitVector &b, unsigned Idx) { 46 WordRef = &b.Bits[Idx / BITWORD_SIZE]; 47 BitPos = Idx % BITWORD_SIZE; 48 } 49 50 ~reference() {} 51 52 reference &operator=(reference t) { 53 *this = bool(t); 54 return *this; 55 } 56 57 reference& operator=(bool t) { 58 if (t) 59 *WordRef |= 1L << BitPos; 60 else 61 *WordRef &= ~(1L << BitPos); 62 return *this; 63 } 64 65 operator bool() const { 66 return ((*WordRef) & (1L << BitPos)) ? true : false; 67 } 68 }; 69 70 71 /// BitVector default ctor - Creates an empty bitvector. 72 BitVector() : Size(0), Capacity(0) { 73 Bits = 0; 74 } 75 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); 82 if (t) 83 clear_unused_bits(); 84 } 85 86 /// BitVector copy ctor. 87 BitVector(const BitVector &RHS) : Size(RHS.size()) { 88 if (Size == 0) { 89 Bits = 0; 90 Capacity = 0; 91 return; 92 } 93 94 Capacity = NumBitWords(RHS.size()); 95 Bits = new BitWord[Capacity]; 96 std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits); 97 } 98 99 ~BitVector() { 100 delete[] Bits; 101 } 102 103 /// empty - Tests whether there are no bits in this bitvector. 104 bool empty() const { return Size == 0; } 105 106 /// size - Returns the number of bits in this bitvector. 107 unsigned size() const { return Size; } 108 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]); 117 else 118 assert(0 && "Unsupported!"); 119 return NumBits; 120 } 121 122 /// any - Returns true if any bit is set. 123 bool any() const { 124 for (unsigned i = 0; i < NumBitWords(size()); ++i) 125 if (Bits[i] != 0) 126 return true; 127 return false; 128 } 129 130 /// all - Returns true if all bits are set. 131 bool all() const { 132 // TODO: Optimize this. 133 return count() == size(); 134 } 135 136 /// none - Returns true if none of the bits are set. 137 bool none() const { 138 return !any(); 139 } 140 141 /// find_first - Returns the index of the first set bit, -1 if none 142 /// of the bits are set. 143 int find_first() const { 144 for (unsigned i = 0; i < NumBitWords(size()); ++i) 145 if (Bits[i] != 0) { 146 if (sizeof(BitWord) == 4) 147 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]); 148 else if (sizeof(BitWord) == 8) 149 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]); 150 else 151 assert(0 && "Unsupported!"); 152 } 153 return -1; 154 } 155 156 /// find_next - Returns the index of the next set bit following the 157 /// "Prev" bit. Returns -1 if the next set bit is not found. 158 int find_next(unsigned Prev) const { 159 ++Prev; 160 if (Prev >= Size) 161 return -1; 162 163 unsigned WordPos = Prev / BITWORD_SIZE; 164 unsigned BitPos = Prev % BITWORD_SIZE; 165 BitWord Copy = Bits[WordPos]; 166 // Mask off previous bits. 167 Copy &= ~0L << BitPos; 168 169 if (Copy != 0) { 170 if (sizeof(BitWord) == 4) 171 return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy); 172 else if (sizeof(BitWord) == 8) 173 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy); 174 else 175 assert(0 && "Unsupported!"); 176 } 177 178 // Check subsequent words. 179 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i) 180 if (Bits[i] != 0) { 181 if (sizeof(BitWord) == 4) 182 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]); 183 else if (sizeof(BitWord) == 8) 184 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]); 185 else 186 assert(0 && "Unsupported!"); 187 } 188 return -1; 189 } 190 191 /// clear - Clear all bits. 192 void clear() { 193 Size = 0; 194 } 195 196 /// resize - Grow or shrink the bitvector. 197 void resize(unsigned N, bool t = false) { 198 if (N > Capacity * BITWORD_SIZE) { 199 unsigned OldCapacity = Capacity; 200 grow(N); 201 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t); 202 } 203 204 // Set any old unused bits that are now included in the BitVector. This 205 // may set bits that are not included in the new vector, but we will clear 206 // them back out below. 207 if (N > Size) 208 set_unused_bits(t); 209 210 // Update the size, and clear out any bits that are now unused 211 unsigned OldSize = Size; 212 Size = N; 213 if (t || N < OldSize) 214 clear_unused_bits(); 215 } 216 217 void reserve(unsigned N) { 218 if (N > Capacity * BITWORD_SIZE) 219 grow(N); 220 } 221 222 // Set, reset, flip 223 BitVector &set() { 224 init_words(Bits, Capacity, true); 225 clear_unused_bits(); 226 return *this; 227 } 228 229 BitVector &set(unsigned Idx) { 230 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE); 231 return *this; 232 } 233 234 BitVector &reset() { 235 init_words(Bits, Capacity, false); 236 return *this; 237 } 238 239 BitVector &reset(unsigned Idx) { 240 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE)); 241 return *this; 242 } 243 244 BitVector &flip() { 245 for (unsigned i = 0; i < NumBitWords(size()); ++i) 246 Bits[i] = ~Bits[i]; 247 clear_unused_bits(); 248 return *this; 249 } 250 251 BitVector &flip(unsigned Idx) { 252 Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE); 253 return *this; 254 } 255 256 // No argument flip. 257 BitVector operator~() const { 258 return BitVector(*this).flip(); 259 } 260 261 // Indexing. 262 reference operator[](unsigned Idx) { 263 assert (Idx < Size && "Out-of-bounds Bit access."); 264 return reference(*this, Idx); 265 } 266 267 bool operator[](unsigned Idx) const { 268 assert (Idx < Size && "Out-of-bounds Bit access."); 269 BitWord Mask = 1L << (Idx % BITWORD_SIZE); 270 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0; 271 } 272 273 bool test(unsigned Idx) const { 274 return (*this)[Idx]; 275 } 276 277 // Comparison operators. 278 bool operator==(const BitVector &RHS) const { 279 unsigned ThisWords = NumBitWords(size()); 280 unsigned RHSWords = NumBitWords(RHS.size()); 281 unsigned i; 282 for (i = 0; i != std::min(ThisWords, RHSWords); ++i) 283 if (Bits[i] != RHS.Bits[i]) 284 return false; 285 286 // Verify that any extra words are all zeros. 287 if (i != ThisWords) { 288 for (; i != ThisWords; ++i) 289 if (Bits[i]) 290 return false; 291 } else if (i != RHSWords) { 292 for (; i != RHSWords; ++i) 293 if (RHS.Bits[i]) 294 return false; 295 } 296 return true; 297 } 298 299 bool operator!=(const BitVector &RHS) const { 300 return !(*this == RHS); 301 } 302 303 // Intersection, union, disjoint union. 304 BitVector &operator&=(const BitVector &RHS) { 305 unsigned ThisWords = NumBitWords(size()); 306 unsigned RHSWords = NumBitWords(RHS.size()); 307 unsigned i; 308 for (i = 0; i != std::min(ThisWords, RHSWords); ++i) 309 Bits[i] &= RHS.Bits[i]; 310 311 // Any bits that are just in this bitvector become zero, because they aren't 312 // in the RHS bit vector. Any words only in RHS are ignored because they 313 // are already zero in the LHS. 314 for (; i != ThisWords; ++i) 315 Bits[i] = 0; 316 317 return *this; 318 } 319 320 BitVector &operator|=(const BitVector &RHS) { 321 if (size() < RHS.size()) 322 resize(RHS.size()); 323 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i) 324 Bits[i] |= RHS.Bits[i]; 325 return *this; 326 } 327 328 BitVector &operator^=(const BitVector &RHS) { 329 if (size() < RHS.size()) 330 resize(RHS.size()); 331 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i) 332 Bits[i] ^= RHS.Bits[i]; 333 return *this; 334 } 335 336 // Assignment operator. 337 const BitVector &operator=(const BitVector &RHS) { 338 if (this == &RHS) return *this; 339 340 Size = RHS.size(); 341 unsigned RHSWords = NumBitWords(Size); 342 if (Size <= Capacity * BITWORD_SIZE) { 343 if (Size) 344 std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits); 345 clear_unused_bits(); 346 return *this; 347 } 348 349 // Grow the bitvector to have enough elements. 350 Capacity = RHSWords; 351 BitWord *NewBits = new BitWord[Capacity]; 352 std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits); 353 354 // Destroy the old bits. 355 delete[] Bits; 356 Bits = NewBits; 357 358 return *this; 359 } 360 361 void swap(BitVector &RHS) { 362 std::swap(Bits, RHS.Bits); 363 std::swap(Size, RHS.Size); 364 std::swap(Capacity, RHS.Capacity); 365 } 366 367private: 368 unsigned NumBitWords(unsigned S) const { 369 return (S + BITWORD_SIZE-1) / BITWORD_SIZE; 370 } 371 372 // Set the unused bits in the high words. 373 void set_unused_bits(bool t = true) { 374 // Set high words first. 375 unsigned UsedWords = NumBitWords(Size); 376 if (Capacity > UsedWords) 377 init_words(&Bits[UsedWords], (Capacity-UsedWords), t); 378 379 // Then set any stray high bits of the last used word. 380 unsigned ExtraBits = Size % BITWORD_SIZE; 381 if (ExtraBits) { 382 Bits[UsedWords-1] &= ~(~0L << ExtraBits); 383 Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits; 384 } 385 } 386 387 // Clear the unused bits in the high words. 388 void clear_unused_bits() { 389 set_unused_bits(false); 390 } 391 392 void grow(unsigned NewSize) { 393 unsigned OldCapacity = Capacity; 394 Capacity = NumBitWords(NewSize); 395 BitWord *NewBits = new BitWord[Capacity]; 396 397 // Copy the old bits over. 398 if (OldCapacity != 0) 399 std::copy(Bits, &Bits[OldCapacity], NewBits); 400 401 // Destroy the old bits. 402 delete[] Bits; 403 Bits = NewBits; 404 405 clear_unused_bits(); 406 } 407 408 void init_words(BitWord *B, unsigned NumWords, bool t) { 409 memset(B, 0 - (int)t, NumWords*sizeof(BitWord)); 410 } 411}; 412 413inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) { 414 BitVector Result(LHS); 415 Result &= RHS; 416 return Result; 417} 418 419inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) { 420 BitVector Result(LHS); 421 Result |= RHS; 422 return Result; 423} 424 425inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) { 426 BitVector Result(LHS); 427 Result ^= RHS; 428 return Result; 429} 430 431} // End llvm namespace 432 433namespace std { 434 /// Implement std::swap in terms of BitVector swap. 435 inline void 436 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) { 437 LHS.swap(RHS); 438 } 439} 440 441#endif 442