27#ifndef INCLUDED_CACHE_ADT
28#define INCLUDED_CACHE_ADT
33#include <unordered_map>
115 float min_credit_density = FLT_MAX;
116 for(
typename Entries::const_iterator it = entries.begin(); it != entries.end(); ++it)
118 const float credit_density = Entries::entry_from_it(it).credit_density();
119 min_credit_density = std::min(min_credit_density, credit_density);
121 return min_credit_density;
131template<
class Entry,
class Entries>
149template<
class Entry,
class Entries>
224template<
typename Key,
typename Entry,
template<
class Entry_,
class Entries>
class McdCalc =
McdCalc_Cached>
244 *pentry = &it->second;
260 mcd_calc.notify_impending_increase_or_remove(entry);
264 const float gain = 0.75f;
265 entry.credit = gain*entry.cost + (1.0f-gain)*entry.credit;
267 mcd_calc.notify_increased_or_removed(entry);
281 ENSURE(min_credit_density > 0.0f);
285 Entry& entry = it->second;
287 charge(entry, min_credit_density);
290 entry_list.push_back(entry);
293 MapIt it_to_remove = it++;
294 map.erase(it_to_remove);
303 if(entry_list.empty())
308 class Map :
public std::unordered_map<Key, Entry>
314 typedef typename Map::iterator
MapIt;
315 typedef typename Map::const_iterator
MapCIt;
321 typedef std::pair<MapIt, bool> PairIB;
322 typename Map::value_type val = std::make_pair(
key, entry);
323 PairIB ret =
map.insert(val);
334 const Entry& entry = it->second;
335 mcd_calc.notify_impending_increase_or_remove(entry);
336 mcd_calc.notify_increased_or_removed(entry);
342 entry.credit -= delta * entry.size;
355 Entry& entry = it->second;
356 entry.credit -= delta * entry.size;
368 return entry.credit < 0.0001f;
384template<
typename Key,
class Entry>
411 while(!
pri_q.empty())
413 for(
MapCIt it = this->
map.begin(); it != this->map.end(); ++it)
429 Entry& entry = Map::entry_from_it(least_valuable_it);
431 entry_list.push_back(entry);
438 const float credit_density = entry.credit_density();
439 ENSURE(credit_density > 0.0f);
453 return Map::entry_from_it(it1).credit_density() >
454 Map::entry_from_it(it2).credit_density();
465 class PriQ:
public std::priority_queue<MapIt, std::vector<MapIt>, CD_greater>
475 std::make_heap(this->c.begin(), this->c.end(), this->comp);
515 return val / divisor;
535template<
typename Key,
typename Entry>
554 *pentry = &it->entry;
565 for(
It it =
lru.begin(); it !=
lru.end(); ++it)
567 if(&entry == &it->entry)
569 add(it->key, it->entry);
579 entry_list.push_back(
lru.front().entry);
596 typedef std::list<KeyAndEntry>
List;
597 typedef typename List::iterator
It;
598 typedef typename List::const_iterator
CIt;
644typename Key,
typename Item,
654 void add(
const Key&
key,
const Item& item,
size_t size,
size_t cost)
675 bool retrieve(
const Key&
key, Item& item,
size_t* psize = 0,
bool refill_credit =
true)
678 if(!
mgr.find(
key, &entry))
683 *psize = entry->
size;
691 bool peek(
const Key&
key, Item& item,
size_t* psize = 0)
uint32_t Entry
Definition: SilhouetteRenderer.cpp:138
float ll_calc_min_credit_density(const Entries &entries)
Definition: cache_adt.h:113
Definition: cache_adt.h:650
Cache()
Definition: cache_adt.h:652
void remove(const Key &key)
Definition: cache_adt.h:663
std::list< Entry > entries_awaiting_eviction
Definition: cache_adt.h:732
CacheEntry< Item, Divider > Entry
Definition: cache_adt.h:729
bool remove_least_valuable(Item *pItem=0, size_t *pSize=0)
Definition: cache_adt.h:698
void add(const Key &key, const Item &item, size_t size, size_t cost)
Definition: cache_adt.h:654
bool empty() const
Definition: cache_adt.h:723
bool peek(const Key &key, Item &item, size_t *psize=0)
Definition: cache_adt.h:691
bool retrieve(const Key &key, Item &item, size_t *psize=0, bool refill_credit=true)
Definition: cache_adt.h:675
Manager< Key, Entry > mgr
Definition: cache_adt.h:734
Definition: cache_adt.h:509
float operator()(float val, float divisor) const
Definition: cache_adt.h:513
Divider_Naive()
Definition: cache_adt.h:511
Divider_Naive(float x)
Definition: cache_adt.h:512
Definition: cache_adt.h:522
Divider_Recip(float x)
Definition: cache_adt.h:526
Divider_Recip()
Definition: cache_adt.h:525
float operator()(float val, float divisor) const
Definition: cache_adt.h:527
float recip
Definition: cache_adt.h:523
Definition: cache_adt.h:537
List::iterator It
Definition: cache_adt.h:597
void on_access(Entry &entry)
Definition: cache_adt.h:563
std::list< KeyAndEntry > List
Definition: cache_adt.h:596
List::const_iterator CIt
Definition: cache_adt.h:598
void add(const Key &key, const Entry &entry)
Definition: cache_adt.h:544
void remove(const Key &key)
Definition: cache_adt.h:558
bool find(const Key &key, const Entry **pentry) const
Definition: cache_adt.h:549
void remove_least_valuable(std::list< Entry > &entry_list)
Definition: cache_adt.h:577
bool empty() const
Definition: cache_adt.h:539
List lru
Definition: cache_adt.h:599
Definition: cache_adt.h:309
static const Entry & entry_from_it(typename Map::const_iterator it)
Definition: cache_adt.h:312
static Entry & entry_from_it(typename Map::iterator it)
Definition: cache_adt.h:311
Definition: cache_adt.h:378
Definition: cache_adt.h:466
void ensure_heap_order()
Definition: cache_adt.h:468
Definition: cache_adt.h:386
Landlord_Naive< Key, Entry >::MapIt MapIt
Definition: cache_adt.h:388
void add(const Key &key, const Entry &entry)
Definition: cache_adt.h:394
void remove_least_valuable(std::list< Entry > &entry_list)
Definition: cache_adt.h:426
void on_access(Entry &entry)
Definition: cache_adt.h:417
Landlord_Naive< Key, Entry > Parent
Definition: cache_adt.h:446
PriQ pri_q
Definition: cache_adt.h:478
Landlord_Naive< Key, Entry >::Map Map
Definition: cache_adt.h:387
void remove(const Key &key)
Definition: cache_adt.h:404
Landlord_Naive< Key, Entry >::MapCIt MapCIt
Definition: cache_adt.h:389
float pending_delta
Definition: cache_adt.h:482
void commit_pending_delta()
Definition: cache_adt.h:484
Landlord_Lazy()
Definition: cache_adt.h:392
Definition: cache_adt.h:377
Definition: cache_adt.h:226
void charge_all(float delta)
Definition: cache_adt.h:351
void remove_least_valuable(std::list< Entry > &entry_list)
Definition: cache_adt.h:270
MapIt add_(const Key &key, const Entry &entry)
Definition: cache_adt.h:319
McdCalc< Entry, Map > mcd_calc
Definition: cache_adt.h:372
void on_access(Entry &entry)
Definition: cache_adt.h:258
void add(const Key &key, const Entry &entry)
Definition: cache_adt.h:233
void remove(const Key &key)
Definition: cache_adt.h:248
void charge(Entry &entry, float delta)
Definition: cache_adt.h:340
Map map
Definition: cache_adt.h:316
bool find(const Key &key, const Entry **pentry) const
Definition: cache_adt.h:239
void remove_(MapIt it)
Definition: cache_adt.h:332
Map::iterator MapIt
Definition: cache_adt.h:314
bool should_evict(const Entry &entry)
Definition: cache_adt.h:363
Map::const_iterator MapCIt
Definition: cache_adt.h:315
bool empty() const
Definition: cache_adt.h:228
Definition: cache_adt.h:151
void notify_decreased(const Entry &entry)
Definition: cache_adt.h:163
float min_credit_density
Definition: cache_adt.h:208
bool is_min_entry
Definition: cache_adt.h:212
void notify_impending_increase_or_remove(const Entry &entry)
Definition: cache_adt.h:168
float operator()(const Entries &entries)
Definition: cache_adt.h:185
void notify_increased_or_removed(const Entry &entry)
Definition: cache_adt.h:174
bool min_valid
Definition: cache_adt.h:209
McdCalc_Cached()
Definition: cache_adt.h:153
void notify_added(const Entry &entry)
Definition: cache_adt.h:155
Definition: cache_adt.h:133
void notify_decreased(const Entry &) const
Definition: cache_adt.h:136
void notify_increased_or_removed(const Entry &) const
Definition: cache_adt.h:138
void notify_impending_increase_or_remove(const Entry &) const
Definition: cache_adt.h:137
void notify_added(const Entry &) const
Definition: cache_adt.h:135
float operator()(const Entries &entries) const
Definition: cache_adt.h:139
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning.
Definition: code_annotation.h:40
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Definition: debug.h:326
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:277
bool feq(double d1, double d2, double epsilon=0.00001)
are the given floats nearly "equal"?
Definition: lib.h:88
const Status LOGIC
Definition: status.h:411
Definition: cache_adt.h:607
Item item
Definition: cache_adt.h:608
float credit
Definition: cache_adt.h:611
CacheEntry(const Item &item_, size_t size_, size_t cost_)
Definition: cache_adt.h:620
size_t size
Definition: cache_adt.h:609
Divider divider
Definition: cache_adt.h:613
CacheEntry()
Definition: cache_adt.h:616
size_t cost
Definition: cache_adt.h:610
float credit_density() const
Definition: cache_adt.h:631
Definition: cache_adt.h:585
bool operator==(const KeyAndEntry &rhs) const
Definition: cache_adt.h:589
KeyAndEntry(const Key &key)
Definition: cache_adt.h:586
KeyAndEntry(const Key &key, const Entry &entry)
Definition: cache_adt.h:587
Key key
Definition: cache_adt.h:592
bool operator!=(const KeyAndEntry &rhs) const
Definition: cache_adt.h:590
Entry entry
Definition: cache_adt.h:593
Definition: cache_adt.h:450
bool operator()(MapIt it1, MapIt it2) const
Definition: cache_adt.h:451
pthread_key_t key
Definition: wpthread.cpp:140