November 20, 2023
A C++ ordered map implementation designed to power an LRU Cache simulation.
I developed this library to efficiently simulate a Least Recently Used (LRU) Cache, addressing a fundamental limitation of standard C++ maps. While std::map and std::unordered_map offer fast lookups, they cannot natively track element usage history or maintain insertion order. This project implements a custom data structure that combines the speed of a hash map with the ordering capabilities of a linked list, delivering constant-time access and sequence management—critical requirements for cache eviction policies. I implemented the DoublyLinkedList from scratch instead of wrapping std::list to have full control over memory management and iterator semantics, demonstrating deep understanding of smart pointers and low-level pointer manipulation.
find, insert) while strictly enforcing the order of elements.The Challenge: The core difficulty was implementing the "promote on access" behavior required for an LRU cache without degrading performance. In a naive implementation, moving an accessed element to the front of the queue would involve removing and re-inserting the data, which triggers expensive memory allocation and deallocation. I needed a way to reorder elements dynamically while ensuring all operations remained strictly O(1).
The Solution: I engineered a composite structure using a Hash Map pointing to nodes in a custom Doubly Linked List.
splice-like operation that unlinks a node from its current position and relinks it to the list head by strictly updating pointers.