38#ifndef __BASE_MEMOIZER_HH__
39#define __BASE_MEMOIZER_HH__
81template <
typename Ret,
typename... Args>
101 auto key = std::make_tuple(args...);
102 if (
auto it =
cache.find(key); it !=
cache.end()) {
106 auto emplaced =
cache.emplace(key,
func(args...));
107 return emplaced.first->second;
136 constexpr size_t num_args = std::tuple_size_v<std::decay_t<args_type>>;
138 static_assert(!std::is_reference_v<
139 typename std::tuple_element<
144 template <
size_t Start,
size_t End,
size_t Inc,
class F>
148 if constexpr (Start < End) {
149 func(std::integral_constant<
decltype(Start), Start>());
156 const std::function<Ret(Args...)>
func;
158 mutable std::map<args_type, ret_type>
cache;
This class takes a function as a constructor argument and memoizes it: every time the function gets i...
std::map< args_type, ret_type > cache
Result cache.
auto operator()(Args... args) const
Memoizer & operator=(const Memoizer &rhs)=delete
std::tuple< Args... > args_type
bool cached(args_type args) const
True if the passed value is cached, false otherwise.
constexpr void iterateTupleArgs(F &&func)
void flush()
Clear the memoization cache.
const std::function< Ret(Args...)> func
Memoized function.
constexpr Memoizer(Ret(*_func)(Args...))
size_t cacheSize() const
Return the size of the memoizer cache.
constexpr void validateMemoizer()
Validate the memoizer and produce an error if the function signature contains a reference.
Memoizer(const Memoizer &rhs)=delete
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.