carl  24.04
Computer ARithmetic Library
carl::Cache< T > Class Template Reference

#include <Cache.h>

Collaboration diagram for carl::Cache< T >:

Data Structures

struct  Info
 

Public Types

using Ref = std::size_t
 
using Container = std::unordered_set< TypeInfoPair< T, Info > *, pointerHash< TypeInfoPair< T, Info > >, pointerEqual< TypeInfoPair< T, Info > >>
 

Public Member Functions

 Cache (size_t _maxCacheSize=10000, double _cacheReductionAmount=0.2, double _decay=0.98)
 
 Cache (const Cache &)=delete
 
Cacheoperator= (const Cache &)=delete
 
 ~Cache ()
 
std::pair< Ref, bool > cache (T *_toCache, bool(*_canBeUpdated)(const T &, const T &)=&returnFalse< T >, void(*_update)(const T &, const T &)=&doNothing< T >)
 Caches the given object. More...
 
void reg (Ref _refStoragePos)
 Registers the entry to the given reference. More...
 
void dereg (Ref _refStoragePos)
 Deregisters the entry to the given reference. More...
 
void rehash (Ref _refStoragePos)
 Removes and reinserts the entry with the given reference, after its hash value is recalculated. More...
 
void decayActivity ()
 Decays all activities by increasing the activity increment. More...
 
void strengthenActivity (Ref _refStoragePos)
 Strenghtens the activity of the entry in the cache with the given reference, by increasing its activity. More...
 
void print (std::ostream &_out=std::cout) const
 Prints all information stored in this cache to std::cout. More...
 
const T & get (Ref _refStoragePos) const
 

Static Public Attributes

static const Ref NO_REF
 

Private Member Functions

void clean ()
 Removes a certain amount of unused entries in the cache. More...
 
std::size_t erase (TypeInfoPair< T, Info > *_toRemove)
 Removes the entry at the given position in the cache. More...
 
Container::iterator erase (typename Container::iterator _toRemove)
 Removes the entry at the given position in the cache. More...
 
bool hasDuplicates (const std::vector< Ref > &_vec) const
 
bool checkNumOfUnusedEntries () const
 
size_t sumOfAllUsageCounts () const
 

Private Attributes

std::size_t mMaxCacheSize
 The threshold for the cache's size which should not be exceeded, except more of the cache entries are still in use. More...
 
std::size_t mNumOfUnusedEntries = 0
 The current number of entries in the cache, which are not used. More...
 
double mCacheReductionAmount
 The percentage of the cache, which shall be removed at best, if the cache size exceeds the threshold. More...
 
double mMaxActivity = 0.0
 The threshold for the maximum activity. More...
 
double mActivityIncrement = 1.0
 The reciprocal of the factor to multiply an activity with in order to increase it. More...
 
double mDecay
 The decay (between 0.9 and 1.0) of the given increments on activities. More...
 
double mActivityThreshold = 1e100
 The threshold limiting the maximum activity. More...
 
double mActivityDecrementFactor = 1e-100
 The factor multiplied to all activities in order to rescale (decrease) them. More...
 
std::recursive_mutex mMutex
 A mutex for situation where any member is changed. More...
 
Container mCache
 The container storing all cached entries. More...
 
std::vector< TypeInfoPair< T, Info > * > mCacheRefs
 Stores at the reference of an entry in the cache an iterator to this entry. More...
 
std::stack< RefmUnusedPositionsInCacheRefs
 A stack containing free references, which have been used before but freed now. More...
 

Detailed Description

template<typename T>
class carl::Cache< T >

Definition at line 47 of file Cache.h.

Member Typedef Documentation

◆ Container

template<typename T >
using carl::Cache< T >::Container = std::unordered_set<TypeInfoPair<T,Info>*, pointerHash<TypeInfoPair<T,Info> >, pointerEqual<TypeInfoPair<T,Info> >>

Definition at line 77 of file Cache.h.

◆ Ref

