27 #ifndef INCLUDED_CACHE_ADT
28 #define INCLUDED_CACHE_ADT
36 #if CONFIG_ENABLE_BOOST
37 # include <boost/unordered_map.hpp>
38 # define MAP boost::unordered_map
40 # define MAP stdext::hash_map
123 float min_credit_density = FLT_MAX;
124 for(
typename Entries::const_iterator it = entries.begin(); it != entries.end(); ++it)
126 const float credit_density = Entries::entry_from_it(it).credit_density();
127 min_credit_density = std::min(min_credit_density, credit_density);
129 return min_credit_density;
139 template<
class Entry,
class Entries>
157 template<
class Entry,
class Entries>
232 template<
typename Key,
typename Entry,
template<
class Entry_,
class Entries>
class McdCalc =
McdCalc_Cached>
241 void add(
const Key&
key,
const Entry& entry)
244 (void)
add_(key, entry);
247 bool find(
const Key&
key,
const Entry** pentry)
const
252 *pentry = &it->second;
256 void remove(
const Key&
key)
268 mcd_calc.notify_impending_increase_or_remove(entry);
272 const float gain = 0.75f;
273 entry.credit = gain*entry.cost + (1.0f-gain)*entry.credit;
275 mcd_calc.notify_increased_or_removed(entry);
289 ENSURE(min_credit_density > 0.0f);
293 Entry& entry = it->second;
295 charge(entry, min_credit_density);
298 entry_list.push_back(entry);
301 MapIt it_to_remove = it++;
302 map.erase(it_to_remove);
311 if(entry_list.empty())
319 static Entry&
entry_from_it(
typename Map::iterator it) {
return it->second; }
320 static const Entry&
entry_from_it(
typename Map::const_iterator it) {
return it->second; }
322 typedef typename Map::iterator
MapIt;
323 typedef typename Map::const_iterator
MapCIt;
329 typedef std::pair<MapIt, bool> PairIB;
330 typename Map::value_type val = std::make_pair(key, entry);
331 PairIB ret =
map.insert(val);
342 const Entry& entry = it->second;
343 mcd_calc.notify_impending_increase_or_remove(entry);
344 mcd_calc.notify_increased_or_removed(entry);
350 entry.credit -= delta * entry.size;
363 Entry& entry = it->second;
364 entry.credit -= delta * entry.size;
376 return entry.credit < 0.0001f;
392 template<
typename Key,
class Entry>
402 void add(
const Key&
key,
const Entry& entry)
412 void remove(
const Key&
key)
419 while(!
pri_q.empty())
421 for(
MapCIt it = this->
map.begin(); it != this->
map.end(); ++it)
437 Entry& entry = Map::entry_from_it(least_valuable_it);
439 entry_list.push_back(entry);
446 const float credit_density = entry.credit_density();
447 ENSURE(credit_density > 0.0f);
461 return Map::entry_from_it(it1).credit_density() >
462 Map::entry_from_it(it2).credit_density();
473 class PriQ:
public std::priority_queue<MapIt, std::vector<MapIt>, CD_greater>
483 std::make_heap(this->c.begin(), this->c.end(), this->comp);
523 return val / divisor;
543 template<
typename Key,
typename Entry>
552 void add(
const Key&
key,
const Entry& entry)
557 bool find(
const Key&
key,
const Entry** pentry)
const
562 *pentry = &it->entry;
566 void remove(
const Key&
key)
573 for(
It it =
lru.begin(); it !=
lru.end(); ++it)
575 if(&entry == &it->entry)
577 add(it->key, it->entry);
587 entry_list.push_back(
lru.front().entry);
604 typedef std::list<KeyAndEntry>
List;
605 typedef typename List::iterator
It;
606 typedef typename List::const_iterator
CIt;
652 typename Key,
typename Item,
662 void add(
const Key&
key,
const Item& item,
size_t size,
size_t cost)
664 return mgr.add(key,
Entry(item, size, cost));
671 void remove(
const Key&
key)
683 bool retrieve(
const Key&
key, Item& item,
size_t* psize = 0,
bool refill_credit =
true)
686 if(!
mgr.find(key, &entry))
691 *psize = entry->
size;
699 bool peek(
const Key&
key, Item& item,
size_t* psize = 0)
701 return retrieve(key, item, psize,
false);
745 #endif // #ifndef INCLUDED_CACHE_ADT
void notify_impending_increase_or_remove(const Entry &entry)
float operator()(float val, float divisor) const
void commit_pending_delta()
bool retrieve(const Key &key, Item &item, size_t *psize=0, bool refill_credit=true)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
McdCalc< Entry, Map > mcd_calc
void notify_decreased(const Entry &) const
void add(const Key &key, const Entry &entry)
void notify_increased_or_removed(const Entry &entry)
float operator()(const Entries &entries) const
Landlord_Naive< Key, Entry >::Map Map
void notify_impending_increase_or_remove(const Entry &) const
bool remove_least_valuable(Item *pItem=0, size_t *pSize=0)
bool find(const Key &key, const Entry **pentry) const
void charge(Entry &entry, float delta)
bool should_evict(const Entry &entry)
void notify_increased_or_removed(const Entry &) const
void on_access(Entry &entry)
bool operator()(MapIt it1, MapIt it2) const
void add(const Key &key, const Item &item, size_t size, size_t cost)
static const Entry & entry_from_it(typename Map::const_iterator it)
KeyAndEntry(const Key &key, const Entry &entry)
void charge_all(float delta)
CacheEntry(const Item &item_, size_t size_, size_t cost_)
float operator()(float val, float divisor) const
void add(const Key &key, const Entry &entry)
Manager< Key, Entry > mgr
Landlord_Naive< Key, Entry > Parent
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
bool peek(const Key &key, Item &item, size_t *psize=0)
Map::const_iterator MapCIt
bool operator==(const KeyAndEntry &rhs) const
void remove_least_valuable(std::list< Entry > &entry_list)
void notify_added(const Entry &) const
void remove_least_valuable(std::list< Entry > &entry_list)
void on_access(Entry &entry)
Landlord_Naive< Key, Entry >::MapIt MapIt
static Entry & entry_from_it(typename Map::iterator it)
void on_access(Entry &entry)
CacheEntry< Item, Divider > Entry
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Landlord_Naive< Key, Entry >::MapCIt MapCIt
float credit_density() const
void remove(const Key &key)
void remove_least_valuable(std::list< Entry > &entry_list)
std::list< KeyAndEntry > List
void add(const Key &key, const Entry &entry)
bool feq(double d1, double d2, double epsilon=0.00001)
are the given floats nearly "equal"?
std::list< Entry > entries_awaiting_eviction
bool operator!=(const KeyAndEntry &rhs) const
KeyAndEntry(const Key &key)
void notify_decreased(const Entry &entry)
void notify_added(const Entry &entry)
MapIt add_(const Key &key, const Entry &entry)
float ll_calc_min_credit_density(const Entries &entries)
bool find(const Key &key, const Entry **pentry) const
float operator()(const Entries &entries)