LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | List of all members
llvm::SparseSet< ValueT, KeyFunctorT, SparseT > Class Template Reference

#include <SparseSet.h>

Inheritance diagram for llvm::SparseSet< ValueT, KeyFunctorT, SparseT >:
Inheritance graph
[legend]

Public Types

typedef ValueT value_type
 
typedef ValueT & reference
 
typedef const ValueT & const_reference
 
typedef ValueT * pointer
 
typedef const ValueT * const_pointer
 
typedef DenseT::iterator iterator
 
typedef DenseT::const_iterator const_iterator
 

Public Member Functions

 SparseSet ()
 
 ~SparseSet ()
 
void setUniverse (unsigned U)
 
const_iterator begin () const
 
const_iterator end () const
 
iterator begin ()
 
iterator end ()
 
bool empty () const
 
unsigned size () const
 
void clear ()
 
iterator findIndex (unsigned Idx)
 
iterator find (const KeyT &Key)
 
const_iterator find (const KeyT &Key) const
 
bool count (const KeyT &Key) const
 
std::pair< iterator, boolinsert (const ValueT &Val)
 
ValueT & operator[] (const KeyT &Key)
 
iterator erase (iterator I)
 
bool erase (const KeyT &Key)
 

Detailed Description

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
class llvm::SparseSet< ValueT, KeyFunctorT, SparseT >

SparseSet - Fast set implmentation for objects that can be identified by small unsigned keys.

SparseSet allocates memory proportional to the size of the key universe, so it is not recommended for building composite data structures. It is useful for algorithms that require a single set with fast operations.

Compared to DenseSet and DenseMap, SparseSet provides constant-time fast clear() and iteration as fast as a vector. The find(), insert(), and erase() operations are all constant time, and typically faster than a hash table. The iteration order doesn't depend on numerical key values, it only depends on the order of insert() and erase() operations. When no elements have been erased, the iteration order is the insertion order.

Compared to BitVector, SparseSet<unsigned> uses 8x-40x more memory, but offers constant-time clear() and size() operations as well as fast iteration independent on the size of the universe.

SparseSet contains a dense vector holding all the objects and a sparse array holding indexes into the dense vector. Most of the memory is used by the sparse array which is the size of the key universe. The SparseT template parameter provides a space/speed tradeoff for sets holding many elements.

When SparseT is uint32_t, find() only touches 2 cache lines, but the sparse array uses 4 x Universe bytes.

When SparseT is uint8_t (the default), find() touches up to 2+[N/256] cache lines, but the sparse array is 4x smaller. N is the number of elements in the set.

For sets that may grow to thousands of elements, SparseT should be set to uint16_t or uint32_t.

Template Parameters
ValueTThe type of objects in the set.
KeyFunctorTA functor that computes an unsigned index from KeyT.
SparseTAn unsigned integer type. See above.

Definition at line 120 of file SparseSet.h.

Member Typedef Documentation

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
typedef DenseT::const_iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::const_iterator

Definition at line 167 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
typedef const ValueT* llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::const_pointer

Definition at line 139 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
typedef const ValueT& llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::const_reference

Definition at line 137 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
typedef DenseT::iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::iterator

Definition at line 166 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
typedef ValueT* llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::pointer

Definition at line 138 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
typedef ValueT& llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::reference

Definition at line 136 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
typedef ValueT llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::value_type

Definition at line 135 of file SparseSet.h.

Constructor & Destructor Documentation

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::SparseSet ( )
inline

Definition at line 141 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::~SparseSet ( )
inline

Definition at line 142 of file SparseSet.h.

Member Function Documentation

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
const_iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::begin ( ) const
inline
template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::begin ( )
inline

Definition at line 171 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
void llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::clear ( )
inline

clear - Clears the set. This is a very fast constant time operation.

Definition at line 189 of file SparseSet.h.

Referenced by llvm::ScheduleDAGInstrs::buildSchedGraph(), llvm::LiveRegUnits::clear(), llvm::LiveRegUnits::init(), and llvm::RegPressureTracker::reset().

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
bool llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::count ( const KeyT &  Key) const
inline

count - Returns true if this set contains an element identified by Key.

Definition at line 232 of file SparseSet.h.

