SmallFifoMap.hpp

Store key value pairs in a fixed-sized array, bumping out the oldest value when full. Optimized for small N. Requires N < 256.

template<class Key, class Value, size_t N>
class SmallFifoMap
#include <SmallFifoMap.hpp>

Public Types

using iterator = typename storage_t::iterator
using const_iterator = typename storage_t::const_iterator

Public Functions

inline iterator begin()
inline const_iterator begin() const
inline const_iterator cbegin() const
inline iterator end()
inline const_iterator end() const
inline const_iterator cend() const
inline size_t size() const

How many key-value pairs are in the cache?

inline bool empty() const

Does the cache contain any key-value pairs?

inline void clear()

Clear the cache.

inline iterator find(const Key &key)

Find key-value pair iterator in cache.

inline const_iterator find(const Key &key) const

Find key-value pair iterator in cache.

inline Value *get(const Key &key)

Get corresponding value from cache. Return nullptr if key not in cache.

inline Value const *get(const Key &key) const

Get corresponding value from cache. Return nullptr if key not in cache.

inline Value &operator[](const Key &key)

Get corresponding value from cache.

inline const Value &operator[](const Key &key) const

Get corresponding value from cache.

template<class K, class V, class = std::enable_if_t<std::is_convertible<K, Key>{} && std::is_convertible<V, Value>{}>>
inline void set(K &&key, V &&val)

Put a key-value pair in the cache.

Public Static Functions

static inline constexpr size_t capacity()

How many key-value pairs can the cache contain?

Private Types

using value_type = std::pair<Key, Value>
using storage_t = array<value_type, N>

Private Members

storage_t storage
unsigned char size_ = {}
unsigned char oldest = {}