Files
MicrOBU/obu-firmware/external/vanetza-idf/vanetza/common/lru_cache.hpp
T
Ashin Walpola d107534eb2 Keep vanetza-idf in obu-firmware, so a plain clone builds the firmware
obu-firmware builds against the vanetza-idf C-ITS library, which until now
came from the colleague's microbu-esp32c5 tree beside the repository and was
not tracked here, so a clone of this repository could not build the firmware
it ships. The library alone is now part of obu-firmware, as
obu-firmware/external/vanetza-idf: their external/vanetza-idf at commit
cf4b99f, unchanged (9775 files; see its PROVENANCE.md). CMake takes it from
there by default; -DVANETZA_IDF_DIR still points the build elsewhere.

The rest of the colleague's tree (their own VAM firmware, PKI tooling,
station-link Python tools, the V2X2MAP bridge) stays out of this repository
and gitignored; nothing is pushed to their repository. NOTES.md, docs/06,
TODO.md and the pcap verifier's usage line point at the new location.
2026-09-24 10:56:05 +02:00

102 lines
2.5 KiB
C++

#ifndef LRU_CACHE_HPP_CII58WXX
#define LRU_CACHE_HPP_CII58WXX
#include <cassert>
#include <functional>
#include <list>
#include <unordered_map>
namespace vanetza
{
/**
* \brief Least-Recently-Used cache
*
* If an entry is accessed which is not yet cached, it will created
* by invocation of the generator function.
*
* \tparam KEY key type
* \tparam VALUE value_type, i.e. type of cache entries
* \tparam GENERATOR generator function for creating values from key
*/
template<typename KEY, typename VALUE, typename GENERATOR = std::function<VALUE(const KEY&)>>
class LruCache
{
public:
using generator = GENERATOR;
using key_type = KEY;
using value_type = VALUE;
/**
* \param g user-defined generator function
* \param capacity maximum number of cache entries
*/
LruCache(generator g, std::size_t capacity) :
m_generator(g),
m_capacity(capacity)
{
}
/**
* \brief Access a cache entry
*
* If cache entry does not yet exist it will be created.
* The least recently accessed entry might get dropped.
*
* \param key identifier of cache entry
* \return cached value
*/
value_type& operator[](const key_type& key)
{
auto found = m_cache.find(key);
if (found == m_cache.end()) {
return add(key);
} else {
return refresh(found);
}
}
private:
using list_type = std::list<key_type>;
using entry_type = std::pair<value_type, typename list_type::iterator>;
using map_type = std::unordered_map<key_type, entry_type>;
value_type& add(const key_type& k)
{
if (m_cache.size() >= m_capacity) {
remove();
}
m_index.emplace_front(k);
entry_type entry = std::make_pair(m_generator(k), m_index.begin());
auto insertion = m_cache.insert(std::make_pair(k, std::move(entry)));
assert(insertion.second == true);
return insertion.first->second.first;
}
value_type& refresh(typename map_type::iterator found)
{
m_index.splice(m_index.begin(), m_index, found->second.second);
assert(m_index.begin() == found->second.second);
return found->second.first;
}
void remove()
{
if (!m_index.empty()) {
m_cache.erase(m_index.back());
m_index.pop_back();
}
}
generator m_generator;
std::size_t m_capacity;
list_type m_index;
map_type m_cache;
};
} // namespace vanetza
#endif /* LRU_CACHE_HPP_CII58WXX */