Cache.hpp
Go to the documentation of this file.
1 #ifndef _QUORI_FACE_CACHE_HPP_
2 #define _QUORI_FACE_CACHE_HPP_
3 
4 #include <deque>
5 #include <cstdint>
6 #include <functional>
7 #include <mutex>
8 
9 namespace quori_face
10 {
19  template<typename K, typename V>
20  class Cache
21  {
22  public:
28  Cache(const std::size_t size = 5UL)
29  : size_(size)
30  {
31  }
32 
41  const V &getOrCompute(const K &key, const std::function<V (const K &k)> &f)
42  {
43  std::lock_guard<std::mutex> guard(mut_);
44 
45  for (auto it = entries_.cbegin(); it != entries_.cend(); ++it)
46  {
47  if (it->first == key) return it->second;
48  }
49 
50  entries_.push_front(std::make_pair(key, f(key)));
51  if (entries_.size() > size_) entries_.pop_back();
52  return entries_.front().second;
53  }
54 
55  private:
56  mutable std::mutex mut_;
57  std::deque<std::pair<K, V>> entries_;
58  std::size_t size_;
59  };
60 }
61 
62 #endif
quori_face::Cache
A simple memoization object that eliminates repetitive computations.
Definition: Cache.hpp:20
size
GLsizeiptr size
Definition: glcorearb.h:640
quori_face::Cache::entries_
std::deque< std::pair< K, V > > entries_
Definition: Cache.hpp:57
quori_face::Cache::size_
std::size_t size_
Definition: Cache.hpp:58
quori_face
Definition: Cache.hpp:9
quori_face::Cache::getOrCompute
const V & getOrCompute(const K &key, const std::function< V(const K &k)> &f)
Definition: Cache.hpp:41
quori_face::Cache::mut_
std::mutex mut_
Definition: Cache.hpp:56
f
GLdouble f
Definition: glcorearb.h:291
quori_face::Cache::Cache
Cache(const std::size_t size=5UL)
Definition: Cache.hpp:28


quori_face
Author(s):
autogenerated on Wed Mar 2 2022 00:53:20