29 return { ++Current_ };
37 void Touch (ValueAddon& add)
39 add.LastAccess_ = ++Current_;
49 template<
typename K,
typename V,
typename CS = CacheStrat::LRU>
56 typename CS::ValueAddon CacheInfo_;
59 QHash<K, ValueHolder> Hash_;
61 size_t CurrentCost_ = 0;
62 const size_t MaxCost_;
67 : MaxCost_ { maxCost }
80 template<
typename K,
typename V,
typename CS>
86 template<
typename K,
typename V,
typename CS>
90 CacheStratState_.Clear ();
93 template<
typename K,
typename V,
typename CS>
96 return Hash_.contains (k);
99 template<
typename K,
typename V,
typename CS>
102 if (!Hash_.contains (key))
104 Hash_.insert (key, { {}, 1, CacheStratState_.CreateInfo () });
110 CacheStratState_.Touch (Hash_ [key].CacheInfo_);
112 return Hash_ [key].V_;
115 template<
typename K,
typename V,
typename CS>
116 void AssocCache<K, V, CS>::CheckShrink ()
118 while (CurrentCost_ > MaxCost_)
120 const auto pos = std::min_element (Hash_.begin (), Hash_.end (),
121 [] (
const ValueHolder& left,
const ValueHolder& right)
122 { return left.CacheInfo_ < right.CacheInfo_; });
123 CurrentCost_ -= pos->Cost_;