10 #include "../util/container_types.h"
16 #include <unordered_set>
20 template<
typename T,
class I>
23 template<
typename T,
class I>
25 return *_tipA.first == *_tipB.first;
30 template<
typename T,
class I>
33 return _tip.first->hash();
51 using Ref = std::size_t;
70 explicit Info(
double _activity ):
148 explicit Cache(
size_t _maxCacheSize = 10000,
double _cacheReductionAmount = 0.2,
double _decay = 0.98 );
163 std::pair<Ref,bool>
cache( T* _toCache,
bool (*_canBeUpdated)(
const T&,
const T& ) = &returnFalse<T>,
void (*_update)(
const T&,
const T& ) = &doNothing<T> );
199 void print( std::ostream& _out = std::cout )
const;
208 assert(
mCacheRefs[_refStoragePos] !=
nullptr );
209 assert(
mCacheRefs[_refStoragePos]->second.usageCount > 0 );
228 std::lock_guard<std::recursive_mutex> lock(
mMutex );
229 assert( _toRemove->second.usageCount == 0 );
230 for(
const Ref& ref : _toRemove->second.refStoragePositions )
238 assert(_toRemove->second.usageCount == 0);
239 auto result =
mCache.erase( _toRemove );
240 T* toDel = _toRemove->first;
252 typename Container::iterator
erase(
typename Container::iterator _toRemove )
255 std::lock_guard<std::recursive_mutex> lock(
mMutex );
256 assert( (*_toRemove)->second.usageCount == 0 );
257 for(
const Ref& ref : (*_toRemove)->second.refStoragePositions )
265 assert((*_toRemove)->second.usageCount == 0);
266 T* toDel = (*_toRemove)->first;
268 auto result =
mCache.erase( _toRemove );
277 std::set<Ref> vecEntries;
278 for (
const Ref& r: _vec) {
279 if (!vecEntries.insert(r).second) {
288 std::size_t actualNumOfUnusedEntries = 0;
289 for(
auto iter =
mCache.begin(); iter !=
mCache.end(); ++iter )
291 if( (*iter)->second.usageCount == 0 )
293 ++actualNumOfUnusedEntries;
301 std::size_t result = 0;
302 for(
auto iter =
mCache.begin(); iter !=
mCache.end(); ++iter )
304 result += (*iter)->second.usageCount;
carl is the main namespace for the library.
void doNothing(const T &, const T &)
std::pair< T *, I > TypeInfoPair
bool returnFalse(const T &, const T &)
bool operator==(const BasicConstraint< P > &lhs, const BasicConstraint< P > &rhs)
std::size_t operator()(const carl::TypeInfoPair< T, I > &_tip) const
double mActivityDecrementFactor
The factor multiplied to all activities in order to rescale (decrease) them.
std::size_t mMaxCacheSize
The threshold for the cache's size which should not be exceeded, except more of the cache entries are...
double mCacheReductionAmount
The percentage of the cache, which shall be removed at best, if the cache size exceeds the threshold.
void dereg(Ref _refStoragePos)
Deregisters the entry to the given reference.
const T & get(Ref _refStoragePos) const
size_t sumOfAllUsageCounts() const
std::unordered_set< TypeInfoPair< T, Info > *, pointerHash< TypeInfoPair< T, Info > >, pointerEqual< TypeInfoPair< T, Info > >> Container
Cache(const Cache &)=delete
bool hasDuplicates(const std::vector< Ref > &_vec) const
std::vector< TypeInfoPair< T, Info > * > mCacheRefs
Stores at the reference of an entry in the cache an iterator to this entry.
bool checkNumOfUnusedEntries() const
std::size_t erase(TypeInfoPair< T, Info > *_toRemove)
Removes the entry at the given position in the cache.
Cache & operator=(const Cache &)=delete
Container::iterator erase(typename Container::iterator _toRemove)
Removes the entry at the given position in the cache.
void print(std::ostream &_out=std::cout) const
Prints all information stored in this cache to std::cout.
double mDecay
The decay (between 0.9 and 1.0) of the given increments on activities.
void clean()
Removes a certain amount of unused entries in the cache.
double mActivityThreshold
The threshold limiting the maximum activity.
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.
void rehash(Ref _refStoragePos)
Removes and reinserts the entry with the given reference, after its hash value is recalculated.
void strengthenActivity(Ref _refStoragePos)
Strenghtens the activity of the entry in the cache with the given reference, by increasing its activi...
Container mCache
The container storing all cached entries.
double mMaxActivity
The threshold for the maximum activity.
std::stack< Ref > mUnusedPositionsInCacheRefs
A stack containing free references, which have been used before but freed now.
void reg(Ref _refStoragePos)
Registers the entry to the given reference.
Cache(size_t _maxCacheSize=10000, double _cacheReductionAmount=0.2, double _decay=0.98)
double mActivityIncrement
The reciprocal of the factor to multiply an activity with in order to increase it.
std::size_t mNumOfUnusedEntries
The current number of entries in the cache, which are not used.
std::recursive_mutex mMutex
A mutex for situation where any member is changed.
void decayActivity()
Decays all activities by increasing the activity increment.
double activity
Stores the activity of the entry in the cache for which this information hold.
std::vector< Ref > refStoragePositions
Stores the reference of the entry in the cache for which this information hold.
std::size_t usageCount
Store the number of usages of the entry in the cache for which this information hold by external obje...
Alternative specialization of std::equal_to for pointer types.
Alternative specialization of std::hash for pointer types.