1 #ifndef itkTimeSeriesDatabaseHelper_h 2 #define itkTimeSeriesDatabaseHelper_h 11 namespace TimeSeriesDatabaseHelper {
25 : m_ItsCounter(nullptr) {
if (p) m_ItsCounter =
new counter(p);}
29 {acquire(r.m_ItsCounter);}
34 acquire(r.m_ItsCounter);
39 ElementType&
operator*()
const throw() {
return *m_ItsCounter->ptr;}
40 ElementType*
operator->()
const throw() {
return m_ItsCounter->ptr;}
41 ElementType*
get()
const throw() {
return m_ItsCounter ? m_ItsCounter->ptr :
nullptr;}
43 {
return (m_ItsCounter ? m_ItsCounter->count == 1 :
true);}
48 counter(ElementType* p = 0,
unsigned c = 1) : ptr(p), count(c) {}
53 void acquire(counter* c)
throw()
62 if (--m_ItsCounter->count == 0) {
63 delete m_ItsCounter->ptr;
66 m_ItsCounter =
nullptr;
101 template <
typename KeyType,
typename ValueType>
131 return lru_list.size();
138 return lru_list.empty();
152 void insert(
const KeyType& key,
const ValueType& value)
159 ValueType* valptr = find(key);
174 lru_list.push_front(key);
175 cached_value cv(value, lru_list.begin());
176 table.insert(make_pair(key, cv));
181 if (lru_list.size() > maxsize)
183 KeyType lru_key = lru_list.back();
184 table.erase(lru_key);
200 ValueType*
find(
const KeyType& key)
202 TableIteratorType ti =
table.find(key);
206 if (ti ==
table.end())
214 ListIteratorType li = ti->second.cache_i;
215 lru_list.splice(lru_list.begin(), lru_list, li);
217 return &(ti->second.value);
227 ostr <<
"Debug dump of LRUCache\n";
228 ostr <<
"-------------------\n\n";
230 if (lru_list.empty())
232 ostr <<
"The cache is empty\n";
235 ostr <<
"Sorted from MRU to LRU:\n\n";
237 for (ListIteratorType i = lru_list.begin(); i != lru_list.end(); ++i)
239 ostr <<
"Key: " << *i << endl;
241 TableIteratorType ti =
table.find(*i);
242 assert(ti !=
table.end());
244 ostr <<
"Value: " << ti->second.value <<
"\n|\n";
257 ostr <<
"LRUCache statistics\n";
258 ostr <<
"----------------\n\n";
259 ostr << format_str(
"Max size: %ld, cur size %ld. Cache is %5.2lf%% full\n\n",
260 maxsize, lru_list.size(), 100.0 * lru_list.size() / maxsize);
261 ostr << format_str(
"Lookups: %7ld\nHits: %7ld\nHit rate: %7.2lf%%\n\n",
262 stats.finds, stats.finds_hit, 100.0 * stats.finds_hit / (stats.finds+1e-15));
263 ostr << format_str(
"Items removed by LRU: %ld\n\n", stats.removed);
266 void statistics(ostream & vtkNotUsed(ostr) )
const 272 string format_str(
const char* format, ...)
const 278 va_start(arglist, format);
279 char* buf =
new char[10000];
281 vsprintf(buf, format, arglist);
289 typedef typename list<KeyType>::iterator ListIteratorType;
293 cached_value(ValueType value_, ListIteratorType cache_i_)
294 : value(value_), cache_i(cache_i_)
299 ListIteratorType cache_i;
302 typedef typename map<KeyType, cached_value>::iterator TableIteratorType;
314 list<KeyType> lru_list;
318 map<KeyType, cached_value>
table;
322 struct cache_statistics
331 finds = finds_hit = removed = 0;
335 unsigned long finds_hit;
336 unsigned long removed;
counted_ptr(const counted_ptr &r)
ElementType * operator->() const
Simplified inverse ITK transforms.
ValueType * find(const KeyType &key)
LRUCache(unsigned maxsize_=100)
void debug_dump(ostream &ostr=cerr)
void insert(const KeyType &key, const ValueType &value)
void statistics(ostream &ostr=cerr) const
counted_ptr & operator=(const counted_ptr &r)
ElementType & operator*() const
counted_ptr(ElementType *p=0)
allocate a new counter
void set_maxsize(unsigned maxsize_)