OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DictionaryCache.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef DICTIONARY_CACHE_HPP
18 #define DICTIONARY_CACHE_HPP
19 
20 #include <cstddef>
21 #include <list>
22 #include <memory>
23 #include <unordered_map>
24 
25 template <typename key_t, typename value_t>
27  public:
29 
30  void put(const key_t& key, const std::shared_ptr<value_t> value) {
31  auto it = cache_items.find(key);
32  if (it != cache_items.end()) {
33  cache_items.erase(it);
34  }
35  cache_items.insert({key, value});
36  }
37 
38  std::shared_ptr<value_t> get(const key_t& key) {
39  auto it = cache_items.find(key);
40  if (it == cache_items.end()) {
41  return nullptr;
42  }
43  return it->second;
44  }
45 
46  void remove(const key_t& key) { cache_items.erase(key); }
47 
48  bool is_empty() { return cache_items.empty(); }
49 
50  void invalidateInvertedIndex() noexcept {
51  if (!cache_items.empty()) {
52  cache_items.clear();
53  }
54  }
55 
56  private:
57  std::unordered_map<key_t, std::shared_ptr<value_t>> cache_items;
58 };
59 
60 #endif // DICTIONARY_CACHE_HPP
void invalidateInvertedIndex() noexcept
std::unordered_map< key_t, std::shared_ptr< value_t > > cache_items
void put(const key_t &key, const std::shared_ptr< value_t > value)