15 #ifndef TLX_CONTAINER_LRU_CACHE_HEADER 16 #define TLX_CONTAINER_LRU_CACHE_HEADER 24 #include <unordered_map> 41 template <
typename Key,
typename Alloc = std::allocator<Key> >
45 using List =
typename std::list<Key, Alloc>;
48 using Map =
typename std::unordered_map<
50 typename Alloc::template rebind<
51 std::pair<const Key, ListIterator> >::other>;
65 void put(
const Key& key) {
67 typename Map::iterator it =
map_.find(key);
68 if (it !=
map_.end()) {
69 list_.erase(it->second);
74 list_.push_front(key);
76 map_.insert(std::make_pair(key,
list_.begin()));
81 typename Map::iterator it =
map_.find(key);
82 if (it ==
map_.end()) {
83 throw std::range_error(
"There is no such key in cache");
93 typename Map::iterator it =
map_.find(key);
94 if (it !=
map_.end()) {
103 typename Map::iterator it =
map_.find(key);
104 if (it ==
map_.end()) {
105 throw std::range_error(
"There is no such key in cache");
108 list_.erase(it->second);
115 typename Map::iterator it =
map_.find(key);
116 if (it !=
map_.end()) {
117 list_.erase(it->second);
126 return map_.find(key) !=
map_.end();
137 typename List::iterator last =
list_.end();
161 template <
typename Key,
typename Value,
162 typename Alloc = std::allocator<std::pair<Key, Value> > >
169 using List =
typename std::list<KeyValuePair, Alloc>;
170 using ListIterator =
typename List::iterator;
172 using Map =
typename std::unordered_map<
174 typename Alloc::template rebind<
175 std::pair<const Key, ListIterator> >::other>;
191 typename Map::iterator it =
map_.find(key);
192 if (it !=
map_.end()) {
193 list_.erase(it->second);
200 map_.insert(std::make_pair(key,
list_.begin()));
205 typename Map::iterator it =
map_.find(key);
206 if (it ==
map_.end()) {
207 throw std::range_error(
"There is no such key in cache");
216 typename Map::iterator it =
map_.find(key);
217 if (it !=
map_.end()) {
226 typename Map::iterator it =
map_.find(key);
227 if (it ==
map_.end()) {
228 throw std::range_error(
"There is no such key in cache");
231 list_.erase(it->second);
238 typename Map::iterator it =
map_.find(key);
239 if (it !=
map_.end()) {
240 list_.erase(it->second);
248 const Value&
get(
const Key& key) {
249 typename Map::iterator it =
map_.find(key);
250 if (it ==
map_.end()) {
251 throw std::range_error(
"There is no such key in cache");
254 return it->second->second;
260 typename Map::iterator it =
map_.find(key);
261 if (it ==
map_.end()) {
262 throw std::range_error(
"There is no such key in cache");
266 return it->second->second;
272 return map_.find(key) !=
map_.end();
283 typename List::iterator last =
list_.end();
286 map_.erase(last->first);
302 #endif // !TLX_CONTAINER_LRU_CACHE_HEADER typename std::list< KeyValuePair, Alloc > List
bool exists(const Key &key) const
test if key exists in LRU cache
void put(const Key &key)
put or replace/touch item in LRU cache
bool exists(const Key &key) const
test if key exists in LRU cache
void erase(const Key &key)
remove key from LRU cache
This is an expected O(1) LRU cache which contains a set of key-only elements.
void erase(const Key &key)
remove key from LRU cache
typename List::iterator ListIterator
List list_
list of entries in least-recently used order.
const Value & get_touch(const Key &key)
get and touch value from LRU cache for key.
void touch(const Key &key)
touch value from LRU cache for key.
void put(const Key &key, const Value &value)
put or replace/touch item in LRU cache
LruCacheMap(const Alloc &alloc=Alloc())
This is an expected O(1) LRU cache which contains a map of (key -> value) elements.
typename std::unordered_map< thrill::data::ByteBlock *, ListIterator, std::hash< thrill::data::ByteBlock * >, std::equal_to< thrill::data::ByteBlock * >, typename thrill::mem::FixedPoolAllocator< thrill::data::ByteBlock * > ::template rebind< std::pair< const thrill::data::ByteBlock *, ListIterator > >::other > Map
Map map_
map for accelerated access to keys
void touch(const Key &key)
touch pair in LRU cache for key. Throws if it is not in the map.
bool touch_if_exists(const Key &key) noexcept
touch value from LRU cache for key.
bool erase_if_exists(const Key &key) noexcept
remove key from LRU cache
LruCacheSet(const Alloc &alloc=Alloc())
Map map_
map for accelerated access to keys
KeyValuePair pop()
return the least recently used key value pair
Key pop()
return the least recently used key value pair
bool erase_if_exists(const Key &key) noexcept
remove key from LRU cache
typename std::pair< Key, Value > KeyValuePair
List list_
list of entries in least-recently used order.
HashCrc32< T > hash
Select a hashing method.
bool touch_if_exists(const Key &key) noexcept
touch pair in LRU cache for key. Returns true if it exists.
size_t size() const noexcept
return number of items in LRU cache
typename std::unordered_map< Key, ListIterator, std::hash< Key >, std::equal_to< Key >, typename Alloc::template rebind< std::pair< const Key, ListIterator > >::other > Map
size_t size() const noexcept
return number of items in LRU cache
typename std::list< thrill::data::ByteBlock *, thrill::mem::FixedPoolAllocator< thrill::data::ByteBlock * > > List