memo_function.hpp

A function that memorizes previous results to speed up any repeated calls.

Note

Status: BETA

template<class T>
class memo_function
#include <memo_function.hpp>

Identical to std::function, but memorizes prior results (caches them) so that the function doesn’t have to process those results again. (note: Genetic version is undefinined; must have a function signature.)

template<class R, class ARG>
class memo_function<R(ARG)>
#include <memo_function.hpp>

The simplest form of a memoized function with a single argument that is used as the cache key (no need for more complex caching).

Public Types

using size_t = std::size_t
using return_t = R
using index_t = std::decay_t<ARG>
using fun_t = std::function<R(ARG)>
using this_t = memo_function<R(ARG)>

Public Functions

template<typename T>
inline memo_function(T &&fun_info)
memo_function(const this_t&) = default
memo_function(this_t&&) = default
inline memo_function()
this_t &operator=(const this_t&) = default

Copy another memo_function of the same type.

this_t &operator=(this_t&&) = default

Move to here another memo function of the same type.

inline this_t &operator=(const fun_t &_f)

Set a new std::function of the appropriate type.

inline this_t &operator=(fun_t &&_f)

Move to here an std::function of the appropriate type.

template<typename T>
inline this_t &operator=(T &&arg)

A universal copy/move for other combinations that work with std::function.

inline size_t size() const

How many values have been cached?

inline bool Has(const ARG &k) const

Test if a certain input has been cached.

inline void Clear()

Clear out the cache.

inline void Erase(const ARG &k)

Erase a specific entry from the cache.

template<class KEY>
inline return_t operator()(KEY &&k) const

Call the memo_function.

inline operator bool() const

Identify if the memo_function has been set.

inline operator std::function<R(ARG)>()

Convert a memo_function to a regular std::function for function calls.

inline std::function<R(ARG)> to_function()

More explicit conversion of a memo_function to a regular std::function for function calls.

Private Members

mutable std::unordered_map<index_t, return_t> cache_map

Cached results.

fun_t fun

Function to call.

template<class R, class A1, class A2, class ...EXTRA>
class memo_function<R(A1, A2, EXTRA...)>
#include <memo_function.hpp>

Memoize functions for when we have more than one argument… we need to convert inputs to a tuple to make this work.

Public Types

using size_t = std::size_t
using return_t = R
using fun_t = std::function<R(A1, A2, EXTRA...)>
using hash_t = TupleHash<A1, A2, EXTRA...>
using this_t = memo_function<R(A1, A2, EXTRA...)>
using tuple_t = std::tuple<std::decay_t<A1>, std::decay_t<A2>, std::decay_t<EXTRA>...>

Public Functions

template<typename ...Ts>
inline memo_function(Ts&&... args)
memo_function(const memo_function&) = default
memo_function(memo_function&&) = default
this_t &operator=(const this_t&) = default
this_t &operator=(this_t&&) = default
inline this_t &operator=(const fun_t &_f)
inline this_t &operator=(fun_t &&_f)
template<typename T>
inline this_t &operator=(T &&arg)
inline size_t size() const
inline bool Has(const A1 &k1, const A2 &k2, const EXTRA&... k_extra) const
inline void Clear()
inline void Erase(const A1 &k1, const A2 &k2, const EXTRA&... k_extra)
inline return_t operator()(const A1 &k1, const A2 &k2, const EXTRA&... k_extra)
inline operator bool() const
inline operator std::function<R(A1, A2, EXTRA...)>() const
inline std::function<R(A1, A2, EXTRA...)> to_function() const

Public Static Functions

static inline size_t Hash(const A1 &k1, const A2 &k2, const EXTRA&... k_extra)

Private Members

std::unordered_map<tuple_t, return_t, hash_t> cache_map
fun_t fun
template<class R>
class memo_function<R()>
#include <memo_function.hpp>

Zero argument functions are trivial (since they need to cache only a single value), but should still work.

Public Types

using size_t = std::size_t
using return_t = R
using index_t = void
using fun_t = std::function<R()>
using this_t = memo_function<R()>

Public Functions

template<typename T>
inline memo_function(T &&fun_info)
memo_function(const this_t&) = default
memo_function(this_t&&) = default
inline memo_function()
this_t &operator=(const this_t&) = default
this_t &operator=(this_t&&) = default
inline this_t &operator=(const fun_t &_f)
inline this_t &operator=(fun_t &&_f)
template<typename T>
inline this_t &operator=(T &&arg)
inline size_t size() const
inline bool Has() const
inline void Clear()
inline void Erase()
inline return_t operator()()
inline operator bool()
inline operator std::function<R()>()
inline std::function<R()> to_function()

Private Members

return_t cached_value
bool has_cache
fun_t fun