Skip to content

feat(data-structures): add LRU cache#208

Merged
0xDevNinja merged 1 commit into
mainfrom
feat/lru-cache
Apr 28, 2026
Merged

feat(data-structures): add LRU cache#208
0xDevNinja merged 1 commit into
mainfrom
feat/lru-cache

Conversation

@0xDevNinja

Copy link
Copy Markdown
Owner

Summary

  • Adds LruCache<K, V> in src/data_structures/lru_cache.rs with new, get, put, len, is_empty, capacity, contains_key.
  • O(1) amortized get/put. Implemented with a Vec-backed slab of doubly-linked nodes (indices, not pointers) plus a HashMap<K, usize> for index lookup. Most-recently-used at the head, least-recently-used at the tail. Eviction recycles slab slots via a free-list. Entirely safe Rust.
  • capacity == 0 is documented and tested: every put returns the just-inserted value as evicted; the cache stays empty.

Test plan

  • cargo fmt --check
  • cargo clippy --all-targets -- -D warnings
  • cargo test (318 tests pass)
  • Unit tests cover empty cache, capacity-0, capacity-1 (newest only), basic put+get, LRU eviction order over 4 inserts into a 3-item cache, get promoting an entry to MRU, updating an existing key bumping it to MRU without eviction, get of an absent key returning None, and len / is_empty / capacity correctness.
  • quickcheck property test compares the implementation against a Vec<(K, V)>-backed brute-force LRU oracle (capacity 0..=8, ops capped at 50), checking get results, the live-key set, and stored values.

Closes #67

@0xDevNinja 0xDevNinja added this to the v0.2.0 — Foundations milestone Apr 28, 2026
@0xDevNinja 0xDevNinja merged commit c077d6a into main Apr 28, 2026
6 checks passed
@0xDevNinja 0xDevNinja deleted the feat/lru-cache branch April 28, 2026 17:26
A bounded-capacity key/value cache with O(1) amortized get and put. Uses
a Vec-backed slab of doubly-linked nodes plus a HashMap for index lookup,
keeping the implementation entirely in safe Rust.

Closes #67.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add LRU cache

1 participant