Molassembler  1.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Cache.h
Go to the documentation of this file.
1 
10 #ifndef INCLUDE_MOLASSEMBLER_TEMPLE_CACHE_H
11 #define INCLUDE_MOLASSEMBLER_TEMPLE_CACHE_H
12 
13 #include <boost/any.hpp>
14 #include <boost/optional.hpp>
15 
16 #include <map>
17 #include <string>
18 #include <functional>
19 #include <cassert>
20 #include <vector>
21 
22 namespace Scine {
23 namespace Molassembler {
24 namespace Temple {
25 
27 template<typename KeyType, typename ValueType>
28 class MinimalCache {
29 public:
30  using key_type = KeyType;
31  using value_type = ValueType;
32 
36  void add(const KeyType key, const ValueType value) {
37  cache_.emplace(
38  std::move(key),
39  std::move(value)
40  );
41  }
42 
44  void invalidate() {
45  cache_.clear();
46  }
47 
49  void invalidate(const KeyType& key) {
50  if(cache_.count(key) == 1) {
51  cache_.erase(key);
52  }
53  }
55 
59  boost::optional<const ValueType&> getOption(const KeyType& key) const {
60  if(cache_.count(key) == 1) {
61  return boost::optional<const ValueType&>(
62  cache_.at(key)
63  );
64  }
65 
66  return {};
67  }
68 
70  bool has(const KeyType& key) const {
71  return cache_.count(key) > 0;
72  }
73 
75  const ValueType& get(const KeyType& key) const {
76  if(cache_.count(key) == 0) {
77  throw "Fetching member in Cache whose key does not exist!";
78  }
79 
80  return cache_.at(key);
81  }
83 
84 private:
85 /* Private members */
87  std::map<KeyType, ValueType> cache_;
88 };
89 
98 template<typename KeyType>
99 class Cache {
100 public:
103  Cache() = default;
104  Cache(
105  const std::initializer_list<
106  std::pair<
107  KeyType,
108  std::function<
109  boost::any()
110  >
111  >
112  >& initList
113  ) {
114  // add all generators
115  for(const auto& pair: initList) {
116  generationMap_[pair.first] = pair.second;
117  }
118  }
120 
121 
125  template<typename T>
126  void add(const KeyType& key, const T& value) {
127  cache_.emplace(
128  key,
129  value
130  );
131  }
132 
137  template<typename T>
138  T getGeneratable(const KeyType& key) {
139  // if this is false, user has violated contract
140  assert(generationMap_.count(key) == 1);
141 
142  if(cache_.count(key) == 1) {
143  return boost::any_cast<T>(
144  cache_.at(key)
145  );
146  }
147 
148  cache_.emplace(
149  key,
150  generationMap_.at(key)() // calling it!
151  );
152 
153  return boost::any_cast<T>(
154  cache_.at(key)
155  );
156  }
157 
158  /* C++17
159  * get rid of the raw pointer using map's extract and insert
160  */
162  template<typename T>
164  const KeyType& key,
165  std::function<
166  void(T*)
167  > modifyingUnaryFunction
168  ) {
169  assert(generationMap_.count(key) == 1);
170 
171  // if the generatable does not exist yet, generate it
172  if(cache_.count(key) == 0) {
173  cache_.emplace(
174  key,
175  generationMap_.at(key)()
176  );
177  }
178 
179  // modify it
180  modifyingUnaryFunction(
181  boost::any_cast<T>(
182  &(
183  cache_.find(key) -> second
184  )
185  )
186  );
187 
188  }
189 
191  void invalidate() {
192  cache_.clear();
193  }
194 
196  void invalidate(const KeyType& key) {
197  if(cache_.count(key) == 1) {
198  cache_.erase(key);
199  }
200  }
202 
206  template<typename T>
207  boost::optional<T> getOption(const KeyType& key) const {
208  if(cache_.count(key) == 1) {
209  return boost::optional<T>(
210  boost::any_cast<T>(
211  cache_.at(key)
212  )
213  );
214  }
215 
216  return {};
217  }
218 
220  bool has(const KeyType& key) const {
221  return (cache_.count(key) > 0);
222  }
224 
225 private:
226 /* Private members */
228  std::map<
229  KeyType,
230  boost::any
232 
234  std::map<
235  KeyType,
236  std::function<
237  boost::any()
238  >
240 };
241 
242 } // namespace Temple
243 } // namespace Molassembler
244 } // namespace Scine
245 
246 #endif
void invalidate(const KeyType &key)
Selectively invalidates cache entries.
Definition: Cache.h:196
void invalidate()
Invalidates the entire cache, removing all stored data.
Definition: Cache.h:44
std::map< KeyType, ValueType > cache_
Cache data.
Definition: Cache.h:87
void changeGeneratable(const KeyType &key, std::function< > modifyingUnaryFunction)
Permits the modification of a generatable cache item via a raw pointer.
Definition: Cache.h:163
constexpr Get< 1 > second
Calls std::get&lt;1&gt; on any argument it is invoked with.
Definition: Functor.h:125
void invalidate(const KeyType &key)
Selectively invalidates cache entries.
Definition: Cache.h:49
void add(const KeyType key, const ValueType value)
Adds a data value for a key value into the cache.
Definition: Cache.h:36
T getGeneratable(const KeyType &key)
Definition: Cache.h:138
A minimal wrapper around a map class with cache semantics.
Definition: Cache.h:28
bool has(const KeyType &key) const
Tests whether the cache contains an entry for a key.
Definition: Cache.h:220
boost::optional< const ValueType & > getOption(const KeyType &key) const
Fetches a cache entry via an optional.
Definition: Cache.h:59
bool has(const KeyType &key) const
Tests whether the cache contains an entry for a key.
Definition: Cache.h:70
std::map< KeyType, std::function< boost::any() > > generationMap_
Map of key values to generating functions.
Definition: Cache.h:239
constexpr auto map(const ArrayType< T, size > &array, UnaryFunction &&function)
Maps all elements of any array-like container with a unary function.
Definition: Containers.h:62
std::map< KeyType, boost::any > cache_
Cache data.
Definition: Cache.h:231
void invalidate()
Invalidates the entire cache, removing all stored data.
Definition: Cache.h:191
boost::optional< T > getOption(const KeyType &key) const
Fetches a cache entry via an optional.
Definition: Cache.h:207
void add(const KeyType &key, const T &value)
Adds a data value for a key value into the cache.
Definition: Cache.h:126