template<typename T >
using carl::Cache< T >::Ref = std::size_t

Definition at line 51 of file Cache.h.

Constructor & Destructor Documentation

◆ Cache() [1/2]

template<typename T >
carl::Cache< T >::Cache ( size_t  _maxCacheSize = 10000,
double  _cacheReductionAmount = 0.2,
double  _decay = 0.98 
)
explicit

◆ Cache() [2/2]

template<typename T >
carl::Cache< T >::Cache ( const Cache< T > &  )
delete

◆ ~Cache()

template<typename T >
carl::Cache< T >::~Cache ( )

Member Function Documentation

◆ cache()

template<typename T >
std::pair<Ref,bool> carl::Cache< T >::cache ( T *  _toCache,
bool(*)(const T &, const T &)  _canBeUpdated = &returnFalse< T >,
void(*)(const T &, const T &)  _update = &doNothing< T > 
)

Caches the given object.

Parameters
_toCacheThe object to cache.
_canBeUpdatedA function, which determines whether, in the case an equal object has already been cached, the given object can update the information in this already cached object.
_updateA function which updates an object in the cache, which is equal to the given object, by the information in the given object. After this function has been applied, the corresponding entry in the cache will be reinserted in it after been rehashed.
Returns
The reference of the entry, which can be used outside this class to access the entry.

◆ checkNumOfUnusedEntries()

template<typename T >
bool carl::Cache< T >::checkNumOfUnusedEntries ( ) const
inlineprivate

Definition at line 286 of file Cache.h.

Here is the caller graph for this function:

◆ clean()

template<typename T >
void carl::Cache< T >::clean ( )
private

Removes a certain amount of unused entries in the cache.

◆ decayActivity()

template<typename T >
void carl::Cache< T >::decayActivity ( )

Decays all activities by increasing the activity increment.

◆ dereg()

template<typename T >
void carl::Cache< T >::dereg ( Ref  _refStoragePos)

Deregisters the entry to the given reference.

It mainly decreases the usage counter of this entry in the cache.

Parameters
_refStoragePosThe reference of the entry to deregister.

◆ erase() [1/2]

template<typename T >
std::size_t carl::Cache< T >::erase ( TypeInfoPair< T, Info > *  _toRemove)
inlineprivate

Removes the entry at the given position in the cache.

Parameters
_toRemoveThe position to the entry to remove from the cache.
Returns

Definition at line 225 of file Cache.h.

Here is the call graph for this function:

◆ erase() [2/2]

template<typename T >
Container::iterator carl::Cache< T >::erase ( typename Container::iterator  _toRemove)
inlineprivate

Removes the entry at the given position in the cache.

Parameters
_toRemoveThe position to the entry to remove from the cache.
Returns
An iterator to the entry in the cache right after the entry which has to be removed.

Definition at line 252 of file Cache.h.

Here is the call graph for this function:

◆ get()

template<typename T >
const T& carl::Cache< T >::get ( Ref  _refStoragePos) const
inline
Parameters
_refStoragePosThe reference of the entry to obtain the object from.
Returns
The object in the entry with the given reference.

Definition at line 205 of file Cache.h.

◆ hasDuplicates()

template<typename T >
bool carl::Cache< T >::hasDuplicates ( const std::vector< Ref > &  _vec) const
inlineprivate

Definition at line 275 of file Cache.h.

◆ operator=()

template<typename T >
Cache& carl::Cache< T >::operator= ( const Cache< T > &  )
delete

◆ print()

template<typename T >
void carl::Cache< T >::print ( std::ostream &  _out = std::cout) const

Prints all information stored in this cache to std::cout.

Parameters
_outThe stream to print on.

◆ reg()

template<typename T >
void carl::Cache< T >::reg ( Ref  _refStoragePos)

Registers the entry to the given reference.

It mainly increases the usage counter of this entry in the cache.

Parameters
_refStoragePosThe reference of the entry to register.

◆ rehash()

