11 #ifndef EMP_MEMO_FUNCTIONS_H 12 #define EMP_MEMO_FUNCTIONS_H 14 #include <unordered_map> 16 #include "../base/assert.h" 17 #include "../meta/meta.h" 29 template <
class R,
class ARG>
32 using size_t = std::size_t;
35 using fun_t = std::function<R(ARG)>;
39 mutable std::unordered_map<index_t, return_t> cache_map;
63 this_t &
operator=(T && arg) { cache_map.clear(); fun = std::forward<T>(arg);
return *
this; }
66 size_t size()
const {
return cache_map.size(); }
69 bool Has(
const ARG & k)
const {
return cache_map.find(k) != cache_map.end(); }
72 void Clear() { cache_map.clear(); }
75 void Erase(
const ARG & k) { cache_map.erase(k); }
81 auto cache_it = cache_map.find(k);
82 if (cache_it != cache_map.end())
return cache_it->second;
84 return cache_map.emplace(std::forward<KEY>(k), result).first->second;
88 operator bool()
const {
return (
bool) fun; }
91 operator std::function<R(ARG)>() {
92 return [
this](
const ARG & arg){
return operator()(arg); };
97 return [
this](
const ARG & arg){
return operator()(arg); };
103 template <
class R,
class A1,
class A2,
class... EXTRA>
106 using size_t = std::size_t;
108 using fun_t = std::function<R(A1,A2,EXTRA...)>;
111 using tuple_t = std::tuple<std::decay_t<A1>,std::decay_t<A2>,std::decay_t<EXTRA>...>;
114 std::unordered_map<tuple_t, return_t, hash_t> cache_map;
118 template <
typename... Ts>
127 template <
typename T>
128 this_t &
operator=(T && arg) { cache_map.clear(); fun = std::forward<T>(arg);
return *
this; }
130 size_t size()
const {
return cache_map.size(); }
132 inline static size_t Hash(
const A1 & k1,
const A2 & k2,
const EXTRA &... k_extra) {
135 bool Has(
const A1 & k1,
const A2 & k2,
const EXTRA &... k_extra)
const {
136 return cache_map.find(std::make_tuple(k1,k2,k_extra...)) != cache_map.end();
139 void Erase(
const A1 & k1,
const A2 & k2,
const EXTRA &... k_extra) {
140 cache_map.erase(std::make_tuple(k1,k2,k_extra...));
145 auto cache_it = cache_map.find(std::make_tuple(k1,k2,k_extra...));
146 if (cache_it != cache_map.end())
return cache_it->second;
147 return cache_map.emplace(std::make_tuple(k1,k2,k_extra...),
148 fun(k1, k2, k_extra...)).first->second;
151 operator bool()
const {
return (
bool) fun; }
154 operator std::function<R(A1,A2,EXTRA...)>()
const {
155 return [
this](A1 k1, A2 k2, EXTRA... k_extra) {
156 return operator()(k1, k2, k_extra...);
160 return [
this](A1 k1, A2 k2, EXTRA... k_extra) {
161 return operator()(k1, k2, k_extra...);
170 using size_t = std::size_t;
182 template <
typename T>
183 memo_function(T && fun_info) : cached_value(), has_cache(false), fun(
std::forward<T>(fun_info)) { ; }
192 template <
typename T>
193 this_t &
operator=(T && arg) { has_cache=
false; fun = std::forward<T>(arg);
return *
this; }
195 size_t size()
const {
return (
size_t) has_cache; }
197 bool Has()
const {
return has_cache; }
203 if (has_cache ==
false) { cached_value = fun(); has_cache =
true; }
207 operator bool() {
return (
bool) fun; }
210 operator std::function<R()>() {
211 return [
this](){
return operator()(); };
214 return [
this](){
return operator()(); };
size_t size() const
Definition: memo_function.h:195
std::function< R()> fun_t
Definition: memo_function.h:173
return_t operator()()
Definition: memo_function.h:201
void index_t
Definition: memo_function.h:172
std::function< R()> to_function()
Definition: memo_function.h:213
bool Has(const ARG &k) const
Test if a certain input has been cached.
Definition: memo_function.h:69
Setup tuples to be able to be used in hash tables.
Definition: tuple_utils.h:51
Definition: BitVector.h:785
std::function< R(ARG)> fun_t
Definition: memo_function.h:35
std::size_t CombineHash(const T &x)
Definition: meta.h:207
this_t & operator=(const fun_t &_f)
Definition: memo_function.h:190
void Erase(const ARG &k)
Erase a specific entry from the cache.
Definition: memo_function.h:75
size_t size() const
How many values have been cached?
Definition: memo_function.h:66
bool Has() const
Definition: memo_function.h:197
Definition: memo_function.h:30
this_t & operator=(const fun_t &_f)
Set a new std::function of the appropriate type.
Definition: memo_function.h:56
this_t & operator=(fun_t &&_f)
Definition: memo_function.h:191
return_t operator()(KEY &&k) const
Call the memo_function.
Definition: memo_function.h:79
void Clear()
Clear out the cache.
Definition: memo_function.h:72
this_t & operator=(fun_t &&_f)
Move to here an std::function of the appropriate type.
Definition: memo_function.h:59
void Erase()
Definition: memo_function.h:199
this_t & operator=(T &&arg)
A universal copy/move for other combinations that work with std::function.
Definition: memo_function.h:63
std::decay_t< ARG > index_t
Definition: memo_function.h:34
R return_t
Definition: memo_function.h:171
memo_function()
Definition: memo_function.h:186
Functions to simplify the use of std::tuple.
R return_t
Definition: memo_function.h:33
void Clear()
Definition: memo_function.h:198
memo_function(T &&fun_info)
Definition: memo_function.h:183
Definition: memo_function.h:26
If we are in emscripten, make sure to include the header.
Definition: array.h:37
std::function< R(ARG)> to_function()
Convert a memo_function to a regular std::function for function calls.
Definition: memo_function.h:96
#define emp_assert(...)
Definition: assert.h:199
Definition: memo_function.h:168
this_t & operator=(T &&arg)
Definition: memo_function.h:193
memo_function()
Definition: memo_function.h:47
memo_function(T &&fun_info)
Definition: memo_function.h:44