DDD 1.9.0.20250409152518
Cache.hh
Go to the documentation of this file.
1#ifndef _CACHE_HH_
2#define _CACHE_HH_
3
5
6template
7 <
8 typename FuncType
9 , typename ParamType
10 , typename ResType
11 , typename EvalFunc=int
12 >
13class Cache
14{
15private:
16 mutable size_t peak_;
17
18 typedef typename hash_map< std::pair<FuncType, ParamType>, ResType >::type
21
22public:
23 Cache () : peak_ (0) {};
24 Cache (size_t s) : peak_ (0), cache_ (s) {};
25
27 void clear (bool keepstats = false) {
28 peak();
29 cache_.clear();
30 }
31
32 size_t peak () const {
33 size_t s = size();
34 if ( peak_ < s )
35 peak_ = s;
36 return peak_;
37 }
38
39
40 size_t size () const {
41 return cache_.size();
42 }
43
44 ResType eval (const FuncType & func, const ParamType & param) const {
45 return func.eval(param);
46 }
47
48 bool
49 should_insert (const FuncType & ) const
50 {
51 return true;
52 }
53
54 std::pair<bool,ResType>
55 insert(const FuncType& hom, const ParamType& node)
56 {
57 bool found;
58
59 { // lock on current bucket
60 typename hash_map::const_accessor access;
61 found = cache_.find ( access, std::make_pair(hom,node));
62 if (found)
63 return std::make_pair(false, access->second);
64 } // end of lock on the current bucket
65
66 // wasn't in cache
67 ResType result = eval(hom, node);
68 if (should_insert (hom))
69 {
70 // lock on current bucket
71 typename hash_map::accessor access;
72 bool insertion = cache_.insert ( access, std::make_pair(hom,node));
73 if (insertion) {
74 // should happen except in MT case
75 access->second = result;
76 }
77 return std::make_pair(insertion,result);
78 }
79 else
80 return std::make_pair (false, result);
81 }
82
83#ifdef HASH_STAT
84 std::map<std::string, size_t> get_hits() const { return cache_.get_hits(); }
85 std::map<std::string, size_t> get_misses() const { return cache_.get_misses(); }
86 std::map<std::string, size_t> get_bounces() const { return cache_.get_bounces(); }
87#endif // HASH_STAT
88};
89
90#endif /* _CACHE_HH_ */
Definition Cache.hh:14
size_t peak_
Definition Cache.hh:16
bool should_insert(const FuncType &) const
Definition Cache.hh:49
size_t peak() const
Definition Cache.hh:32
std::pair< bool, ResType > insert(const FuncType &hom, const ParamType &node)
Definition Cache.hh:55
hash_map< std::pair< FuncType, ParamType >, ResType >::type hash_map
Definition Cache.hh:19
void clear(bool keepstats=false)
clear the cache, discarding all values.
Definition Cache.hh:27
hash_map cache_
Definition Cache.hh:20
Cache(size_t s)
Definition Cache.hh:24
Cache()
Definition Cache.hh:23
ResType eval(const FuncType &func, const ParamType &param) const
Definition Cache.hh:44
size_t size() const
Definition Cache.hh:40
Definition ext_hash_map.hh:96
Definition ext_hash_map.hh:38
Definition ext_hash_map.hh:21
size_type size() const
Definition ext_hash_map.hh:199
bool find(accessor &result, const Key &key)
Definition ext_hash_map.hh:217
bool insert(accessor &result, const Key &key)
Definition ext_hash_map.hh:235
void clear()
Definition ext_hash_map.hh:211

Please comment this page and report errors about it on the RefDocComments page.
Generated on Wed Apr 9 2025 15:27:42 for DDD by doxygen 1.9.8