21 #include <Reaktoro/Common/Meta.hpp>
22 #include <Reaktoro/Common/TraitsUtils.hpp>
23 #include <Reaktoro/Common/Types.hpp>
82 template<
typename CacheType,
typename T>
83 constexpr
auto sameValue(
const CacheType& a,
const T& b)
85 return MemoizationTraits<Decay<T>>::equal(a, b);
89 template<
typename CacheType,
typename T>
90 constexpr
auto assignValue(CacheType& a,
const T& b)
92 return MemoizationTraits<Decay<T>>::assign(a, b);
96 template<
typename Tuple1,
typename Tuple2>
97 auto sameValues(
const Tuple1& tuple1,
const Tuple2& tuple2)
99 using std::tuple_size_v;
101 constexpr
auto N1 = tuple_size_v<Tuple1>;
102 constexpr
auto N2 = tuple_size_v<Tuple2>;
103 static_assert(N1 == N2);
105 For<N1>([&](
auto i) constexpr {
106 res = res && sameValue(get<i>(tuple1), get<i>(tuple2));
112 template<
typename Tuple1,
typename Tuple2>
113 auto assignValues(Tuple1& tuple1,
const Tuple2& tuple2)
115 using std::tuple_size_v;
117 constexpr
auto N1 = tuple_size_v<Tuple1>;
118 constexpr
auto N2 = tuple_size_v<Tuple2>;
119 static_assert(N1 == N2);
120 For<N1>([&](
auto i) constexpr {
121 assignValue(get<i>(tuple1), get<i>(tuple2));
127 using CacheType =
typename MemoizationTraits<Decay<T>>::CacheType;
152 template<
typename Ret,
typename... Args>
155 auto cache = std::make_shared<
Map<
Tuple<Args...>, Ret>>();
156 return [=](Args... args)
mutable -> Ret
160 Tuple<Args...> t(args...);
161 if(cache->find(t) == cache->end())
162 (*cache)[t] = f(args...);
168 template<
typename Fun, Requires<!isFunction<Fun>> = true>
175 template<
typename Ret,
typename... Args>
180 auto firsttime =
true;
181 return [=](Args... args)
mutable -> Ret
185 if(detail::sameValues(cache, std::tie(args...)) && !firsttime)
187 detail::assignValues(cache, std::tie(args...));
189 return result = f(args...);
194 template<
typename Fun, Requires<!isFunction<Fun>> = true>
201 template<
typename Ret,
typename RetRef,
typename... Args>
206 auto firsttime =
true;
207 return [=](RetRef res, Args... args)
mutable ->
void
211 else if(detail::sameValues(cache, std::tie(args...)) && !firsttime)
217 detail::assignValues(cache, std::tie(args...));
226 template<
typename Ret,
typename Fun, Requires<!isFunction<Fun>> = true>
229 return memoizeLastUsingRef<Ret>(
asFunction(f));
234 template<
typename Ret,
typename... Args>
237 return memoizeLastUsingRef<Ret, Ret&>(f);
243 template<
typename Fun, Requires<!isFunction<Fun>> = true>
The class used to control memoization in the application.
Definition: Memoization.hpp:133
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:28
auto memoize(Fn< Ret(Args...)> f) -> Fn< Ret(Args...)>
Return a memoized version of given function f.
Definition: Memoization.hpp:153
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:202
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:176
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:73
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:65
Type CacheType
The type of the object constructed with a given object of type T.
Definition: Memoization.hpp:56