Template Class SmallMap

Class Documentation

template<typename K, typename V>
class SmallMap

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:
  • K – Type of the map keys.

  • V – Type of the map values (should support empty constructor).

Public Functions

inline V &operator[](const K &key)

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

Parameters:

key – The key to find.

Returns:

Reference to the value stored for the given key.

inline const V &at(const K &key) const

Get the stored value for the given key.

Parameters:

key – The key to find.

Throws:

std::out_of_range – if the key is not stored in this map..

Returns:

Const reference to the value stored for the given key.

inline bool contains(const K &key) const

Check whether the given key is stored in this map.

Parameters:

key – The key to find.

Returns:

Whether the key is stored in this map.

template<typename ...Args>
inline 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.

Parameters:
  • key – The key to find.

  • args – Arguments for the constructor of the inserted value.

Returns:

Reference to the value stored for the given key.

inline size_t size() const

Return the number of elements of this map.

Returns:

The number of elements.

inline bool empty() const

Return whether this map is empty.

Returns:

Whether the map is empty.