Struct std::collections::lru_cache::LruCacheExperimental [-]  [+] [src]

pub struct LruCache<K, V> {
    // some fields omitted
}

An LRU Cache.

Methods

impl<K: Hash + Eq, V> LruCache<K, V>

fn new(capacity: uint) -> LruCache<K, V>

Create an LRU Cache that holds at most capacity items.

Example

fn main() { use std::collections::LruCache; let mut cache: LruCache<int, &str> = LruCache::new(10); }
use std::collections::LruCache;
let mut cache: LruCache<int, &str> = LruCache::new(10);

fn put(&mut self, k: K, v: V)

Deprecated: Replaced with insert.

fn insert(&mut self, k: K, v: V) -> Option<V>

Inserts a key-value pair into the cache. If the key already existed, the old value is returned.

Example

fn main() { use std::collections::LruCache; let mut cache = LruCache::new(2); cache.insert(1i, "a"); cache.insert(2, "b"); assert_eq!(cache.get(&1), Some(&"a")); assert_eq!(cache.get(&2), Some(&"b")); }
use std::collections::LruCache;
let mut cache = LruCache::new(2);

cache.insert(1i, "a");
cache.insert(2, "b");
assert_eq!(cache.get(&1), Some(&"a"));
assert_eq!(cache.get(&2), Some(&"b"));

fn get(&mut self, k: &K) -> Option<&V>

Return a value corresponding to the key in the cache.

Example

fn main() { use std::collections::LruCache; let mut cache = LruCache::new(2); cache.insert(1i, "a"); cache.insert(2, "b"); cache.insert(2, "c"); cache.insert(3, "d"); assert_eq!(cache.get(&1), None); assert_eq!(cache.get(&2), Some(&"c")); }
use std::collections::LruCache;
let mut cache = LruCache::new(2);

cache.insert(1i, "a");
cache.insert(2, "b");
cache.insert(2, "c");
cache.insert(3, "d");

assert_eq!(cache.get(&1), None);
assert_eq!(cache.get(&2), Some(&"c"));

fn pop(&mut self, k: &K) -> Option<V>

Deprecated: Renamed to remove.

fn remove(&mut self, k: &K) -> Option<V>

Remove and return a value corresponding to the key from the cache.

Example

fn main() { use std::collections::LruCache; let mut cache = LruCache::new(2); cache.insert(2i, "a"); assert_eq!(cache.remove(&1), None); assert_eq!(cache.remove(&2), Some("a")); assert_eq!(cache.remove(&2), None); assert_eq!(cache.len(), 0); }
use std::collections::LruCache;
let mut cache = LruCache::new(2);

cache.insert(2i, "a");

assert_eq!(cache.remove(&1), None);
assert_eq!(cache.remove(&2), Some("a"));
assert_eq!(cache.remove(&2), None);
assert_eq!(cache.len(), 0);

fn capacity(&self) -> uint

Return the maximum number of key-value pairs the cache can hold.

Example

fn main() { use std::collections::LruCache; let mut cache: LruCache<int, &str> = LruCache::new(2); assert_eq!(cache.capacity(), 2); }
use std::collections::LruCache;
let mut cache: LruCache<int, &str> = LruCache::new(2);
assert_eq!(cache.capacity(), 2);

fn change_capacity(&mut self, capacity: uint)

Deprecated: Renamed to set_capacity.

fn set_capacity(&mut self, capacity: uint)

Change the number of key-value pairs the cache can hold. Remove least-recently-used key-value pairs if necessary.

Example

fn main() { use std::collections::LruCache; let mut cache = LruCache::new(2); cache.insert(1i, "a"); cache.insert(2, "b"); cache.insert(3, "c"); assert_eq!(cache.get(&1), None); assert_eq!(cache.get(&2), Some(&"b")); assert_eq!(cache.get(&3), Some(&"c")); cache.set_capacity(3); cache.insert(1i, "a"); cache.insert(2, "b"); assert_eq!(cache.get(&1), Some(&"a")); assert_eq!(cache.get(&2), Some(&"b")); assert_eq!(cache.get(&3), Some(&"c")); cache.set_capacity(1); assert_eq!(cache.get(&1), None); assert_eq!(cache.get(&2), None); assert_eq!(cache.get(&3), Some(&"c")); }
use std::collections::LruCache;
let mut cache = LruCache::new(2);

cache.insert(1i, "a");
cache.insert(2, "b");
cache.insert(3, "c");

assert_eq!(cache.get(&1), None);
assert_eq!(cache.get(&2), Some(&"b"));
assert_eq!(cache.get(&3), Some(&"c"));

cache.set_capacity(3);
cache.insert(1i, "a");
cache.insert(2, "b");

assert_eq!(cache.get(&1), Some(&"a"));
assert_eq!(cache.get(&2), Some(&"b"));
assert_eq!(cache.get(&3), Some(&"c"));

cache.set_capacity(1);

assert_eq!(cache.get(&1), None);
assert_eq!(cache.get(&2), None);
assert_eq!(cache.get(&3), Some(&"c"));

fn len(&self) -> uint

Return the number of key-value pairs in the cache.

fn is_empty(&self) -> bool

Returns whether the cache is currently empty.

fn clear(&mut self)

Clear the cache of all key-value pairs.

Trait Implementations

impl<K: Hash + Eq, V> Extend<(K, V)> for LruCache<K, V>

fn extend<T: Iterator<(K, V)>>(&mut self, iter: T)

impl<A: Show + Hash + Eq, B: Show> Show for LruCache<A, B>

fn fmt(&self, f: &mut Formatter) -> Result

Return a string that lists the key-value pairs from most-recently used to least-recently used.

impl<K, V> Drop for LruCache<K, V>

fn drop(&mut self)