osgEarth 2.1.1
|
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_iter > | map_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 |
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();
typedef std::list<K>::iterator osgEarth::LRUCache< K, T >::lru_iter [protected] |
typedef std::list<K> osgEarth::LRUCache< K, T >::lru_type [protected] |
typedef map_type::iterator osgEarth::LRUCache< K, T >::map_iter [protected] |
typedef std::map<K, map_value_type> osgEarth::LRUCache< K, T >::map_type [protected] |
typedef std::pair<T, lru_iter> osgEarth::LRUCache< K, T >::map_value_type [protected] |
osgEarth::LRUCache< K, T >::LRUCache | ( | unsigned | max = 100 | ) | [inline] |
void osgEarth::LRUCache< K, T >::erase | ( | const K & | key | ) | [inline] |
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 ); } }
unsigned osgEarth::LRUCache< K, T >::getMaxSize | ( | ) | const [inline] |
CacheStats osgEarth::LRUCache< K, T >::getStats | ( | ) | const [inline] |
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(); } } }
void osgEarth::LRUCache< K, T >::setMaxSize | ( | unsigned | max | ) | [inline] |
unsigned osgEarth::LRUCache< K, T >::_buf [protected] |
unsigned osgEarth::LRUCache< K, T >::_hits [protected] |
lru_type osgEarth::LRUCache< K, T >::_lru [protected] |
map_type osgEarth::LRUCache< K, T >::_map [protected] |
unsigned osgEarth::LRUCache< K, T >::_max [protected] |
unsigned osgEarth::LRUCache< K, T >::_queries [protected] |