Better LRU Cache implementation
This commit is contained in:
		
							parent
							
								
									e849d18c1b
								
							
						
					
					
						commit
						a04eb2bba1
					
				@ -1,127 +1,72 @@
 | 
			
		||||
#include <climits>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vector>
 | 
			
		||||
/*
 | 
			
		||||
    open source routing machine
 | 
			
		||||
    Copyright (C) Dennis Luxen, others 2010
 | 
			
		||||
 | 
			
		||||
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>
 | 
			
		||||
 | 
			
		||||
template < class T >
 | 
			
		||||
struct Countfn {
 | 
			
		||||
    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)
 | 
			
		||||
 | 
			
		||||
template<typename ValueT>
 | 
			
		||||
class LRUCache {
 | 
			
		||||
private:
 | 
			
		||||
    List _list;               ///< Main cache storage
 | 
			
		||||
    Map _index;               ///< Cache storage index
 | 
			
		||||
    unsigned long _max_size;  ///< Maximum abstract size of the cache
 | 
			
		||||
    unsigned long _curr_size; ///< Current abstract size of the cache
 | 
			
		||||
 | 
			
		||||
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();
 | 
			
		||||
    struct CacheEntry {
 | 
			
		||||
        CacheEntry(unsigned k, ValueT v) : key(k), value(v) {}
 | 
			
		||||
        unsigned key;
 | 
			
		||||
        ValueT value;
 | 
			
		||||
    };
 | 
			
		||||
    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 {
 | 
			
		||||
        return _index.find( key ) != _index.end();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    inline void remove( const Key &key ) {
 | 
			
		||||
        Map_Iter miter = _index.find( key );
 | 
			
		||||
        if( miter == _index.end() ) return;
 | 
			
		||||
        _remove( miter );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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;
 | 
			
		||||
    bool Holds(unsigned key) {
 | 
			
		||||
        if(positionMap.find(key) != positionMap.end()) {
 | 
			
		||||
            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 );
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    inline const Key_List get_all_keys( void ) {
 | 
			
		||||
        Key_List ret;
 | 
			
		||||
        for( List_cIter liter = _list.begin(); liter != _list.end(); liter++ )
 | 
			
		||||
            ret.push_back( liter->first );
 | 
			
		||||
        return ret;
 | 
			
		||||
    void Insert(const unsigned key, ValueT value) {
 | 
			
		||||
        itemsInCache.push_front(CacheEntry(key, value));
 | 
			
		||||
        positionMap.insert(std::make_pair(key, itemsInCache.begin()));
 | 
			
		||||
        if(itemsInCache.size() > capacity) {
 | 
			
		||||
            positionMap.erase(itemsInCache.back().key);
 | 
			
		||||
            itemsInCache.pop_back();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    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;
 | 
			
		||||
    }
 | 
			
		||||
    bool Fetch(const unsigned key, ValueT& result) {
 | 
			
		||||
        if(Holds(key)) {
 | 
			
		||||
            CacheEntry e = *(positionMap.find(key)->second);
 | 
			
		||||
            result = e.value;
 | 
			
		||||
 | 
			
		||||
    inline void _remove( const Map_Iter &miter ) {
 | 
			
		||||
        _curr_size -= Sizefn()( miter->second->second );
 | 
			
		||||
        _list.erase( miter->second );
 | 
			
		||||
        _index.erase( miter );
 | 
			
		||||
            //move to front
 | 
			
		||||
            itemsInCache.splice(positionMap.find(key)->second, itemsInCache, itemsInCache.begin());
 | 
			
		||||
            positionMap.find(key)->second = itemsInCache.begin();
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    inline void _remove( const Key &key ) {
 | 
			
		||||
        Map_Iter miter = _index.find( key );
 | 
			
		||||
        _remove( miter );
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    unsigned Size() const {
 | 
			
		||||
        return itemsInCache.size();
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
#endif //LRUCACHE_H
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user