#pragma once #include #include #include #include #include #include namespace Void::Pipeline { // ── memoize_fn ──────────────────────────────────────────────────────────────── // Wrap a unary callable with an argument-keyed call cache. // // auto sq = memoize_fn([](int x){ return x * x; }); // sq(5); // computed once, cached forever // sq(5); // returned from cache namespace Detail { template struct MemoOne { Fn fn; std::unordered_map cache; R operator()(const Arg& arg) { auto it = cache.find(arg); if (it != cache.end()) return it->second; auto [ins, _] = cache.emplace(arg, std::invoke(fn, arg)); return ins->second; } }; template void hash_combine(std::size_t& seed, const T& v) { seed ^= std::hash{}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } template std::size_t hash_tuple_impl(const Tuple& t, std::index_sequence) { std::size_t seed = 0; (hash_combine(seed, std::get(t)), ...); return seed; } template struct TupleHash { std::size_t operator()(const std::tuple& t) const { return hash_tuple_impl(t, std::index_sequence_for{}); } }; } // namespace Detail // Explicit Arg template parameter is required for key type deduction. // Example: memoize_fn(lambda) where lambda takes int. template , Arg>> [[nodiscard]] auto memoize_fn(F&& fn) { return Detail::MemoOne, Arg, R>{std::forward(fn), {}}; } // ── memoize ─────────────────────────────────────────────────────────────────── // Wraps a callable F(self, Args...) -> R and caches results by argument tuple. // The first parameter is a self-reference enabling recursive memoization. // All arguments must be hashable. R must be copyable. // // auto fib = memoize([](auto& self, int n) -> int { // if (n < 2) return n; // return self(n-1) + self(n-2); // }); template class Memoized { using Fn = std::decay_t; public: explicit Memoized(F&& fn) : fn_{std::forward(fn)} {} template auto operator()(Args&&... args) -> std::invoke_result_t { using R = std::invoke_result_t; using Key = std::tuple...>; using Map = std::unordered_map...>>; // One cache per unique (Fn, Args...) combination. auto& cache = get_cache(); Key key{std::forward(args)...}; const auto it = cache.find(key); if (it != cache.end()) return it->second; R result = std::apply([this](const auto&... a) -> R { return std::invoke(fn_, *this, a...); }, key); auto [ins, _] = cache.emplace(key, std::move(result)); return ins->second; } // Clear the cache for a specific argument type signature. template void clear() { using R = std::invoke_result_t; using Key = std::tuple...>; using Map = std::unordered_map...>>; get_cache().clear(); } private: Fn fn_; template Map& get_cache() { static Map cache; return cache; } }; // Factory function: deduce F automatically. template [[nodiscard]] Memoized memoize(F&& fn) { return Memoized{std::forward(fn)}; } // ── SimpleCache ──────────────────────────────────────────────────────── // Straightforward key→value cache with optional miss function. template class SimpleCache { public: // get_or_insert: compute value if not cached. template [[nodiscard]] const V& get_or(const K& key, F&& compute) { const auto it = map_.find(key); if (it != map_.end()) return it->second; return map_.emplace(key, std::invoke(std::forward(compute), key)).first->second; } [[nodiscard]] std::optional get(const K& key) const { const auto it = map_.find(key); if (it == map_.end()) return std::nullopt; return it->second; } void put(K key, V value) { map_.insert_or_assign(std::move(key), std::move(value)); } bool erase(const K& key) { return map_.erase(key) > 0; } void clear() { map_.clear(); } [[nodiscard]] std::size_t size() const noexcept { return map_.size(); } [[nodiscard]] bool empty() const noexcept { return map_.empty(); } private: std::unordered_map map_; }; } // namespace Void::Pipeline