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, 
00032                       typename allocator::template rebind<std::pair<const key_type, iterator> >::other > map_type;
00033 };
00034 
00035 template <typename _Key, typename _Tp,
00036           typename _Traits = _lru_default_traits<_Key, _Tp> >
00037 class lru
00038 {
00039     typedef typename _Traits::list_type       list_type;
00040     typedef typename _Traits::map_type        map_type;
00041     typedef typename _Traits::allocator       allocator;
00042     typedef typename map_type::iterator       map_iterator;
00043     typedef typename map_type::const_iterator const_map_iterator;
00044     
00045 public:
00046     typedef typename _Traits::key_type          key_type;
00047     typedef typename allocator::value_type      value_type;
00048     typedef typename allocator::pointer         pointer;
00049     typedef typename allocator::const_pointer   const_pointer;
00050     typedef typename allocator::const_reference const_reference;
00051     typedef typename allocator::reference       reference;
00052     typedef typename allocator::size_type       size_type;
00053     typedef typename list_type::iterator        iterator;
00054     typedef typename list_type::const_iterator  const_iterator;
00055   
00060     reference front()
00061     {
00062         return _data.front().second;
00063     }
00064 
00069     const_reference front() const
00070     {
00071         return _data.front().second;
00072     }
00073 
00078     reference back()
00079     {
00080         return _data.back().second;
00081     }
00082 
00087     const_reference back (int i) const
00088     {
00089         return _data.back().second;
00090     }
00091 
00092 
00096     const void pop_front()
00097     {
00098         key_type k = _data.front().first;
00099         _data.pop_front();
00100         _index.erase(k);
00101     }
00105     const void pop_back()
00106     {
00107         key_type k = _data.back().first;
00108         _data.pop_back();
00109         _index.erase(k);
00110     }
00111 
00117     iterator find (const key_type & key)
00118     {
00119         const map_iterator it = _index.find(key);
00120         if (it == _index.end()) return _data.end();
00121         return it->second;
00122     }
00123     
00129     const_iterator find (const key_type & key) const
00130     {
00131         const map_iterator it = _index.find(key);
00132         if (it == _index.end()) return _data.end();
00133         return it->second;
00134     }
00135 
00146     reference insert (const key_type & key, const_reference data)
00147     {
00148         std::pair<key_type, value_type> x(key, data);
00149         /* insert item as mru */
00150         iterator it = _data.insert(_data.begin(), x);
00151         /* store index */
00152         _index[key] = it;
00153 
00154         return it->second;
00155     }
00156 
00160     reference insert (const key_type & key)
00161     {
00162         return touch (key);
00163     }
00164 
00170     reference touch (const key_type & key)
00171     {
00172         /* query _index for iterator */
00173         iterator it = _index[key];
00174         /* update _data
00175            move it to the front
00176          */
00177         _data.splice(_data.begin(), _data, it);
00178         return it->second;
00179     }
00180 
00184     size_type size() const
00185     {
00186         return _data.size();
00187     }    
00188     
00192     void resize(size_type new_size)
00193     {
00194         assert(new_size <= size());
00195 
00196         while (new_size < size())
00197             pop_back();
00198     }
00199     
00203     void clear()
00204     {
00205         _data.clear();
00206         _index.clear();
00207     }    
00208     
00209 private:
00210     list_type _data;
00211     map_type  _index;
00212   
00213 };
00214 
00215 } // namespace Dune
00216 
00217 #endif // DUNE_COMMON_LRU_HH

Generated on Sun Nov 15 22:28:13 2009 for dune-common by  doxygen 1.5.6