fixed_size_hash_set.rs

Overview

The fixed_size_hash_set.rs file defines a generic data structure named FixedSizeHashSet<T>, which implements a hash set with a fixed capacity. This structure maintains a collection of unique elements (T) with a limited size and supports eviction of the oldest inserted elements when the capacity is reached. It is optimized for rapid membership checks and insertion, while automatically managing storage size by removing older entries in a round-robin fashion.

This file targets scenarios where a bounded set of unique items must be maintained efficiently, such as caching finalized blocks in a blockchain system or managing a limited memory footprint for sets of identifiers.


Detailed Descriptions

FixedSizeHashSet<T> Struct

Purpose

FixedSizeHashSet<T> is a fixed-capacity hash set that stores unique elements with efficient membership queries, and evicts the oldest elements in insertion order when the capacity is exceeded.

Fields

Type Constraints


Implementation Details

new(capacity: usize) -> FixedSizeHashSet<T>

Creates a new FixedSizeHashSet with the specified capacity.

contains(&self, id: &T) -> bool

Checks if the set contains the specified element.

insert(&mut self, id: T)

Inserts a new element into the set. If the element already exists, the method returns immediately without altering the set. If insertion causes the set to exceed its capacity, the oldest element (based on insertion order) is evicted to make room for the new element.


Debug Trait Implementation

The file implements the std::fmt::Debug trait for FixedSizeHashSet<T>, which allows formatted debugging output.


Important Implementation Notes


Interaction with Other System Components

This file is designed to serve as a utility for managing collections of unique items with bounded memory. It is likely used in contexts where:

Interaction with storage or blockchain logic would involve:


Mermaid Diagram: Structure of FixedSizeHashSet<T>

classDiagram
class FixedSizeHashSet~T~ {
-capacity: usize
-blocks: HashSet~T~
-eviction_order: Vec~T~
-eviction_order_cursor: usize
+new(capacity: usize): Self
+contains(id: &T): bool
+insert(id: T)
+fmt(f: &mut Formatter): fmt::Result
}

The diagram shows the internal fields and the main public methods of the FixedSizeHashSet<T> struct. The generic type T is constrained by traits for hashing, equality, and cloning.