5#ifndef DUNE_COMMON_LRU_HH
6#define DUNE_COMMON_LRU_HH
27 template <
typename Key,
typename Tp,
28 typename Alloc = std::allocator<Tp> >
29 struct _lru_default_traits
32 typedef Alloc allocator;
33 typedef std::list< std::pair<Key, Tp> > list_type;
34 typedef typename list_type::iterator iterator;
35 typedef typename std::less<key_type> cmp;
36 typedef std::map< key_type, iterator, cmp,
37 typename std::allocator_traits<allocator>::template rebind_alloc<std::pair<const key_type, iterator> > > map_type;
49 template <
typename Key,
typename Tp,
50 typename Traits = _lru_default_traits<Key, Tp> >
53 typedef typename Traits::list_type list_type;
54 typedef typename Traits::map_type map_type;
55 typedef typename Traits::allocator allocator;
56 typedef typename map_type::iterator map_iterator;
57 typedef typename map_type::const_iterator const_map_iterator;
62 using pointer =
typename allocator::value_type*;
76 return _data.front().second;
85 return _data.front().second;
94 return _data.back().second;
103 return _data.back().second;
133 const map_iterator it = _index.find(key);
134 if (it == _index.end())
return _data.end();
145 const map_iterator it = _index.find(key);
146 if (it == _index.end())
return _data.end();
163 std::pair<key_type, value_type> x(key, data);
165 iterator it = _data.insert(_data.begin(), x);
167 _index.insert(std::make_pair(key,it));
188 map_iterator it = _index.find(key);
189 if (it == _index.end())
191 "Failed to touch key " << key <<
", it is not in the lru container");
195 _data.splice(_data.begin(), _data, it->second);
196 return it->second->second;
215 assert(new_size <=
size());
217 while (new_size <
size())
A few common exception classes.
#define DUNE_THROW(E, m)
Definition exceptions.hh:218
Dune namespace.
Definition alignedallocator.hh:13
Default exception class for range errors.
Definition exceptions.hh:254
LRU Cache Container.
Definition lru.hh:52
void pop_back()
Removes the last element.
Definition lru.hh:119
iterator find(const key_type &key)
Finds the element whose key is k.
Definition lru.hh:131
reference insert(const key_type &key)
mark data associated with key as most recent
Definition lru.hh:175
list_type::const_iterator const_iterator
Definition lru.hh:68
void resize(size_type new_size)
ensure a maximum size of the container
Definition lru.hh:213
allocator::size_type size_type
Definition lru.hh:66
list_type::iterator iterator
Definition lru.hh:67
allocator::value_type value_type
Definition lru.hh:61
const_reference front() const
Definition lru.hh:83
typename allocator::value_type const * const_pointer
Definition lru.hh:63
size_type size() const
Retrieve number of entries in the container.
Definition lru.hh:202
typename allocator::value_type & reference
Definition lru.hh:65
reference back()
Definition lru.hh:92
void pop_front()
Removes the first element.
Definition lru.hh:110
reference front()
Definition lru.hh:74
void clear()
Definition lru.hh:224
reference touch(const key_type &key)
mark data associated with key as most recent
Definition lru.hh:185
reference insert(const key_type &key, const_reference data)
Insert a value into the container.
Definition lru.hh:161
typename allocator::value_type * pointer
Definition lru.hh:62
const_iterator find(const key_type &key) const
Finds the element whose key is k.
Definition lru.hh:143
typename allocator::value_type const & const_reference
Definition lru.hh:64
Traits::key_type key_type
Definition lru.hh:60
const_reference back(int i) const
Definition lru.hh:101