Files
Library/include/Void/Containers/LRU.hpp
T
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

100 lines
2.9 KiB
C++

#pragma once
#include <cstddef>
#include <list>
#include <optional>
#include <stdexcept>
#include <unordered_map>
#include <utility>
namespace Void::LRU
{
// ── LruCache<K, V> ────────────────────────────────────────────────────────────
// Fixed-capacity LRU cache. get() promotes to MRU; put() evicts LRU when full.
// All operations are O(1) amortized via a doubly-linked list + hash map.
template <typename K, typename V>
class LruCache
{
using List = std::list<std::pair<K, V>>;
using Map = std::unordered_map<K, typename List::iterator>;
public:
explicit LruCache(std::size_t capacity)
: capacity_{capacity}
{
if (capacity == 0) throw std::invalid_argument{"capacity must be > 0"};
}
[[nodiscard]] std::size_t capacity() const noexcept { return capacity_; }
[[nodiscard]] std::size_t size() const noexcept { return map_.size(); }
[[nodiscard]] bool empty() const noexcept { return map_.empty(); }
[[nodiscard]] bool full() const noexcept { return map_.size() == capacity_; }
// Retrieve a value. Returns nullopt on miss; promotes the entry on hit.
[[nodiscard]] std::optional<V> get(const K& key)
{
auto it = map_.find(key);
if (it == map_.end()) return std::nullopt;
list_.splice(list_.begin(), list_, it->second);
return it->second->second;
}
// Peek without changing recency order.
[[nodiscard]] const V* peek(const K& key) const
{
auto it = map_.find(key);
if (it == map_.end()) return nullptr;
return &it->second->second;
}
// Insert or update. If the key already exists its recency is updated.
// If the cache is full the least-recently-used entry is evicted.
void put(const K& key, V value)
{
auto it = map_.find(key);
if (it != map_.end()) {
it->second->second = std::move(value);
list_.splice(list_.begin(), list_, it->second);
return;
}
if (map_.size() == capacity_) {
map_.erase(list_.back().first);
list_.pop_back();
}
list_.emplace_front(key, std::move(value));
map_[key] = list_.begin();
}
bool erase(const K& key)
{
auto it = map_.find(key);
if (it == map_.end()) return false;
list_.erase(it->second);
map_.erase(it);
return true;
}
void clear() noexcept
{
list_.clear();
map_.clear();
}
[[nodiscard]] bool contains(const K& key) const
{
return map_.contains(key);
}
// Iterate entries in MRU→LRU order (const).
[[nodiscard]] const List& entries() const noexcept { return list_; }
private:
std::size_t capacity_;
List list_;
Map map_;
};
} // namespace Void::LRU