LLVM  8.0.1
Classes | Public Types | Public Member Functions | Public Attributes | List of all members
llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT > Class Template Reference

Fast multiset implementation for objects that can be identified by small unsigned keys. More...

#include "llvm/ADT/SparseMultiSet.h"

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

Classes

class  iterator_base
 Our iterators are iterators over the collection of objects that share a key. More...
 

Public Types

using value_type = ValueT
 
using reference = ValueT &
 
using const_reference = const ValueT &
 
using pointer = ValueT *
 
using const_pointer = const ValueT *
 
using size_type = unsigned
 
using iterator = iterator_base< SparseMultiSet * >
 
using const_iterator = iterator_base< const SparseMultiSet * >
 
using RangePair = std::pair< iterator, iterator >
 

Public Member Functions

 SparseMultiSet ()=default
 
 SparseMultiSet (const SparseMultiSet &)=delete
 
SparseMultiSetoperator= (const SparseMultiSet &)=delete
 
 ~SparseMultiSet ()
 
void setUniverse (unsigned U)
 Set the universe size which determines the largest key the set can hold. More...
 
iterator end ()
 Returns an iterator past this container. More...
 
const_iterator end () const
 
bool empty () const
 Returns true if the set is empty. More...
 
size_type size () const
 Returns the number of elements in the set. More...
 
void clear ()
 Clears the set. More...
 
iterator findIndex (unsigned Idx)
 Find an element by its index. More...
 
iterator find (const KeyT &Key)
 Find an element by its key. More...
 
const_iterator find (const KeyT &Key) const
 
size_type count (const KeyT &Key) const
 Returns the number of elements identified by Key. More...
 
bool contains (const KeyT &Key) const
 Returns true if this set contains an element identified by Key. More...
 
iterator getHead (const KeyT &Key)
 Return the head and tail of the subset's list, otherwise returns end(). More...
 
iterator getTail (const KeyT &Key)
 
RangePair equal_range (const KeyT &K)
 The bounds of the range of items sharing Key K. More...
 
iterator insert (const ValueT &Val)
 Insert a new element at the tail of the subset list. More...
 
if (test(I)) *break
 Erases an existing element identified by a valid iterator. More...
 
**iterator erase (iterator I)
 
void eraseAll (const KeyT &K)
 Erase all elements with the given key. More...
 

Public Attributes

I = erase(I)
 

Detailed Description

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

Fast multiset implementation for objects that can be identified by small unsigned keys.

SparseMultiSet 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, SparseMultiSet provides constant-time fast clear() 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. Iteration order is the insertion order. Iteration is only provided over elements of equivalent keys, but iterators are bidirectional.

Compared to BitVector, SparseMultiSet<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.

SparseMultiSet 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 up to 3 cache lines, but the sparse array uses 4 x Universe bytes.

