2007-09-09 03:10:40 +02:00
|
|
|
//===- llvm/ADT/SparseBitVector.h - Efficient Sparse BitVector -*- C++ -*- ===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-09 03:10:40 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the SparseBitVector class. See the doxygen comment for
|
|
|
|
// SparseBitVector for more details on the algorithm used.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_SPARSEBITVECTOR_H
|
|
|
|
#define LLVM_ADT_SPARSEBITVECTOR_H
|
|
|
|
|
2008-05-29 19:41:17 +02:00
|
|
|
#include "llvm/ADT/ilist.h"
|
2009-07-25 02:33:57 +02:00
|
|
|
#include "llvm/ADT/ilist_node.h"
|
2009-10-26 02:35:46 +01:00
|
|
|
#include "llvm/System/DataTypes.h"
|
2009-07-24 11:53:24 +02:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-09-05 10:50:14 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <climits>
|
|
|
|
#include <cstring>
|
2009-01-05 18:59:02 +01:00
|
|
|
|
2007-09-09 03:10:40 +02:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// SparseBitVector is an implementation of a bitvector that is sparse by only
|
|
|
|
/// storing the elements that have non-zero bits set. In order to make this
|
|
|
|
/// fast for the most common cases, SparseBitVector is implemented as a linked
|
|
|
|
/// list of SparseBitVectorElements. We maintain a pointer to the last
|
|
|
|
/// SparseBitVectorElement accessed (in the form of a list iterator), in order
|
|
|
|
/// to make multiple in-order test/set constant time after the first one is
|
|
|
|
/// executed. Note that using vectors to store SparseBitVectorElement's does
|
|
|
|
/// not work out very well because it causes insertion in the middle to take
|
|
|
|
/// enormous amounts of time with a large amount of bits. Other structures that
|
|
|
|
/// have better worst cases for insertion in the middle (various balanced trees,
|
|
|
|
/// etc) do not perform as well in practice as a linked list with this iterator
|
|
|
|
/// kept up to date. They are also significantly more memory intensive.
|
|
|
|
|
|
|
|
|
|
|
|
template <unsigned ElementSize = 128>
|
2008-07-28 23:51:04 +02:00
|
|
|
struct SparseBitVectorElement
|
2009-09-06 10:55:57 +02:00
|
|
|
: public ilist_node<SparseBitVectorElement<ElementSize> > {
|
2007-09-09 03:10:40 +02:00
|
|
|
public:
|
|
|
|
typedef unsigned long BitWord;
|
|
|
|
enum {
|
2009-04-01 20:45:54 +02:00
|
|
|
BITWORD_SIZE = sizeof(BitWord) * CHAR_BIT,
|
2007-09-09 03:10:40 +02:00
|
|
|
BITWORDS_PER_ELEMENT = (ElementSize + BITWORD_SIZE - 1) / BITWORD_SIZE,
|
|
|
|
BITS_PER_ELEMENT = ElementSize
|
|
|
|
};
|
2007-09-11 19:42:22 +02:00
|
|
|
|
2007-09-09 03:10:40 +02:00
|
|
|
private:
|
|
|
|
// Index of Element in terms of where first bit starts.
|
|
|
|
unsigned ElementIndex;
|
|
|
|
BitWord Bits[BITWORDS_PER_ELEMENT];
|
2007-09-11 19:42:22 +02:00
|
|
|
// Needed for sentinels
|
2009-05-12 23:50:43 +02:00
|
|
|
friend struct ilist_sentinel_traits<SparseBitVectorElement>;
|
2007-09-11 19:42:22 +02:00
|
|
|
SparseBitVectorElement() {
|
2008-03-18 17:55:06 +01:00
|
|
|
ElementIndex = ~0U;
|
2007-09-11 19:42:22 +02:00
|
|
|
memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
|
|
|
|
}
|
|
|
|
|
2007-09-09 03:10:40 +02:00
|
|
|
public:
|
|
|
|
explicit SparseBitVectorElement(unsigned Idx) {
|
|
|
|
ElementIndex = Idx;
|
|
|
|
memset(&Bits[0], 0, sizeof (BitWord) * BITWORDS_PER_ELEMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison.
|
|
|
|
bool operator==(const SparseBitVectorElement &RHS) const {
|
|
|
|
if (ElementIndex != RHS.ElementIndex)
|
|
|
|
return false;
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
|
|
|
|
if (Bits[i] != RHS.Bits[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const SparseBitVectorElement &RHS) const {
|
|
|
|
return !(*this == RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the bits that make up word Idx in our element.
|
|
|
|
BitWord word(unsigned Idx) const {
|
|
|
|
assert (Idx < BITWORDS_PER_ELEMENT);
|
|
|
|
return Bits[Idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned index() const {
|
|
|
|
return ElementIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
|
|
|
|
if (Bits[i])
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(unsigned Idx) {
|
|
|
|
Bits[Idx / BITWORD_SIZE] |= 1L << (Idx % BITWORD_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool test_and_set (unsigned Idx) {
|
|
|
|
bool old = test(Idx);
|
2007-09-17 01:59:53 +02:00
|
|
|
if (!old) {
|
2007-09-09 03:10:40 +02:00
|
|
|
set(Idx);
|
2007-09-17 01:59:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset(unsigned Idx) {
|
|
|
|
Bits[Idx / BITWORD_SIZE] &= ~(1L << (Idx % BITWORD_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool test(unsigned Idx) const {
|
|
|
|
return Bits[Idx / BITWORD_SIZE] & (1L << (Idx % BITWORD_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned count() const {
|
|
|
|
unsigned NumBits = 0;
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
|
|
|
|
if (sizeof(BitWord) == 4)
|
|
|
|
NumBits += CountPopulation_32(Bits[i]);
|
|
|
|
else if (sizeof(BitWord) == 8)
|
|
|
|
NumBits += CountPopulation_64(Bits[i]);
|
|
|
|
else
|
|
|
|
assert(0 && "Unsupported!");
|
|
|
|
return NumBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// find_first - Returns the index of the first set bit.
|
|
|
|
int find_first() const {
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i)
|
|
|
|
if (Bits[i] != 0) {
|
|
|
|
if (sizeof(BitWord) == 4)
|
|
|
|
return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
|
|
|
|
else if (sizeof(BitWord) == 8)
|
|
|
|
return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
|
|
|
|
else
|
|
|
|
assert(0 && "Unsupported!");
|
|
|
|
}
|
|
|
|
assert(0 && "Illegal empty element");
|
2008-03-30 20:22:13 +02:00
|
|
|
return 0; // Not reached
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
|
|
|
|
2007-10-04 23:27:17 +02:00
|
|
|
/// find_next - Returns the index of the next set bit starting from the
|
|
|
|
/// "Curr" bit. Returns -1 if the next set bit is not found.
|
|
|
|
int find_next(unsigned Curr) const {
|
|
|
|
if (Curr >= BITS_PER_ELEMENT)
|
2007-09-09 03:10:40 +02:00
|
|
|
return -1;
|
|
|
|
|
2007-10-04 23:27:17 +02:00
|
|
|
unsigned WordPos = Curr / BITWORD_SIZE;
|
|
|
|
unsigned BitPos = Curr % BITWORD_SIZE;
|
2007-09-09 03:10:40 +02:00
|
|
|
BitWord Copy = Bits[WordPos];
|
|
|
|
assert (WordPos <= BITWORDS_PER_ELEMENT
|
|
|
|
&& "Word Position outside of element");
|
|
|
|
|
|
|
|
// Mask off previous bits.
|
|
|
|
Copy &= ~0L << BitPos;
|
|
|
|
|
|
|
|
if (Copy != 0) {
|
|
|
|
if (sizeof(BitWord) == 4)
|
|
|
|
return WordPos * BITWORD_SIZE + CountTrailingZeros_32(Copy);
|
|
|
|
else if (sizeof(BitWord) == 8)
|
|
|
|
return WordPos * BITWORD_SIZE + CountTrailingZeros_64(Copy);
|
|
|
|
else
|
|
|
|
assert(0 && "Unsupported!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check subsequent words.
|
|
|
|
for (unsigned i = WordPos+1; i < BITWORDS_PER_ELEMENT; ++i)
|
|
|
|
if (Bits[i] != 0) {
|
|
|
|
if (sizeof(BitWord) == 4)
|
|
|
|
return i * BITWORD_SIZE + CountTrailingZeros_32(Bits[i]);
|
|
|
|
else if (sizeof(BitWord) == 8)
|
|
|
|
return i * BITWORD_SIZE + CountTrailingZeros_64(Bits[i]);
|
|
|
|
else
|
|
|
|
assert(0 && "Unsupported!");
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Union this element with RHS and return true if this one changed.
|
|
|
|
bool unionWith(const SparseBitVectorElement &RHS) {
|
|
|
|
bool changed = false;
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
|
|
|
|
BitWord old = changed ? 0 : Bits[i];
|
|
|
|
|
|
|
|
Bits[i] |= RHS.Bits[i];
|
2007-09-17 00:31:47 +02:00
|
|
|
if (!changed && old != Bits[i])
|
2007-09-09 03:10:40 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
// Return true if we have any bits in common with RHS
|
|
|
|
bool intersects(const SparseBitVectorElement &RHS) const {
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
|
|
|
|
if (RHS.Bits[i] & Bits[i])
|
2007-09-11 06:11:28 +02:00
|
|
|
return true;
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
|
2007-09-09 03:10:40 +02:00
|
|
|
// Intersect this Element with RHS and return true if this one changed.
|
|
|
|
// BecameZero is set to true if this element became all-zero bits.
|
|
|
|
bool intersectWith(const SparseBitVectorElement &RHS,
|
|
|
|
bool &BecameZero) {
|
|
|
|
bool changed = false;
|
|
|
|
bool allzero = true;
|
|
|
|
|
|
|
|
BecameZero = false;
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
|
|
|
|
BitWord old = changed ? 0 : Bits[i];
|
|
|
|
|
|
|
|
Bits[i] &= RHS.Bits[i];
|
|
|
|
if (Bits[i] != 0)
|
|
|
|
allzero = false;
|
|
|
|
|
2007-09-17 00:31:47 +02:00
|
|
|
if (!changed && old != Bits[i])
|
2007-09-09 03:10:40 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
2007-09-17 00:31:47 +02:00
|
|
|
BecameZero = allzero;
|
2007-09-09 03:10:40 +02:00
|
|
|
return changed;
|
|
|
|
}
|
2007-09-11 03:38:07 +02:00
|
|
|
// Intersect this Element with the complement of RHS and return true if this
|
|
|
|
// one changed. BecameZero is set to true if this element became all-zero
|
|
|
|
// bits.
|
|
|
|
bool intersectWithComplement(const SparseBitVectorElement &RHS,
|
2007-09-17 00:31:47 +02:00
|
|
|
bool &BecameZero) {
|
2007-09-11 03:38:07 +02:00
|
|
|
bool changed = false;
|
|
|
|
bool allzero = true;
|
|
|
|
|
|
|
|
BecameZero = false;
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
|
|
|
|
BitWord old = changed ? 0 : Bits[i];
|
|
|
|
|
|
|
|
Bits[i] &= ~RHS.Bits[i];
|
|
|
|
if (Bits[i] != 0)
|
|
|
|
allzero = false;
|
|
|
|
|
2007-09-17 00:31:47 +02:00
|
|
|
if (!changed && old != Bits[i])
|
2007-09-11 03:38:07 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
2007-09-17 00:31:47 +02:00
|
|
|
BecameZero = allzero;
|
2007-09-11 03:38:07 +02:00
|
|
|
return changed;
|
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
// Three argument version of intersectWithComplement that intersects
|
|
|
|
// RHS1 & ~RHS2 into this element
|
|
|
|
void intersectWithComplement(const SparseBitVectorElement &RHS1,
|
|
|
|
const SparseBitVectorElement &RHS2,
|
|
|
|
bool &BecameZero) {
|
|
|
|
bool allzero = true;
|
|
|
|
|
|
|
|
BecameZero = false;
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
|
|
|
|
Bits[i] = RHS1.Bits[i] & ~RHS2.Bits[i];
|
|
|
|
if (Bits[i] != 0)
|
|
|
|
allzero = false;
|
|
|
|
}
|
2007-09-17 00:31:47 +02:00
|
|
|
BecameZero = allzero;
|
2007-09-11 06:11:28 +02:00
|
|
|
}
|
2007-09-25 00:20:45 +02:00
|
|
|
|
2007-09-24 21:45:49 +02:00
|
|
|
// Get a hash value for this element;
|
|
|
|
uint64_t getHashValue() const {
|
|
|
|
uint64_t HashVal = 0;
|
|
|
|
for (unsigned i = 0; i < BITWORDS_PER_ELEMENT; ++i) {
|
|
|
|
HashVal ^= Bits[i];
|
|
|
|
}
|
|
|
|
return HashVal;
|
|
|
|
}
|
2007-09-09 03:10:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <unsigned ElementSize = 128>
|
|
|
|
class SparseBitVector {
|
2007-09-11 19:42:22 +02:00
|
|
|
typedef ilist<SparseBitVectorElement<ElementSize> > ElementList;
|
2007-09-09 03:10:40 +02:00
|
|
|
typedef typename ElementList::iterator ElementListIter;
|
|
|
|
typedef typename ElementList::const_iterator ElementListConstIter;
|
|
|
|
enum {
|
|
|
|
BITWORD_SIZE = SparseBitVectorElement<ElementSize>::BITWORD_SIZE
|
|
|
|
};
|
|
|
|
|
|
|
|
// Pointer to our current Element.
|
|
|
|
ElementListIter CurrElementIter;
|
|
|
|
ElementList Elements;
|
|
|
|
|
|
|
|
// This is like std::lower_bound, except we do linear searching from the
|
|
|
|
// current position.
|
|
|
|
ElementListIter FindLowerBound(unsigned ElementIndex) {
|
|
|
|
|
|
|
|
if (Elements.empty()) {
|
|
|
|
CurrElementIter = Elements.begin();
|
|
|
|
return Elements.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure our current iterator is valid.
|
|
|
|
if (CurrElementIter == Elements.end())
|
|
|
|
--CurrElementIter;
|
|
|
|
|
|
|
|
// Search from our current iterator, either backwards or forwards,
|
|
|
|
// depending on what element we are looking for.
|
|
|
|
ElementListIter ElementIter = CurrElementIter;
|
2007-09-11 19:42:22 +02:00
|
|
|
if (CurrElementIter->index() == ElementIndex) {
|
2007-09-09 03:10:40 +02:00
|
|
|
return ElementIter;
|
2007-09-11 19:42:22 +02:00
|
|
|
} else if (CurrElementIter->index() > ElementIndex) {
|
2007-09-09 03:10:40 +02:00
|
|
|
while (ElementIter != Elements.begin()
|
2007-09-11 19:42:22 +02:00
|
|
|
&& ElementIter->index() > ElementIndex)
|
2007-09-09 03:10:40 +02:00
|
|
|
--ElementIter;
|
|
|
|
} else {
|
|
|
|
while (ElementIter != Elements.end() &&
|
2007-10-04 07:25:51 +02:00
|
|
|
ElementIter->index() < ElementIndex)
|
2007-09-09 03:10:40 +02:00
|
|
|
++ElementIter;
|
|
|
|
}
|
|
|
|
CurrElementIter = ElementIter;
|
|
|
|
return ElementIter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator to walk set bits in the bitmap. This iterator is a lot uglier
|
|
|
|
// than it would be, in order to be efficient.
|
2007-09-11 03:38:07 +02:00
|
|
|
class SparseBitVectorIterator {
|
2007-09-09 03:10:40 +02:00
|
|
|
private:
|
|
|
|
bool AtEnd;
|
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
const SparseBitVector<ElementSize> *BitVector;
|
2007-09-09 03:10:40 +02:00
|
|
|
|
|
|
|
// Current element inside of bitmap.
|
|
|
|
ElementListConstIter Iter;
|
|
|
|
|
|
|
|
// Current bit number inside of our bitmap.
|
|
|
|
unsigned BitNumber;
|
|
|
|
|
|
|
|
// Current word number inside of our element.
|
|
|
|
unsigned WordNumber;
|
|
|
|
|
|
|
|
// Current bits from the element.
|
|
|
|
typename SparseBitVectorElement<ElementSize>::BitWord Bits;
|
|
|
|
|
|
|
|
// Move our iterator to the first non-zero bit in the bitmap.
|
|
|
|
void AdvanceToFirstNonZero() {
|
|
|
|
if (AtEnd)
|
|
|
|
return;
|
2007-09-11 03:38:07 +02:00
|
|
|
if (BitVector->Elements.empty()) {
|
2007-09-09 03:10:40 +02:00
|
|
|
AtEnd = true;
|
|
|
|
return;
|
|
|
|
}
|
2007-09-11 03:38:07 +02:00
|
|
|
Iter = BitVector->Elements.begin();
|
2007-09-11 19:42:22 +02:00
|
|
|
BitNumber = Iter->index() * ElementSize;
|
|
|
|
unsigned BitPos = Iter->find_first();
|
2007-09-09 03:10:40 +02:00
|
|
|
BitNumber += BitPos;
|
|
|
|
WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
|
2007-09-11 19:42:22 +02:00
|
|
|
Bits = Iter->word(WordNumber);
|
2007-09-09 03:10:40 +02:00
|
|
|
Bits >>= BitPos % BITWORD_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move our iterator to the next non-zero bit.
|
|
|
|
void AdvanceToNextNonZero() {
|
|
|
|
if (AtEnd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (Bits && !(Bits & 1)) {
|
|
|
|
Bits >>= 1;
|
|
|
|
BitNumber += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we ran out of Bits in this word.
|
|
|
|
if (!Bits) {
|
2007-10-04 23:27:17 +02:00
|
|
|
int NextSetBitNumber = Iter->find_next(BitNumber % ElementSize) ;
|
2007-09-09 03:10:40 +02:00
|
|
|
// If we ran out of set bits in this element, move to next element.
|
|
|
|
if (NextSetBitNumber == -1 || (BitNumber % ElementSize == 0)) {
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter;
|
2007-09-09 03:10:40 +02:00
|
|
|
WordNumber = 0;
|
|
|
|
|
|
|
|
// We may run out of elements in the bitmap.
|
2007-09-11 03:38:07 +02:00
|
|
|
if (Iter == BitVector->Elements.end()) {
|
2007-09-09 03:10:40 +02:00
|
|
|
AtEnd = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set up for next non zero word in bitmap.
|
2007-09-11 19:42:22 +02:00
|
|
|
BitNumber = Iter->index() * ElementSize;
|
|
|
|
NextSetBitNumber = Iter->find_first();
|
2007-09-09 03:10:40 +02:00
|
|
|
BitNumber += NextSetBitNumber;
|
|
|
|
WordNumber = (BitNumber % ElementSize) / BITWORD_SIZE;
|
2007-09-11 19:42:22 +02:00
|
|
|
Bits = Iter->word(WordNumber);
|
2007-09-09 03:10:40 +02:00
|
|
|
Bits >>= NextSetBitNumber % BITWORD_SIZE;
|
|
|
|
} else {
|
|
|
|
WordNumber = (NextSetBitNumber % ElementSize) / BITWORD_SIZE;
|
2007-09-11 19:42:22 +02:00
|
|
|
Bits = Iter->word(WordNumber);
|
2007-09-09 03:10:40 +02:00
|
|
|
Bits >>= NextSetBitNumber % BITWORD_SIZE;
|
2007-09-17 20:05:59 +02:00
|
|
|
BitNumber = Iter->index() * ElementSize;
|
|
|
|
BitNumber += NextSetBitNumber;
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
// Preincrement.
|
|
|
|
inline SparseBitVectorIterator& operator++() {
|
2007-09-11 06:11:28 +02:00
|
|
|
++BitNumber;
|
2007-09-09 03:10:40 +02:00
|
|
|
Bits >>= 1;
|
|
|
|
AdvanceToNextNonZero();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Postincrement.
|
|
|
|
inline SparseBitVectorIterator operator++(int) {
|
|
|
|
SparseBitVectorIterator tmp = *this;
|
|
|
|
++*this;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the current set bit number.
|
|
|
|
unsigned operator*() const {
|
|
|
|
return BitNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const SparseBitVectorIterator &RHS) const {
|
|
|
|
// If they are both at the end, ignore the rest of the fields.
|
2007-09-26 02:11:59 +02:00
|
|
|
if (AtEnd && RHS.AtEnd)
|
2007-09-09 03:10:40 +02:00
|
|
|
return true;
|
|
|
|
// Otherwise they are the same if they have the same bit number and
|
|
|
|
// bitmap.
|
|
|
|
return AtEnd == RHS.AtEnd && RHS.BitNumber == BitNumber;
|
|
|
|
}
|
|
|
|
bool operator!=(const SparseBitVectorIterator &RHS) const {
|
|
|
|
return !(*this == RHS);
|
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
SparseBitVectorIterator(): BitVector(NULL) {
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
|
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
SparseBitVectorIterator(const SparseBitVector<ElementSize> *RHS,
|
|
|
|
bool end = false):BitVector(RHS) {
|
|
|
|
Iter = BitVector->Elements.begin();
|
2007-09-09 03:10:40 +02:00
|
|
|
BitNumber = 0;
|
|
|
|
Bits = 0;
|
|
|
|
WordNumber = ~0;
|
|
|
|
AtEnd = end;
|
|
|
|
AdvanceToFirstNonZero();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
typedef SparseBitVectorIterator iterator;
|
|
|
|
|
|
|
|
SparseBitVector () {
|
|
|
|
CurrElementIter = Elements.begin ();
|
|
|
|
}
|
|
|
|
|
|
|
|
~SparseBitVector() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// SparseBitVector copy ctor.
|
|
|
|
SparseBitVector(const SparseBitVector &RHS) {
|
|
|
|
ElementListConstIter ElementIter = RHS.Elements.begin();
|
|
|
|
while (ElementIter != RHS.Elements.end()) {
|
2007-09-11 19:42:22 +02:00
|
|
|
Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
|
|
|
|
++ElementIter;
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CurrElementIter = Elements.begin ();
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2009-03-21 06:40:09 +01:00
|
|
|
// Clear.
|
|
|
|
void clear() {
|
|
|
|
Elements.clear();
|
|
|
|
}
|
|
|
|
|
2008-04-07 19:38:23 +02:00
|
|
|
// Assignment
|
|
|
|
SparseBitVector& operator=(const SparseBitVector& RHS) {
|
|
|
|
Elements.clear();
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2008-04-07 19:38:23 +02:00
|
|
|
ElementListConstIter ElementIter = RHS.Elements.begin();
|
|
|
|
while (ElementIter != RHS.Elements.end()) {
|
|
|
|
Elements.push_back(SparseBitVectorElement<ElementSize>(*ElementIter));
|
|
|
|
++ElementIter;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrElementIter = Elements.begin ();
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2008-04-07 19:38:23 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2007-09-09 03:10:40 +02:00
|
|
|
|
|
|
|
// Test, Reset, and Set a bit in the bitmap.
|
|
|
|
bool test(unsigned Idx) {
|
|
|
|
if (Elements.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned ElementIndex = Idx / ElementSize;
|
|
|
|
ElementListIter ElementIter = FindLowerBound(ElementIndex);
|
|
|
|
|
|
|
|
// If we can't find an element that is supposed to contain this bit, there
|
|
|
|
// is nothing more to do.
|
|
|
|
if (ElementIter == Elements.end() ||
|
2007-09-11 19:42:22 +02:00
|
|
|
ElementIter->index() != ElementIndex)
|
2007-09-09 03:10:40 +02:00
|
|
|
return false;
|
2007-09-11 19:42:22 +02:00
|
|
|
return ElementIter->test(Idx % ElementSize);
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset(unsigned Idx) {
|
|
|
|
if (Elements.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned ElementIndex = Idx / ElementSize;
|
|
|
|
ElementListIter ElementIter = FindLowerBound(ElementIndex);
|
|
|
|
|
|
|
|
// If we can't find an element that is supposed to contain this bit, there
|
|
|
|
// is nothing more to do.
|
|
|
|
if (ElementIter == Elements.end() ||
|
2007-09-11 19:42:22 +02:00
|
|
|
ElementIter->index() != ElementIndex)
|
2007-09-09 03:10:40 +02:00
|
|
|
return;
|
2007-09-11 19:42:22 +02:00
|
|
|
ElementIter->reset(Idx % ElementSize);
|
2007-09-09 03:10:40 +02:00
|
|
|
|
|
|
|
// When the element is zeroed out, delete it.
|
2007-09-11 19:42:22 +02:00
|
|
|
if (ElementIter->empty()) {
|
2007-09-09 03:10:40 +02:00
|
|
|
++CurrElementIter;
|
|
|
|
Elements.erase(ElementIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(unsigned Idx) {
|
|
|
|
unsigned ElementIndex = Idx / ElementSize;
|
2007-09-11 19:42:22 +02:00
|
|
|
SparseBitVectorElement<ElementSize> *Element;
|
|
|
|
ElementListIter ElementIter;
|
2007-09-09 03:10:40 +02:00
|
|
|
if (Elements.empty()) {
|
|
|
|
Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
|
2007-09-11 19:42:22 +02:00
|
|
|
ElementIter = Elements.insert(Elements.end(), Element);
|
|
|
|
|
2007-09-09 03:10:40 +02:00
|
|
|
} else {
|
2007-09-11 19:42:22 +02:00
|
|
|
ElementIter = FindLowerBound(ElementIndex);
|
2007-09-09 03:10:40 +02:00
|
|
|
|
2007-09-11 19:42:22 +02:00
|
|
|
if (ElementIter == Elements.end() ||
|
|
|
|
ElementIter->index() != ElementIndex) {
|
2007-09-09 03:10:40 +02:00
|
|
|
Element = new SparseBitVectorElement<ElementSize>(ElementIndex);
|
2007-10-04 07:25:51 +02:00
|
|
|
// We may have hit the beginning of our SparseBitVector, in which case,
|
|
|
|
// we may need to insert right after this element, which requires moving
|
|
|
|
// the current iterator forward one, because insert does insert before.
|
2007-10-04 07:29:36 +02:00
|
|
|
if (ElementIter != Elements.end() &&
|
|
|
|
ElementIter->index() < ElementIndex)
|
2007-10-04 07:25:51 +02:00
|
|
|
ElementIter = Elements.insert(++ElementIter, Element);
|
|
|
|
else
|
|
|
|
ElementIter = Elements.insert(ElementIter, Element);
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
|
|
|
}
|
2007-10-04 07:25:51 +02:00
|
|
|
CurrElementIter = ElementIter;
|
2007-10-04 23:27:17 +02:00
|
|
|
|
2007-09-11 19:42:22 +02:00
|
|
|
ElementIter->set(Idx % ElementSize);
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
bool test_and_set (unsigned Idx) {
|
|
|
|
bool old = test(Idx);
|
2007-09-17 01:59:53 +02:00
|
|
|
if (!old) {
|
2007-09-11 03:38:07 +02:00
|
|
|
set(Idx);
|
2007-09-17 01:59:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-09-24 21:45:49 +02:00
|
|
|
bool operator!=(const SparseBitVector &RHS) const {
|
2007-09-17 01:59:53 +02:00
|
|
|
return !(*this == RHS);
|
|
|
|
}
|
|
|
|
|
2007-09-24 21:45:49 +02:00
|
|
|
bool operator==(const SparseBitVector &RHS) const {
|
2007-09-17 01:59:53 +02:00
|
|
|
ElementListConstIter Iter1 = Elements.begin();
|
|
|
|
ElementListConstIter Iter2 = RHS.Elements.begin();
|
|
|
|
|
2007-09-24 21:45:49 +02:00
|
|
|
for (; Iter1 != Elements.end() && Iter2 != RHS.Elements.end();
|
|
|
|
++Iter1, ++Iter2) {
|
|
|
|
if (*Iter1 != *Iter2)
|
2007-09-17 01:59:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-24 21:45:49 +02:00
|
|
|
return Iter1 == Elements.end() && Iter2 == RHS.Elements.end();
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
2007-09-09 03:10:40 +02:00
|
|
|
|
|
|
|
// Union our bitmap with the RHS and return true if we changed.
|
|
|
|
bool operator|=(const SparseBitVector &RHS) {
|
|
|
|
bool changed = false;
|
|
|
|
ElementListIter Iter1 = Elements.begin();
|
|
|
|
ElementListConstIter Iter2 = RHS.Elements.begin();
|
|
|
|
|
2007-10-05 07:10:53 +02:00
|
|
|
// If RHS is empty, we are done
|
|
|
|
if (RHS.Elements.empty())
|
2007-09-09 03:10:40 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
while (Iter2 != RHS.Elements.end()) {
|
2007-09-11 19:42:22 +02:00
|
|
|
if (Iter1 == Elements.end() || Iter1->index() > Iter2->index()) {
|
|
|
|
Elements.insert(Iter1,
|
|
|
|
new SparseBitVectorElement<ElementSize>(*Iter2));
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter2;
|
2007-09-09 03:10:40 +02:00
|
|
|
changed = true;
|
2007-09-11 19:42:22 +02:00
|
|
|
} else if (Iter1->index() == Iter2->index()) {
|
|
|
|
changed |= Iter1->unionWith(*Iter2);
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter1;
|
|
|
|
++Iter2;
|
2007-09-09 03:10:40 +02:00
|
|
|
} else {
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter1;
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CurrElementIter = Elements.begin();
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersect our bitmap with the RHS and return true if ours changed.
|
|
|
|
bool operator&=(const SparseBitVector &RHS) {
|
|
|
|
bool changed = false;
|
|
|
|
ElementListIter Iter1 = Elements.begin();
|
|
|
|
ElementListConstIter Iter2 = RHS.Elements.begin();
|
|
|
|
|
2007-09-11 19:42:22 +02:00
|
|
|
// Check if both bitmaps are empty.
|
|
|
|
if (Elements.empty() && RHS.Elements.empty())
|
2007-09-09 03:10:40 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Loop through, intersecting as we go, erasing elements when necessary.
|
|
|
|
while (Iter2 != RHS.Elements.end()) {
|
2007-11-15 19:06:49 +01:00
|
|
|
if (Iter1 == Elements.end()) {
|
|
|
|
CurrElementIter = Elements.begin();
|
2007-09-09 03:10:40 +02:00
|
|
|
return changed;
|
2007-11-15 19:06:49 +01:00
|
|
|
}
|
2007-09-09 03:10:40 +02:00
|
|
|
|
2007-09-11 19:42:22 +02:00
|
|
|
if (Iter1->index() > Iter2->index()) {
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter2;
|
2007-09-11 19:42:22 +02:00
|
|
|
} else if (Iter1->index() == Iter2->index()) {
|
2007-09-09 03:10:40 +02:00
|
|
|
bool BecameZero;
|
2007-09-11 19:42:22 +02:00
|
|
|
changed |= Iter1->intersectWith(*Iter2, BecameZero);
|
2007-09-09 03:10:40 +02:00
|
|
|
if (BecameZero) {
|
|
|
|
ElementListIter IterTmp = Iter1;
|
2007-09-17 00:31:47 +02:00
|
|
|
++Iter1;
|
2007-09-09 03:10:40 +02:00
|
|
|
Elements.erase(IterTmp);
|
2007-09-17 00:31:47 +02:00
|
|
|
} else {
|
|
|
|
++Iter1;
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter2;
|
2007-09-09 03:10:40 +02:00
|
|
|
} else {
|
|
|
|
ElementListIter IterTmp = Iter1;
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter1;
|
2007-09-09 03:10:40 +02:00
|
|
|
Elements.erase(IterTmp);
|
|
|
|
}
|
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
Elements.erase(Iter1, Elements.end());
|
2007-09-09 03:10:40 +02:00
|
|
|
CurrElementIter = Elements.begin();
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2009-05-11 19:04:19 +02:00
|
|
|
// Intersect our bitmap with the complement of the RHS and return true
|
|
|
|
// if ours changed.
|
2007-09-11 03:38:07 +02:00
|
|
|
bool intersectWithComplement(const SparseBitVector &RHS) {
|
|
|
|
bool changed = false;
|
|
|
|
ElementListIter Iter1 = Elements.begin();
|
|
|
|
ElementListConstIter Iter2 = RHS.Elements.begin();
|
|
|
|
|
2007-10-07 06:47:57 +02:00
|
|
|
// If either our bitmap or RHS is empty, we are done
|
|
|
|
if (Elements.empty() || RHS.Elements.empty())
|
2007-09-11 03:38:07 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Loop through, intersecting as we go, erasing elements when necessary.
|
|
|
|
while (Iter2 != RHS.Elements.end()) {
|
2007-11-15 19:06:49 +01:00
|
|
|
if (Iter1 == Elements.end()) {
|
|
|
|
CurrElementIter = Elements.begin();
|
2007-09-11 03:38:07 +02:00
|
|
|
return changed;
|
2007-11-15 19:06:49 +01:00
|
|
|
}
|
2007-09-11 03:38:07 +02:00
|
|
|
|
2007-09-11 19:42:22 +02:00
|
|
|
if (Iter1->index() > Iter2->index()) {
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter2;
|
2007-09-11 19:42:22 +02:00
|
|
|
} else if (Iter1->index() == Iter2->index()) {
|
2007-09-11 03:38:07 +02:00
|
|
|
bool BecameZero;
|
2007-09-11 19:42:22 +02:00
|
|
|
changed |= Iter1->intersectWithComplement(*Iter2, BecameZero);
|
2007-09-11 03:38:07 +02:00
|
|
|
if (BecameZero) {
|
|
|
|
ElementListIter IterTmp = Iter1;
|
2007-09-17 00:31:47 +02:00
|
|
|
++Iter1;
|
2007-09-11 03:38:07 +02:00
|
|
|
Elements.erase(IterTmp);
|
2007-09-17 00:31:47 +02:00
|
|
|
} else {
|
|
|
|
++Iter1;
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter2;
|
2007-09-11 03:38:07 +02:00
|
|
|
} else {
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter1;
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CurrElementIter = Elements.begin();
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool intersectWithComplement(const SparseBitVector<ElementSize> *RHS) const {
|
|
|
|
return intersectWithComplement(*RHS);
|
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
|
|
|
|
|
2009-05-11 19:04:19 +02:00
|
|
|
// Three argument version of intersectWithComplement.
|
|
|
|
// Result of RHS1 & ~RHS2 is stored into this bitmap.
|
2007-09-11 06:11:28 +02:00
|
|
|
void intersectWithComplement(const SparseBitVector<ElementSize> &RHS1,
|
|
|
|
const SparseBitVector<ElementSize> &RHS2)
|
|
|
|
{
|
|
|
|
Elements.clear();
|
2007-11-15 19:06:49 +01:00
|
|
|
CurrElementIter = Elements.begin();
|
2007-09-11 06:11:28 +02:00
|
|
|
ElementListConstIter Iter1 = RHS1.Elements.begin();
|
|
|
|
ElementListConstIter Iter2 = RHS2.Elements.begin();
|
|
|
|
|
2007-10-07 06:47:57 +02:00
|
|
|
// If RHS1 is empty, we are done
|
|
|
|
// If RHS2 is empty, we still have to copy RHS1
|
|
|
|
if (RHS1.Elements.empty())
|
2007-09-11 06:11:28 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Loop through, intersecting as we go, erasing elements when necessary.
|
|
|
|
while (Iter2 != RHS2.Elements.end()) {
|
|
|
|
if (Iter1 == RHS1.Elements.end())
|
|
|
|
return;
|
|
|
|
|
2007-09-11 19:42:22 +02:00
|
|
|
if (Iter1->index() > Iter2->index()) {
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter2;
|
2007-09-11 19:42:22 +02:00
|
|
|
} else if (Iter1->index() == Iter2->index()) {
|
2007-09-11 06:11:28 +02:00
|
|
|
bool BecameZero = false;
|
|
|
|
SparseBitVectorElement<ElementSize> *NewElement =
|
2007-09-11 19:42:22 +02:00
|
|
|
new SparseBitVectorElement<ElementSize>(Iter1->index());
|
|
|
|
NewElement->intersectWithComplement(*Iter1, *Iter2, BecameZero);
|
|
|
|
if (!BecameZero) {
|
2007-09-11 06:11:28 +02:00
|
|
|
Elements.push_back(NewElement);
|
|
|
|
}
|
2007-09-11 19:42:22 +02:00
|
|
|
else
|
|
|
|
delete NewElement;
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter1;
|
|
|
|
++Iter2;
|
|
|
|
} else {
|
2007-10-07 06:47:57 +02:00
|
|
|
SparseBitVectorElement<ElementSize> *NewElement =
|
|
|
|
new SparseBitVectorElement<ElementSize>(*Iter1);
|
|
|
|
Elements.push_back(NewElement);
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter1;
|
|
|
|
}
|
|
|
|
}
|
2007-09-11 19:42:22 +02:00
|
|
|
|
2007-09-11 06:11:28 +02:00
|
|
|
// copy the remaining elements
|
|
|
|
while (Iter1 != RHS1.Elements.end()) {
|
|
|
|
SparseBitVectorElement<ElementSize> *NewElement =
|
2007-09-11 19:42:22 +02:00
|
|
|
new SparseBitVectorElement<ElementSize>(*Iter1);
|
2007-09-11 06:11:28 +02:00
|
|
|
Elements.push_back(NewElement);
|
2007-09-11 19:42:22 +02:00
|
|
|
++Iter1;
|
2007-09-11 06:11:28 +02:00
|
|
|
}
|
2007-09-11 19:42:22 +02:00
|
|
|
|
2007-09-11 06:11:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void intersectWithComplement(const SparseBitVector<ElementSize> *RHS1,
|
|
|
|
const SparseBitVector<ElementSize> *RHS2) {
|
|
|
|
intersectWithComplement(*RHS1, *RHS2);
|
|
|
|
}
|
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
bool intersects(const SparseBitVector<ElementSize> *RHS) const {
|
|
|
|
return intersects(*RHS);
|
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
// Return true if we share any bits in common with RHS
|
|
|
|
bool intersects(const SparseBitVector<ElementSize> &RHS) const {
|
|
|
|
ElementListConstIter Iter1 = Elements.begin();
|
|
|
|
ElementListConstIter Iter2 = RHS.Elements.begin();
|
|
|
|
|
2007-09-11 19:42:22 +02:00
|
|
|
// Check if both bitmaps are empty.
|
|
|
|
if (Elements.empty() && RHS.Elements.empty())
|
2007-09-11 03:38:07 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Loop through, intersecting stopping when we hit bits in common.
|
|
|
|
while (Iter2 != RHS.Elements.end()) {
|
|
|
|
if (Iter1 == Elements.end())
|
|
|
|
return false;
|
|
|
|
|
2007-09-11 19:42:22 +02:00
|
|
|
if (Iter1->index() > Iter2->index()) {
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter2;
|
2007-09-11 19:42:22 +02:00
|
|
|
} else if (Iter1->index() == Iter2->index()) {
|
|
|
|
if (Iter1->intersects(*Iter2))
|
2007-09-11 03:38:07 +02:00
|
|
|
return true;
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter1;
|
|
|
|
++Iter2;
|
2007-09-11 03:38:07 +02:00
|
|
|
} else {
|
2007-09-11 06:11:28 +02:00
|
|
|
++Iter1;
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
|
2009-05-11 19:04:19 +02:00
|
|
|
// Return true iff all bits set in this SparseBitVector are
|
|
|
|
// also set in RHS.
|
|
|
|
bool contains(const SparseBitVector<ElementSize> &RHS) const {
|
|
|
|
SparseBitVector<ElementSize> Result(*this);
|
|
|
|
Result &= RHS;
|
|
|
|
return (Result == RHS);
|
|
|
|
}
|
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
// Return the first set bit in the bitmap. Return -1 if no bits are set.
|
|
|
|
int find_first() const {
|
|
|
|
if (Elements.empty())
|
|
|
|
return -1;
|
2007-09-11 19:42:22 +02:00
|
|
|
const SparseBitVectorElement<ElementSize> &First = *(Elements.begin());
|
|
|
|
return (First.index() * ElementSize) + First.find_first();
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the SparseBitVector is empty
|
|
|
|
bool empty() const {
|
|
|
|
return Elements.empty();
|
|
|
|
}
|
2007-09-11 06:11:28 +02:00
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
unsigned count() const {
|
|
|
|
unsigned BitCount = 0;
|
|
|
|
for (ElementListConstIter Iter = Elements.begin();
|
|
|
|
Iter != Elements.end();
|
|
|
|
++Iter)
|
2007-09-11 19:42:22 +02:00
|
|
|
BitCount += Iter->count();
|
2007-09-11 06:11:28 +02:00
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
return BitCount;
|
|
|
|
}
|
2007-09-09 03:10:40 +02:00
|
|
|
iterator begin() const {
|
2007-09-11 03:38:07 +02:00
|
|
|
return iterator(this);
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() const {
|
2007-11-14 00:04:06 +01:00
|
|
|
return iterator(this, true);
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
2007-09-11 19:42:22 +02:00
|
|
|
|
2007-09-24 21:45:49 +02:00
|
|
|
// Get a hash value for this bitmap.
|
|
|
|
uint64_t getHashValue() const {
|
|
|
|
uint64_t HashVal = 0;
|
|
|
|
for (ElementListConstIter Iter = Elements.begin();
|
|
|
|
Iter != Elements.end();
|
|
|
|
++Iter) {
|
|
|
|
HashVal ^= Iter->index();
|
|
|
|
HashVal ^= Iter->getHashValue();
|
|
|
|
}
|
|
|
|
return HashVal;
|
|
|
|
}
|
2007-09-09 03:10:40 +02:00
|
|
|
};
|
2007-09-11 03:38:07 +02:00
|
|
|
|
|
|
|
// Convenience functions to allow Or and And without dereferencing in the user
|
2007-09-11 06:11:28 +02:00
|
|
|
// code.
|
|
|
|
|
2007-09-11 03:38:07 +02:00
|
|
|
template <unsigned ElementSize>
|
2007-09-11 06:11:28 +02:00
|
|
|
inline bool operator |=(SparseBitVector<ElementSize> &LHS,
|
|
|
|
const SparseBitVector<ElementSize> *RHS) {
|
|
|
|
return LHS |= *RHS;
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned ElementSize>
|
2007-09-11 06:11:28 +02:00
|
|
|
inline bool operator |=(SparseBitVector<ElementSize> *LHS,
|
|
|
|
const SparseBitVector<ElementSize> &RHS) {
|
|
|
|
return LHS->operator|=(RHS);
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned ElementSize>
|
2007-09-11 06:11:28 +02:00
|
|
|
inline bool operator &=(SparseBitVector<ElementSize> *LHS,
|
2007-09-11 03:38:07 +02:00
|
|
|
const SparseBitVector<ElementSize> &RHS) {
|
2007-09-11 06:11:28 +02:00
|
|
|
return LHS->operator&=(RHS);
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned ElementSize>
|
2007-09-11 06:11:28 +02:00
|
|
|
inline bool operator &=(SparseBitVector<ElementSize> &LHS,
|
2007-09-11 03:38:07 +02:00
|
|
|
const SparseBitVector<ElementSize> *RHS) {
|
2009-03-21 06:40:09 +01:00
|
|
|
return LHS &= *RHS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience functions for infix union, intersection, difference operators.
|
|
|
|
|
|
|
|
template <unsigned ElementSize>
|
|
|
|
inline SparseBitVector<ElementSize>
|
|
|
|
operator|(const SparseBitVector<ElementSize> &LHS,
|
|
|
|
const SparseBitVector<ElementSize> &RHS) {
|
|
|
|
SparseBitVector<ElementSize> Result(LHS);
|
|
|
|
Result |= RHS;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned ElementSize>
|
|
|
|
inline SparseBitVector<ElementSize>
|
|
|
|
operator&(const SparseBitVector<ElementSize> &LHS,
|
|
|
|
const SparseBitVector<ElementSize> &RHS) {
|
|
|
|
SparseBitVector<ElementSize> Result(LHS);
|
|
|
|
Result &= RHS;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned ElementSize>
|
|
|
|
inline SparseBitVector<ElementSize>
|
|
|
|
operator-(const SparseBitVector<ElementSize> &LHS,
|
|
|
|
const SparseBitVector<ElementSize> &RHS) {
|
|
|
|
SparseBitVector<ElementSize> Result;
|
|
|
|
Result.intersectWithComplement(LHS, RHS);
|
|
|
|
return Result;
|
2007-09-11 03:38:07 +02:00
|
|
|
}
|
2007-09-17 00:31:47 +02:00
|
|
|
|
2007-09-12 00:58:27 +02:00
|
|
|
|
2009-05-11 19:04:19 +02:00
|
|
|
|
|
|
|
|
2007-09-12 00:58:27 +02:00
|
|
|
// Dump a SparseBitVector to a stream
|
|
|
|
template <unsigned ElementSize>
|
2009-07-24 11:53:24 +02:00
|
|
|
void dump(const SparseBitVector<ElementSize> &LHS, raw_ostream &out) {
|
2010-05-22 07:13:17 +02:00
|
|
|
out << "[";
|
|
|
|
|
|
|
|
typename SparseBitVector<ElementSize>::iterator bi = LHS.begin(),
|
|
|
|
be = LHS.end();
|
|
|
|
if (bi != be) {
|
|
|
|
out << *bi;
|
|
|
|
for (++bi; bi != be; ++bi) {
|
|
|
|
out << " " << *bi;
|
|
|
|
}
|
2007-09-12 00:58:27 +02:00
|
|
|
}
|
2010-05-22 07:13:17 +02:00
|
|
|
out << "]\n";
|
2007-09-09 03:10:40 +02:00
|
|
|
}
|
2009-03-21 06:40:09 +01:00
|
|
|
} // end namespace llvm
|
2007-09-24 21:45:49 +02:00
|
|
|
|
2007-09-09 03:10:40 +02:00
|
|
|
#endif
|