DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
TimeStampMemoizer.h
1
16
17
#pragma once
18
30
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
48
namespace
DGtal
49
{
50
52
// template class TimeStampMemoizer
73
template
<
typename
TKey,
typename
TValue>
74
class
TimeStampMemoizer
75
{
76
public
:
77
typedef
TKey
Key
;
78
typedef
TValue
Value
;
79
typedef
TimeStampMemoizer< TKey, TValue >
Self
;
80
typedef
std::size_t
Size
;
81
typedef
DGtal::uint32_t
TimeStamp
;
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
114
TimeStampMemoizer
(
TimeStampMemoizer
&& other ) =
default
;
115
120
TimeStampMemoizer
&
operator=
(
const
TimeStampMemoizer
& other ) =
default
;
121
126
TimeStampMemoizer
&
operator=
(
TimeStampMemoizer
&& other ) =
default
;
127
131
~TimeStampMemoizer
() =
default
;
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
157
Size
timeStamp
()
const
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 Data ------------------------------
243
protected
:
245
Size
myMaxSize
;
247
double
myRatio
;
249
TimeStamp
myTimeStamp
;
251
std::unordered_map< Key, StoredValue >
myMap
;
253
Size
myHits
;
255
bool
myVerbose
;
256
257
// ------------------------- Private Data --------------------------------
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)
DGtal::TimeStampMemoizer
Aim: A generic class to store a given maximum number of pairs (key, value). The class tends to memori...
Definition
TimeStampMemoizer.h:75
DGtal::TimeStampMemoizer::myMap
std::unordered_map< Key, StoredValue > myMap
The map memoizing computations.
Definition
TimeStampMemoizer.h:251
DGtal::TimeStampMemoizer::get
std::pair< Value, bool > get(const Key &key)
Definition
TimeStampMemoizer.h:175
DGtal::TimeStampMemoizer::Size
std::size_t Size
Definition
TimeStampMemoizer.h:80
DGtal::TimeStampMemoizer::hits
Size hits() const
Definition
TimeStampMemoizer.h:151
DGtal::TimeStampMemoizer::operator=
TimeStampMemoizer & operator=(TimeStampMemoizer &&other)=default
DGtal::TimeStampMemoizer::operator=
TimeStampMemoizer & operator=(const TimeStampMemoizer &other)=default
DGtal::TimeStampMemoizer::maxSize
Size maxSize() const
Definition
TimeStampMemoizer.h:143
DGtal::TimeStampMemoizer::map
const std::unordered_map< Key, StoredValue > & map() const
Definition
TimeStampMemoizer.h:163
DGtal::TimeStampMemoizer::TimeStamp
DGtal::uint32_t TimeStamp
Definition
TimeStampMemoizer.h:81
DGtal::TimeStampMemoizer::TimeStampMemoizer
TimeStampMemoizer(const TimeStampMemoizer &other)=default
DGtal::TimeStampMemoizer::cleanUp
void cleanUp()
Clean-up the memoizer by removing a fraction of its oldest elements.
Definition
TimeStampMemoizer.h:199
DGtal::TimeStampMemoizer::TimeStampMemoizer
TimeStampMemoizer(Size max_size=0, double ratio=0.5, bool verbose=false)
Definition
TimeStampMemoizer.h:98
DGtal::TimeStampMemoizer::myHits
Size myHits
The number of hits since the last clean-up.
Definition
TimeStampMemoizer.h:253
DGtal::TimeStampMemoizer::selfDisplay
void selfDisplay(std::ostream &out) const
Definition
TimeStampMemoizer.h:225
DGtal::TimeStampMemoizer::myTimeStamp
TimeStamp myTimeStamp
Current time.
Definition
TimeStampMemoizer.h:249
DGtal::TimeStampMemoizer::~TimeStampMemoizer
~TimeStampMemoizer()=default
DGtal::TimeStampMemoizer::Key
TKey Key
Definition
TimeStampMemoizer.h:77
DGtal::TimeStampMemoizer::size
Size size() const
Definition
TimeStampMemoizer.h:137
DGtal::TimeStampMemoizer::TimeStampMemoizer
TimeStampMemoizer(TimeStampMemoizer &&other)=default
DGtal::TimeStampMemoizer::timeStamp
Size timeStamp() const
Definition
TimeStampMemoizer.h:157
DGtal::TimeStampMemoizer::myMaxSize
Size myMaxSize
The maximal number of memoized items.
Definition
TimeStampMemoizer.h:245
DGtal::TimeStampMemoizer::isValid
bool isValid() const
Definition
TimeStampMemoizer.h:237
DGtal::TimeStampMemoizer::set
void set(const Key &key, const Value &value)
Definition
TimeStampMemoizer.h:192
DGtal::TimeStampMemoizer::myVerbose
bool myVerbose
when 'true', traces some information.
Definition
TimeStampMemoizer.h:255
DGtal::TimeStampMemoizer::StoredValue
std::pair< Value, TimeStamp > StoredValue
Definition
TimeStampMemoizer.h:82
DGtal::TimeStampMemoizer::Value
TValue Value
Definition
TimeStampMemoizer.h:78
DGtal::TimeStampMemoizer::myRatio
double myRatio
The minimal ratio to remove if the maximal number of items is reached.
Definition
TimeStampMemoizer.h:247
DGtal::TimeStampMemoizer::Self
TimeStampMemoizer< TKey, TValue > Self
Definition
TimeStampMemoizer.h:79
DGtal::Trace::info
std::ostream & info()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::uint32_t
std::uint32_t uint32_t
unsigned 32-bit integer.
Definition
BasicTypes.h:62
DGtal::operator<<
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
DGtal::trace
Trace trace
src
DGtal
base
TimeStampMemoizer.h
Generated on
for DGtal by
1.18.0