Public Member Functions | Private Attributes | List of all members
cras::SmallMap< K, V > Class Template Reference

Simple map implemented on top of a std::list<std::pair>. The map is append-only, with lock-free reads and mutex-protected insert. More...

#include <small_map.hpp>

Public Member Functions

const V & at (const K &key) const
 Get the stored value for the given key. More...
 
bool contains (const K &key) const
 Check whether the given key is stored in this map. More...
 
bool empty () const
 Return whether this map is empty. More...
 
template<typename... Args>
V & insertIfNew (const K &key, Args &&... args)
 Search this map for the key. If it is not found, store the key with a value constructed from args. More...
 
V & operator[] (const K &key)
 Find (or insert a default-constructed value) the value for the given key. More...
 
size_t size () const
 Return the number of elements of this map. More...
 

Private Attributes

::std::list<::std::pair< K, V > > data
 
::std::mutex mutex
 

Detailed Description

template<typename K, typename V>
class cras::SmallMap< K, V >

Simple map implemented on top of a std::list<std::pair>. The map is append-only, with lock-free reads and mutex-protected insert.

This map is suitable for storing a small number of elements, as it uses non-sorted linear lookup of the values. However, if this condition is met, the map is reasonably fast and efficient.

Template Parameters
KType of the map keys.
VType of the map values (should support empty constructor).

Definition at line 32 of file small_map.hpp.

Member Function Documentation

◆ at()

template<typename K , typename V >
const V& cras::SmallMap< K, V >::at ( const K &  key) const
inline

Get the stored value for the given key.

Parameters
keyThe key to find.
Returns
Const reference to the value stored for the given key.
Exceptions
std::out_of_rangeif the key is not stored in this map..

Definition at line 51 of file small_map.hpp.

◆ contains()

template<typename K , typename V >
bool cras::SmallMap< K, V >::contains ( const K &  key) const
inline

Check whether the given key is stored in this map.

Parameters
keyThe key to find.
Returns
Whether the key is stored in this map.

Definition at line 65 of file small_map.hpp.

◆ empty()

template<typename K , typename V >
bool cras::SmallMap< K, V >::empty ( ) const
inline

Return whether this map is empty.

Returns
Whether the map is empty.

Definition at line 108 of file small_map.hpp.

◆ insertIfNew()

template<typename K , typename V >
template<typename... Args>
V& cras::SmallMap< K, V >::insertIfNew ( const K &  key,
Args &&...  args 
)
inline

Search this map for the key. If it is not found, store the key with a value constructed from args.

Parameters
keyThe key to find.
argsArguments for the constructor of the inserted value.
Returns
Reference to the value stored for the given key.

Definition at line 79 of file small_map.hpp.

◆ operator[]()

template<typename K , typename V >
V& cras::SmallMap< K, V >::operator[] ( const K &  key)
inline

Find (or insert a default-constructed value) the value for the given key.

Parameters
keyThe key to find.
Returns
Reference to the value stored for the given key.

Definition at line 40 of file small_map.hpp.

◆ size()

template<typename K , typename V >
size_t cras::SmallMap< K, V >::size ( ) const
inline

Return the number of elements of this map.

Returns
The number of elements.

Definition at line 99 of file small_map.hpp.

Member Data Documentation

◆ data

template<typename K , typename V >
::std::list<::std::pair<K, V> > cras::SmallMap< K, V >::data
private

Definition at line 114 of file small_map.hpp.

◆ mutex

template<typename K , typename V >
::std::mutex cras::SmallMap< K, V >::mutex
private

Definition at line 115 of file small_map.hpp.


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


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Tue Nov 26 2024 03:49:04