template<typename T >
void carl::Cache< T >::rehash ( Ref  _refStoragePos)

Removes and reinserts the entry with the given reference, after its hash value is recalculated.

Parameters
_refStoragePosThe reference of the entry to apply the given function to.
Returns
The new reference.

◆ strengthenActivity()

template<typename T >
void carl::Cache< T >::strengthenActivity ( Ref  _refStoragePos)

Strenghtens the activity of the entry in the cache with the given reference, by increasing its activity.

Parameters
_refStoragePosThe reference of the entry in the cache to strengthen its activity.

◆ sumOfAllUsageCounts()

template<typename T >
size_t carl::Cache< T >::sumOfAllUsageCounts ( ) const
inlineprivate

Definition at line 299 of file Cache.h.

Field Documentation

◆ mActivityDecrementFactor

template<typename T >
double carl::Cache< T >::mActivityDecrementFactor = 1e-100
private

The factor multiplied to all activities in order to rescale (decrease) them.

Definition at line 122 of file Cache.h.

◆ mActivityIncrement

template<typename T >
double carl::Cache< T >::mActivityIncrement = 1.0
private

The reciprocal of the factor to multiply an activity with in order to increase it.

This member can increased by a user interface.

Definition at line 106 of file Cache.h.

◆ mActivityThreshold

template<typename T >
double carl::Cache< T >::mActivityThreshold = 1e100
private

The threshold limiting the maximum activity.

If this threshold is exceeded, all activities are rescaled.

Definition at line 117 of file Cache.h.

◆ mCache

template<typename T >
Container carl::Cache< T >::mCache
private

The container storing all cached entries.

It maps the objects to store to cache information, which cover a usage counter, the position in mCacheRefs, being the entries reference, and the activity of this entry.

Definition at line 133 of file Cache.h.

◆ mCacheReductionAmount

template<typename T >
double carl::Cache< T >::mCacheReductionAmount
private

The percentage of the cache, which shall be removed at best, if the cache size exceeds the threshold.

(NOT YET USED)

Definition at line 95 of file Cache.h.

◆ mCacheRefs

template<typename T >
std::vector<TypeInfoPair<T,Info>*> carl::Cache< T >::mCacheRefs
private

Stores at the reference of an entry in the cache an iterator to this entry.

This reference can be used to access the entry outside this class.

Definition at line 139 of file Cache.h.

◆ mDecay

template<typename T >
double carl::Cache< T >::mDecay
private

The decay (between 0.9 and 1.0) of the given increments on activities.

It is applied by increasing the increment by multiplying this members reciprocal to it.

Definition at line 112 of file Cache.h.

◆ mMaxActivity

template<typename T >
double carl::Cache< T >::mMaxActivity = 0.0
private

The threshold for the maximum activity.

In case it is exceeded, all activities are rescaled.

Definition at line 100 of file Cache.h.

◆ mMaxCacheSize

template<typename T >
std::size_t carl::Cache< T >::mMaxCacheSize
private

The threshold for the cache's size which should not be exceeded, except more of the cache entries are still in use.

Definition at line 85 of file Cache.h.

◆ mMutex

template<typename T >
std::recursive_mutex carl::Cache< T >::mMutex
private

A mutex for situation where any member is changed.

TODO: Refine the locking-situations.

Definition at line 127 of file Cache.h.

◆ mNumOfUnusedEntries

template<typename T >
std::size_t carl::Cache< T >::mNumOfUnusedEntries = 0
private

The current number of entries in the cache, which are not used.

Definition at line 90 of file Cache.h.

◆ mUnusedPositionsInCacheRefs

template<typename T >
std::stack<Ref> carl::Cache< T >::mUnusedPositionsInCacheRefs
private

A stack containing free references, which have been used before but freed now.

Definition at line 141 of file Cache.h.

◆ NO_REF

template<typename T >
const Ref carl::Cache< T >::NO_REF
static

Definition at line 145 of file Cache.h.


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