fixed_size_hash_map.rs

Overview

This file implements a generic, fixed-capacity hash map data structure named FixedSizeHashMap. The primary purpose of this structure is to maintain a collection of key-value pairs with a strict size limit (capacity). When the map reaches its capacity and a new element is inserted, the map evicts the oldest entry in insertion order to make space for the new one. This eviction strategy ensures a bounded memory footprint and predictable resource usage, suitable for caching or limited storage scenarios.

The map uses an internal HashMap for fast key-based lookups and a Vec to track the insertion order for eviction purposes. The eviction is a simple cyclic buffer approach, which allows efficient replacement of entries without the overhead of complex data structures.

Struct: FixedSizeHashMap<K, V>

Description

A fixed-capacity hash map supporting insertions, lookups, and a simple eviction policy that removes the oldest entry when capacity is exceeded.

Generic Parameters

Fields

Implementation Details

Methods

new(capacity: usize) -> Self

Constructs a new FixedSizeHashMap with the specified maximum capacity.

contains_key(&self, id: &K) -> bool

Checks if the map contains a given key.

insert(&mut self, id: K, value: V)

Inserts a new key-value pair into the map. If the capacity is exceeded, evicts the oldest entry.

get<Q>(&self, k: &Q) -> Option<&V>

Generic key lookup supporting borrowed forms of K.

blocks(&self) -> &HashMap<K, V>

Returns a reference to the internal HashMap storing the key-value pairs.

Trait Implementations

Debug

Important Implementation Notes

Interaction with Other System Components

Mermaid Diagram

classDiagram
class FixedSizeHashMap {
-capacity: usize
-blocks: HashMap<K, V>
-eviction_order: Vec<K>
-eviction_order_cursor: usize
+new(capacity)
+contains_key(id)
+insert(id, value)
+get(k)
+blocks()
+fmt()
}