7#ifndef PXR_EXEC_VDF_LRU_CACHE_H
8#define PXR_EXEC_VDF_LRU_CACHE_H
17PXR_NAMESPACE_OPEN_SCOPE
26template <
typename Key,
typename Value,
typename Hash >
40 explicit VdfLRUCache(
size_t capacity) : _capacity(capacity) {}
51 bool Lookup(
const Key &key, Value **value);
61 _Entry(
size_t h,
const Key &k) : hash(h), key(k) {}
70 using _List = std::list<_Entry>;
74 const size_t _capacity;
79template <
typename Key,
typename Value,
typename Hash >
85 const size_t hash = Hash()(key);
88 typename _List::iterator it = _list.begin();
89 for (; it != _list.end(); ++it) {
91 if (it->hash == hash && it->key == key) {
95 if (it != _list.begin()) {
96 _list.splice(_list.begin(), _list, it);
107 if (_list.size() < _capacity) {
108 _list.emplace_front(hash, key);
114 _list.splice(_list.begin(), _list, --_list.end());
115 _list.front().hash = hash;
116 _list.front().key = key;
120 *value = &_list.front().value;
124template <
typename Key,
typename Value,
typename Hash >
131PXR_NAMESPACE_CLOSE_SCOPE
A simple cache with a fixed capacity and a least-recently-used eviction policy.
VdfLRUCache(size_t capacity)
Constructs a new cache with a fixed capacity.
void Clear()
Removes all entries from the cache.
bool Lookup(const Key &key, Value **value)
Performs a lookup into the cache and returns true if the cache contains an entry for the given key.