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: &LruCache<K, V>) -> uint
How many entries it holds.
Returns: the count
Capacity
pub func Capacity(self: &LruCache<K, V>) -> uint
How many entries it keeps before evicting.
Returns: the capacity
Contains
pub func Contains(self: &LruCache<K, V>, 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(self: &var LruCache<K, V>, 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: &LruCache<K, V>, 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(
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.
| Name | Type | Description |
|---|---|---|
key | K | the key |
value | V | the value |
Returns: the key that was evicted, if one was, or why there was no room
Remove
pub func Remove(self: &var LruCache<K, V>, 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(self: &var LruCache<K, V>)
Forgets every entry, keeping the storage.
KeysByRecency
pub func KeysByRecency(
self: &LruCache<K, V>,
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