3#ifndef DUNE_COMMON_LRU_HH
4#define DUNE_COMMON_LRU_HH
24 template <
typename _Key,
typename _Tp,
25 typename _Alloc = std::allocator<_Tp> >
26 struct _lru_default_traits
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;
46 template <
typename _Key,
typename _Tp,
47 typename _Traits = _lru_default_traits<_Key, _Tp> >
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;
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;
73 return _data.front().second;
82 return _data.front().second;
91 return _data.back().second;
98 const_reference
back (
int i)
const
100 return _data.back().second;
109 key_type k = _data.front().first;
118 key_type k = _data.back().first;
128 iterator
find (
const key_type & key)
130 const map_iterator it = _index.find(key);
131 if (it == _index.end())
return _data.end();
140 const_iterator
find (
const key_type & key)
const
142 const map_iterator it = _index.find(key);
143 if (it == _index.end())
return _data.end();
158 reference
insert (
const key_type & key, const_reference data)
160 std::pair<key_type, value_type> x(key, data);
162 iterator it = _data.insert(_data.begin(), x);
164 _index.insert(std::make_pair(key,it));
182 reference
touch (
const key_type & key)
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");
192 _data.splice(_data.begin(), _data, it->second);
193 return it->second->second;
212 assert(new_size <=
size());
214 while (new_size <
size())
Default exception class for range errors.
Definition: exceptions.hh:279
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:243
Dune namespace.
Definition: alignment.hh:10