21 #include <Reaktoro/Common/Meta.hpp>
22 #include <Reaktoro/Common/TraitsUtils.hpp>
23 #include <Reaktoro/Common/Types.hpp>
73 template<
typename CacheType,
typename T>
74 constexpr
auto sameValue(
const CacheType& a,
const T& b)
76 return MemoizationTraits<Decay<T>>::equal(a, b);
80 template<
typename CacheType,
typename T>
81 constexpr
auto assignValue(CacheType& a,
const T& b)
83 return MemoizationTraits<Decay<T>>::assign(a, b);
87 template<
typename Tuple1,
typename Tuple2>
88 auto sameValues(
const Tuple1& tuple1,
const Tuple2& tuple2)
90 using std::tuple_size_v;
92 constexpr
auto N1 = tuple_size_v<Tuple1>;
93 constexpr
auto N2 = tuple_size_v<Tuple2>;
94 static_assert(N1 == N2);
96 For<N1>([&](
auto i) constexpr {
97 res = res && sameValue(get<i>(tuple1), get<i>(tuple2));
103 template<
typename Tuple1,
typename Tuple2>
104 auto assignValues(Tuple1& tuple1,
const Tuple2& tuple2)
106 using std::tuple_size_v;
108 constexpr
auto N1 = tuple_size_v<Tuple1>;
109 constexpr
auto N2 = tuple_size_v<Tuple2>;
110 static_assert(N1 == N2);
111 For<N1>([&](
auto i) constexpr {
112 assignValue(get<i>(tuple1), get<i>(tuple2));
118 using CacheType =
typename MemoizationTraits<Decay<T>>::CacheType;
143 template<
typename Ret,
typename... Args>
146 auto cache = std::make_shared<
Map<
Tuple<Args...>, Ret>>();
147 return [=](Args... args)
mutable -> Ret
151 Tuple<Args...> t(args...);
152 if(cache->find(t) == cache->end())
153 (*cache)[t] = f(args...);
159 template<
typename Fun, Requires<!isFunction<Fun>> = true>
166 template<
typename Ret,
typename... Args>
171 auto firsttime =
true;
172 return [=](Args... args)
mutable -> Ret
176 if(detail::sameValues(cache, std::tie(args...)) && !firsttime)
178 detail::assignValues(cache, std::tie(args...));
180 return result = f(args...);
185 template<
typename Fun, Requires<!isFunction<Fun>> = true>
192 template<
typename Ret,
typename RetRef,
typename... Args>
197 auto firsttime =
true;
198 return [=](RetRef res, Args... args)
mutable ->
void
202 else if(detail::sameValues(cache, std::tie(args...)) && !firsttime)
208 detail::assignValues(cache, std::tie(args...));
217 template<
typename Ret,
typename Fun, Requires<!isFunction<Fun>> = true>
220 return memoizeLastUsingRef<Ret>(
asFunction(f));
225 template<
typename Ret,
typename... Args>
228 return memoizeLastUsingRef<Ret, Ret&>(f);
234 template<
typename Fun, Requires<!isFunction<Fun>> = true>
The class used to control memoization in the application.
Definition: Memoization.hpp:124
Memoization()=delete
Deleted default constructor.
static auto isDisabled() -> bool
Return true if memoization is currently disabled.
static auto isEnabled() -> bool
Return true if memoization is currently enabled.
static auto enable() -> void
Enable memoization optimization.
static auto disable() -> void
Disable memoization optimization.
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto memoize(Fn< Ret(Args...)> f) -> Fn< Ret(Args...)>
Return a memoized version of given function f.
Definition: Memoization.hpp:144
auto memoizeLastUsingRef(Fn< void(RetRef, Args...)> f) -> Fn< void(RetRef, Args...)>
Return a memoized version of given function f that caches only the arguments used in the last call.
Definition: Memoization.hpp:193
auto memoizeLast(Fn< Ret(Args...)> f) -> Fn< Ret(Args...)>
Return a memoized version of given function f that caches only the arguments used in the last call.
Definition: Memoization.hpp:167
constexpr auto asFunction(const Fun &f)
Convert lambda/function pointers/member functions to std::function.
Definition: TraitsUtils.hpp:92
std::tuple< Args... > Tuple
Convenient alias for std::tuple<Args...>.
Definition: Types.hpp:94
std::function< F > Fn
Convenient alias for std::function<R(Args...)>.
Definition: Types.hpp:110
std::unordered_map< Key, T > Map
Convenient alias for std::unordered_map<Key, T>.
Definition: Types.hpp:74
Used to enable function arguments of a type T to be cached in a memoized version of the function.
Definition: Memoization.hpp:30
static auto assign(CacheType &a, const Type &b)
Assign the value of b, of type T, into a, of type CacheType.
Definition: Memoization.hpp:64
Decay< T > Type
The type T without const, reference, and pointer specifiers.
Definition: Memoization.hpp:32
static auto equal(const CacheType &a, const Type &b)
Check if a, of type CacheType, is equal to b, of type T.
Definition: Memoization.hpp:56
Type CacheType
The type of the object constructed with a given object of type T.
Definition: Memoization.hpp:47