19 #if !defined(__LRUCACHE_H)
22 #include <mitsuba/mitsuba.h>
23 #include <boost/bimap.hpp>
24 #include <boost/bimap/list_of.hpp>
25 #include <boost/bimap/set_of.hpp>
26 #include <boost/function.hpp>
61 template <
typename K,
typename KComp,
typename V>
struct LRUCache :
public Object {
67 typedef boost::bimaps::bimap<
68 boost::bimaps::set_of<K, KComp>,
69 boost::bimaps::list_of<dummy_type>,
77 const boost::function<V(
const K&)>& generatorFunction,
78 const boost::function<
void (
const V&)>& cleanupFunction = NULL)
79 : m_capacity(capacity), m_generatorFunction(generatorFunction),
80 m_cleanupFunction(cleanupFunction) {
85 typename cache_type::right_iterator
86 src = m_cache.right.begin();
87 if (m_cleanupFunction) {
88 while (src != m_cache.right.end())
89 m_cleanupFunction((*src++).info);
94 return m_cache.size() == m_capacity;
98 V
get(
const K& k,
bool &hit) {
100 const typename cache_type::left_iterator it
101 = m_cache.left.find(k);
103 if (it == m_cache.left.end()) {
107 const V v = m_generatorFunction(k);
115 m_cache.right.relocate(
117 m_cache.project_right(it)
127 template <
typename IT>
void get_keys(IT dst)
const {
128 typename cache_type::right_const_reverse_iterator
129 src = m_cache.right.rbegin();
130 while (src != m_cache.right.rend())
131 *dst++=(*src++).second;
135 SAssert(m_cache.size() <= m_capacity);
136 if (m_cache.size() == m_capacity) {
137 if (m_cleanupFunction)
138 m_cleanupFunction(m_cache.right.begin()->info);
141 m_cache.right.erase(m_cache.right.begin());
145 m_cache.insert(
typename cache_type::value_type(k,0,v));
150 boost::function<V(const K&)> m_generatorFunction;
151 boost::function<void(const V&)> m_cleanupFunction;
void insert(const K &k, const V &v)
Definition: lrucache.h:134
boost::bimaps::bimap< boost::bimaps::set_of< K, KComp >, boost::bimaps::list_of< dummy_type >, boost::bimaps::with_info< V > > cache_type
Definition: lrucache.h:70
bool isFull() const
Definition: lrucache.h:93
int dummy_type
Definition: lrucache.h:63
virtual ~LRUCache()
Definition: lrucache.h:84
LRUCache()
Definition: lrucache.h:72
#define SAssert(cond)
``Static'' assertion (to be used outside of classes that derive from Object)
Definition: logger.h:79
void get_keys(IT dst) const
Definition: lrucache.h:127
LRUCache(size_t capacity, const boost::function< V(const K &)> &generatorFunction, const boost::function< void(const V &)> &cleanupFunction=NULL)
Definition: lrucache.h:76
Generic LRU cache implementation.
Definition: lrucache.h:61
Parent of all Mitsuba classes.
Definition: object.h:38