boat
C++23 geospatial library
Loading...
Searching...
No Matches
lru.hpp
Go to the documentation of this file.
1
// Andrew Naplavkov
2
3
#ifndef BOAT_GUI_CACHES_LRU_HPP
4
#define BOAT_GUI_CACHES_LRU_HPP
5
6
#include <boat/detail/linked_hash_map.hpp>
7
#include <
boat/gui/caches/cache.hpp
>
8
#include <mutex>
9
10
namespace
boat::gui::caches
{
11
12
class
lru
:
public
cache
{
13
std::mutex guard_;
14
size_t
capacity_;
15
linked_hash_map<any_hashable, std::any> data_;
16
17
public
:
18
explicit
lru
(
size_t
capacity) : capacity_{capacity} {}
19
20
std::any
get
(any_hashable
const
& key)
override
21
{
22
auto
lock = std::lock_guard{guard_};
23
if
(
auto
it = data_.find(key); it != data_.end()) {
24
data_.transfer(it, data_.end());
25
return
it->second;
26
}
27
return
{};
28
}
29
30
void
put
(any_hashable key, std::any val)
override
31
{
32
auto
lock = std::lock_guard{guard_};
33
data_.insert(data_.end(), {std::move(key), std::move(val)});
34
if
(data_.size() > capacity_)
35
data_.erase(data_.begin());
36
}
37
};
38
39
}
// namespace boat::gui::caches
40
41
#endif
// BOAT_GUI_CACHES_LRU_HPP
cache.hpp
boat::gui::caches::lru::put
void put(any_hashable key, std::any val) override
Definition
lru.hpp:30
boat::gui::caches::lru::get
std::any get(any_hashable const &key) override
Returns an empty std::any on a miss.
Definition
lru.hpp:20
boat::gui::caches::lru::lru
lru(size_t capacity)
Definition
lru.hpp:18
boat::gui::caches
Definition
cache.hpp:11
boat::gui::caches::cache
Definition
cache.hpp:13
include
boat
gui
caches
lru.hpp
Generated by
1.15.0