libosmscout 1.1.1
Loading...
Searching...
No Matches
osmscout::Cache< K, V, IK > Class Template Reference

#include <libosmscout/include/osmscout/util/Cache.h>

Classes

struct  CacheEntry
class  ValueSizer

Public Types

using OrderList = std::list<CacheEntry>
using CacheRef = typename OrderList::iterator
using Map = std::unordered_map<K, typename OrderList::iterator>

Public Member Functions

 Cache (size_t maxSize)
bool IsActive () const
bool GetEntry (const K &key, CacheRef &reference)
Cache::CacheRef SetEntry (const CacheEntry &entry)
void SetMaxSize (size_t maxSize)
size_t GetMaxSize () const
void Flush ()
size_t GetSize () const
size_t GetMemory (const ValueSizer &sizer) const
void DumpStatistics (const char *cacheName, const ValueSizer &sizer)

Detailed Description

template<class K, class V, class IK = PageId>
class osmscout::Cache< K, V, IK >

Generic FIFO cache implementation with O(n log n) semantic.

Template parameter class K holds the key value (must be a numerical value), parameter class V holds the data class that is to be cached, and parameter IK holds the internal key value, must be an unsigned value, default is PageId.

  • The cache is not threadsafe.
  • It uses a std::vector<std::list>> as a hash table for data lookup
  • It uses an std::list for implementing FIFO characteristics.

Implementation details: If std::unordered_map ist available we use this for fast detection (O(1)) if an object is already in the cast. If it is not available, we use a vector as a hashtable via the key value with list as entry type for hash code conflict handling.

Member Typedef Documentation

◆ CacheRef

template<class K, class V, class IK = PageId>
using osmscout::Cache< K, V, IK >::CacheRef = typename OrderList::iterator

◆ Map

template<class K, class V, class IK = PageId>
using osmscout::Cache< K, V, IK >::Map = std::unordered_map<K, typename OrderList::iterator>

◆ OrderList

template<class K, class V, class IK = PageId>
using osmscout::Cache< K, V, IK >::OrderList = std::list<CacheEntry>

Constructor & Destructor Documentation

◆ Cache()

template<class K, class V, class IK = PageId>
osmscout::Cache< K, V, IK >::Cache ( size_t maxSize)
inlineexplicit

Create a new cache object with the given max size.

Member Function Documentation

◆ DumpStatistics()

template<class K, class V, class IK = PageId>
void osmscout::Cache< K, V, IK >::DumpStatistics ( const char * cacheName,
const ValueSizer & sizer )
inline

Dump some cache statistics to std::cout.

◆ Flush()

template<class K, class V, class IK = PageId>
void osmscout::Cache< K, V, IK >::Flush ( )
inline

Completely flush the cache removing all entries from it.

◆ GetEntry()

template<class K, class V, class IK = PageId>
bool osmscout::Cache< K, V, IK >::GetEntry ( const K & key,
CacheRef & reference )
inline

Getting the value with the given key from cache.

If there is no valued stored with the given key, false will be returned and the reference will be untouched.

If there is a value with the given key, reference will return a reference to the value and the value will moved to the front position of the cache to assure FIFO behaviour.

◆ GetMaxSize()

template<class K, class V, class IK = PageId>
size_t osmscout::Cache< K, V, IK >::GetMaxSize ( ) const
inline

Returns the maximum size of the cache

◆ GetMemory()

template<class K, class V, class IK = PageId>
size_t osmscout::Cache< K, V, IK >::GetMemory ( const ValueSizer & sizer) const
inline

◆ GetSize()

template<class K, class V, class IK = PageId>
size_t osmscout::Cache< K, V, IK >::GetSize ( ) const
inline

Returns the current size of the cache.

◆ IsActive()

template<class K, class V, class IK = PageId>
bool osmscout::Cache< K, V, IK >::IsActive ( ) const
inline

Returns if the cache is active (maxSize > 0)

◆ SetEntry()

template<class K, class V, class IK = PageId>
Cache::CacheRef osmscout::Cache< K, V, IK >::SetEntry ( const CacheEntry & entry)
inline

Set or update the cache with the given value for the given key.

If the key is not available in the cache the value will be added to the front of the cache (FIFO semantic) else the value will be updated (also moving it to the front of the cache (FIFO again).

◆ SetMaxSize()

template<class K, class V, class IK = PageId>
void osmscout::Cache< K, V, IK >::SetMaxSize ( size_t maxSize)
inline

Set a new cache max size, possible striping the oldest entries from cache if the new size is smaller than the old one.


The documentation for this class was generated from the following file: