Cache.hpp

similar to an std::unordered_map, but all lookups come with a function to generate the result should the lookup fail.

Note

Status: BETA

template<class KEY, class T, class HASH = std::hash<KEY>, class PRED = std::equal_to<KEY>, class ALLOC = std::allocator<std::pair<const KEY, T>>>
class Cache
#include <Cache.hpp>

Public Types

using key_type = KEY

Type we are using to look up values.

using mapped_type = T

Contents of the value we look up.

using hasher = HASH

Hash method to use.

using key_equal = PRED

Function to test if two values are identical.

using allocator_type = ALLOC

Function to allocate new space.

Public Functions

inline Cache()
Cache(const Cache&) = default
Cache(Cache&&) = default
Cache &operator=(const Cache&) = default
Cache &operator=(Cache&&) = default
inline size_t size() const

How many entries are stored in the cache?

inline bool Has(const KEY &k) const

Determine if a specific key is already in the cache.

inline void Clear()

Erase contents of cache.

inline void Erase(const KEY &k)

Erase a specific entry from cache.

inline T Get(KEY k, const std::function<T(KEY k)> &calc_fun)

Lookup a specific key; provide a function to use if value is not in cache.

inline const T &GetRef(const KEY &k, const std::function<T(const KEY &k)> &calc_fun)

A version of Get that allows calls with const references instead of pass-by-value.

Private Members

std::unordered_map<KEY, T, HASH, PRED, ALLOC> cache_map