Template Class CExpirationMap

Nested Relationships

Nested Types

Class Documentation

template<class Key, class T, class ClockType = std::chrono::steady_clock, class Compare = std::less<Key>, class Alloc = std::allocator<std::pair<const Key, T>>>
class CExpirationMap

A map that stores key-value pairs and the time at which they have last been updated.

This class is not threadsafe and needs to be protected by locks / mutexes in multithreaded environments.

From the outside / for the user, this class acts as a regular std::map. However, it provides one additional function (erase_expired) that removes expired elements from this map. Elements are considered to be expired, if they have not been accessed (via operator[]) within a given timeout period.

Internally, this is realized by storing both a map and a list. The map stores the regular key, and then the actual value and additional an iterator into the timestamp list. The timestamp list stores together a timestamp and the key which was inserted into the list at that given timestamp. It is always kept in a sorted order.

Whenever a map element is accessed, the responding timestamp is updated and moved to the end of the list. This happens in constant time.

Template Parameters:
  • Key – The type of the keys.

  • T – The type of the values.

  • ClockType – The type of the clock based on which the Map expires its elements

Public Types

using allocator_type = Alloc
using value_type = std::pair<const Key, T>
using reference = typename Alloc::reference
using size_type = typename Alloc::size_type
using key_type = Key
using mapped_type = T

Public Functions

inline CExpirationMap()
inline explicit CExpirationMap(typename ClockType::duration t)
inline void set_expiration(typename ClockType::duration t)

set expiration time

inline iterator begin() noexcept
inline iterator end() noexcept
inline const_iterator begin() const noexcept
inline const_iterator end() const noexcept
inline const_iterator cbegin() const noexcept
inline const_iterator cend() const noexcept
inline bool empty() const noexcept
inline size_type size() const noexcept
inline size_type max_size() const noexcept
inline T &operator[](const Key &k)

Accesses the value associated with the given key, resetting its expiration time.

Parameters:

key – The key to access the value for.

Returns:

The value associated with the key.

inline mapped_type &at(const key_type &k)
inline const mapped_type &at(const key_type &k) const
inline std::pair<iterator, bool> insert(const value_type &val)
inline iterator find(const key_type &k)
inline const_iterator find(const Key &k) const
inline std::map<Key, T> erase_expired()

Erase all expired key-value pairs from the map.

This function erases all expired key-value pairs from the internal map / timestamp list. The CExpirationMap class does not call this function internally, it has to be called explicitly by the user.

inline bool erase(const Key &k)
inline void clear()
class const_iterator

Public Types

using iterator_category = std::bidirectional_iterator_tag
using value_type = std::pair<Key, T>
using difference_type = std::ptrdiff_t
using pointer = std::pair<Key, T>*
using reference = std::pair<Key, T>&

Public Functions

inline explicit const_iterator(const iterator &other)
inline explicit const_iterator(const typename InternalMapType::const_iterator _it)
inline const_iterator &operator++()
inline const_iterator &operator--()
inline std::pair<Key, T> operator*() const
inline bool operator==(const const_iterator &rhs) const
inline bool operator!=(const const_iterator &rhs) const
class iterator

Public Types

using iterator_category = std::bidirectional_iterator_tag
using value_type = std::pair<Key, T>
using difference_type = std::ptrdiff_t
using pointer = std::pair<Key, T>*
using reference = std::pair<Key, T>&

Public Functions

inline explicit iterator(const typename InternalMapType::iterator _it)
inline iterator &operator++()
inline iterator &operator--()
inline std::pair<Key, T> operator*() const
inline bool operator==(const iterator &rhs) const
inline bool operator!=(const iterator &rhs) const