20 #ifndef LLVM_ADT_SPARSESET_H
21 #define LLVM_ADT_SPARSESET_H
25 #include "llvm/Support/DataTypes.h"
52 template<
typename ValueT>
55 return Val.getSparseSetIndex();
63 template<
typename KeyT,
typename ValueT,
typename KeyFunctorT>
72 template<
typename KeyT,
typename KeyFunctorT>
75 return KeyFunctorT()(Key);
117 template<
typename ValueT,
119 typename SparseT = uint8_t>
121 static_assert(std::numeric_limits<SparseT>::is_integer &&
122 !std::numeric_limits<SparseT>::is_signed,
123 "SparseT must be an unsigned integer type");
125 typedef typename KeyFunctorT::argument_type KeyT;
131 KeyFunctorT KeyIndexOf;
158 assert(
empty() &&
"Can only resize universe on an empty map");
160 if (U >= Universe/4 && U <= Universe)
166 Sparse =
reinterpret_cast<SparseT*
>(calloc(U,
sizeof(SparseT)));
205 assert(Idx < Universe &&
"Key out of range");
206 const unsigned Stride = std::numeric_limits<SparseT>::max() + 1u;
207 for (
unsigned i = Sparse[Idx], e =
size(); i < e; i += Stride) {
208 const unsigned FoundIdx = ValIndexOf(Dense[i]);
209 assert(FoundIdx < Universe &&
"Invalid key in set. Did object mutate?");
236 return find(Key) ==
end() ? 0 : 1;
249 std::pair<iterator, bool>
insert(
const ValueT &Val) {
250 unsigned Idx = ValIndexOf(Val);
253 return std::make_pair(I,
false);
254 Sparse[Idx] =
size();
256 return std::make_pair(
end() - 1,
true);
263 return *
insert(ValueT(Key)).first;
281 assert(
unsigned(I -
begin()) <
size() &&
"Invalid iterator");
282 if (I !=
end() - 1) {
284 unsigned BackIdx = ValIndexOf(Dense.
back());
285 assert(BackIdx < Universe &&
"Invalid key in set. Did object mutate?");
286 Sparse[BackIdx] = I -
begin();
ValueT & operator[](const KeyT &Key)
array subscript - If an element already exists with this key, return it.
void push_back(const T &Elt)
unsigned operator()(const ValueT &Val) const
DenseT::const_iterator const_iterator
std::pair< iterator, bool > insert(const ValueT &Val)
insert - Attempts to insert a new element.
bool erase(const KeyT &Key)
erase - Erases an element identified by Key, if it exists.
const ValueT & const_reference
size_type count(const KeyT &Key) const
count - Returns 1 if this set contains an element identified by Key, 0 otherwise. ...
SparseSetValTraits - Objects in a SparseSet are identified by keys that can be uniquely converted to ...
bool empty() const
empty - Returns true if the set is empty.
const_iterator end() const
const_iterator find(const KeyT &Key) const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
iterator findIndex(unsigned Idx)
findIndex - Find an element by its index.
DenseT::iterator iterator
iterator erase(iterator I)
erase - Erases an existing element identified by a valid iterator.
size_type size() const
size - Returns the number of elements in the set.
void setUniverse(unsigned U)
setUniverse - Set the universe size which determines the largest key the set can hold.
const_iterator begin() const
const ValueT * const_pointer
SparseSetValFunctor - Helper class for selecting SparseSetValTraits.
SparseSet - Fast set implmentation for objects that can be identified by small unsigned keys...
iterator find(const KeyT &Key)
find - Find an element by its key.
void clear()
clear - Clears the set.
unsigned operator()(const KeyT &Key) const
static unsigned getValIndex(const ValueT &Val)