36 LruCache(T* array,
unsigned count) : m_free(array), m_list(0) {
39 for (
unsigned a = 0 ; a < count-1 ; ++a) {
40 array[a].m_next = array + a + 1;
42 array[count-1].m_next = 0;
49 m_list = item->m_next;
50 item->m_next = m_free;
61 unsigned len()
const {
63 const T* item = m_list;
73 T*
find(
const decltype(T::m_key)& key) {
75 while (ptr && !(ptr->m_key == key)) {
84 T*
query(
const decltype(T::m_key)& key) {
89 return update(0, m_free);
90 }
else if (m_list->m_key == key) {
95 T** ptr = &m_list->m_next;
97 if ((*ptr)->m_key == key)
return update(ptr, *ptr);
98 if ((*ptr)->m_next) ptr = &((*ptr)->m_next);
105 return update(0, m_free);
109 return update(ptr, *ptr);
116 T* update(T** prev, T* item) {
117 if (item == m_free) m_free = m_free->m_next;
118 if (prev) *prev = (*prev)->m_next;
119 item->m_next = m_list;
Template class implementing a least-recently-used (LRU) cache.
LruCache(T *array, unsigned count)
Given a backing array, initialize an empty cache.
unsigned len() const
Count the number of stored items.
T * find(const decltype(T::m_key)&key)
Query the cache without modifying its contents.
bool is_empty() const
Is this an empty list?
void clear()
Reset this cache to the empty state.
T * query(const decltype(T::m_key)&key)
Query the cache, updating the recently-used list.