When SparseT is uint8_t (the default), find() touches up to 3+[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.

Multiset behavior is provided by providing doubly linked lists for values that are inlined in the dense vector. SparseMultiSet is a good choice when one desires a growable number of entries per key, as it will retain the SparseSet algorithmic properties despite being growable. Thus, it is often a better choice than a SparseSet of growable containers or a vector of vectors. SparseMultiSet also keeps iterators valid after erasure (provided the iterators don't point to the element erased), allowing for more intuitive and fast removal.

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 86 of file SparseMultiSet.h.

Member Typedef Documentation

◆ const_iterator

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
using llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::const_iterator = iterator_base<const SparseMultiSet *>

Definition at line 313 of file SparseMultiSet.h.

◆ const_pointer

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

Definition at line 190 of file SparseMultiSet.h.

◆ const_reference

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

Definition at line 188 of file SparseMultiSet.h.

◆ iterator

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
using llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::iterator = iterator_base<SparseMultiSet *>

Definition at line 312 of file SparseMultiSet.h.

◆ pointer

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

Definition at line 189 of file SparseMultiSet.h.

◆ RangePair

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
using llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::RangePair = std::pair<iterator, iterator>

Definition at line 316 of file SparseMultiSet.h.

◆ reference

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

Definition at line 187 of file SparseMultiSet.h.

◆ size_type

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
using llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::size_type = unsigned

Definition at line 191 of file SparseMultiSet.h.

◆ value_type

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
using llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::value_type = ValueT

Definition at line 186 of file SparseMultiSet.h.

Constructor & Destructor Documentation

◆ SparseMultiSet() [1/2]

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

◆ SparseMultiSet() [2/2]

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::SparseMultiSet ( const SparseMultiSet< ValueT, KeyFunctorT, SparseT > &  )
delete

◆ ~SparseMultiSet()

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

Definition at line 196 of file SparseMultiSet.h.

Member Function Documentation

◆ clear()

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

Clears the set.

This is a very fast constant time operation.

Definition at line 343 of file SparseMultiSet.h.

Referenced by llvm::ScheduleDAGInstrs::buildSchedGraph().

◆ contains()

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

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

Definition at line 396 of file SparseMultiSet.h.

Referenced by llvm::ScheduleDAGInstrs::addPhysRegDataDeps(), llvm::ScheduleDAGInstrs::addPhysRegDeps(), and llvm::ScheduleDAGInstrs::addSchedBarrierDeps().

◆ count()

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

Returns the number of elements identified by Key.

This will be linear in the number of elements of that key.

Definition at line 387 of file SparseMultiSet.h.

◆ empty()

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

Returns true if the set is empty.

This is not the same as BitVector::empty().

Definition at line 329 of file SparseMultiSet.h.

Referenced by llvm::ScheduleDAGInstrs::buildSchedGraph(), and llvm::SparseMultiSet< VReg2SUnit, VirtReg2IndexFunctor >::setUniverse().

◆ end() [1/2]

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

◆ end() [2/2]

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

Definition at line 321 of file SparseMultiSet.h.

◆ equal_range()

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
RangePair llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::equal_range ( const KeyT &  K)
inline

The bounds of the range of items sharing Key K.

First member is the head of the list, and the second member is a decrementable end iterator for that key.

Definition at line 412 of file SparseMultiSet.h.

Referenced by llvm::ScheduleDAGInstrs::addPhysRegDeps().

◆ erase()

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

◆ eraseAll()

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
void llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::eraseAll ( const KeyT &  K)
inline

Erase all elements with the given key.

This invalidates all iterators of that key.

Definition at line 483 of file SparseMultiSet.h.

Referenced by llvm::ScheduleDAGInstrs::addPhysRegDeps().

◆ find() [1/2]

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

◆ find() [2/2]

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

Definition at line 380 of file SparseMultiSet.h.

◆ findIndex()

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

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 355 of file SparseMultiSet.h.

Referenced by llvm::SparseMultiSet< VReg2SUnit, VirtReg2IndexFunctor >::eraseAll(), llvm::SparseMultiSet< VReg2SUnit, VirtReg2IndexFunctor >::find(), and llvm::SparseMultiSet< VReg2SUnit, VirtReg2IndexFunctor >::insert().

◆ getHead()

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

Return the head and tail of the subset's list, otherwise returns end().

Definition at line 401 of file SparseMultiSet.h.

◆ getTail()

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

Definition at line 402 of file SparseMultiSet.h.

◆ if()

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
* llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::if ( test(I )

Erases an existing element identified by a valid iterator.

This invalidates iterators pointing at the same entry, but erase() returns an iterator pointing to the next element in the subset's list. This makes it possible to erase selected elements while iterating over the subset:

tie(I, E) = Set.equal_range(Key); while (I != E) if (test(*I)) I = Set.erase(I); else ++I;

Note that if the last element in the subset list is erased, this will return an end iterator which can be decremented to get the new tail (if it exists):

tie(B, I) = Set.equal_range(Key); for (bool isBegin = B == I; !isBegin; /* empty

◆ insert()

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
iterator llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::insert ( const ValueT Val)
inline

Insert a new element at the tail of the subset list.

Returns an iterator to the newly added entry.

Definition at line 420 of file SparseMultiSet.h.

Referenced by llvm::ScheduleDAGInstrs::addPhysRegDeps(), llvm::ScheduleDAGInstrs::addSchedBarrierDeps(), llvm::ScheduleDAGInstrs::addVRegDefDeps(), and llvm::ScheduleDAGInstrs::addVRegUseDeps().

◆ operator=()

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
SparseMultiSet& llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::operator= ( const SparseMultiSet< ValueT, KeyFunctorT, SparseT > &  )
delete

◆ setUniverse()

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

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 203 of file SparseMultiSet.h.

Referenced by llvm::ScheduleDAGInstrs::buildSchedGraph().

◆ size()

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

Returns the number of elements in the set.

This is not the same as BitVector::size() which returns the size of the universe.

Definition at line 336 of file SparseMultiSet.h.

Referenced by llvm::SparseMultiSet< VReg2SUnit, VirtReg2IndexFunctor >::empty().

Member Data Documentation

◆ I

template<typename ValueT, typename KeyFunctorT = identity<unsigned>, typename SparseT = uint8_t>
* llvm::SparseMultiSet< ValueT, KeyFunctorT, SparseT >::I = erase(I)

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