2007-02-15 05:56:11 +00:00
|
|
|
//===- llvm/ADT/BitVector.h - Bit vectors -----------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-02-15 05:56:11 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the BitVector class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_BITVECTOR_H
|
|
|
|
#define LLVM_ADT_BITVECTOR_H
|
|
|
|
|
2012-06-01 18:52:53 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2012-02-05 22:14:15 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2007-02-15 05:56:11 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2007-04-04 21:14:49 +00:00
|
|
|
#include <algorithm>
|
2007-04-04 22:13:39 +00:00
|
|
|
#include <cassert>
|
2009-04-01 18:45:54 +00:00
|
|
|
#include <climits>
|
2010-11-26 18:25:20 +00:00
|
|
|
#include <cstdlib>
|
2007-02-15 05:56:11 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class BitVector {
|
|
|
|
typedef unsigned long BitWord;
|
|
|
|
|
2009-04-01 18:45:54 +00:00
|
|
|
enum { BITWORD_SIZE = (unsigned)sizeof(BitWord) * CHAR_BIT };
|
2007-02-15 05:56:11 +00:00
|
|
|
|
2009-01-09 19:25:42 +00:00
|
|
|
BitWord *Bits; // Actual bits.
|
2007-02-15 05:56:11 +00:00
|
|
|
unsigned Size; // Size of bitvector in bits.
|
|
|
|
unsigned Capacity; // Size of allocated memory in BitWord.
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Encapsulation of a single bit.
|
|
|
|
class reference {
|
|
|
|
friend class BitVector;
|
|
|
|
|
|
|
|
BitWord *WordRef;
|
|
|
|
unsigned BitPos;
|
|
|
|
|
|
|
|
reference(); // Undefined
|
|
|
|
|
|
|
|
public:
|
|
|
|
reference(BitVector &b, unsigned Idx) {
|
2007-04-26 15:07:47 +00:00
|
|
|
WordRef = &b.Bits[Idx / BITWORD_SIZE];
|
|
|
|
BitPos = Idx % BITWORD_SIZE;
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~reference() {}
|
|
|
|
|
2010-04-30 20:50:28 +00:00
|
|
|
reference &operator=(reference t) {
|
|
|
|
*this = bool(t);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
reference& operator=(bool t) {
|
|
|
|
if (t)
|
2007-02-15 19:03:23 +00:00
|
|
|
*WordRef |= 1L << BitPos;
|
2007-02-15 05:56:11 +00:00
|
|
|
else
|
2007-02-15 19:03:23 +00:00
|
|
|
*WordRef &= ~(1L << BitPos);
|
2007-02-15 05:56:11 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator bool() const {
|
2007-10-17 14:56:40 +00:00
|
|
|
return ((*WordRef) & (1L << BitPos)) ? true : false;
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// BitVector default ctor - Creates an empty bitvector.
|
|
|
|
BitVector() : Size(0), Capacity(0) {
|
2008-05-29 19:52:31 +00:00
|
|
|
Bits = 0;
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// BitVector ctor - Creates a bitvector of specified number of bits. All
|
|
|
|
/// bits are initialized to the specified value.
|
2007-02-15 19:05:25 +00:00
|
|
|
explicit BitVector(unsigned s, bool t = false) : Size(s) {
|
2007-02-15 05:56:11 +00:00
|
|
|
Capacity = NumBitWords(s);
|
2010-11-26 18:25:20 +00:00
|
|
|
Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
|
2007-02-15 05:56:11 +00:00
|
|
|
init_words(Bits, Capacity, t);
|
2007-02-15 19:05:25 +00:00
|
|
|
if (t)
|
|
|
|
clear_unused_bits();
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// BitVector copy ctor.
|
|
|
|
BitVector(const BitVector &RHS) : Size(RHS.size()) {
|
2007-02-15 19:05:25 +00:00
|
|
|
if (Size == 0) {
|
2008-05-29 19:52:31 +00:00
|
|
|
Bits = 0;
|
2007-02-15 20:14:06 +00:00
|
|
|
Capacity = 0;
|
2007-02-15 19:05:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
Capacity = NumBitWords(RHS.size());
|
2010-11-26 18:25:20 +00:00
|
|
|
Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
|
|
|
|
std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2012-06-01 18:52:53 +00:00
|
|
|
BitVector(BitVector &&RHS)
|
|
|
|
: Bits(RHS.Bits), Size(RHS.Size), Capacity(RHS.Capacity) {
|
|
|
|
RHS.Bits = 0;
|
|
|
|
}
|
|
|
|
|
2007-03-20 02:10:56 +00:00
|
|
|
~BitVector() {
|
2010-11-26 18:25:20 +00:00
|
|
|
std::free(Bits);
|
2007-03-20 02:10:56 +00:00
|
|
|
}
|
2007-02-15 05:56:11 +00:00
|
|
|
|
2010-01-05 15:04:49 +00:00
|
|
|
/// empty - Tests whether there are no bits in this bitvector.
|
|
|
|
bool empty() const { return Size == 0; }
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
/// size - Returns the number of bits in this bitvector.
|
|
|
|
unsigned size() const { return Size; }
|
|
|
|
|
|
|
|
/// count - Returns the number of bits which are set.
|
|
|
|
unsigned count() const {
|
|
|
|
unsigned NumBits = 0;
|
|
|
|
for (unsigned i = 0; i < NumBitWords(size()); ++i)
|
2007-02-15 19:09:36 +00:00
|
|
|
if (sizeof(BitWord) == 4)
|
2008-05-05 18:30:58 +00:00
|
|
|
NumBits += CountPopulation_32((uint32_t)Bits[i]);
|
2007-02-15 19:09:36 +00:00
|
|
|
else if (sizeof(BitWord) == 8)
|
|
|
|
NumBits += CountPopulation_64(Bits[i]);
|
|
|
|
else
|
2012-02-05 22:14:15 +00:00
|
|
|
llvm_unreachable("Unsupported!");
|
2007-02-15 05:56:11 +00:00
|
|
|
return NumBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// any - Returns true if any bit is set.
|
|
|
|
bool any() const {
|
|
|
|
for (unsigned i = 0; i < NumBitWords(size()); ++i)
|
|
|
|
if (Bits[i] != 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-27 15:48:37 +00:00
|
|
|
/// all - Returns true if all bits are set.
|
|
|
|
bool all() const {
|
2013-06-07 15:14:31 +00:00
|
|
|
for (unsigned i = 0; i < Size / BITWORD_SIZE; ++i)
|
2013-06-07 14:14:38 +00:00
|
|
|
if (Bits[i] != ~0UL)
|
|
|
|
return false;
|
|
|
|
|
2013-06-07 15:14:31 +00:00
|
|
|
// If bits remain check that they are ones. The unused bits are always zero.
|
|
|
|
if (unsigned Remainder = Size % BITWORD_SIZE)
|
|
|
|
return Bits[Size / BITWORD_SIZE] == (1UL << Remainder) - 1;
|
|
|
|
|
|
|
|
return true;
|
2010-09-27 15:48:37 +00:00
|
|
|
}
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
/// none - Returns true if none of the bits are set.
|
|
|
|
bool none() const {
|
|
|
|
return !any();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// find_first - Returns the index of the first set bit, -1 if none
|
|
|
|
/// of the bits are set.
|
|
|
|
int find_first() const {
|
|
|
|
for (unsigned i = 0; i < NumBitWords(size()); ++i)
|
2007-03-02 02:31:37 +00:00
|
|
|
if (Bits[i] != 0) {
|
2007-04-16 18:10:23 +00:00
|
|
|
if (sizeof(BitWord) == 4)
|
2013-05-24 22:23:49 +00:00
|
|
|
return i * BITWORD_SIZE + countTrailingZeros((uint32_t)Bits[i]);
|
2012-02-05 22:14:15 +00:00
|
|
|
if (sizeof(BitWord) == 8)
|
2013-05-24 22:23:49 +00:00
|
|
|
return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
|
2012-02-05 22:14:15 +00:00
|
|
|
llvm_unreachable("Unsupported!");
|
2007-03-02 02:31:37 +00:00
|
|
|
}
|
2007-02-15 05:56:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// find_next - Returns the index of the next set bit following the
|
|
|
|
/// "Prev" bit. Returns -1 if the next set bit is not found.
|
|
|
|
int find_next(unsigned Prev) const {
|
|
|
|
++Prev;
|
|
|
|
if (Prev >= Size)
|
|
|
|
return -1;
|
|
|
|
|
2007-04-26 15:07:47 +00:00
|
|
|
unsigned WordPos = Prev / BITWORD_SIZE;
|
|
|
|
unsigned BitPos = Prev % BITWORD_SIZE;
|
2007-02-15 05:56:11 +00:00
|
|
|
BitWord Copy = Bits[WordPos];
|
|
|
|
// Mask off previous bits.
|
2012-08-24 23:29:28 +00:00
|
|
|
Copy &= ~0UL << BitPos;
|
2007-02-15 05:56:11 +00:00
|
|
|
|
2007-03-02 02:31:37 +00:00
|
|
|
if (Copy != 0) {
|
|
|
|
if (sizeof(BitWord) == 4)
|
2013-05-24 22:23:49 +00:00
|
|
|
return WordPos * BITWORD_SIZE + countTrailingZeros((uint32_t)Copy);
|
2012-02-05 22:14:15 +00:00
|
|
|
if (sizeof(BitWord) == 8)
|
2013-05-24 22:23:49 +00:00
|
|
|
return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
|
2012-02-05 22:14:15 +00:00
|
|
|
llvm_unreachable("Unsupported!");
|
2007-03-02 02:31:37 +00:00
|
|
|
}
|
2007-02-15 05:56:11 +00:00
|
|
|
|
|
|
|
// Check subsequent words.
|
|
|
|
for (unsigned i = WordPos+1; i < NumBitWords(size()); ++i)
|
2007-03-02 02:31:37 +00:00
|
|
|
if (Bits[i] != 0) {
|
2007-04-16 18:10:23 +00:00
|
|
|
if (sizeof(BitWord) == 4)
|
2013-05-24 22:23:49 +00:00
|
|
|
return i * BITWORD_SIZE + countTrailingZeros((uint32_t)Bits[i]);
|
2012-02-05 22:14:15 +00:00
|
|
|
if (sizeof(BitWord) == 8)
|
2013-05-24 22:23:49 +00:00
|
|
|
return i * BITWORD_SIZE + countTrailingZeros(Bits[i]);
|
2012-02-05 22:14:15 +00:00
|
|
|
llvm_unreachable("Unsupported!");
|
2007-03-02 02:31:37 +00:00
|
|
|
}
|
2007-02-15 05:56:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// clear - Clear all bits.
|
|
|
|
void clear() {
|
2007-02-15 19:10:34 +00:00
|
|
|
Size = 0;
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// resize - Grow or shrink the bitvector.
|
2007-02-15 19:12:39 +00:00
|
|
|
void resize(unsigned N, bool t = false) {
|
2007-04-26 15:07:47 +00:00
|
|
|
if (N > Capacity * BITWORD_SIZE) {
|
2007-02-15 05:56:11 +00:00
|
|
|
unsigned OldCapacity = Capacity;
|
|
|
|
grow(N);
|
|
|
|
init_words(&Bits[OldCapacity], (Capacity-OldCapacity), t);
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
|
|
|
// Set any old unused bits that are now included in the BitVector. This
|
|
|
|
// may set bits that are not included in the new vector, but we will clear
|
2007-08-06 20:52:17 +00:00
|
|
|
// them back out below.
|
|
|
|
if (N > Size)
|
|
|
|
set_unused_bits(t);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-08-06 20:52:17 +00:00
|
|
|
// Update the size, and clear out any bits that are now unused
|
|
|
|
unsigned OldSize = Size;
|
2007-02-15 05:56:11 +00:00
|
|
|
Size = N;
|
2007-08-06 20:52:17 +00:00
|
|
|
if (t || N < OldSize)
|
|
|
|
clear_unused_bits();
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void reserve(unsigned N) {
|
2007-04-26 15:07:47 +00:00
|
|
|
if (N > Capacity * BITWORD_SIZE)
|
2007-02-15 05:56:11 +00:00
|
|
|
grow(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set, reset, flip
|
|
|
|
BitVector &set() {
|
2007-02-15 19:18:12 +00:00
|
|
|
init_words(Bits, Capacity, true);
|
|
|
|
clear_unused_bits();
|
2007-02-15 05:56:11 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitVector &set(unsigned Idx) {
|
2007-04-26 15:07:47 +00:00
|
|
|
Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
|
2007-02-15 05:56:11 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-10-15 22:05:27 +00:00
|
|
|
/// set - Efficiently set a range of bits in [I, E)
|
|
|
|
BitVector &set(unsigned I, unsigned E) {
|
|
|
|
assert(I <= E && "Attempted to set backwards range!");
|
|
|
|
assert(E <= size() && "Attempted to set out-of-bounds range!");
|
|
|
|
|
|
|
|
if (I == E) return *this;
|
|
|
|
|
2012-10-16 06:04:27 +00:00
|
|
|
if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
|
|
|
|
BitWord EMask = 1UL << (E % BITWORD_SIZE);
|
|
|
|
BitWord IMask = 1UL << (I % BITWORD_SIZE);
|
2012-10-15 22:05:27 +00:00
|
|
|
BitWord Mask = EMask - IMask;
|
|
|
|
Bits[I / BITWORD_SIZE] |= Mask;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
|
|
|
|
Bits[I / BITWORD_SIZE] |= PrefixMask;
|
|
|
|
I = RoundUpToAlignment(I, BITWORD_SIZE);
|
|
|
|
|
|
|
|
for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
|
|
|
|
Bits[I / BITWORD_SIZE] = ~0UL;
|
|
|
|
|
|
|
|
BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
|
2013-12-19 02:35:26 +00:00
|
|
|
if (I < E)
|
|
|
|
Bits[I / BITWORD_SIZE] |= PostfixMask;
|
2012-10-15 22:05:27 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
BitVector &reset() {
|
2007-02-15 19:18:12 +00:00
|
|
|
init_words(Bits, Capacity, false);
|
2007-02-15 05:56:11 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitVector &reset(unsigned Idx) {
|
2007-04-26 15:07:47 +00:00
|
|
|
Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
|
2007-02-15 05:56:11 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-10-15 22:05:27 +00:00
|
|
|
/// reset - Efficiently reset a range of bits in [I, E)
|
|
|
|
BitVector &reset(unsigned I, unsigned E) {
|
|
|
|
assert(I <= E && "Attempted to reset backwards range!");
|
|
|
|
assert(E <= size() && "Attempted to reset out-of-bounds range!");
|
|
|
|
|
|
|
|
if (I == E) return *this;
|
|
|
|
|
2012-10-16 06:04:27 +00:00
|
|
|
if (I / BITWORD_SIZE == E / BITWORD_SIZE) {
|
|
|
|
BitWord EMask = 1UL << (E % BITWORD_SIZE);
|
|
|
|
BitWord IMask = 1UL << (I % BITWORD_SIZE);
|
2012-10-15 22:05:27 +00:00
|
|
|
BitWord Mask = EMask - IMask;
|
|
|
|
Bits[I / BITWORD_SIZE] &= ~Mask;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitWord PrefixMask = ~0UL << (I % BITWORD_SIZE);
|
|
|
|
Bits[I / BITWORD_SIZE] &= ~PrefixMask;
|
|
|
|
I = RoundUpToAlignment(I, BITWORD_SIZE);
|
|
|
|
|
|
|
|
for (; I + BITWORD_SIZE <= E; I += BITWORD_SIZE)
|
|
|
|
Bits[I / BITWORD_SIZE] = 0UL;
|
|
|
|
|
|
|
|
BitWord PostfixMask = (1UL << (E % BITWORD_SIZE)) - 1;
|
2013-12-19 02:35:26 +00:00
|
|
|
if (I < E)
|
|
|
|
Bits[I / BITWORD_SIZE] &= ~PostfixMask;
|
2012-10-15 22:05:27 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
BitVector &flip() {
|
|
|
|
for (unsigned i = 0; i < NumBitWords(size()); ++i)
|
|
|
|
Bits[i] = ~Bits[i];
|
|
|
|
clear_unused_bits();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitVector &flip(unsigned Idx) {
|
2007-04-26 15:07:47 +00:00
|
|
|
Bits[Idx / BITWORD_SIZE] ^= 1L << (Idx % BITWORD_SIZE);
|
2007-02-15 05:56:11 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indexing.
|
|
|
|
reference operator[](unsigned Idx) {
|
2007-12-10 22:28:35 +00:00
|
|
|
assert (Idx < Size && "Out-of-bounds Bit access.");
|
2007-02-15 05:56:11 +00:00
|
|
|
return reference(*this, Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator[](unsigned Idx) const {
|
2009-01-09 19:25:42 +00:00
|
|
|
assert (Idx < Size && "Out-of-bounds Bit access.");
|
2007-04-26 15:07:47 +00:00
|
|
|
BitWord Mask = 1L << (Idx % BITWORD_SIZE);
|
|
|
|
return (Bits[Idx / BITWORD_SIZE] & Mask) != 0;
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool test(unsigned Idx) const {
|
|
|
|
return (*this)[Idx];
|
|
|
|
}
|
|
|
|
|
2012-05-14 15:01:19 +00:00
|
|
|
/// Test if any common bits are set.
|
|
|
|
bool anyCommon(const BitVector &RHS) const {
|
|
|
|
unsigned ThisWords = NumBitWords(size());
|
|
|
|
unsigned RHSWords = NumBitWords(RHS.size());
|
|
|
|
for (unsigned i = 0, e = std::min(ThisWords, RHSWords); i != e; ++i)
|
|
|
|
if (Bits[i] & RHS.Bits[i])
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
// Comparison operators.
|
|
|
|
bool operator==(const BitVector &RHS) const {
|
2007-10-12 03:48:59 +00:00
|
|
|
unsigned ThisWords = NumBitWords(size());
|
|
|
|
unsigned RHSWords = NumBitWords(RHS.size());
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
|
2007-02-15 05:56:11 +00:00
|
|
|
if (Bits[i] != RHS.Bits[i])
|
|
|
|
return false;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-12 03:48:59 +00:00
|
|
|
// Verify that any extra words are all zeros.
|
|
|
|
if (i != ThisWords) {
|
|
|
|
for (; i != ThisWords; ++i)
|
|
|
|
if (Bits[i])
|
|
|
|
return false;
|
|
|
|
} else if (i != RHSWords) {
|
|
|
|
for (; i != RHSWords; ++i)
|
|
|
|
if (RHS.Bits[i])
|
|
|
|
return false;
|
|
|
|
}
|
2007-02-15 05:56:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const BitVector &RHS) const {
|
|
|
|
return !(*this == RHS);
|
|
|
|
}
|
|
|
|
|
2012-09-06 09:17:37 +00:00
|
|
|
/// Intersection, union, disjoint union.
|
2009-01-26 11:07:20 +00:00
|
|
|
BitVector &operator&=(const BitVector &RHS) {
|
2007-10-11 06:12:33 +00:00
|
|
|
unsigned ThisWords = NumBitWords(size());
|
|
|
|
unsigned RHSWords = NumBitWords(RHS.size());
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
|
2007-02-15 05:56:11 +00:00
|
|
|
Bits[i] &= RHS.Bits[i];
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-10-11 06:12:33 +00:00
|
|
|
// Any bits that are just in this bitvector become zero, because they aren't
|
|
|
|
// in the RHS bit vector. Any words only in RHS are ignored because they
|
|
|
|
// are already zero in the LHS.
|
|
|
|
for (; i != ThisWords; ++i)
|
|
|
|
Bits[i] = 0;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-09-06 09:17:37 +00:00
|
|
|
/// reset - Reset bits that are set in RHS. Same as *this &= ~RHS.
|
2012-01-29 01:29:22 +00:00
|
|
|
BitVector &reset(const BitVector &RHS) {
|
|
|
|
unsigned ThisWords = NumBitWords(size());
|
|
|
|
unsigned RHSWords = NumBitWords(RHS.size());
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
|
|
|
|
Bits[i] &= ~RHS.Bits[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-09-06 09:17:37 +00:00
|
|
|
/// test - Check if (This - RHS) is zero.
|
|
|
|
/// This is the same as reset(RHS) and any().
|
|
|
|
bool test(const BitVector &RHS) const {
|
|
|
|
unsigned ThisWords = NumBitWords(size());
|
|
|
|
unsigned RHSWords = NumBitWords(RHS.size());
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i != std::min(ThisWords, RHSWords); ++i)
|
|
|
|
if ((Bits[i] & ~RHS.Bits[i]) != 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (; i != ThisWords ; ++i)
|
|
|
|
if (Bits[i] != 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-26 11:07:20 +00:00
|
|
|
BitVector &operator|=(const BitVector &RHS) {
|
2010-02-10 05:54:04 +00:00
|
|
|
if (size() < RHS.size())
|
|
|
|
resize(RHS.size());
|
|
|
|
for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
|
2007-02-15 05:56:11 +00:00
|
|
|
Bits[i] |= RHS.Bits[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-01-26 11:07:20 +00:00
|
|
|
BitVector &operator^=(const BitVector &RHS) {
|
2010-02-10 05:54:04 +00:00
|
|
|
if (size() < RHS.size())
|
|
|
|
resize(RHS.size());
|
|
|
|
for (size_t i = 0, e = NumBitWords(RHS.size()); i != e; ++i)
|
2007-02-15 05:56:11 +00:00
|
|
|
Bits[i] ^= RHS.Bits[i];
|
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
// Assignment operator.
|
|
|
|
const BitVector &operator=(const BitVector &RHS) {
|
|
|
|
if (this == &RHS) return *this;
|
|
|
|
|
2007-02-15 08:15:58 +00:00
|
|
|
Size = RHS.size();
|
|
|
|
unsigned RHSWords = NumBitWords(Size);
|
2007-04-26 15:07:47 +00:00
|
|
|
if (Size <= Capacity * BITWORD_SIZE) {
|
2010-03-16 19:07:05 +00:00
|
|
|
if (Size)
|
2010-11-26 18:25:20 +00:00
|
|
|
std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
|
2007-02-15 05:56:11 +00:00
|
|
|
clear_unused_bits();
|
|
|
|
return *this;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
// Grow the bitvector to have enough elements.
|
2007-08-06 20:52:17 +00:00
|
|
|
Capacity = RHSWords;
|
2010-11-26 18:25:20 +00:00
|
|
|
BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
|
|
|
|
std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
|
2007-02-15 05:56:11 +00:00
|
|
|
|
|
|
|
// Destroy the old bits.
|
2010-11-26 18:25:20 +00:00
|
|
|
std::free(Bits);
|
2007-02-15 05:56:11 +00:00
|
|
|
Bits = NewBits;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-06-01 18:52:53 +00:00
|
|
|
const BitVector &operator=(BitVector &&RHS) {
|
|
|
|
if (this == &RHS) return *this;
|
|
|
|
|
|
|
|
std::free(Bits);
|
|
|
|
Bits = RHS.Bits;
|
|
|
|
Size = RHS.Size;
|
|
|
|
Capacity = RHS.Capacity;
|
|
|
|
|
|
|
|
RHS.Bits = 0;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-01-05 15:04:49 +00:00
|
|
|
void swap(BitVector &RHS) {
|
|
|
|
std::swap(Bits, RHS.Bits);
|
|
|
|
std::swap(Size, RHS.Size);
|
|
|
|
std::swap(Capacity, RHS.Capacity);
|
|
|
|
}
|
|
|
|
|
2012-01-17 01:24:32 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Portable bit mask operations.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// These methods all operate on arrays of uint32_t, each holding 32 bits. The
|
|
|
|
// fixed word size makes it easier to work with literal bit vector constants
|
|
|
|
// in portable code.
|
|
|
|
//
|
|
|
|
// The LSB in each word is the lowest numbered bit. The size of a portable
|
|
|
|
// bit mask is always a whole multiple of 32 bits. If no bit mask size is
|
|
|
|
// given, the bit mask is assumed to cover the entire BitVector.
|
|
|
|
|
|
|
|
/// setBitsInMask - Add '1' bits from Mask to this vector. Don't resize.
|
|
|
|
/// This computes "*this |= Mask".
|
|
|
|
void setBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
|
|
|
|
applyMask<true, false>(Mask, MaskWords);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// clearBitsInMask - Clear any bits in this vector that are set in Mask.
|
|
|
|
/// Don't resize. This computes "*this &= ~Mask".
|
|
|
|
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
|
|
|
|
applyMask<false, false>(Mask, MaskWords);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// setBitsNotInMask - Add a bit to this vector for every '0' bit in Mask.
|
|
|
|
/// Don't resize. This computes "*this |= ~Mask".
|
|
|
|
void setBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
|
|
|
|
applyMask<true, true>(Mask, MaskWords);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// clearBitsNotInMask - Clear a bit in this vector for every '0' bit in Mask.
|
|
|
|
/// Don't resize. This computes "*this &= Mask".
|
|
|
|
void clearBitsNotInMask(const uint32_t *Mask, unsigned MaskWords = ~0u) {
|
|
|
|
applyMask<false, true>(Mask, MaskWords);
|
|
|
|
}
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
private:
|
|
|
|
unsigned NumBitWords(unsigned S) const {
|
2007-04-26 15:07:47 +00:00
|
|
|
return (S + BITWORD_SIZE-1) / BITWORD_SIZE;
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-08-06 20:52:17 +00:00
|
|
|
// Set the unused bits in the high words.
|
|
|
|
void set_unused_bits(bool t = true) {
|
|
|
|
// Set high words first.
|
|
|
|
unsigned UsedWords = NumBitWords(Size);
|
|
|
|
if (Capacity > UsedWords)
|
|
|
|
init_words(&Bits[UsedWords], (Capacity-UsedWords), t);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-08-06 20:52:17 +00:00
|
|
|
// Then set any stray high bits of the last used word.
|
2007-04-26 15:07:47 +00:00
|
|
|
unsigned ExtraBits = Size % BITWORD_SIZE;
|
2007-02-15 21:38:15 +00:00
|
|
|
if (ExtraBits) {
|
2012-08-24 23:29:28 +00:00
|
|
|
BitWord ExtraBitMask = ~0UL << ExtraBits;
|
|
|
|
if (t)
|
|
|
|
Bits[UsedWords-1] |= ExtraBitMask;
|
|
|
|
else
|
|
|
|
Bits[UsedWords-1] &= ~ExtraBitMask;
|
2007-02-15 18:59:15 +00:00
|
|
|
}
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
|
|
|
|
2007-08-06 20:52:17 +00:00
|
|
|
// Clear the unused bits in the high words.
|
|
|
|
void clear_unused_bits() {
|
|
|
|
set_unused_bits(false);
|
|
|
|
}
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
void grow(unsigned NewSize) {
|
2010-11-26 18:25:20 +00:00
|
|
|
Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
|
|
|
|
Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-12-10 22:28:35 +00:00
|
|
|
clear_unused_bits();
|
2007-02-15 05:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_words(BitWord *B, unsigned NumWords, bool t) {
|
2007-02-15 19:18:12 +00:00
|
|
|
memset(B, 0 - (int)t, NumWords*sizeof(BitWord));
|
2009-01-09 19:25:42 +00:00
|
|
|
}
|
2012-01-17 01:24:32 +00:00
|
|
|
|
|
|
|
template<bool AddBits, bool InvertMask>
|
|
|
|
void applyMask(const uint32_t *Mask, unsigned MaskWords) {
|
|
|
|
assert(BITWORD_SIZE % 32 == 0 && "Unsupported BitWord size.");
|
|
|
|
MaskWords = std::min(MaskWords, (size() + 31) / 32);
|
|
|
|
const unsigned Scale = BITWORD_SIZE / 32;
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; MaskWords >= Scale; ++i, MaskWords -= Scale) {
|
|
|
|
BitWord BW = Bits[i];
|
|
|
|
// This inner loop should unroll completely when BITWORD_SIZE > 32.
|
|
|
|
for (unsigned b = 0; b != BITWORD_SIZE; b += 32) {
|
|
|
|
uint32_t M = *Mask++;
|
|
|
|
if (InvertMask) M = ~M;
|
|
|
|
if (AddBits) BW |= BitWord(M) << b;
|
|
|
|
else BW &= ~(BitWord(M) << b);
|
|
|
|
}
|
|
|
|
Bits[i] = BW;
|
|
|
|
}
|
|
|
|
for (unsigned b = 0; MaskWords; b += 32, --MaskWords) {
|
|
|
|
uint32_t M = *Mask++;
|
|
|
|
if (InvertMask) M = ~M;
|
|
|
|
if (AddBits) Bits[i] |= BitWord(M) << b;
|
|
|
|
else Bits[i] &= ~(BitWord(M) << b);
|
|
|
|
}
|
|
|
|
if (AddBits)
|
|
|
|
clear_unused_bits();
|
|
|
|
}
|
2007-02-15 05:56:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End llvm namespace
|
2010-01-05 15:04:49 +00:00
|
|
|
|
|
|
|
namespace std {
|
|
|
|
/// Implement std::swap in terms of BitVector swap.
|
|
|
|
inline void
|
|
|
|
swap(llvm::BitVector &LHS, llvm::BitVector &RHS) {
|
|
|
|
LHS.swap(RHS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-15 05:56:11 +00:00
|
|
|
#endif
|