ModErn Text Analysis
META Enumerates Textual Applications
Public Types | Public Member Functions | Private Attributes | List of all members
meta::util::sparse_vector< Index, Value > Class Template Reference

Represents a sparse vector, indexed by type Index and storing values of type Value. More...

#include <sparse_vector.h>

Public Types

using pair_type = std::pair< Index, Value >
 
using container_type = std::vector< pair_type >
 
using iterator = typename container_type::iterator
 
using const_iterator = typename container_type::const_iterator
 

Public Member Functions

 sparse_vector ()=default
 Creates an empty sparse_vector.
 
 sparse_vector (uint64_t size)
 Creates a sparse vector with size elements. More...
 
template<class Iter >
 sparse_vector (Iter begin, Iter end)
 Creates a sparse vector from a pair of iterators of pairs. More...
 
Value & operator[] (const Index &index)
 Gets the value associated with a given index in the vector, adding it with a value-initialized Value if not present. More...
 
Value at (const Index &index) const
 Gets the value associated with a given index in the vector. More...
 
const_iterator find (const Index &index) const
 
template<class... Ts>
void emplace_back (Ts &&...ts)
 Adds a new element to the vector, placing it at the back. More...
 
void reserve (uint64_t size)
 Reserves space for elements. More...
 
void clear ()
 Empties the vector.
 
void shrink_to_fit ()
 Optimises the memory usage of the vector.
 
void condense ()
 Condenses the vector to include only non-zero (value initialized) entries.
 
uint64_t size () const
 
bool empty () const
 
const container_type & contents () const
 
void contents (container_type cont)
 
iterator begin ()
 
const_iterator begin () const
 
const_iterator cbegin () const
 
iterator end ()
 
const_iterator end () const
 
const_iterator cend () const
 

Private Attributes

container_type storage_
 Internal storage for the sparse vector: a sorted vector of pairs.
 

Detailed Description

template<class Index, class Value>
class meta::util::sparse_vector< Index, Value >

Represents a sparse vector, indexed by type Index and storing values of type Value.

This stores the elements in the vector in sorted order by the Index type.

Constructor & Destructor Documentation

template<class Index , class Value >
meta::util::sparse_vector< Index, Value >::sparse_vector ( uint64_t  size)

Creates a sparse vector with size elements.

Parameters
sizeThe number of elements in the new sparse vector.
template<class Index , class Value >
template<class Iter >
meta::util::sparse_vector< Index, Value >::sparse_vector ( Iter  begin,
Iter  end 
)

Creates a sparse vector from a pair of iterators of pairs.

Parameters
beginThe iterator to the beginning of the sequence
endThe iterator to the end of the sequence

Member Function Documentation

template<class Index, class Value >
Value & meta::util::sparse_vector< Index, Value >::operator[] ( const Index &  index)

Gets the value associated with a given index in the vector, adding it with a value-initialized Value if not present.

Parameters
indexThe index of the sparse vector to access
Returns
a reference to the value associated with that index into the vector
template<class Index, class Value >
Value meta::util::sparse_vector< Index, Value >::at ( const Index &  index) const

Gets the value associated with a given index in the vector.

If not present, this will return a value-initialized Value and will NOT modify the vector.

Parameters
indexThe index of the sparse vector to access
Returns
the value associated with that index into the vector, or a value-initialized Value if there is none
template<class Index, class Value >
auto meta::util::sparse_vector< Index, Value >::find ( const Index &  index) const
Parameters
indexThe index of the sparse vector to search for
Returns
an iterator to that position, or the end iterator if it does not exist
template<class Index , class Value >
template<class... Ts>
void meta::util::sparse_vector< Index, Value >::emplace_back ( Ts &&...  ts)

Adds a new element to the vector, placing it at the back.

This assumes that you are adding elements in sorted order by Index—otherwise, the behavior of the vector afterwards is undefined.

Parameters
tsThe arguments to forward to the pair constructor of the new element
template<class Index , class Value >
void meta::util::sparse_vector< Index, Value >::reserve ( uint64_t  size)

Reserves space for elements.

Parameters
sizeThe number of elements to reserve space for
template<class Index , class Value >
uint64_t meta::util::sparse_vector< Index, Value >::size ( ) const
Returns
the number of non-zero elements in the vector
template<class Index , class Value >
bool meta::util::sparse_vector< Index, Value >::empty ( ) const
Returns
whether the vector is empty
template<class Index , class Value >
auto meta::util::sparse_vector< Index, Value >::contents ( ) const
Returns
the contents of the vector
template<class Index , class Value >
void meta::util::sparse_vector< Index, Value >::contents ( container_type  cont)
Parameters
contThe new contents of the vector
template<class Index , class Value >
auto meta::util::sparse_vector< Index, Value >::begin ( )
Returns
an iterator to the beginning of the vector
template<class Index , class Value >
auto meta::util::sparse_vector< Index, Value >::begin ( ) const
Returns
a const_iterator to the beginning of the vector
template<class Index , class Value >
auto meta::util::sparse_vector< Index, Value >::cbegin ( ) const
Returns
a const_iterator to the beginning of the vector
template<class Index , class Value >
auto meta::util::sparse_vector< Index, Value >::end ( )
Returns
an iterator to the end of the vector
template<class Index , class Value >
auto meta::util::sparse_vector< Index, Value >::end ( ) const
Returns
a const_iterator to the end of the vector
template<class Index , class Value >
auto meta::util::sparse_vector< Index, Value >::cend ( ) const
Returns
a const_iterator to the end of the vector

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