Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
Memoizer.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#include "Stroika/Foundation/Containers/Common.h"
5
7
8 /*
9 ********************************************************************************
10 ******************************** Cache::Memoizer *******************************
11 ********************************************************************************
12 */
13 template <typename RESULT, template <qStroika_template_template_BWA (typename, typename)> class CACHE, typename... ARGS>
14 requires (ICache<CACHE<tuple<ARGS...>, RESULT>, tuple<ARGS...>, RESULT>)
15 Memoizer<RESULT, CACHE, ARGS...>::Memoizer (const function<RESULT (ARGS...)>& f, CACHE<tuple<ARGS...>, RESULT>&& cache)
16 : fFunction_{f}
17 , fCache_{forward<CACHE<tuple<ARGS...>, RESULT>> (cache)}
18 {
19 }
20 template <typename RESULT, template <qStroika_template_template_BWA (typename, typename)> class CACHE, typename... ARGS>
21 requires (ICache<CACHE<tuple<ARGS...>, RESULT>, tuple<ARGS...>, RESULT>)
23 {
24 // Lookup the value in the cache, and if that fails, call fFunction_ (and add that to the cache)
25 return fCache_.LookupValue (make_tuple (args...), [&] (const tuple<ARGS...>& t) { return apply (fFunction_, t); });
26 }
27
28 /*
29 ********************************************************************************
30 ************************ Cache::Factory::Memoizer::Make ************************
31 ********************************************************************************
32 */
33 namespace Private_ {
34 template <template <typename...> typename CACHE, typename F, typename Tuple>
35 struct memoizer_builder;
36 template <template <typename...> typename CACHE, typename F, typename... Args>
37 struct memoizer_builder<CACHE, F, std::tuple<Args...>> {
39 };
40 }
41 template <template <qStroika_template_template_BWA (typename, typename)> typename CACHE, typename FUNCTION>
42 auto Factory::Memoizer::Make (FUNCTION&& f)
43 {
44 using ArgsTuple = typename Common::FunctionTraits<FUNCTION>::args_tuple;
45 typename Private_::memoizer_builder<CACHE, FUNCTION, ArgsTuple>::type m (forward<FUNCTION> (f));
46 return m;
47 }
48
49}
LRUCache implements a simple least-recently-used caching strategy, with optional hashing (of keys) to...
Definition LRUCache.h:224
Cache the results of expensive computations transparently.
Definition Memoizer.h:69
STL namespace.
Extract the number of arguments, return type, and each individual argument type from a lambda or simp...