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>
173 min_credit_density = std::min(min_credit_density, entry.credit_density());
179 is_min_entry =
feq(min_credit_density, entry.credit_density());
189 min_credit_density = -1.0f;
201 return min_credit_density;
211 min_credit_density = FLT_MAX;
232 template<
typename Key,
typename Entry,
template<
class Entry_,
class Entries>
class McdCalc =
McdCalc_Cached>
244 (void)add_(key, entry);
249 MapCIt it = map.find(key);
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);
288 const float min_credit_density = mcd_calc(map);
289 ENSURE(min_credit_density > 0.0f);
291 for(
MapIt it = map.begin(); it != map.end();)
293 Entry& entry = it->second;
295 charge(entry, min_credit_density);
296 if(should_evict(entry))
298 entry_list.push_back(entry);
301 MapIt it_to_remove = it++;
302 map.erase(it_to_remove);
306 mcd_calc.notify_decreased(entry);
311 if(entry_list.empty())
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);
334 mcd_calc.notify_added(entry);
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;
361 for(MapIt it = map.begin(); it != map.end(); ++it)
363 Entry& entry = it->second;
364 entry.credit -= delta * entry.size;
365 if(!should_evict(entry))
366 mcd_calc.notify_decreased(entry);
376 return entry.credit < 0.0001f;
392 template<
typename Key,
class Entry>
406 commit_pending_delta();
408 MapIt it = Parent::add_(key, entry);
412 void remove(
const Key&
key)
419 while(!pri_q.empty())
421 for(MapCIt it = this->map.begin(); it != this->map.end(); ++it)
427 Parent::on_access(entry);
431 pri_q.ensure_heap_order();
436 MapIt least_valuable_it = pri_q.top(); pri_q.pop();
437 Entry& entry = Map::entry_from_it(least_valuable_it);
439 entry_list.push_back(entry);
445 entry.credit -= pending_delta*entry.size;
446 const float credit_density = entry.credit_density();
447 ENSURE(credit_density > 0.0f);
448 pending_delta += credit_density;
450 Parent::remove_(least_valuable_it);
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);
494 if(pending_delta > 0.0f)
496 this->charge_all(pending_delta);
497 pending_delta = 0.0f;
523 return val / divisor;
543 template<
typename Key,
typename Entry>
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;
629 : item(item_), divider((float)size_)
633 credit = (float)cost;
641 return divider(credit, (
float)size);
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;
694 mgr.on_access((
Entry&)*entry);
699 bool peek(
const Key&
key, Item& item,
size_t* psize = 0)
701 return retrieve(key, item, psize,
false);
712 if(entries_awaiting_eviction.empty())
717 mgr.remove_least_valuable(entries_awaiting_eviction);
718 ENSURE(!entries_awaiting_eviction.empty());
721 const Entry& entry = entries_awaiting_eviction.front();
726 entries_awaiting_eviction.pop_front();
745 #endif // #ifndef INCLUDED_CACHE_ADT void notify_impending_increase_or_remove(const Entry &entry)
Definition: cache_adt.h:176
float operator()(float val, float divisor) const
Definition: cache_adt.h:535
void commit_pending_delta()
Definition: cache_adt.h:492
const Status LOGIC
Definition: status.h:409
bool retrieve(const Key &key, Item &item, size_t *psize=0, bool refill_credit=true)
Definition: cache_adt.h:683
Definition: cache_adt.h:316
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
Definition: code_annotation.h:38
void remove_(MapIt it)
Definition: cache_adt.h:340
Definition: cache_adt.h:614
float min_credit_density
Definition: cache_adt.h:216
McdCalc< Entry, Map > mcd_calc
Definition: cache_adt.h:380
CacheEntry()
Definition: cache_adt.h:624
Definition: cache_adt.h:385
void notify_decreased(const Entry &) const
Definition: cache_adt.h:144
void add(const Key &key, const Entry &entry)
Definition: cache_adt.h:241
void notify_increased_or_removed(const Entry &entry)
Definition: cache_adt.h:182
Divider divider
Definition: cache_adt.h:621
float operator()(const Entries &entries) const
Definition: cache_adt.h:147
Landlord_Naive< Key, Entry >::Map Map
Definition: cache_adt.h:395
void notify_impending_increase_or_remove(const Entry &) const
Definition: cache_adt.h:145
Item item
Definition: cache_adt.h:616
bool operator==(const FCDJointWeightPair &a, const FCDJointWeightPair &b)
Definition: GeomReindex.cpp:59
#define MAP
Definition: cache_adt.h:40
bool remove_least_valuable(Item *pItem=0, size_t *pSize=0)
Definition: cache_adt.h:706
Divider_Naive()
Definition: cache_adt.h:519
float credit
Definition: cache_adt.h:619
bool find(const Key &key, const Entry **pentry) const
Definition: cache_adt.h:557
void charge(Entry &entry, float delta)
Definition: cache_adt.h:348
bool should_evict(const Entry &entry)
Definition: cache_adt.h:371
The MIT License free of charge
Definition: LICENSE.txt:5
void notify_increased_or_removed(const Entry &) const
Definition: cache_adt.h:146
void on_access(Entry &entry)
Definition: cache_adt.h:266
bool operator()(MapIt it1, MapIt it2) const
Definition: cache_adt.h:459
void add(const Key &key, const Item &item, size_t size, size_t cost)
Definition: cache_adt.h:662
Definition: cache_adt.h:158
static const Entry & entry_from_it(typename Map::const_iterator it)
Definition: cache_adt.h:320
Definition: cache_adt.h:592
KeyAndEntry(const Key &key, const Entry &entry)
Definition: cache_adt.h:595
void charge_all(float delta)
Definition: cache_adt.h:359
CacheEntry(const Item &item_, size_t size_, size_t cost_)
Definition: cache_adt.h:628
float operator()(float val, float divisor) const
Definition: cache_adt.h:521
Definition: cache_adt.h:473
void add(const Key &key, const Entry &entry)
Definition: cache_adt.h:552
Entry entry
Definition: cache_adt.h:601
Manager< Key, Entry > mgr
Definition: cache_adt.h:742
Definition: cache_adt.h:657
Landlord_Naive< Key, Entry > Parent
Definition: cache_adt.h:454
float pending_delta
Definition: cache_adt.h:490
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:287
PriQ pri_q
Definition: cache_adt.h:486
size_t cost
Definition: cache_adt.h:618
Definition: cache_adt.h:544
bool peek(const Key &key, Item &item, size_t *psize=0)
Definition: cache_adt.h:699
List::const_iterator CIt
Definition: cache_adt.h:606
Map::const_iterator MapCIt
Definition: cache_adt.h:323
Map map
Definition: cache_adt.h:324
void ensure_heap_order()
Definition: cache_adt.h:476
bool operator==(const KeyAndEntry &rhs) const
Definition: cache_adt.h:597
void remove_least_valuable(std::list< Entry > &entry_list)
Definition: cache_adt.h:434
void notify_added(const Entry &) const
Definition: cache_adt.h:143
Definition: cache_adt.h:457
void remove_least_valuable(std::list< Entry > &entry_list)
Definition: cache_adt.h:585
bool empty() const
Definition: cache_adt.h:547
pthread_key_t key
Definition: wpthread.cpp:140
Definition: cache_adt.h:233
void on_access(Entry &entry)
Definition: cache_adt.h:425
List lru
Definition: cache_adt.h:607
size_t size
Definition: cache_adt.h:617
uint32_t Entry
Definition: SilhouetteRenderer.cpp:135
Landlord_Naive< Key, Entry >::MapIt MapIt
Definition: cache_adt.h:396
static Entry & entry_from_it(typename Map::iterator it)
Definition: cache_adt.h:319
Definition: cache_adt.h:516
void on_access(Entry &entry)
Definition: cache_adt.h:571
Key key
Definition: cache_adt.h:600
CacheEntry< Item, Divider > Entry
Definition: cache_adt.h:737
Cache()
Definition: cache_adt.h:660
Landlord_Lazy()
Definition: cache_adt.h:400
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Definition: debug.h:336
McdCalc_Cached()
Definition: cache_adt.h:161
Divider_Recip()
Definition: cache_adt.h:533
Definition: cache_adt.h:393
Landlord_Naive< Key, Entry >::MapCIt MapCIt
Definition: cache_adt.h:397
float credit_density() const
Definition: cache_adt.h:639
void remove_least_valuable(std::list< Entry > &entry_list)
Definition: cache_adt.h:278
std::list< KeyAndEntry > List
Definition: cache_adt.h:604
bool empty() const
Definition: cache_adt.h:236
float recip
Definition: cache_adt.h:531
void add(const Key &key, const Entry &entry)
Definition: cache_adt.h:402
bool min_valid
Definition: cache_adt.h:217
Definition: cache_adt.h:140
bool feq(double d1, double d2, double epsilon=0.00001)
are the given floats nearly "equal"?
Definition: lib.h:96
bool is_min_entry
Definition: cache_adt.h:220
std::list< Entry > entries_awaiting_eviction
Definition: cache_adt.h:740
Definition: cache_adt.h:386
Map::iterator MapIt
Definition: cache_adt.h:322
bool operator!=(const KeyAndEntry &rhs) const
Definition: cache_adt.h:598
List::iterator It
Definition: cache_adt.h:605
Divider_Naive(float x)
Definition: cache_adt.h:520
Divider_Recip(float x)
Definition: cache_adt.h:534
Definition: cache_adt.h:529
KeyAndEntry(const Key &key)
Definition: cache_adt.h:594
void notify_decreased(const Entry &entry)
Definition: cache_adt.h:171
void notify_added(const Entry &entry)
Definition: cache_adt.h:163
MapIt add_(const Key &key, const Entry &entry)
Definition: cache_adt.h:327
bool empty() const
Definition: cache_adt.h:731
float ll_calc_min_credit_density(const Entries &entries)
Definition: cache_adt.h:121
bool find(const Key &key, const Entry **pentry) const
Definition: cache_adt.h:247
float operator()(const Entries &entries)
Definition: cache_adt.h:193