lru.hh
Go to the documentation of this file.00001 #ifndef DUNE_COMMON_LRU_HH
00002 #define DUNE_COMMON_LRU_HH
00003
00004 #include <list>
00005 #include <utility>
00006 #include <map>
00007
00020 namespace Dune {
00021
00022 template <typename _Key, typename _Tp,
00023 typename _Alloc = std::allocator<_Tp> >
00024 struct _lru_default_traits
00025 {
00026 typedef _Key key_type;
00027 typedef _Alloc allocator;
00028 typedef std::list< std::pair<_Key, _Tp> > list_type;
00029 typedef typename list_type::iterator iterator;
00030 typedef typename std::less<key_type> cmp;
00031 typedef std::map< key_type, iterator, cmp, allocator > map_type;
00032 };
00033
00034 template <typename _Key, typename _Tp,
00035 typename _Traits = _lru_default_traits<_Key, _Tp> >
00036 class lru
00037 {
00038 typedef typename _Traits::list_type list_type;
00039 typedef typename _Traits::map_type map_type;
00040 typedef typename _Traits::allocator allocator;
00041 typedef typename map_type::iterator map_iterator;
00042 typedef typename map_type::const_iterator const_map_iterator;
00043
00044 public:
00045 typedef typename _Traits::key_type key_type;
00046 typedef typename allocator::value_type value_type;
00047 typedef typename allocator::pointer pointer;
00048 typedef typename allocator::const_pointer const_pointer;
00049 typedef typename allocator::const_reference const_reference;
00050 typedef typename allocator::reference reference;
00051 typedef typename allocator::size_type size_type;
00052 typedef typename list_type::iterator iterator;
00053 typedef typename list_type::const_iterator const_iterator;
00054
00059 reference front()
00060 {
00061 return _data.front().second;
00062 }
00063
00068 const_reference front() const
00069 {
00070 return _data.front().second;
00071 }
00072
00077 reference back()
00078 {
00079 return _data.back().second;
00080 }
00081
00086 const_reference back (int i) const
00087 {
00088 return _data.back().second;
00089 }
00090
00091
00095 const void pop_front()
00096 {
00097 key_type k = _data.front().first;
00098 _data.pop_front();
00099 _index.erase(k);
00100 }
00104 const void pop_back()
00105 {
00106 key_type k = _data.back().first;
00107 _data.pop_back();
00108 _index.erase(k);
00109 }
00110
00116 iterator find (const key_type & key)
00117 {
00118 const map_iterator it = _index.find(key);
00119 if (it == _index.end()) return _data.end();
00120 return it->second;
00121 }
00122
00128 const_iterator find (const key_type & key) const
00129 {
00130 const map_iterator it = _index.find(key);
00131 if (it == _index.end()) return _data.end();
00132 return it->second;
00133 }
00134
00145 reference insert (const key_type & key, const_reference data)
00146 {
00147 std::pair<key_type, value_type> x(key, data);
00148
00149 iterator it = _data.insert(_data.begin(), x);
00150
00151 _index[key] = it;
00152
00153 return it->second;
00154 }
00155
00159 reference insert (const key_type & key)
00160 {
00161 return touch (key);
00162 }
00163
00169 reference touch (const key_type & key)
00170 {
00171
00172 iterator it = _index[key];
00173
00174
00175
00176 _data.splice(_data.begin(), _data, it);
00177 return it->second;
00178 }
00179
00183 size_type size() const
00184 {
00185 return _data.size();
00186 }
00187
00191 void resize(size_type new_size)
00192 {
00193 assert(new_size <= size());
00194
00195 while (new_size < size())
00196 pop_back();
00197 }
00198
00202 void clear()
00203 {
00204 _data.clear();
00205 _index.clear();
00206 }
00207
00208 private:
00209 list_type _data;
00210 map_type _index;
00211
00212 };
00213
00214 }
00215
00216 #endif // DUNE_COMMON_LRU_HH