Time Efficient Least Frequently Used Cache
agentultra.com·15h
🦀Rust
Preview
Report Post

In the last post I wrote up a naive implementation of a Least Frequently Used cache. A better and, for a time, more common implementation would have used a tree with the min-heap property to give O(log n). In the conclusion I mentioned that there’s a way to implement one with O(1) time complexity for both operations at the cost of some extra memory. Here is such an implementation in C++23.

We’ll start with the same main function as before:

#include <cassert>
#include <cstdint>
#include <print>

int main() {
LFU<std::string, uint64_t> freq_cache(3);

freq_cache.set("foo", 11);
freq_cache.set("bar", 22);
freq_cache.set("baz", 33);

freq_cache.get("foo");
std::optional<uint64_t> foo = freq_cache.get("foo");
foo = fr...

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help