Referenced by llvm::LiveRegUnits::contains(), llvm::LiveRegSet::contains(), and llvm::SchedDFSImpl::visitPostorderNode().

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
bool llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::empty ( ) const
inline
template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
const_iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::end ( ) const
inline
template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::end ( )
inline

Definition at line 172 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::erase ( iterator  I)
inline

erase - Erases an existing element identified by a valid iterator.

This invalidates all iterators, but erase() returns an iterator pointing to the next element. This makes it possible to erase selected elements while iterating over the set:

for (SparseSet::iterator I = Set.begin(); I != Set.end();) if (test(*I)) I = Set.erase(I); else ++I;

Note that end() changes when elements are erased, unlike std::list.

Definition at line 277 of file SparseSet.h.

Referenced by llvm::LiveRegSet::erase(), llvm::SparseSet< unsigned, llvm::VirtReg2IndexFunctor >::erase(), llvm::LiveRegUnits::removeReg(), llvm::LiveRegUnits::removeRegsInMask(), updatePhysDepsDownwards(), updatePhysDepsUpwards(), and llvm::SchedDFSImpl::visitPostorderNode().

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
bool llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::erase ( const KeyT &  Key)
inline

erase - Erases an element identified by Key, if it exists.

Parameters
KeyThe key identifying the element to erase.
Returns
True when an element was erased, false if no element was found.

Definition at line 296 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::find ( const KeyT &  Key)
inline

find - Find an element by its key.

Parameters
KeyA valid key to find.
Returns
An iterator to the element identified by key, or end().

Definition at line 222 of file SparseSet.h.

Referenced by llvm::ScheduleDAGInstrs::addVRegDefDeps(), llvm::ScheduleDAGInstrs::addVRegUseDeps(), llvm::SparseSet< unsigned, llvm::VirtReg2IndexFunctor >::count(), llvm::SparseSet< unsigned, llvm::VirtReg2IndexFunctor >::erase(), updatePhysDepsDownwards(), and updatePhysDepsUpwards().

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
const_iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::find ( const KeyT &  Key) const
inline

Definition at line 226 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
iterator llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::findIndex ( unsigned  Idx)
inline

findIndex - Find an element by its index.

Parameters
IdxA valid index to find.
Returns
An iterator to the element identified by key, or end().

Definition at line 199 of file SparseSet.h.

Referenced by llvm::SparseSet< unsigned, llvm::VirtReg2IndexFunctor >::find(), and llvm::SparseSet< unsigned, llvm::VirtReg2IndexFunctor >::insert().

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
std::pair<iterator, bool> llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::insert ( const ValueT &  Val)
inline

insert - Attempts to insert a new element.

If Val is successfully inserted, return (I, true), where I is an iterator pointing to the newly inserted element.

If the set already contains an element with the same key as Val, return (I, false), where I is an iterator pointing to the existing element.

Insertion invalidates all iterators.

Definition at line 246 of file SparseSet.h.

Referenced by llvm::LiveRegUnits::addReg(), llvm::ScheduleDAGInstrs::addVRegDefDeps(), llvm::LiveRegSet::insert(), and llvm::SparseSet< unsigned, llvm::VirtReg2IndexFunctor >::operator[]().

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
ValueT& llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::operator[] ( const KeyT &  Key)
inline

array subscript - If an element already exists with this key, return it. Otherwise, automatically construct a new value from Key, insert it, and return the newly inserted element.

Definition at line 259 of file SparseSet.h.

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
void llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::setUniverse ( unsigned  U)
inline

setUniverse - Set the universe size which determines the largest key the set can hold. The universe must be sized before any elements can be added.

Parameters
UUniverse size. All object keys must be less than U.

Definition at line 150 of file SparseSet.h.

Referenced by llvm::ScheduleDAGInstrs::buildSchedGraph(), llvm::LiveRegUnits::init(), llvm::RegPressureTracker::init(), and llvm::SchedDFSImpl::SchedDFSImpl().

template<typename ValueT, typename KeyFunctorT = llvm::identity<unsigned>, typename SparseT = uint8_t>
unsigned llvm::SparseSet< ValueT, KeyFunctorT, SparseT >::size ( ) const
inline

The documentation for this class was generated from the following file: