osgEarth 2.1.1
Classes | Public Member Functions | Protected Types | Protected Attributes

osgEarth::LRUCache< K, T > Class Template Reference

List of all members.

Classes

struct  Record

Public Member Functions

 LRUCache (unsigned max=100)
void insert (const K &key, const T &value)
Record get (const K &key)
void erase (const K &key)
void setMaxSize (unsigned max)
unsigned getMaxSize () const
CacheStats getStats () const

Protected Types

typedef std::list< K >::iterator lru_iter
typedef std::list< K > lru_type
typedef std::pair< T, lru_itermap_value_type
typedef std::map< K,
map_value_type
map_type
typedef map_type::iterator map_iter

Protected Attributes

map_type _map
lru_type _lru
unsigned _max
unsigned _buf
unsigned _queries
unsigned _hits

Detailed Description

template<typename K, typename T>
class osgEarth::LRUCache< K, T >

Least-recently-used cache class. K = key type, T = value type

usage: LRUCache<K,T> cache; cache.put( key, value ); LRUCache.Record rec = cache.get( key ); if ( rec.valid() ) const T& value = rec.value();

Definition at line 70 of file Utils.


Member Typedef Documentation

template<typename K, typename T>
typedef std::list<K>::iterator osgEarth::LRUCache< K, T >::lru_iter [protected]

Definition at line 83 of file Utils.

template<typename K, typename T>
typedef std::list<K> osgEarth::LRUCache< K, T >::lru_type [protected]

Definition at line 84 of file Utils.

template<typename K, typename T>
typedef map_type::iterator osgEarth::LRUCache< K, T >::map_iter [protected]

Definition at line 87 of file Utils.

template<typename K, typename T>
typedef std::map<K, map_value_type> osgEarth::LRUCache< K, T >::map_type [protected]

Definition at line 86 of file Utils.

template<typename K, typename T>
typedef std::pair<T, lru_iter> osgEarth::LRUCache< K, T >::map_value_type [protected]

Definition at line 85 of file Utils.


Constructor & Destructor Documentation

template<typename K, typename T>
osgEarth::LRUCache< K, T >::LRUCache ( unsigned  max = 100) [inline]

Definition at line 97 of file Utils.

                                      : _max(max) {
            _buf = _max/10;
            _queries = 0;
            _hits = 0;
        }

Member Function Documentation

template<typename K, typename T>
void osgEarth::LRUCache< K, T >::erase ( const K &  key) [inline]

Definition at line 143 of file Utils.

                                   {
            map_iter mi = _map.find( key );
            if ( mi != _map.end() ) {
                _lru.erase( mi->second.second );
                _map.erase( mi );
            }
        }
template<typename K, typename T>
Record osgEarth::LRUCache< K, T >::get ( const K &  key) [inline]

Definition at line 127 of file Utils.

                                   {
            _queries++;
            map_iter mi = _map.find( key );
            if ( mi != _map.end() ) {
                _lru.erase( mi->second.second );
                _lru.push_back( key );
                lru_iter new_iter = _lru.end(); new_iter--;
                mi->second.second = new_iter;
                _hits++;
                return Record( &(mi->second.first) );
            }
            else {
                return Record( 0L );
            }
        }

Here is the caller graph for this function:

template<typename K, typename T>
unsigned osgEarth::LRUCache< K, T >::getMaxSize ( ) const [inline]

Definition at line 161 of file Utils.

                                    {
            return _max;
        }

Here is the caller graph for this function:

template<typename K, typename T>
CacheStats osgEarth::LRUCache< K, T >::getStats ( ) const [inline]

Definition at line 165 of file Utils.

                                    {
            return CacheStats(
                _lru.size(), _max, _queries, _queries > 0 ? (float)_hits/(float)_queries : 0.0f );
        }

Here is the caller graph for this function:

template<typename K, typename T>
void osgEarth::LRUCache< K, T >::insert ( const K &  key,
const T &  value 
) [inline]

Definition at line 103 of file Utils.

                                                    {
            map_iter mi = _map.find( key );
            if ( mi != _map.end() ) {
                _lru.erase( mi->second.second );
                mi->second.first = value;
                _lru.push_back( key );
                mi->second.second = _lru.end();
                mi->second.second--;
            }
            else {
                _lru.push_back( key );
                lru_iter last = _lru.end(); last--;
                _map[key] = std::make_pair(value, last);
            }

            if ( _lru.size() > _max ) {
                for( unsigned i=0; i < _buf; ++i ) {
                    const K& key = _lru.front();
                    _map.erase( key );
                    _lru.pop_front();
                }
            }
        }

Here is the caller graph for this function:

template<typename K, typename T>
void osgEarth::LRUCache< K, T >::setMaxSize ( unsigned  max) [inline]

Definition at line 151 of file Utils.

                                        {
            _max = max;
            _buf = max/10;
            while( _lru.size() > _max ) {
                const K& key = _lru.front();
                _map.erase( key );
                _lru.pop_front();
            }
        }

Here is the caller graph for this function:


Member Data Documentation

template<typename K, typename T>
unsigned osgEarth::LRUCache< K, T >::_buf [protected]

Definition at line 92 of file Utils.

template<typename K, typename T>
unsigned osgEarth::LRUCache< K, T >::_hits [protected]

Definition at line 94 of file Utils.

template<typename K, typename T>
lru_type osgEarth::LRUCache< K, T >::_lru [protected]

Definition at line 90 of file Utils.

template<typename K, typename T>
map_type osgEarth::LRUCache< K, T >::_map [protected]

Definition at line 89 of file Utils.

template<typename K, typename T>
unsigned osgEarth::LRUCache< K, T >::_max [protected]

Definition at line 91 of file Utils.

template<typename K, typename T>
unsigned osgEarth::LRUCache< K, T >::_queries [protected]

Definition at line 93 of file Utils.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines