NCBI C++ ToolKit
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
CCache< TKey, TValue, THandler, TLock, TSize > Class Template Reference

Search Toolkit Book for CCache

Cache template. More...

#include <util/ncbi_cache.hpp>

+ Inheritance diagram for CCache< TKey, TValue, THandler, TLock, TSize >:
+ Collaboration diagram for CCache< TKey, TValue, THandler, TLock, TSize >:

Classes

struct  SValueWithIndex
 

Public Types

enum  EAddFlags { fAdd_NoReplace = (1 << 0) }
 Flags to control the details of adding new elements to the cache via Add(). More...
 
enum  EAddResult { eAdd_Inserted , eAdd_Replaced , eAdd_NotInserted }
 Result of element insertion. More...
 
enum  EGetFlags { fGet_NoTouch = (1 << 0) , fGet_NoCreate = (1 << 1) , fGet_NoInsert = (1 << 2) }
 Cache retrieval flags. More...
 
enum  EGetResult { eGet_Found , eGet_CreatedAndAdded , eGet_CreatedNotAdded }
 Get() result. More...
 
typedef TKey TKeyType
 
typedef TValue TValueType
 
typedef TSize TSizeType
 
typedef SCacheElement< TKeyType, TSizeTypeTCacheElement
 
typedef TSizeType TWeight
 
typedef TSizeType TOrder
 
typedef int TAddFlags
 bitwise OR of EAddFlags More...
 
typedef int TGetFlags
 bitwise OR of EGetFlags More...
 

Public Member Functions

 CCache (TSizeType capacity, THandler *handler=NULL)
 Create cache object with the given capacity. More...
 
TValueType operator[] (const TKeyType &key)
 Get cache element by the key. More...
 
TSizeType GetCapacity (void) const
 Get current capacity of the cache (max allowed number of elements) More...
 
void SetCapacity (TSizeType new_capacity)
 Set new capacity of the cache. More...
 
TSizeType GetSize (void) const
 Get current number of elements in the cache. More...
 
void SetSize (TSizeType new_size)
 Truncate the cache leaving at most new_size elements. More...
 
TOrder Add (const TKeyType &key, const TValueType &value, TWeight weight=1, TAddFlags add_flags=0, EAddResult *result=NULL)
 Add new element to the cache or replace the existing value. More...
 
TValueType Get (const TKeyType &key, TGetFlags get_flags=0, EGetResult *result=NULL)
 Get an object from the cache by its key. More...
 
bool Remove (const TKeyType &key)
 Remove element from cache. Do nothing if the key is not cached. More...
 
 ~CCache (void)
 

Private Types

typedef CCacheElement_Less< TCacheElement * > TCacheLess
 
typedef set< TCacheElement *, TCacheLessTCacheSet
 
typedef TCacheSet::iterator TCacheSet_I
 
typedef map< TKeyType, SValueWithIndexTCacheMap
 
typedef TCacheMap::iterator TCacheMap_I
 
typedef TLock TLockType
 
typedef TLockType::TWriteLockGuard TGuardType
 
typedef THandler THandlerType
 

Private Member Functions

 CCache (const CCache &)
 
CCacheoperator= (const CCache &)
 
TOrder x_GetNextCounter (void)
 
void x_PackElementIndex (void)
 
TCacheElementx_InsertElement (const TKeyType &key, TWeight weight)
 
void x_UpdateElement (TCacheElement *elem)
 
void x_EraseElement (TCacheSet_I &set_iter, TCacheMap_I &map_iter)
 
void x_EraseLast (void)
 
TWeight x_GetBaseWeight (void) const
 

Private Attributes

TLockType m_Lock
 
TSizeType m_Capacity
 
TCacheSet m_CacheSet
 
TCacheMap m_CacheMap
 
TOrder m_Counter
 
unique_ptr< THandlerTypem_Handler
 

Detailed Description

template<class TKey, class TValue, class THandler = CCacheElement_Handler<TKey, TValue>, class TLock = TCacheLock_Default, class TSize = Uint4>
class CCache< TKey, TValue, THandler, TLock, TSize >

Cache template.

TKey and TValue define types stored in the cache. TLock must define TWriteLockGuard subtype so that a value of type TLock can be used to initialize TWriteLockGuard. TSize is an integer type used for element indexing. THandler must provide the following callback methods: void RemoveElement(const TKey& key, TValue& value) void InsertElement(const TKey& key, const TValue& value) ECache_InsertFlag CanInsertElement(const TKey& key, const TValue& value) TValue CreateValue(const TKey& key)

See also
CCacheElement_Handler

Definition at line 152 of file ncbi_cache.hpp.


The documentation for this class was generated from the following files:
Modified on Fri Sep 20 14:58:16 2024 by modify_doxy.py rev. 669887