BitVector.h revision 19de016955f744cf2466fadcd28e9bf8847dd260
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 <cstdlib> 22#include <cstring> 23 24namespace llvm { 25 26class BitVector { 27 typedef unsigned long BitWord; 28 29 enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT }; 30 31 BitWord *Bits; // Actual bits. 32 unsigned Size; // Size of bitvector in bits. 33 unsigned Capacity; // Size of allocated memory in BitWord. 34 35public: 36 // Encapsulation of a single bit. 37 class reference { 38 friend class BitVector; 39 40 BitWord *WordRef; 41 unsigned BitPos; 42 43 reference(); // Undefined 44 45 public: 46 reference(BitVector &b, unsigned Idx) { 47 WordRef = &b.Bits[Idx / BITWORD_SIZE]; 48 BitPos = Idx % BITWORD_SIZE; 49 } 50 51 ~reference() {} 52 53 reference &operator=(reference t) { 54 *this = bool(t); 55 return *this; 56 } 57 58 reference& operator=(bool t) { 59 if (t) 60 *WordRef |= 1L << BitPos; 61 else 62 *WordRef &= ~(1L << BitPos); 63 return *this; 64 } 65 66 operator bool() const { 67 return ((*WordRef) & (1L << BitPos)) ? true : false; 68 } 69 }; 70 71 72 /// BitVector default ctor - Creates an empty bitvector. 73 BitVector() : Size(0), Capacity(0) { 74 Bits = 0; 75 } 76 77 /// BitVector ctor - Creates a bitvector of specified number of bits. All 78 /// bits are initialized to the specified value. 79 explicit BitVector(unsigned s, bool t = false) : Size(s) { 80 Capacity = NumBitWords(s); 81 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord)); 82 init_words(Bits, Capacity, t); 83 if (t) 84 clear_unused_bits(); 85 } 86 87 /// BitVector copy ctor. 88 BitVector(const BitVector &RHS) : Size(RHS.size()) { 89 if (Size == 0) { 90 Bits = 0; 91 Capacity = 0; 92 return; 93 } 94 95 Capacity = NumBitWords(RHS.size()); 96 Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord)); 97 std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord)); 98 } 99 100 ~BitVector() { 101 std::free(Bits); 102 } 103 104 /// empty - Tests whether there are no bits in this bitvector. 105 bool empty() const { return Size == 0; } 106 107 /// size - Returns the number of bits in this bitvector. 108 unsigned size() const { return Size; } 109 110 /// count - Returns the number of bits which are set. 111 unsigned count() const { 112 unsigned NumBits = 0; 113 for (unsigned i = 0; i < NumBitWords(size()); ++i) 114 if (sizeof(BitWord) == 4) 115 NumBits += CountPopulation_32((uint32_t)Bits[i]); 116 else if (sizeof(BitWord) == 8) 117 NumBits += CountPopulation_64(Bits[i]); 118 else 119 assert(0 && "Unsupported!"); 120 return NumBits; 121 } 122 123 /// any - Returns true if any bit is set. 124 bool any() const { 125 for (unsigned i = 0; i < NumBitWords(size()); ++i) 126 if (Bits[i] != 0) 127 return true; 128 return false; 129 } 130 131 /// all - Returns true if all bits are set. 132 bool all() const { 133 // TODO: Optimize this. 134 return count() == size(); 135 } 136 137 /// none - Returns true if none of the bits are set. 138 bool none() const { 139 return !any(); 140 } 141 142 /// find_first - Returns the index of the first set bit, -1 if none 143 /// of the bits are set. 144 int find_first() const { 145 for (unsigned i = 0; i < NumBitWords(size()); ++i) 146 if (Bits[i] != 0) { 147 if (sizeof(BitWord) == 4) 148 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]); 149 else if (sizeof(BitWord) == 8) 150 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]); 151 else 152 assert(0 && "Unsupported!"); 153 } 154 return -1; 155 } 156 157 /// find_next - Returns the index of the next set bit following the 158 /// "Prev" bit. Returns -1 if the next set bit is not found. 159 int find_next(unsigned Prev) const { 160 ++Prev; 161 if (Prev >= Size) 162 return -1; 163 164 unsigned WordPos = Prev / BITWORD_SIZE; 165 unsigned BitPos = Prev % BITWORD_SIZE; 166 BitWord Copy = Bits[WordPos]; 167 // Mask off previous bits. 168 Copy &= ~0L << BitPos; 169 170 if (Copy != 0) { 171 if (sizeof(BitWord) == 4) 172 return WordPos * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Copy); 173 else if (sizeof(BitWord) == 8) 174 return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy); 175 else 176 assert(0 && "Unsupported!"); 177 } 178 179 // Check subsequent words. 180 for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i) 181 if (Bits[i] != 0) { 182 if (sizeof(BitWord) == 4) 183 return i * BITWORD_SIZE + CountTrailingZeros_32((uint32_t)Bits[i]); 184 else if (sizeof(BitWord) == 8) 185 return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]); 186 else 187 assert(0 && "Unsupported!"); 188 } 189 return -1; 190 } 191 192 /// clear - Clear all bits. 193 void clear() { 194 Size = 0; 195 } 196 197 /// resize - Grow or shrink the bitvector. 198 void resize(unsigned N, bool t = false) { 199 if (N > Capacity * BITWORD_SIZE) { 200 unsigned OldCapacity = Capacity; 201 grow(N); 202 init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t); 203 } 204 205 // Set any old unused bits that are now included in the BitVector. This 206 // may set bits that are not included in the new vector, but we will clear 207 // them back out below. 208 if (N > Size) 209 set_unused_bits(t); 210 211 // Update the size, and clear out any bits that are now unused 212 unsigned OldSize = Size; 213 Size = N; 214 if (t || N < OldSize) 215 clear_unused_bits(); 216 } 217 218 void reserve(unsigned N) { 219 if (N > Capacity * BITWORD_SIZE) 220 grow(N); 221 } 222 223 // Set, reset, flip 224 BitVector &set() { 225 init_words(Bits, Capacity, true); 226 clear_unused_bits(); 227 return *this; 228 } 229 230 BitVector &set(unsigned Idx) { 231 Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE); 232 return *this; 233 } 234 235 BitVector &reset() { 236 init_words(Bits, Capacity, false); 237 return *this; 238 } 239 240 BitVector &reset(unsigned Idx) { 241 Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE)); 242 return *this; 243 } 244 245 BitVector &flip() { 246 for (unsigned i = 0; i < NumBitWords(size()); ++i) 247 Bits[i] = ~Bits[i]; 248 clear_unused_bits(); 249 return *this; 250 } 251 252 BitVector &flip(unsigned Idx) { 253 Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE); 254 return *this; 255 } 256 257 // No argument flip. 258 BitVector operator~() const { 259 return BitVector(*this).flip(); 260 } 261 262 // Indexing. 263 reference operator[](unsigned Idx) { 264 assert (Idx < Size && "Out-of-bounds Bit access."); 265 return reference(*this, Idx); 266 } 267 268 bool operator[](unsigned Idx) const { 269 assert (Idx < Size && "Out-of-bounds Bit access."); 270 BitWord Mask = 1L << (Idx % BITWORD_SIZE); 271 return (Bits[Idx / BITWORD_SIZE] & Mask) != 0; 272 } 273 274 bool test(unsigned Idx) const { 275 return (*this)[Idx]; 276 } 277 278 // Comparison operators. 279 bool operator==(const BitVector &RHS) const { 280 unsigned ThisWords = NumBitWords(size()); 281 unsigned RHSWords = NumBitWords(RHS.size()); 282 unsigned i; 283 for (i = 0; i != std::min(ThisWords, RHSWords); ++i) 284 if (Bits[i] != RHS.Bits[i]) 285 return false; 286 287 // Verify that any extra words are all zeros. 288 if (i != ThisWords) { 289 for (; i != ThisWords; ++i) 290 if (Bits[i]) 291 return false; 292 } else if (i != RHSWords) { 293 for (; i != RHSWords; ++i) 294 if (RHS.Bits[i]) 295 return false; 296 } 297 return true; 298 } 299 300 bool operator!=(const BitVector &RHS) const { 301 return !(*this == RHS); 302 } 303 304 // Intersection, union, disjoint union. 305 BitVector &operator&=(const BitVector &RHS) { 306 unsigned ThisWords = NumBitWords(size()); 307 unsigned RHSWords = NumBitWords(RHS.size()); 308 unsigned i; 309 for (i = 0; i != std::min(ThisWords, RHSWords); ++i) 310 Bits[i] &= RHS.Bits[i]; 311 312 // Any bits that are just in this bitvector become zero, because they aren't 313 // in the RHS bit vector. Any words only in RHS are ignored because they 314 // are already zero in the LHS. 315 for (; i != ThisWords; ++i) 316 Bits[i] = 0; 317 318 return *this; 319 } 320 321 // reset - Reset bits that are set in RHS. Same as *this &= ~RHS. 322 BitVector &reset(const BitVector &RHS) { 323 unsigned ThisWords = NumBitWords(size()); 324 unsigned RHSWords = NumBitWords(RHS.size()); 325 unsigned i; 326 for (i = 0; i != std::min(ThisWords, RHSWords); ++i) 327 Bits[i] &= ~RHS.Bits[i]; 328 return *this; 329 } 330 331 BitVector &operator|=(const BitVector &RHS) { 332 if (size() < RHS.size()) 333 resize(RHS.size()); 334 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i) 335 Bits[i] |= RHS.Bits[i]; 336 return *this; 337 } 338 339 BitVector &operator^=(const BitVector &RHS) { 340 if (size() < RHS.size()) 341 resize(RHS.size()); 342 for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i) 343 Bits[i] ^= RHS.Bits[i]; 344 return *this; 345 } 346 347 // Assignment operator. 348 const BitVector &operator=(const BitVector &RHS) { 349 if (this == &RHS) return *this; 350 351 Size = RHS.size(); 352 unsigned RHSWords = NumBitWords(Size); 353 if (Size <= Capacity * BITWORD_SIZE) { 354 if (Size) 355 std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord)); 356 clear_unused_bits(); 357 return *this; 358 } 359 360 // Grow the bitvector to have enough elements. 361 Capacity = RHSWords; 362 BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord)); 363 std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord)); 364 365 // Destroy the old bits. 366 std::free(Bits); 367 Bits = NewBits; 368 369 return *this; 370 } 371 372 void swap(BitVector &RHS) { 373 std::swap(Bits, RHS.Bits); 374 std::swap(Size, RHS.Size); 375 std::swap(Capacity, RHS.Capacity); 376 } 377 378 //===--------------------------------------------------------------------===// 379 // Portable bit mask operations. 380 //===--------------------------------------------------------------------===// 381 // 382 // These methods all operate on arrays of uint32_t, each holding 32 bits. The 383 // fixed word size makes it easier to work with literal bit vector constants 384 // in portable code. 385 // 386 // The LSB in each word is the lowest numbered bit. The size of a portable 387 // bit mask is always a whole multiple of 32 bits. If no bit mask size is 388 // given, the bit mask is assumed to cover the entire BitVector. 389 390 /// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize. 391 /// This computes "*this |= Mask". 392 void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) { 393 applyMask<true, false>(Mask, MaskWords); 394 } 395 396 /// clearBitsInMask - Clear any bits in this vector that are set in Mask. 397 /// Don't resize. This computes "*this &= ~Mask". 398 void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) { 399 applyMask<false, false>(Mask, MaskWords); 400 } 401 402 /// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask. 403 /// Don't resize. This computes "*this |= ~Mask". 404 void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) { 405 applyMask<true, true>(Mask, MaskWords); 406 } 407 408 /// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask. 409 /// Don't resize. This computes "*this &= Mask". 410 void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) { 411 applyMask<false, true>(Mask, MaskWords); 412 } 413 414private: 415 unsigned NumBitWords(unsigned S) const { 416 return (S + BITWORD_SIZE-1) / BITWORD_SIZE; 417 } 418 419 // Set the unused bits in the high words. 420 void set_unused_bits(bool t = true) { 421 // Set high words first. 422 unsigned UsedWords = NumBitWords(Size); 423 if (Capacity > UsedWords) 424 init_words(&Bits[UsedWords], (Capacity-UsedWords), t); 425 426 // Then set any stray high bits of the last used word. 427 unsigned ExtraBits = Size % BITWORD_SIZE; 428 if (ExtraBits) { 429 Bits[UsedWords-1] &= ~(~0L << ExtraBits); 430 Bits[UsedWords-1] |= (0 - (BitWord)t) << ExtraBits; 431 } 432 } 433 434 // Clear the unused bits in the high words. 435 void clear_unused_bits() { 436 set_unused_bits(false); 437 } 438 439 void grow(unsigned NewSize) { 440 Capacity = std::max(NumBitWords(NewSize), Capacity * 2); 441 Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord)); 442 443 clear_unused_bits(); 444 } 445 446 void init_words(BitWord *B, unsigned NumWords, bool t) { 447 memset(B, 0 - (int)t, NumWords*sizeof(BitWord)); 448 } 449 450 template<bool AddBits, bool InvertMask> 451 void applyMask(const uint32_t *Mask, unsigned MaskWords) { 452 assert(BITWORD_SIZE % 32 == 0 && "Unsupported BitWord size."); 453 MaskWords = std::min(MaskWords, (size() + 31) / 32); 454 const unsigned Scale = BITWORD_SIZE / 32; 455 unsigned i; 456 for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) { 457 BitWord BW = Bits[i]; 458 // This inner loop should unroll completely when BITWORD_SIZE > 32. 459 for (unsigned b = 0; b != BITWORD_SIZE; b += 32) { 460 uint32_t M = *Mask++; 461 if (InvertMask) M = ~M; 462 if (AddBits) BW |= BitWord(M) << b; 463 else BW &= ~(BitWord(M) << b); 464 } 465 Bits[i] = BW; 466 } 467 for (unsigned b = 0; MaskWords; b += 32, --MaskWords) { 468 uint32_t M = *Mask++; 469 if (InvertMask) M = ~M; 470 if (AddBits) Bits[i] |= BitWord(M) << b; 471 else Bits[i] &= ~(BitWord(M) << b); 472 } 473 if (AddBits) 474 clear_unused_bits(); 475 } 476}; 477 478inline BitVector operator&(const BitVector &LHS, const BitVector &RHS) { 479 BitVector Result(LHS); 480 Result &= RHS; 481 return Result; 482} 483 484inline BitVector operator|(const BitVector &LHS, const BitVector &RHS) { 485 BitVector Result(LHS); 486 Result |= RHS; 487 return Result; 488} 489 490inline BitVector operator^(const BitVector &LHS, const BitVector &RHS) { 491 BitVector Result(LHS); 492 Result ^= RHS; 493 return Result; 494} 495 496} // End llvm namespace 497 498namespace std { 499 /// Implement std::swap in terms of BitVector swap. 500 inline void 501 swap(llvm::BitVector &LHS, llvm::BitVector &RHS) { 502 LHS.swap(RHS); 503 } 504} 505 506#endif 507