LruCache<K, V> structv0.1.0

Source
A map that holds at most capacity entries and evicts the least recently used.
pub struct LruCache<K, V> {
    // private fields
}

Move-only.

Type parameters

NameDescription
Kthe key type, which the hash and equality functions take
Vthe value type

Constructors

LruCache

pub func LruCache(
    allocator: Allocator,
    capacity: uint,
    hash: func(K, uint64, uint64) -> uint64,
    equals: func(K, K) -> bool
) -> LruCache<K, V>

An empty cache that holds at most capacity entries, at least one.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
capacityuinthow many entries it keeps
hashfunc(K, uint64, uint64) -> uint64how a key is hashed, taking the key and two seed words
equalsfunc(K, K) -> boolwhether two keys are the same

Returns: the cache

Source

Methods

Length

pub func Length(self: &LruCache<K, V>) -> uint

How many entries it holds.

Returns: the count

Source

Capacity

pub func Capacity(self: &LruCache<K, V>) -> uint

How many entries it keeps before evicting.

Returns: the capacity

Source

Contains

pub func Contains(self: &LruCache<K, V>, key: K) -> bool

Whether key has an entry, without counting as a use.

NameTypeDescription
keyKthe key

Returns: whether it is cached

Source

Get

pub func Get(self: &var LruCache<K, V>, key: K) -> V?

A copy of the value for key, which becomes the most recently used.

NameTypeDescription
keyKthe key

Returns: the value, or none when it is not cached

Source

Peek

pub func Peek(self: &LruCache<K, V>, key: K) -> V?

A copy of the value for key, without counting as a use.

NameTypeDescription
keyKthe key

Returns: the value, or none when it is not cached

Source

Put

pub func Put(
    self: &var LruCache<K, V>,
    key: K,
    value: V
) -> K? ! CollectionError

Caches value for key as the most recently used, replacing its value if it has one, and evicting the least recently used entry when the cache is full.

NameTypeDescription
keyKthe key
valueVthe value

Returns: the key that was evicted, if one was, or why there was no room

Source

Remove

pub func Remove(self: &var LruCache<K, V>, key: K) -> V?

Takes the entry for key out of the cache.

NameTypeDescription
keyKthe key

Returns: its value, or none when it was not cached

Source

Clear

pub func Clear(self: &var LruCache<K, V>)

Forgets every entry, keeping the storage.

Source

KeysByRecency

pub func KeysByRecency(
    self: &LruCache<K, V>,
    into: var K[..]
) -> uint

The keys from the most to the least recently used.

NameTypeDescription
intovar K[..]receives the keys in that order, as many as fit

Returns: how many were written

Source