17 releases

0.2.5 Jun 22, 2020
0.2.4 Jun 21, 2020
0.2.1 May 21, 2020
0.1.19 May 15, 2020

#1400 in Algorithms

Download history 49/week @ 2024-07-22 58/week @ 2024-07-29 124/week @ 2024-08-05 57/week @ 2024-08-12 54/week @ 2024-08-19 67/week @ 2024-08-26 95/week @ 2024-09-02 31/week @ 2024-09-09 69/week @ 2024-09-16 117/week @ 2024-09-23 126/week @ 2024-09-30 7/week @ 2024-10-07 56/week @ 2024-10-14 67/week @ 2024-10-21 15/week @ 2024-10-28 47/week @ 2024-11-04

185 downloads per month

MIT/Apache

11KB
217 lines

lfu-cache

A rust implementation of a Least Frequently Used (LFU) cache.

It supports insertions and retrievals, both of which are performed in constant time. In the event of tie between multiple least frequently used entries, the least recently used entry is evicted.

Usage:

 extern crate lfu;
 use lfu::LFUCache;

 fn main() {
     let mut lfu = LFUCache::new(2).unwrap(); //initialize an lfu with a maximum capacity of 2 entries
     lfu.set(2, 2);
     lfu.set(3, 3);
     lfu.set(3, 30);
    
    
    //We're at fully capacity. First purge (2,2) since it's the least-frequently-used entry, then insert the current entry
     lfu.set(4,4); 
    
     assert_eq!(lfu.get(&2), None);
     assert_eq!(lfu.get(&3), Some(&30));
}

Install

https://crates.io/crates/lfu


lib.rs:

An efficient Least Frequently Used Cache implementation.

It supports insertions and retrievals, both of which are performed in constant time. In the event of tie between two least frequently used entries, the least recently used entry is evicted.

Examples

extern crate lfu;
use lfu::LFUCache;

let mut lfu = LFUCache::with_capacity(2).unwrap(); //initialize an lfu with a maximum capacity of 2 entries
lfu.set(2, 2);
lfu.set(3, 3);
lfu.set(3, 30);
lfu.set(4,4); //We're at fully capacity. First purge (2,2) since it's the least-frequently-used entry, then insert the current entry
assert_eq!(lfu.get(&2), None);
assert_eq!(lfu.get(&3), Some(&30));

Dependencies

~110KB