DGtal
1.5.beta
|
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. More... | |
void | selfDisplay (std::ostream &out) const |
bool | isValid () const |
Protected Attributes | |
Size | myMaxSize |
The maximal number of memoized items. More... | |
double | myRatio |
The minimal ratio to remove if the maximal number of items is reached. More... | |
TimeStamp | myTimeStamp |
Current time. More... | |
std::unordered_map< Key, StoredValue > | myMap |
The map memoizing computations. More... | |
Size | myHits |
The number of hits since the last clean-up. More... | |
bool | myVerbose |
when 'true', traces some information. More... | |
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.
|
default |
Copy constructor.
other | the object to clone. |
|
default |
Move constructor.
other | the object to clone. |
|
default |
Destructor.
|
inline |
Clean-up the memoizer by removing a fraction of its oldest elements.
Definition at line 199 of file TimeStampMemoizer.h.
References DGtal::Trace::info(), max(), DGtal::TimeStampMemoizer< TKey, TValue >::myHits, DGtal::TimeStampMemoizer< TKey, TValue >::myMap, DGtal::TimeStampMemoizer< TKey, TValue >::myMaxSize, DGtal::TimeStampMemoizer< TKey, TValue >::myRatio, DGtal::TimeStampMemoizer< TKey, TValue >::myTimeStamp, DGtal::TimeStampMemoizer< TKey, TValue >::myVerbose, DGtal::TimeStampMemoizer< TKey, TValue >::selfDisplay(), and DGtal::trace.
Referenced by DGtal::TimeStampMemoizer< TKey, TValue >::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 DGtal::TimeStampMemoizer< TKey, TValue >::myHits, DGtal::TimeStampMemoizer< TKey, TValue >::myMap, and DGtal::TimeStampMemoizer< TKey, TValue >::myTimeStamp.
Referenced by DGtal::NeighborhoodConvexityAnalyzer< TKSpace, K >::isComplementaryFullyConvex(), and DGtal::NeighborhoodConvexityAnalyzer< TKSpace, K >::isFullyConvex().
|
inline |
get( key )
succeeded and returned a pair (value, true)). Definition at line 151 of file TimeStampMemoizer.h.
References DGtal::TimeStampMemoizer< TKey, TValue >::myHits.
|
inline |
Checks the validity/consistency of the object.
Definition at line 237 of file TimeStampMemoizer.h.
References DGtal::TimeStampMemoizer< TKey, TValue >::myMaxSize.
Referenced by DGtal::NeighborhoodConvexityAnalyzer< TKSpace, K >::isComplementaryFullyConvex(), and DGtal::NeighborhoodConvexityAnalyzer< TKSpace, K >::isFullyConvex().
|
inline |
Definition at line 163 of file TimeStampMemoizer.h.
References DGtal::TimeStampMemoizer< TKey, TValue >::myMap.
|
inline |
Definition at line 143 of file TimeStampMemoizer.h.
References DGtal::TimeStampMemoizer< TKey, TValue >::myMaxSize.
|
default |
Assignment.
other | the object to copy. |
|
default |
Move assignment.
other | the object to copy. |
|
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 DGtal::TimeStampMemoizer< TKey, TValue >::myHits, DGtal::TimeStampMemoizer< TKey, TValue >::myMap, DGtal::TimeStampMemoizer< TKey, TValue >::myMaxSize, DGtal::TimeStampMemoizer< TKey, TValue >::myRatio, and DGtal::TimeStampMemoizer< TKey, TValue >::myTimeStamp.
Referenced by DGtal::TimeStampMemoizer< TKey, TValue >::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 DGtal::TimeStampMemoizer< TKey, TValue >::cleanUp(), DGtal::TimeStampMemoizer< TKey, TValue >::myMap, DGtal::TimeStampMemoizer< TKey, TValue >::myMaxSize, and DGtal::TimeStampMemoizer< TKey, TValue >::myTimeStamp.
Referenced by DGtal::NeighborhoodConvexityAnalyzer< TKSpace, K >::isComplementaryFullyConvex(), and DGtal::NeighborhoodConvexityAnalyzer< TKSpace, K >::isFullyConvex().
|
inline |
Definition at line 137 of file TimeStampMemoizer.h.
References DGtal::TimeStampMemoizer< TKey, TValue >::myMap.
|
inline |
Definition at line 157 of file TimeStampMemoizer.h.
References DGtal::TimeStampMemoizer< TKey, TValue >::myTimeStamp.
|
protected |
The number of hits since the last clean-up.
Definition at line 253 of file TimeStampMemoizer.h.
Referenced by DGtal::TimeStampMemoizer< TKey, TValue >::cleanUp(), DGtal::TimeStampMemoizer< TKey, TValue >::get(), DGtal::TimeStampMemoizer< TKey, TValue >::hits(), and DGtal::TimeStampMemoizer< TKey, TValue >::selfDisplay().
|
protected |
The map memoizing computations.
Definition at line 251 of file TimeStampMemoizer.h.
Referenced by DGtal::TimeStampMemoizer< TKey, TValue >::cleanUp(), DGtal::TimeStampMemoizer< TKey, TValue >::get(), DGtal::TimeStampMemoizer< TKey, TValue >::map(), DGtal::TimeStampMemoizer< TKey, TValue >::selfDisplay(), DGtal::TimeStampMemoizer< TKey, TValue >::set(), and DGtal::TimeStampMemoizer< TKey, TValue >::size().
|
protected |
The maximal number of memoized items.
Definition at line 245 of file TimeStampMemoizer.h.
Referenced by DGtal::TimeStampMemoizer< TKey, TValue >::cleanUp(), DGtal::TimeStampMemoizer< TKey, TValue >::isValid(), DGtal::TimeStampMemoizer< TKey, TValue >::maxSize(), DGtal::TimeStampMemoizer< TKey, TValue >::selfDisplay(), and DGtal::TimeStampMemoizer< TKey, TValue >::set().
|
protected |
The minimal ratio to remove if the maximal number of items is reached.
Definition at line 247 of file TimeStampMemoizer.h.
Referenced by DGtal::TimeStampMemoizer< TKey, TValue >::cleanUp(), and DGtal::TimeStampMemoizer< TKey, TValue >::selfDisplay().
|
protected |
Current time.
Definition at line 249 of file TimeStampMemoizer.h.
Referenced by DGtal::TimeStampMemoizer< TKey, TValue >::cleanUp(), DGtal::TimeStampMemoizer< TKey, TValue >::get(), DGtal::TimeStampMemoizer< TKey, TValue >::selfDisplay(), DGtal::TimeStampMemoizer< TKey, TValue >::set(), and DGtal::TimeStampMemoizer< TKey, TValue >::timeStamp().
|
protected |
when 'true', traces some information.
Definition at line 255 of file TimeStampMemoizer.h.
Referenced by DGtal::TimeStampMemoizer< TKey, TValue >::cleanUp().