Dune Core Modules (2.6.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#include <dune/common/unused.hh>
12
18namespace Dune {
19
20 namespace {
21
22 /*
23 hide the default traits in an empty namespace
24 */
25 template <typename _Key, typename _Tp,
26 typename _Alloc = std::allocator<_Tp> >
27 struct _lru_default_traits
28 {
29 typedef _Key key_type;
30 typedef _Alloc allocator;
31 typedef std::list< std::pair<_Key, _Tp> > list_type;
32 typedef typename list_type::iterator iterator;
33 typedef typename std::less<key_type> cmp;
34 typedef std::map< key_type, iterator, cmp,
35 typename allocator::template rebind<std::pair<const key_type, iterator> >::other > map_type;
36 };
37
38 } // end empty namespace
39
47 template <typename _Key, typename _Tp,
48 typename _Traits = _lru_default_traits<_Key, _Tp> >
49 class lru
50 {
51 typedef typename _Traits::list_type list_type;
52 typedef typename _Traits::map_type map_type;
53 typedef typename _Traits::allocator allocator;
54 typedef typename map_type::iterator map_iterator;
55 typedef typename map_type::const_iterator const_map_iterator;
56
57 public:
58 typedef typename _Traits::key_type key_type;
59 typedef typename allocator::value_type value_type;
60 typedef typename allocator::pointer pointer;
61 typedef typename allocator::const_pointer const_pointer;
62 typedef typename allocator::const_reference const_reference;
63 typedef typename allocator::reference reference;
64 typedef typename allocator::size_type size_type;
65 typedef typename list_type::iterator iterator;
66 typedef typename list_type::const_iterator const_iterator;
67
72 reference front()
73 {
74 return _data.front().second;
75 }
76
81 const_reference front() const
82 {
83 return _data.front().second;
84 }
85
90 reference back()
91 {
92 return _data.back().second;
93 }
94
99 const_reference back (int i) const
100 {
102 return _data.back().second;
103 }
104
105
110 {
111 key_type k = _data.front().first;
112 _data.pop_front();
113 _index.erase(k);
114 }
118 void pop_back()
119 {
120 key_type k = _data.back().first;
121 _data.pop_back();
122 _index.erase(k);
123 }
124
130 iterator find (const key_type & key)
131 {
132 const map_iterator it = _index.find(key);
133 if (it == _index.end()) return _data.end();
134 return it->second;
135 }
136
142 const_iterator find (const key_type & key) const
143 {
144 const map_iterator it = _index.find(key);
145 if (it == _index.end()) return _data.end();
146 return it->second;
147 }
148
160 reference insert (const key_type & key, const_reference data)
161 {
162 std::pair<key_type, value_type> x(key, data);
163 /* insert item as mru */
164 iterator it = _data.insert(_data.begin(), x);
165 /* store index */
166 _index.insert(std::make_pair(key,it));
167
168 return it->second;
169 }
170
174 reference insert (const key_type & key)
175 {
176 return touch (key);
177 }
178
184 reference touch (const key_type & key)
185 {
186 /* query _index for iterator */
187 map_iterator it = _index.find(key);
188 if (it == _index.end())
190 "Failed to touch key " << key << ", it is not in the lru container");
191 /* update _data
192 move it to the front
193 */
194 _data.splice(_data.begin(), _data, it->second);
195 return it->second->second;
196 }
197
201 size_type size() const
202 {
203 return _data.size();
204 }
205
212 void resize(size_type new_size)
213 {
214 assert(new_size <= size());
215
216 while (new_size < size())
217 pop_back();
218 }
219
223 void clear()
224 {
225 _data.clear();
226 _index.clear();
227 }
228
229 private:
230 list_type _data;
231 map_type _index;
232
233 };
234
235} // namespace Dune
236
237#endif // DUNE_COMMON_LRU_HH
Default exception class for range errors.
Definition: exceptions.hh:252
LRU Cache Container.
Definition: lru.hh:50
reference back()
Definition: lru.hh:90
reference touch(const key_type &key)
mark data associated with key as most recent
Definition: lru.hh:184
const_reference back(int i) const
Definition: lru.hh:99
size_type size() const
Retrieve number of entries in the container.
Definition: lru.hh:201
reference insert(const key_type &key)
mark data associated with key as most recent
Definition: lru.hh:174
void pop_back()
Removes the last element.
Definition: lru.hh:118
reference insert(const key_type &key, const_reference data)
Insert a value into the container.
Definition: lru.hh:160
void pop_front()
Removes the first element.
Definition: lru.hh:109
void resize(size_type new_size)
ensure a maximum size of the container
Definition: lru.hh:212
const_reference front() const
Definition: lru.hh:81
iterator find(const key_type &key)
Finds the element whose key is k.
Definition: lru.hh:130
reference front()
Definition: lru.hh:72
const_iterator find(const key_type &key) const
Finds the element whose key is k.
Definition: lru.hh:142
A few common exception classes.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
Dune namespace.
Definition: alignedallocator.hh:10
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 26, 23:30, 2024)