ModErn Text Analysis
META Enumerates Textual Applications
splay_cache.h
Go to the documentation of this file.
1 
10 #ifndef META_SPLAY_CACHE_H_
11 #define META_SPLAY_CACHE_H_
12 
13 #include <memory>
14 #include <mutex>
15 #include <vector>
16 
17 #include "meta.h"
18 #include "util/optional.h"
19 
20 namespace meta
21 {
22 namespace caching
23 {
24 
28 template <class Key, class Value>
30 {
31  public:
39  splay_cache(uint64_t max_size = std::numeric_limits<uint64_t>::max());
40 
45 
51 
55  ~splay_cache();
56 
63  void insert(const Key& key, const Value& value);
64 
70  util::optional<Value> find(const Key& key);
71 
75  uint64_t size() const;
76 
80  void clear();
81 
82  private:
84  splay_cache(const splay_cache&) = delete;
85 
87  splay_cache& operator=(const splay_cache&) = delete;
88 
93  struct node
94  {
100  Key key;
102  Value value;
103 
109  node(const Key& new_key, const Value& new_value)
110  : left(nullptr), right(nullptr), key(new_key), value(new_value)
111  {
112  /* nothing */
113  }
114  };
115 
117  uint64_t size_;
119  uint64_t max_size_;
123  mutable std::mutex mutables_;
124 
129  void clear(node*& subroot);
130 
138  void insert(node*& subroot, const Key& key, const Value& value);
139 
148  void replace(node* subroot, const Key& key, const Value& value);
149 
159  void find(node*& subroot, const Key& key);
160 
165  void rotate_left(node*& subroot);
166 
171  void rotate_right(node*& subroot);
172 
173  public:
177  class splay_cache_exception : public std::runtime_error
178  {
179  public:
180  using std::runtime_error::runtime_error;
181  };
182 };
183 }
184 }
185 
186 #include "caching/splay_cache.tcc"
187 #endif
void insert(const Key &key, const Value &value)
Definition: splay_cache.tcc:47
Contains top-level namespace documentation for the META toolkit.
splay_cache & operator=(splay_cache &&)
splay_cache can be move assigned
Definition: splay_cache.tcc:29
void clear()
Empties the cache.
Definition: splay_cache.tcc:175
void rotate_left(node *&subroot)
Rotates the tree rooted at subroot to the left.
Definition: splay_cache.tcc:146
Key key
the key
Definition: splay_cache.h:100
void replace(node *subroot, const Key &key, const Value &value)
Replaces the key, value pair contained in the node pointed to by subroot with the given key...
Definition: splay_cache.tcc:94
A splay_cache is a fixed-size splay tree for cache operations.
Definition: splay_cache.h:29
~splay_cache()
Frees all objects in the cache.
Definition: splay_cache.tcc:41
uint64_t max_size_
the maximum allowed size for the cache
Definition: splay_cache.h:119
node(const Key &new_key, const Value &new_value)
Constructs a new leaf node with the given key and value pair.
Definition: splay_cache.h:109
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
std::mutex mutables_
the mutex that synchronizes access to the cache
Definition: splay_cache.h:123
uint64_t size() const
Definition: splay_cache.tcc:169
node * right
pointer to the right child
Definition: splay_cache.h:98
Value value
the value
Definition: splay_cache.h:102
util::optional< Value > find(const Key &key)
Definition: splay_cache.tcc:101
node * left
pointer to the left child
Definition: splay_cache.h:96
node * root_
the root of the tree
Definition: splay_cache.h:121
One node in the splay tree contains pointers to children and the templated (key, value) pair...
Definition: splay_cache.h:93
void rotate_right(node *&subroot)
Rotates the tree rooted at subroot to the right.
Definition: splay_cache.tcc:157
splay_cache(uint64_t max_size=std::numeric_limits< uint64_t >::max())
Creates a splay tree cache with maximum size (or unlimited size, if no size is given).
Definition: splay_cache.tcc:13
Basic exception for splay_cache interactions.
Definition: splay_cache.h:177
uint64_t size_
the current size of the cache
Definition: splay_cache.h:117