DGtal 2.0.0
|
Aim: A generic class to store a given maximum number of pairs (key, value). The class tends to memorize pairs which are accessed more frequently than others. It is thus a memoizer, which is used to memorize the result of costly computations. The memoization principle is simple: a timestamp is attached to a pair (key,value). Each time a query is made, if the item was memoized, the result is returned while the timestamp of the item is updated. User can also add or update a value in the memoizer, which updates also its timestamp. After adding a pair (key,value), if the maximal number of items is reached, at least the oldest half (or a fraction) of the items are deleted, leaving space for storing new pairs (key,value). More...
#include <DGtal/base/TimeStampMemoizer.h>
Public Types | |
typedef TKey | Key |
typedef TValue | Value |
typedef TimeStampMemoizer< TKey, TValue > | Self |
typedef std::size_t | Size |
typedef DGtal::uint32_t | TimeStamp |
typedef std::pair< Value, TimeStamp > | StoredValue |
Public Member Functions | |
TimeStampMemoizer (Size max_size=0, double ratio=0.5, bool verbose=false) | |
TimeStampMemoizer (const TimeStampMemoizer &other)=default | |
TimeStampMemoizer (TimeStampMemoizer &&other)=default | |
TimeStampMemoizer & | operator= (const TimeStampMemoizer &other)=default |
TimeStampMemoizer & | operator= (TimeStampMemoizer &&other)=default |
~TimeStampMemoizer ()=default | |
Size | size () const |
Size | maxSize () const |
Size | hits () const |
Size | timeStamp () const |
const std::unordered_map< Key, StoredValue > & | map () const |
std::pair< Value, bool > | get (const Key &key) |
void | set (const Key &key, const Value &value) |
void | cleanUp () |
Clean-up the memoizer by removing a fraction of its oldest elements. | |
void | selfDisplay (std::ostream &out) const |
bool | isValid () const |
Protected Attributes | |
Size | myMaxSize |
The maximal number of memoized items. | |
double | myRatio |
The minimal ratio to remove if the maximal number of items is reached. | |
TimeStamp | myTimeStamp |
Current time. | |
std::unordered_map< Key, StoredValue > | myMap |
The map memoizing computations. | |
Size | myHits |
The number of hits since the last clean-up. | |
bool | myVerbose |
when 'true', traces some information. |
Aim: A generic class to store a given maximum number of pairs (key, value). The class tends to memorize pairs which are accessed more frequently than others. It is thus a memoizer, which is used to memorize the result of costly computations. The memoization principle is simple: a timestamp is attached to a pair (key,value). Each time a query is made, if the item was memoized, the result is returned while the timestamp of the item is updated. User can also add or update a value in the memoizer, which updates also its timestamp. After adding a pair (key,value), if the maximal number of items is reached, at least the oldest half (or a fraction) of the items are deleted, leaving space for storing new pairs (key,value).
Description of template class 'TimeStampMemoizer'
TKey | the type used for keys, must be hashable. |
TValue | the type used for values, must be DefaultConstructible, CopyConstructible, Assignable. |
Definition at line 74 of file TimeStampMemoizer.h.
typedef TKey DGtal::TimeStampMemoizer< TKey, TValue >::Key |
Definition at line 77 of file TimeStampMemoizer.h.
typedef TimeStampMemoizer< TKey, TValue > DGtal::TimeStampMemoizer< TKey, TValue >::Self |
Definition at line 79 of file TimeStampMemoizer.h.
typedef std::size_t DGtal::TimeStampMemoizer< TKey, TValue >::Size |
Definition at line 80 of file TimeStampMemoizer.h.
typedef std::pair< Value, TimeStamp > DGtal::TimeStampMemoizer< TKey, TValue >::StoredValue |
Definition at line 82 of file TimeStampMemoizer.h.
typedef DGtal::uint32_t DGtal::TimeStampMemoizer< TKey, TValue >::TimeStamp |
Definition at line 81 of file TimeStampMemoizer.h.
typedef TValue DGtal::TimeStampMemoizer< TKey, TValue >::Value |
Definition at line 78 of file TimeStampMemoizer.h.
|
inline |
Constructor.
max_size | the maximum number of items that the memoizer will store. |
ratio | is the real number between 0 and 1: when the maximum number is reached, at least the oldest ratio fraction of items are deleted from the memoizer. |
verbose | if 'true', traces some informations. |
Definition at line 98 of file TimeStampMemoizer.h.
References myHits, myMap, myMaxSize, myRatio, myTimeStamp, and myVerbose.
Referenced by operator=(), operator=(), TimeStampMemoizer(), and TimeStampMemoizer().
|
default |
|
default |
|
default |
Destructor.
|
inline |
Clean-up the memoizer by removing a fraction of its oldest elements.
Definition at line 199 of file TimeStampMemoizer.h.
References myHits, myMap, myMaxSize, myRatio, myTimeStamp, myVerbose, selfDisplay(), and DGtal::trace.
Referenced by set().
|
inline |
Given a key, return the associated pair <value, true> if it is found, or return <dummy, false> where dummy is an arbitrary value.
key | any key. |
Definition at line 175 of file TimeStampMemoizer.h.
References myHits, myMap, and myTimeStamp.
|
inline |
Definition at line 151 of file TimeStampMemoizer.h.
References myHits.
|
inline |
Checks the validity/consistency of the object.
Definition at line 237 of file TimeStampMemoizer.h.
References myMaxSize.
|
inline |
Definition at line 163 of file TimeStampMemoizer.h.
References myMap.
|
inline |
Definition at line 143 of file TimeStampMemoizer.h.
References myMaxSize.
|
default |
|
default |
|
inline |
Writes/Displays the object on an output stream.
out | the output stream where the object is written. |
Definition at line 225 of file TimeStampMemoizer.h.
References myHits, myMap, myMaxSize, myRatio, and myTimeStamp.
Referenced by cleanUp().
|
inline |
Memoizes (or update) a pair key and value.
key | any key. |
value | any value. |
Definition at line 192 of file TimeStampMemoizer.h.
References cleanUp(), myMap, myMaxSize, and myTimeStamp.
|
inline |
Definition at line 137 of file TimeStampMemoizer.h.
References myMap.
|
inline |
Definition at line 157 of file TimeStampMemoizer.h.
References myTimeStamp.
|
protected |
The number of hits since the last clean-up.
Definition at line 253 of file TimeStampMemoizer.h.
Referenced by cleanUp(), get(), hits(), selfDisplay(), and TimeStampMemoizer().
|
protected |
The map memoizing computations.
Definition at line 251 of file TimeStampMemoizer.h.
Referenced by cleanUp(), get(), map(), selfDisplay(), set(), size(), and TimeStampMemoizer().
|
protected |
The maximal number of memoized items.
Definition at line 245 of file TimeStampMemoizer.h.
Referenced by cleanUp(), isValid(), maxSize(), selfDisplay(), set(), and TimeStampMemoizer().
|
protected |
The minimal ratio to remove if the maximal number of items is reached.
Definition at line 247 of file TimeStampMemoizer.h.
Referenced by cleanUp(), selfDisplay(), and TimeStampMemoizer().
|
protected |
Current time.
Definition at line 249 of file TimeStampMemoizer.h.
Referenced by cleanUp(), get(), selfDisplay(), set(), timeStamp(), and TimeStampMemoizer().
|
protected |
when 'true', traces some information.
Definition at line 255 of file TimeStampMemoizer.h.
Referenced by cleanUp(), and TimeStampMemoizer().