Communication Library for Autonomous Systems v1.0
Reliable and secure communication library for autonomous vehicle systems
Loading...
Searching...
No Matches
static_size_hashed_cache.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <stdexcept>
6
7#include "api/util/debug.h"
8
15template <typename V, size_t N = 1000> class StaticSizeHashedCache {
16public:
21 StaticSizeHashedCache() { _occupied.fill(false); }
22
32 void add(long int key, V value) {
33 db<StaticSizeHashedCache>(TRC) << "[StaticSizeHashedCache] add called for key: " << key << "\n";
34 size_t index = hash(key);
35
36 for (size_t i = 0; i < N; ++i) {
37 size_t current_index = (index + i) % N;
38 if (!_occupied[current_index] || _keys[current_index] == key) {
39 _keys[current_index] = key;
40 _values[current_index] = value;
41 _occupied[current_index] = true;
42 return;
43 }
44 }
45
46 throw std::runtime_error("Cache is full");
47 }
48
55 V *get(long int key) {
56 db<StaticSizeHashedCache>(TRC) << "[StaticSizeHashedCache] get called for key: " << key << "\n";
57 size_t index = hash(key);
58
59 for (size_t i = 0; i < N; ++i) {
60 size_t current_index = (index + i) % N;
61 if (!_occupied[current_index]) {
62 return nullptr;
63 }
64 if (_keys[current_index] == key) {
65 return &_values[current_index];
66 }
67 }
68
69 return nullptr;
70 }
71
78 bool contains(long int key) const {
79 db<StaticSizeHashedCache>(TRC) << "[StaticSizeHashedCache] contains called for key: " << key << "\n";
80 size_t index = hash(key);
81
82 for (size_t i = 0; i < N; ++i) {
83 size_t current_index = (index + i) % N;
84 if (!_occupied[current_index]) {
85 return false;
86 }
87 if (_keys[current_index] == key) {
88 return true;
89 }
90 }
91
92 return false;
93 }
94
100 template <typename F>
101 void for_each(F&& fn) {
102 for (size_t i = 0; i < N; ++i) {
103 if (_occupied[i]) {
104 fn(_keys[i], _values[i]);
105 }
106 }
107 }
108
109private:
115 size_t hash(long int key) const {
116 return static_cast<unsigned long long>(key) % N;
117 }
118
119 std::array<long int, N> _keys;
120 std::array<V, N> _values;
121 std::array<bool, N> _occupied;
122};
A hash cache with a static size, using linear probing for collision resolution.
Definition static_size_hashed_cache.h:15
void for_each(F &&fn)
Iterate over all occupied entries and apply a functor. The functor receives (key, value&) as paramete...
Definition static_size_hashed_cache.h:101
void add(long int key, V value)
Adds a key-value pair to the cache. If the key already exists, its value is updated....
Definition static_size_hashed_cache.h:32
V * get(long int key)
Retrieves a pointer to the value associated with a given key. It uses linear probing to find the key.
Definition static_size_hashed_cache.h:55
StaticSizeHashedCache()
Constructs a new StaticSizeHashedCache object. Initializes the cache as empty.
Definition static_size_hashed_cache.h:21
bool contains(long int key) const
Determines if a key is already in the data structure. It uses linear probing to find the key.
Definition static_size_hashed_cache.h:78
Select_Debug<(Traits< T >::debugged &&Traits< Debug >::error)> db(Debug_Error l)
Definition debug.h:166
@ TRC
Definition debug.h:231