llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
cache.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include "core/adt/hash.h"
8 
12 template <typename T, typename... Args>
13 class Cache {
14 private:
15  using KeyT = std::tuple<Args...>;
16 
17 public:
18  T operator() (Args... args, std::function<T()> &&f)
19  {
20  KeyT key = {args...};
21  if (auto it = cache_.find(key); it != cache_.end()) {
22  return it->second;
23  }
24 
25  T result = f();
26  cache_.emplace(key, result);
27  return result;
28  }
29 
30 private:
31  std::unordered_map<KeyT, T> cache_;
32 };
Cache
Definition: cache.h:13