8#ifndef SPARSEBITVECTOR_H
9#define SPARSEBITVECTOR_H
21# define HAS_CLZ __has_builtin(__builtin_clz)
22# define HAS_CLZLL __has_builtin(__builtin_clzll)
23# define HAS_CTZ __has_builtin(__builtin_ctz)
24# define HAS_CTZLL __has_builtin(__builtin_ctzll)
51 return std::numeric_limits<T>::digits;
57 T Shift = std::numeric_limits<T>::digits >> 1;
58 T Mask = std::numeric_limits<T>::max() >>
Shift;
73#if defined(__GNUC__) || defined(_MSC_VER)
74template <
typename T>
struct TrailingZerosCounter<
T, 4>
81#if HAS_CTZ || defined(__GNUC__)
83#elif defined(_MSC_VER)
91#if !defined(_MSC_VER) || defined(_M_X64)
92template <
typename T>
struct TrailingZerosCounter<
T, 8>
99#if HAS_CTZLL || defined(__GNUC__)
101#elif defined(_MSC_VER)
121 static_assert(std::numeric_limits<T>::is_integer &&
122 !std::numeric_limits<T>::is_signed,
123 "Only unsigned integral types are allowed.");
132 static_assert(
SizeOfT <= 4,
"Not implemented!");
137 v =
v - ((
v >> 1) & 0x55555555);
138 v = (
v & 0x33333333) + ((
v >> 2) & 0x33333333);
139 return ((
v + (
v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
149 return std::numeric_limits<T>::digits;
165#if defined(__GNUC__) || defined(_MSC_VER)
166template <
typename T>
struct LeadingZerosCounter<
T, 4>
173#if defined(__GNUC__) || HAS_CLZ
175#elif defined(_MSC_VER)
183#if !defined(_MSC_VER) || defined(_M_X64)
184template <
typename T>
struct LeadingZerosCounter<
T, 8>
191#if defined(__GNUC__) || HAS_CLZLL
193#elif defined(_MSC_VER)
213 static_assert(std::numeric_limits<T>::is_integer &&
214 !std::numeric_limits<T>::is_signed,
215 "Only unsigned integral types are allowed.");
227 v =
v - ((
v >> 1) & 0x5555555555555555ULL);
228 v = (
v & 0x3333333333333333ULL) + ((
v >> 2) & 0x3333333333333333ULL);
229 v = (
v + (
v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
241 static_assert(std::numeric_limits<T>::is_integer &&
242 !std::numeric_limits<T>::is_signed,
243 "Only unsigned integral types are allowed.");
299 return !(*
this ==
RHS);
362 assert(
false &&
"SBV: find_first: SBV cannot be empty");
376 assert(
false &&
"SBV: find_last: SBV cannot be empty");
391 &&
"Word Position outside of element");
500template <
unsigned ElementSize = 128>
506 using ElementList = std::list<SparseBitVectorElement<ElementSize>>;
712 return !(*
this ==
RHS);
831 return !(*
this ==
RHS);
859 if (
RHS.Elements.empty())
1020 else if (
this == &
RHS2)
1034 if (
RHS1.Elements.empty())
1089 while (
Iter2 !=
RHS.Elements.end())
1171template <
unsigned ElementSize>
1178template <
unsigned ElementSize>
1182 return LHS->operator|=(
RHS);
1185template <
unsigned ElementSize>
1189 return LHS->operator&=(
RHS);
1192template <
unsigned ElementSize>
1201template <
unsigned ElementSize>
1202inline SparseBitVector<ElementSize>
1211template <
unsigned ElementSize>
1212inline SparseBitVector<ElementSize>
1221template <
unsigned ElementSize>
1222inline SparseBitVector<ElementSize>
1232template <
unsigned ElementSize>
const_iterator begin(void) const
const_iterator end(void) const
bool empty(void) const
Returns true if no bits are set.
SparseBitVectorIterator()=delete
void AdvanceToNextNonZero()
ElementListConstIter Iter
bool operator!=(const SparseBitVectorIterator &RHS) const
void AdvanceToFirstNonZero()
SparseBitVectorIterator(const SparseBitVector< ElementSize > *RHS, bool end=false)
bool operator==(const SparseBitVectorIterator &RHS) const
SparseBitVectorIterator operator++(int)
unsigned operator*() const
SparseBitVectorElement< ElementSize >::BitWord Bits
SparseBitVectorIterator & operator++()
SparseBitVectorIterator iterator
SparseBitVector & operator=(SparseBitVector &&RHS)
bool test(unsigned Idx) const
bool operator&=(const SparseBitVector &RHS)
bool intersects(const SparseBitVector< ElementSize > *RHS) const
void intersectWithComplement(const SparseBitVector< ElementSize > *RHS1, const SparseBitVector< ElementSize > *RHS2)
bool test_and_set(unsigned Idx)
typename ElementList::iterator ElementListIter
bool operator|=(const SparseBitVector &RHS)
bool intersects(const SparseBitVector< ElementSize > &RHS) const
std::list< SparseBitVectorElement< ElementSize > > ElementList
bool operator==(const SparseBitVector &RHS) const
bool intersectWithComplement(const SparseBitVector &RHS)
SparseBitVector(SparseBitVector &&RHS) noexcept
SparseBitVector(const SparseBitVector &RHS)
bool intersectWithComplement(const SparseBitVector< ElementSize > *RHS) const
ElementListIter FindLowerBound(unsigned ElementIndex)
ElementListConstIter FindLowerBoundConst(unsigned ElementIndex) const
bool contains(const SparseBitVector< ElementSize > &RHS) const
bool operator!=(const SparseBitVector &RHS) const
typename ElementList::const_iterator ElementListConstIter
ElementListIter FindLowerBoundImpl(unsigned ElementIndex) const
SparseBitVector & operator=(const SparseBitVector &RHS)
ElementListIter CurrElementIter
void intersectWithComplement(const SparseBitVector< ElementSize > &RHS1, const SparseBitVector< ElementSize > &RHS2)
IntervalValue operator-(const IntervalValue &lhs, const IntervalValue &rhs)
Subtract IntervalValues.
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
bool operator&=(SparseBitVector< ElementSize > *LHS, const SparseBitVector< ElementSize > &RHS)
IntervalValue operator&(const IntervalValue &lhs, const IntervalValue &rhs)
Bitwise AND of IntervalValues.
llvm::Value Value
LLVM Basic classes.
llvm::IRBuilder IRBuilder
bool operator|=(SparseBitVector< ElementSize > &LHS, const SparseBitVector< ElementSize > *RHS)
void dump(const SparseBitVector< ElementSize > &LHS, std::ostream &out)
IntervalValue operator|(const IntervalValue &lhs, const IntervalValue &rhs)
Bitwise OR of IntervalValues.
unsigned countPopulation(T Value)
ZeroBehavior
The behavior an operation has on an input of 0.
@ ZB_Undefined
The returned value is undefined.
@ ZB_Max
The returned value is numeric_limits<T>::max()
@ ZB_Width
The returned value is numeric_limits<T>::digits.
unsigned countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
static unsigned count(T Val, ZeroBehavior)
static unsigned count(T Value)
static unsigned count(T Value)
int find_last() const
find_last - Returns the index of the last set bit.
int find_first() const
find_first - Returns the index of the first set bit.
bool intersects(const SparseBitVectorElement &RHS) const
bool intersectWith(const SparseBitVectorElement &RHS, bool &BecameZero)
bool unionWith(const SparseBitVectorElement &RHS)
bool test(unsigned Idx) const
BitWord Bits[BITWORDS_PER_ELEMENT]
BitWord word(unsigned Idx) const
int find_next(unsigned Curr) const
void intersectWithComplement(const SparseBitVectorElement &RHS1, const SparseBitVectorElement &RHS2, bool &BecameZero)
SparseBitVectorElement(unsigned Idx)
bool test_and_set(unsigned Idx)
SparseBitVectorElement()=default
bool operator==(const SparseBitVectorElement &RHS) const
bool operator!=(const SparseBitVectorElement &RHS) const
bool intersectWithComplement(const SparseBitVectorElement &RHS, bool &BecameZero)
static unsigned count(T Val, ZeroBehavior)