Dune Core Modules (2.5.0)

lru.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_COMMON_LRU_HH
4#define DUNE_COMMON_LRU_HH
5
6#include <list>
7#include <utility>
8#include <map>
9
11
17namespace Dune {
18
19 namespace {
20
21 /*
22 hide the default traits in an empty namespace
23 */
24 template <typename _Key, typename _Tp,
25 typename _Alloc = std::allocator<_Tp> >
26 struct _lru_default_traits
27 {
28 typedef _Key key_type;
29 typedef _Alloc allocator;
30 typedef std::list< std::pair<_Key, _Tp> > list_type;
31 typedef typename list_type::iterator iterator;
32 typedef typename std::less<key_type> cmp;
33 typedef std::map< key_type, iterator, cmp,
34 typename allocator::template rebind<std::pair<const key_type, iterator> >::other > map_type;
35 };
36
37 } // end empty namespace
38
46 template <typename _Key, typename _Tp,
47 typename _Traits = _lru_default_traits<_Key, _Tp> >
48 class lru
49 {
50 typedef typename _Traits::list_type list_type;
51 typedef typename _Traits::map_type map_type;
52 typedef typename _Traits::allocator allocator;
53 typedef typename map_type::iterator map_iterator;
54 typedef typename map_type::const_iterator const_map_iterator;
55
56 public:
57 typedef typename _Traits::key_type key_type;
58 typedef typename allocator::value_type value_type;
59 typedef typename allocator::pointer pointer;
60 typedef typename allocator::const_pointer const_pointer;
61 typedef typename allocator::const_reference const_reference;
62 typedef typename allocator::reference reference;
63 typedef typename allocator::size_type size_type;
64 typedef typename list_type::iterator iterator;
65 typedef typename list_type::const_iterator const_iterator;
66
71 reference front()
72 {
73 return _data.front().second;
74 }
75
80 const_reference front() const
81 {
82 return _data.front().second;
83 }
84
89 reference back()
90 {
91 return _data.back().second;
92 }
93
98 const_reference back (int i) const
99 {
100 return _data.back().second;
101 }
102
103
108 {
109 key_type k = _data.front().first;
110 _data.pop_front();
111 _index.erase(k);
112 }
116 void pop_back()
117 {
118 key_type k = _data.back().first;
119 _data.pop_back();
120 _index.erase(k);
121 }
122
128 iterator find (const key_type & key)
129 {
130 const map_iterator it = _index.find(key);
131 if (it == _index.end()) return _data.end();
132 return it->second;
133 }
134
140 const_iterator find (const key_type & key) const
141 {
142 const map_iterator it = _index.find(key);
143 if (it == _index.end()) return _data.end();
144 return it->second;
145 }
146
158 reference insert (const key_type & key, const_reference data)
159 {
160 std::pair<key_type, value_type> x(key, data);
161 /* insert item as mru */
162 iterator it = _data.insert(_data.begin(), x);
163 /* store index */
164 _index.insert(std::make_pair(key,it));
165
166 return it->second;
167 }
168
172 reference insert (const key_type & key)
173 {
174 return touch (key);
175 }
176
182 reference touch (const key_type & key)
183 {
184 /* query _index for iterator */
185 map_iterator it = _index.find(key);
186 if (it == _index.end())
188 "Failed to touch key " << key << ", it is not in the lru container");
189 /* update _data
190 move it to the front
191 */
192 _data.splice(_data.begin(), _data, it->second);
193 return it->second->second;
194 }
195
199 size_type size() const
200 {
201 return _data.size();
202 }
203
210 void resize(size_type new_size)
211 {
212 assert(new_size <= size());
213
214 while (new_size < size())
215 pop_back();
216 }
217
221 void clear()
222 {
223 _data.clear();
224 _index.clear();
225 }
226
227 private:
228 list_type _data;
229 map_type _index;
230
231 };
232
233} // namespace Dune
234
235#endif // DUNE_COMMON_LRU_HH
Default exception class for range errors.
Definition: exceptions.hh:252
LRU Cache Container.
Definition: lru.hh:49
reference back()
Definition: lru.hh:89
reference touch(const key_type &key)
mark data associated with key as most recent
Definition: lru.hh:182
const_reference back(int i) const
Definition: lru.hh:98
size_type size() const
Retrieve number of entries in the container.
Definition: lru.hh:199
reference insert(const key_type &key)
mark data associated with key as most recent
Definition: lru.hh:172
void pop_back()
Removes the last element.
Definition: lru.hh:116
reference insert(const key_type &key, const_reference data)
Insert a value into the container.
Definition: lru.hh:158
void pop_front()
Removes the first element.
Definition: lru.hh:107
void resize(size_type new_size)
ensure a maximum size of the container
Definition: lru.hh:210
const_reference front() const
Definition: lru.hh:80
iterator find(const key_type &key)
Finds the element whose key is k.
Definition: lru.hh:128
reference front()
Definition: lru.hh:71
const_iterator find(const key_type &key) const
Finds the element whose key is k.
Definition: lru.hh:140
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignment.hh:11
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)