mirror of
https://github.com/NixOS/nix
synced 2025-06-30 07:33:16 +02:00
libutil: Format lru-cache.hh
Rip off the band-aid for further refactors. The diff is very small, so it makes to get it out of the way first.
This commit is contained in:
parent
e088ab3eaf
commit
90d70aa4c9
2 changed files with 14 additions and 6 deletions
|
@ -360,7 +360,6 @@
|
||||||
''^src/libutil/linux/namespaces\.cc$''
|
''^src/libutil/linux/namespaces\.cc$''
|
||||||
''^src/libutil/logging\.cc$''
|
''^src/libutil/logging\.cc$''
|
||||||
''^src/libutil/include/nix/util/logging\.hh$''
|
''^src/libutil/include/nix/util/logging\.hh$''
|
||||||
''^src/libutil/include/nix/util/lru-cache\.hh$''
|
|
||||||
''^src/libutil/memory-source-accessor\.cc$''
|
''^src/libutil/memory-source-accessor\.cc$''
|
||||||
''^src/libutil/include/nix/util/memory-source-accessor\.hh$''
|
''^src/libutil/include/nix/util/memory-source-accessor\.hh$''
|
||||||
''^src/libutil/include/nix/util/pool\.hh$''
|
''^src/libutil/include/nix/util/pool\.hh$''
|
||||||
|
|
|
@ -25,21 +25,28 @@ private:
|
||||||
using Data = std::map<Key, std::pair<LRUIterator, Value>>;
|
using Data = std::map<Key, std::pair<LRUIterator, Value>>;
|
||||||
using LRU = std::list<typename Data::iterator>;
|
using LRU = std::list<typename Data::iterator>;
|
||||||
|
|
||||||
struct LRUIterator { typename LRU::iterator it; };
|
struct LRUIterator
|
||||||
|
{
|
||||||
|
typename LRU::iterator it;
|
||||||
|
};
|
||||||
|
|
||||||
Data data;
|
Data data;
|
||||||
LRU lru;
|
LRU lru;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
LRUCache(size_t capacity) : capacity(capacity) { }
|
LRUCache(size_t capacity)
|
||||||
|
: capacity(capacity)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert or upsert an item in the cache.
|
* Insert or upsert an item in the cache.
|
||||||
*/
|
*/
|
||||||
void upsert(const Key & key, const Value & value)
|
void upsert(const Key & key, const Value & value)
|
||||||
{
|
{
|
||||||
if (capacity == 0) return;
|
if (capacity == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
erase(key);
|
erase(key);
|
||||||
|
|
||||||
|
@ -64,7 +71,8 @@ public:
|
||||||
bool erase(const Key & key)
|
bool erase(const Key & key)
|
||||||
{
|
{
|
||||||
auto i = data.find(key);
|
auto i = data.find(key);
|
||||||
if (i == data.end()) return false;
|
if (i == data.end())
|
||||||
|
return false;
|
||||||
lru.erase(i->second.first.it);
|
lru.erase(i->second.first.it);
|
||||||
data.erase(i);
|
data.erase(i);
|
||||||
return true;
|
return true;
|
||||||
|
@ -77,7 +85,8 @@ public:
|
||||||
std::optional<Value> get(const Key & key)
|
std::optional<Value> get(const Key & key)
|
||||||
{
|
{
|
||||||
auto i = data.find(key);
|
auto i = data.find(key);
|
||||||
if (i == data.end()) return {};
|
if (i == data.end())
|
||||||
|
return {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move this item to the back of the LRU list.
|
* Move this item to the back of the LRU list.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue