DGtal 1.3.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 =
204 std::max( (Size) myTimeStamp
205 - (Size) round( (myMaxSize + 0.5 * myHits) * myRatio ),
206 (Size) 0 );
207 for ( auto it = myMap.begin(), itE = myMap.end(); it != itE; )
208 if ( it->second.second <= threshold )
209 {
210 it = myMap.erase( it );
211 ++nb;
212 }
213 else
214 ++it;
215 if ( myVerbose) trace.info() << " " << nb << " erased." << std::endl;
216 myHits = 0;
217 }
218
219 // ----------------------- Interface --------------------------------------
220 public:
221
226 void selfDisplay ( std::ostream & out ) const
227 {
228 out << "[TimeStampMemoizer " << myMap.size() << "/" << myMaxSize << " items"
229 << " time=" << myTimeStamp << " ratio=" << myRatio
230 << " hits=" << myHits
231 << "]";
232 }
233
238 bool isValid() const
239 {
240 return ( myMaxSize > 0 );
241 }
242
243 // ------------------------- Protected Datas ------------------------------
244 protected:
248 double myRatio;
252 std::unordered_map< Key, StoredValue > myMap;
257
258 // ------------------------- Private Datas --------------------------------
259 private:
260
261 // ------------------------- Hidden services ------------------------------
262 protected:
263
264 // ------------------------- Internals ------------------------------------
265 private:
266
267 }; // end of class TimeStampMemoizer
268
269
281 template <typename TKey, typename TValue>
282 std::ostream&
283 operator<< ( std::ostream & out,
284 const TimeStampMemoizer<TKey, TValue> & object )
285 {
286 object.selfDisplay( out );
287 return out;
288 }
289
290} // namespace DGtal
291
292
294// Includes inline functions.
295
296// //
298
299#endif // !defined TimeStampMemoizer_h
300
301#undef TimeStampMemoizer_RECURSES
302#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:154