LruCache<K, V> structv0.1.0
pub struct LruCache<K, V> {
// private fields
}
Move-only.
Type parameters
| Name | Description |
|---|---|
K | the key type, which the hash and equality functions take |
V | the 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.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
capacity | uint | how many entries it keeps |
hash | func(K, uint64, uint64) -> uint64 | how a key is hashed, taking the key and two seed words |
equals | func(K, K) -> bool | whether two keys are the same |
Returns: the cache
Methods
Length
pub func Length(&self) -> uint
How many entries it holds.
Returns: the count
Capacity
pub func Capacity(&self) -> uint
How many entries it keeps before evicting.
Returns: the capacity
Contains
pub func Contains(&self, key: K) -> bool
Whether key has an entry, without counting as a use.
| Name | Type | Description |
|---|---|---|
key | K | the key |
Returns: whether it is cached
Get
pub func Get(&var self, key: K) -> V?
A copy of the value for key, which becomes the most recently used.
| Name | Type | Description |
|---|---|---|
key | K | the key |
Returns: the value, or none when it is not cached
Peek
pub func Peek(&self, key: K) -> V?
A copy of the value for key, without counting as a use.
| Name | Type | Description |
|---|---|---|
key | K | the key |
Returns: the value, or none when it is not cached
Put
pub func Put(&var self, key: K, value: V) -> K?
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.
value is moved in. A value it replaces, and the value of an evicted entry, are destroyed once the new one is
in place. Terminates the program when the storage for a new entry cannot be had.
| Name | Type | Description |
|---|---|---|
key | K | the key |
value | V | the value |
Returns: the key that was evicted, if one was
Remove
pub func Remove(&var self, key: K) -> V?
Takes the entry for key out of the cache.
| Name | Type | Description |
|---|---|---|
key | K | the key |
Returns: its value, or none when it was not cached
Clear
pub func Clear(&var self)
Forgets every entry, keeping the storage.
KeysByRecency
pub func KeysByRecency(&self, into: var K[..]) -> uint
The keys from the most to the least recently used.
| Name | Type | Description |
|---|---|---|
into | var K[..] | receives the keys in that order, as many as fit |
Returns: how many were written