pub struct LruBTreeMap<Key, Value> { /* private fields */ }
Expand description

A Least Recently Used map with fixed capacity that stores keys using a BTreeMap internally. Inserting and querying has similar performance to using a BTreeMap, but internally this data structure keeps track of the order in which the keys were last touched.

When inserting a new key and the map is at-capacity, the least recently used key will be evicted to make room for the new key.

To avoid unsafe, this crate must store each entry’s key twice. This means that Key must implement Clone. If you’re using expensive-to-clone keys, consider wrapping the key in an Rc/Arc or using an alternate LRU crate.

Implementations

Creates a new map with the maximum capacity.

Panics

Panics if capacity is <= 1 or > u32::MAX.

Returns the stored value for key, if present.

This function touches the key, making it the most recently used key.

Returns the stored value for key, if present.

This function does not touch the key, preserving its current position in the lru cache.

Returns an EntryRef for key, if present.

This function does not touch the key, preserving its current position in the lru cache. The EntryRef can touch the key, depending on which functions are used.

use lrumap::{LruBTreeMap, LruMap, Removed};

let mut lru = LruBTreeMap::new(3);
lru.push(1, 1);
lru.push(2, 2);
lru.push(3, 3);

// The cache has been updated once since entry 2 was touched.
let mut entry = lru.entry(&2).unwrap();
assert_eq!(entry.staleness(), 1);
// Peeking the value will not update the entry's position.
assert_eq!(entry.peek_value(), &2);
assert_eq!(entry.staleness(), 1);
// Querying the value or touching the entry will move it to the
// front of the cache.
assert_eq!(entry.value(), &2);
assert_eq!(entry.staleness(), 0);

assert_eq!(lru.head().unwrap().key(), &2);

Inserts value for key into this map. If a value is already stored for this key, Removed::PreviousValue is returned with the previously stored value. If no value is currently stored and the map is full, the least recently used entry will be returned in Removed::Evicted. Otherwise, None will be returned.

This function touches the key, making it the most recently used key.

use lrumap::{LruBTreeMap, LruMap, Removed};

let mut lru = LruBTreeMap::new(3);
lru.push(1, 1);
lru.push(2, 2);
lru.push(3, 3);

// The cache is now full. The next push will evict an entry.
let removed = lru.push(4, 4);
assert_eq!(removed, Some(Removed::Evicted(1, 1)));

// This leaves the cache with 4 as the most recent key, and 2 as the
// least recent key.
assert_eq!(lru.head().unwrap().key(), &4);
assert_eq!(lru.tail().unwrap().key(), &2);

Pushes all items from iterator into this map. If there are more entries in the iterator than capacity remaining, keys will be evicted as needed.

This function is equivalent to a for loop calling Self::push().

use lrumap::{LruBTreeMap, LruMap};

let mut lru = LruBTreeMap::new(3);
lru.extend([(1, 1), (2, 2), (3, 3), (4, 4)]);

assert_eq!(lru.head().unwrap().key(), &4);
assert_eq!(lru.tail().unwrap().key(), &2);

Returns the most recently touched entry with a key within range.

This function uses BTreeMap::range to identify all entries that match the given range. For each returned entry, the entry’s staleness is compared, and the least stale entry is returned. If no keys match the range, None is returned.

This function does not touch any keys, preserving the current order of the lru cache. The EntryRef returned can be used to peek, touch, or remove the entry.

use lrumap::LruBTreeMap;

let mut lru = LruBTreeMap::new(5);
lru.extend([(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]);

assert_eq!(lru.most_recent_in_range(2..=4).unwrap().key(), &4);
// Change the order by retrieving key 2.
lru.get(&2);
assert_eq!(lru.most_recent_in_range(2..=4).unwrap().key(), &2);

Returns the most recently touched entry with a key within range that passes the condition check.

This function uses BTreeMap::range to identify all entries that match the given range. Each key and value that matches is passed to condition. For each entry where condition returns true, the staleness is compared, and the least stale entry is returned. If no keys match the range, None is returned.

This function does not touch any keys, preserving the current order of the lru cache. The EntryRef returned can be used to peek, touch, or remove the entry.

use lrumap::LruBTreeMap;

let mut lru = LruBTreeMap::<u32, u16>::new(5);
lru.extend([(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]);

let condition = |key: &u32, value: &u16| key == &3 || value == &4;
assert_eq!(
    lru.most_recent_in_range_where(2..=4, condition)
        .unwrap()
        .key(),
    &4
);

// Change the order by retrieving key 2. However, 2 doesn't meet the
// condition, so the result is unchanged.
lru.get(&2);
assert_eq!(
    lru.most_recent_in_range_where(2..=4, condition)
        .unwrap()
        .key(),
    &4
);

// Request 3, moving it to the front. Since 3 matches the condition, the
// result is now 3.
lru.get(&3);
assert_eq!(
    lru.most_recent_in_range_where(2..=4, condition)
        .unwrap()
        .key(),
    &3
);

Trait Implementations

Formats the value using the given formatter. Read more
Which kind of iterator are we turning this into?
The type of the elements being iterated over.
Creates an iterator from a value. Read more
Creates a new map with the maximum capacity. Read more
Returns the number of keys present in this map.
Returns a reference to the most recently used key.
Returns a reference to the least recently used key.
Returns an iterator over the keys and values in order from most recently touched to least recently touched. Read more
Returns the stored value for key, if present. Read more
Returns the stored value for key, if present. Read more
Returns an EntryRef for key, if present. Read more
Inserts value for key into this map. If a value is already stored for this key, Removed::PreviousValue is returned with the previously stored value. If no value is currently stored and the map is full, the least recently used entry will be returned in Removed::Evicted. Otherwise, None will be returned. Read more
Pushes all items from iterator into this map. If there are more entries in the iterator than capacity remaining, keys will be evicted as needed. Read more
Retruns true if this map contains no keys.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.