Better LRU Cache implementation
This commit is contained in:
parent
e849d18c1b
commit
a04eb2bba1
@ -1,127 +1,72 @@
|
|||||||
#include <climits>
|
/*
|
||||||
#include <iostream>
|
open source routing machine
|
||||||
#include <list>
|
Copyright (C) Dennis Luxen, others 2010
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU AFFERO General Public License as published by
|
||||||
|
the Free Software Foundation; either version 3 of the License, or
|
||||||
|
any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
or see http://www.gnu.org/licenses/agpl.txt.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LRUCACHE_H
|
||||||
|
#define LRUCACHE_H
|
||||||
|
|
||||||
|
#include <list>
|
||||||
#include <boost/unordered_map.hpp>
|
#include <boost/unordered_map.hpp>
|
||||||
|
|
||||||
template < class T >
|
template<typename ValueT>
|
||||||
struct Countfn {
|
class LRUCache {
|
||||||
unsigned long operator()( const T &x ) { return 1; }
|
|
||||||
};
|
|
||||||
|
|
||||||
template< class Key, class Data, class Sizefn = Countfn< Data > > class LRUCache {
|
|
||||||
public:
|
|
||||||
typedef std::list< std::pair< Key, Data > > List; ///< Main cache storage typedef
|
|
||||||
typedef typename List::iterator List_Iter; ///< Main cache iterator
|
|
||||||
typedef typename List::const_iterator List_cIter; ///< Main cache iterator (const)
|
|
||||||
typedef std::vector< Key > Key_List; ///< List of keys
|
|
||||||
typedef typename Key_List::iterator Key_List_Iter; ///< Main cache iterator
|
|
||||||
typedef typename Key_List::const_iterator Key_List_cIter; ///< Main cache iterator (const)
|
|
||||||
typedef boost::unordered_map< Key, List_Iter > Map; ///< Index typedef
|
|
||||||
typedef std::pair< Key, List_Iter > Pair; ///< Pair of Map elements
|
|
||||||
typedef typename Map::iterator Map_Iter; ///< Index iterator
|
|
||||||
typedef typename Map::const_iterator Map_cIter; ///< Index iterator (const)
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
List _list; ///< Main cache storage
|
struct CacheEntry {
|
||||||
Map _index; ///< Cache storage index
|
CacheEntry(unsigned k, ValueT v) : key(k), value(v) {}
|
||||||
unsigned long _max_size; ///< Maximum abstract size of the cache
|
unsigned key;
|
||||||
unsigned long _curr_size; ///< Current abstract size of the cache
|
ValueT value;
|
||||||
|
|
||||||
public:
|
|
||||||
LRUCache( const unsigned long Size ) : _max_size( Size ), _curr_size( 0 ) { }
|
|
||||||
|
|
||||||
~LRUCache() { clear(); }
|
|
||||||
|
|
||||||
inline const unsigned long size( void ) const { return _curr_size; }
|
|
||||||
|
|
||||||
inline const unsigned long max_size( void ) const { return _max_size; }
|
|
||||||
|
|
||||||
/// Clears all storage and indices.
|
|
||||||
void clear( void ) {
|
|
||||||
_list.clear();
|
|
||||||
_index.clear();
|
|
||||||
};
|
};
|
||||||
|
unsigned capacity;
|
||||||
|
std::list<CacheEntry> itemsInCache;
|
||||||
|
boost::unordered_map<unsigned, typename std::list<CacheEntry>::iterator > positionMap;
|
||||||
|
public:
|
||||||
|
LRUCache(unsigned c) : capacity(c) {}
|
||||||
|
|
||||||
inline bool exists( const Key &key ) const {
|
bool Holds(unsigned key) {
|
||||||
return _index.find( key ) != _index.end();
|
if(positionMap.find(key) != positionMap.end()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void remove( const Key &key ) {
|
void Insert(const unsigned key, ValueT value) {
|
||||||
Map_Iter miter = _index.find( key );
|
itemsInCache.push_front(CacheEntry(key, value));
|
||||||
if( miter == _index.end() ) return;
|
positionMap.insert(std::make_pair(key, itemsInCache.begin()));
|
||||||
_remove( miter );
|
if(itemsInCache.size() > capacity) {
|
||||||
}
|
positionMap.erase(itemsInCache.back().key);
|
||||||
|
itemsInCache.pop_back();
|
||||||
inline void touch( const Key &key ) {
|
|
||||||
_touch( key );
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Data *fetch_ptr( const Key &key, bool touch = true ) {
|
|
||||||
Map_Iter miter = _index.find( key );
|
|
||||||
if( miter == _index.end() ) return NULL;
|
|
||||||
_touch( key );
|
|
||||||
return &(miter->second->second);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool fetch( const Key &key, Data &data, bool touch_data = true ) {
|
|
||||||
Map_Iter miter = _index.find( key );
|
|
||||||
if( miter == _index.end() ) return false;
|
|
||||||
if( touch_data )
|
|
||||||
_touch( key );
|
|
||||||
data = miter->second->second;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void insert( const Key &key, const Data data ) {
|
|
||||||
// Touch the key, if it exists, then replace the content.
|
|
||||||
Map_Iter miter = _touch( key );
|
|
||||||
if( miter != _index.end() )
|
|
||||||
_remove( miter );
|
|
||||||
|
|
||||||
// Ok, do the actual insert at the head of the list
|
|
||||||
_list.push_front( std::make_pair( key, data ) );
|
|
||||||
List_Iter liter = _list.begin();
|
|
||||||
|
|
||||||
// Store the index
|
|
||||||
_index.insert( std::make_pair( key, liter ) );
|
|
||||||
_curr_size += Sizefn()( data );
|
|
||||||
|
|
||||||
// Check to see if we need to remove an element due to exceeding max_size
|
|
||||||
while( _curr_size > _max_size ) {
|
|
||||||
std::cout << "removing element " << std::endl;
|
|
||||||
// Remove the last element.
|
|
||||||
liter = _list.end();
|
|
||||||
--liter;
|
|
||||||
_remove( liter->first );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bool Fetch(const unsigned key, ValueT& result) {
|
||||||
|
if(Holds(key)) {
|
||||||
|
CacheEntry e = *(positionMap.find(key)->second);
|
||||||
|
result = e.value;
|
||||||
|
|
||||||
inline const Key_List get_all_keys( void ) {
|
//move to front
|
||||||
Key_List ret;
|
itemsInCache.splice(positionMap.find(key)->second, itemsInCache, itemsInCache.begin());
|
||||||
for( List_cIter liter = _list.begin(); liter != _list.end(); liter++ )
|
positionMap.find(key)->second = itemsInCache.begin();
|
||||||
ret.push_back( liter->first );
|
return true;
|
||||||
return ret;
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
unsigned Size() const {
|
||||||
private:
|
return itemsInCache.size();
|
||||||
inline Map_Iter _touch( const Key &key ) {
|
|
||||||
Map_Iter miter = _index.find( key );
|
|
||||||
if( miter == _index.end() ) return miter;
|
|
||||||
// Move the found node to the head of the list.
|
|
||||||
_list.splice( _list.begin(), _list, miter->second );
|
|
||||||
return miter;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void _remove( const Map_Iter &miter ) {
|
|
||||||
_curr_size -= Sizefn()( miter->second->second );
|
|
||||||
_list.erase( miter->second );
|
|
||||||
_index.erase( miter );
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void _remove( const Key &key ) {
|
|
||||||
Map_Iter miter = _index.find( key );
|
|
||||||
_remove( miter );
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
#endif //LRUCACHE_H
|
||||||
|
Loading…
Reference in New Issue
Block a user