5#include <unordered_map>
11namespace memoization {
15inline void hash_combine(std::size_t &seed,
const T &val) {
16 seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
20template <typename tuple, std::size_t index = std::tuple_size<tuple>::value - 1>
23 static void apply(std::size_t &seed,
const tuple &tup) {
24 tupleHasher<tuple, index - 1>::apply(seed, tup);
25 hash_combine(seed, std::get<index>(tup));
30template <
typename tuple>
31struct tupleHasher<tuple, 0> {
33 static void apply(std::size_t &seed,
const tuple &tup) {
34 hash_combine(seed, std::get<0>(tup));
41 template <
typename... T>
42 std::size_t operator()(
const std::tuple<T...> &t)
const {
44 tupleHasher<std::tuple<T...>>::apply(seed, t);
69template <
typename R,
typename... Args>
72 std::function<
R(
Args...)> lambda;
73 std::unordered_map<std::tuple<
Args...>,
R, memoization::tupleHash> cache;
106 if (cache.find(
arg_list) == cache.end()) {
A wrapper for std::vector.
Definition array.hpp:72
void clear()
Clear the data in the array.
Definition array.hpp:884
memoize(R lambda(Args...))
Constructor with a function pointer.
Definition memoize.hpp:89
R operator()(Args... args)
Function call operator.
Definition memoize.hpp:103
void clear()
Clears the cache.
Definition memoize.hpp:118
memoize(std::function< R(Args...)> &&lambda)
Constructor with any function-like object.
Definition memoize.hpp:80