49#define MIN_ACCESS_COUNT 1
50#define TOMBSTONE_KEY 0
66 for(
size_t i = 0; i < map->
size; i++)
90 size_t old_count = map->
count;
97 map->
table = old_table;
104 for(
size_t i = 0; i < map->
size; i++)
108 size_t idx = old_table[i].
key % map->
size;
112 map->
table[idx] = old_table[i];
141 if(target_count_out == 0) target_count_out = map->
target_count;
144 if(map->
count <= target_count_out)
return 0;
146 size_t entries_to_remove = map->
count - target_count_out;
150 size_t histogram[256] = {0};
152 for(
size_t i = 0; i < map->
size; i++)
161 size_t cumulative = 0;
163 for(
int i = 0; i < 256; i++)
165 cumulative += histogram[i];
166 if(cumulative >= entries_to_remove)
176 for(
size_t i = 0; i < map->
size && removed < entries_to_remove; i++)
187 map->
count -= removed;
189#ifdef STATIC_LRU_ENABLE_STATS
190 map->eviction_count += removed;
191 map->eviction_cycles++;
210 if(!map)
return NULL;
226#ifdef STATIC_LRU_ENABLE_STATS
227 map->total_lookups = 0;
229 map->total_inserts = 0;
230 map->eviction_count = 0;
231 map->eviction_cycles = 0;
258#ifdef STATIC_LRU_ENABLE_STATS
259 map->total_inserts++;
263 size_t idx = key % map->
size;
286#ifdef STATIC_LRU_ENABLE_STATS
287 map->total_lookups++;
290 size_t idx = key % map->
size;
301#ifdef STATIC_LRU_ENABLE_STATS
308 idx = (idx + 1) % map->
size;
316 size_t idx = key % map->
size;
320 if(map->
table[idx].
key == key)
return true;
322 idx = (idx + 1) % map->
size;
341#ifdef STATIC_LRU_ENABLE_STATS
344 if(map->total_lookups == 0)
return 0.0;
346 return (
double)map->cache_hits / (double)map->total_lookups;
351 map->total_lookups = 0;
353 map->total_inserts = 0;
354 map->eviction_count = 0;
355 map->eviction_cycles = 0;
static void rehash_in_place(static_lru_hash_map_t *map)
Rehashes all entries in place after eviction.
void static_lru_free_map(static_lru_hash_map_t *map)
Frees all memory associated with a static LRU hash map.
static void age_access_counts(static_lru_hash_map_t *map)
Ages all access counts by halving them.
void static_lru_age_counts(static_lru_hash_map_t *map)
Manually ages all access counts.
size_t static_lru_free_slots(const static_lru_hash_map_t *map)
Returns the number of free slots available.
static_lru_hash_map_t * static_lru_create_map(size_t size)
Creates a new static LRU hash map with fixed size.
size_t static_lru_evict(static_lru_hash_map_t *map, size_t entries_to_keep)
Manually triggers eviction of least-used entries.
bool static_lru_lookup_map(static_lru_hash_map_t *map, uint64_t key, uint64_t *out_value)
Looks up a value by key in the static LRU hash map.
double static_lru_load_factor(const static_lru_hash_map_t *map)
Returns the current load factor of the map.
#define TOMBSTONE_KEY
Marker for empty/deleted slots (key=0 reserved).
static size_t evict_lru_entries(static_lru_hash_map_t *map, size_t target_count_out)
Evicts least-frequently-used entries to make room for new insertions.
bool static_lru_insert_map(static_lru_hash_map_t *map, uint64_t key, uint64_t value)
Inserts a key-value pair into the static LRU hash map.
#define MIN_ACCESS_COUNT
New entries start with this access count.
bool static_lru_contains_key(const static_lru_hash_map_t *map, uint64_t key)
Checks if a key exists in the map WITHOUT updating access count.
Static-memory hash map with LRU-like eviction for fixed RAM usage.
#define STATIC_LRU_TARGET_LOAD_FACTOR
Evict down to 75% capacity.
#define STATIC_LRU_MIN_SIZE
Minimum map size to prevent edge cases.
#define STATIC_LRU_EVICTION_LOAD_FACTOR
Default configuration constants for the static LRU hash map.
#define STATIC_LRU_AGING_INTERVAL
Age access counts every N operations.
Single key/value slot with access tracking for the static LRU hash map.
uint8_t access_count
Access frequency counter (0-255, saturates at 255).
uint64_t value
Associated value payload (64-bit).
uint64_t key
Stored key (64-bit). 0 indicates an empty slot.
Fixed-size hash map with LRU-like eviction for bounded memory usage.
size_t max_count
Eviction trigger threshold (size * EVICTION_LOAD_FACTOR).
uint32_t age_counter
Operations since last aging.
size_t target_count
Target count after eviction (size * TARGET_LOAD_FACTOR).
size_t count
Number of active (filled) entries.
lru_kv_pair_t * table
Array of key/value slots of length == size.
size_t size
Allocated slot capacity (FIXED at creation).
uint32_t _padding
Padding for alignment.