DGtal 1.4.0
Loading...
Searching...
No Matches
TimeStampMemoizer.h
1
17#pragma once
18
31#if defined(TimeStampMemoizer_RECURSES)
32#error Recursive header files inclusion detected in TimeStampMemoizer.h
33#else // defined(TimeStampMemoizer_RECURSES)
35#define TimeStampMemoizer_RECURSES
36
37#if !defined TimeStampMemoizer_h
39#define TimeStampMemoizer_h
40
42// Inclusions
43#include <iostream>
44#include <unordered_map>
45#include "DGtal/base/Common.h"
47
48namespace DGtal
49{
50
52 // template class TimeStampMemoizer
73 template <typename TKey, typename TValue>
75 {
76 public:
77 typedef TKey Key;
78 typedef TValue Value;
80 typedef std::size_t Size;
82 typedef std::pair< Value, TimeStamp > StoredValue;
83
84 // ----------------------- Standard services ------------------------------
85 public:
86
98 TimeStampMemoizer( Size max_size = 0, double ratio = 0.5,
99 bool verbose = false )
100 : myMaxSize( max_size ), myRatio( ratio ), myTimeStamp( 0 ),
101 myMap( max_size ), myHits( 0 ), myVerbose( verbose )
102 {}
103
108 TimeStampMemoizer( const TimeStampMemoizer & other ) = default;
109
115
120 TimeStampMemoizer& operator=( const TimeStampMemoizer & other ) = default;
121
127
132
133 // ----------------------- Memoization services -----------------------------
134 public:
135
137 Size size() const
138 {
139 return myMap.size();
140 }
141
143 Size maxSize() const
144 {
145 return myMaxSize;
146 }
147
151 Size hits() const
152 {
153 return myHits;
154 }
155
158 {
159 return myTimeStamp;
160 }
161
163 const std::unordered_map< Key, StoredValue > & map() const
164 {
165 return myMap;
166 }
167
175 std::pair< Value, bool > get( const Key& key )
176 {
177 auto it = myMap.find( key );
178 if ( it == myMap.end() )
179 return std::make_pair( Value(), false );
180 it->second.second = myTimeStamp++;
181 ++myHits;
182 return std::make_pair( it->second.first, false );
183 }
184
192 void set( const Key& key, const Value& value )
193 {
194 if ( myMap.size() >= myMaxSize ) cleanUp();
195 myMap[ key ] = std::make_pair( value, myTimeStamp++ );
196 }
197
199 void cleanUp()
200 {
201 if ( myVerbose ) selfDisplay( trace.info() );
202 Size nb = 0;
203 TimeStamp threshold = (TimeStamp) std::max( (Size) myTimeStamp
204 - (Size) round( (myMaxSize + 0.5 * myHits) * myRatio ),
205 (Size) 0 );
206 for ( auto it = myMap.begin(), itE = myMap.end(); it != itE; )
207 if ( it->second.second <= threshold )
208 {
209 it = myMap.erase( it );
210 ++nb;
211 }
212 else
213 ++it;
214 if ( myVerbose) trace.info() << " " << nb << " erased." << std::endl;
215 myHits = 0;
216 }
217
218 // ----------------------- Interface --------------------------------------
219 public:
220
225 void selfDisplay ( std::ostream & out ) const
226 {
227 out << "[TimeStampMemoizer " << myMap.size() << "/" << myMaxSize << " items"
228 << " time=" << myTimeStamp << " ratio=" << myRatio
229 << " hits=" << myHits
230 << "]";
231 }
232
237 bool isValid() const
238 {
239 return ( myMaxSize > 0 );
240 }
241
242 // ------------------------- Protected Datas ------------------------------
243 protected:
247 double myRatio;
251 std::unordered_map< Key, StoredValue > myMap;
256
257 // ------------------------- Private Datas --------------------------------
258 private:
259
260 // ------------------------- Hidden services ------------------------------
261 protected:
262
263 // ------------------------- Internals ------------------------------------
264 private:
265
266 }; // end of class TimeStampMemoizer
267
268
280 template <typename TKey, typename TValue>
281 std::ostream&
282 operator<< ( std::ostream & out,
283 const TimeStampMemoizer<TKey, TValue> & object )
284 {
285 object.selfDisplay( out );
286 return out;
287 }
288
289} // namespace DGtal
290
291
293// Includes inline functions.
294
295// //
297
298#endif // !defined TimeStampMemoizer_h
299
300#undef TimeStampMemoizer_RECURSES
301#endif // else defined(TimeStampMemoizer_RECURSES)
Aim: A generic class to store a given maximum number of pairs (key, value). The class tends to memori...
std::unordered_map< Key, StoredValue > myMap
The map memoizing computations.
std::pair< Value, bool > get(const Key &key)
TimeStampMemoizer & operator=(TimeStampMemoizer &&other)=default
TimeStampMemoizer & operator=(const TimeStampMemoizer &other)=default
const std::unordered_map< Key, StoredValue > & map() const
TimeStampMemoizer(const TimeStampMemoizer &other)=default
void cleanUp()
Clean-up the memoizer by removing a fraction of its oldest elements.
TimeStampMemoizer(Size max_size=0, double ratio=0.5, bool verbose=false)
Size myHits
The number of hits since the last clean-up.
void selfDisplay(std::ostream &out) const
TimeStamp myTimeStamp
Current time.
TimeStampMemoizer(TimeStampMemoizer &&other)=default
Size myMaxSize
The maximal number of memoized items.
void set(const Key &key, const Value &value)
bool myVerbose
when 'true', traces some information.
std::pair< Value, TimeStamp > StoredValue
double myRatio
The minimal ratio to remove if the maximal number of items is reached.
TimeStampMemoizer< TKey, TValue > Self
std::ostream & info()
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::uint32_t uint32_t
unsigned 32-bit integer.
Definition BasicTypes.h:63
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
Trace trace
Definition Common.h:153