From 3c45fe9e1d4ad4c27dd443bdb523befd3f0b96c2 Mon Sep 17 00:00:00 2001 From: Jonas Hietala Date: Thu, 24 Jul 2014 12:51:42 +0200 Subject: [PATCH] Documentation examples for LruCache. --- src/libstd/collections/lru_cache.rs | 86 +++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/libstd/collections/lru_cache.rs b/src/libstd/collections/lru_cache.rs index 45301737adb3d..e2aff529b7987 100644 --- a/src/libstd/collections/lru_cache.rs +++ b/src/libstd/collections/lru_cache.rs @@ -92,6 +92,13 @@ impl LruEntry { impl LruCache { /// Create an LRU Cache that holds at most `capacity` items. + /// + /// # Example + /// + /// ``` + /// use std::collections::LruCache; + /// let mut cache: LruCache = LruCache::new(10u); + /// ``` pub fn new(capacity: uint) -> LruCache { let cache = LruCache { map: HashMap::new(), @@ -106,6 +113,18 @@ impl LruCache { } /// Put a key-value pair into cache. + /// + /// # Example + /// + /// ``` + /// use std::collections::LruCache; + /// let mut cache = LruCache::new(2u); + /// + /// cache.put(1i, "a"); + /// cache.put(2, "b"); + /// assert_eq!(cache.get(&1), Some(&"a")); + /// assert_eq!(cache.get(&2), Some(&"b")); + /// ``` pub fn put(&mut self, k: K, v: V) { let (node_ptr, node_opt) = match self.map.find_mut(&KeyRef{k: &k}) { Some(node) => { @@ -137,6 +156,21 @@ impl LruCache { } /// Return a value corresponding to the key in the cache. + /// + /// # Example + /// + /// ``` + /// use std::collections::LruCache; + /// let mut cache = LruCache::new(2u); + /// + /// cache.put(1i, "a"); + /// cache.put(2, "b"); + /// cache.put(2, "c"); + /// cache.put(3, "d"); + /// + /// assert_eq!(cache.get(&1), None); + /// assert_eq!(cache.get(&2), Some(&"c")); + /// ``` pub fn get<'a>(&'a mut self, k: &K) -> Option<&'a V> { let (value, node_ptr_opt) = match self.map.find_mut(&KeyRef{k: k}) { None => (None, None), @@ -156,6 +190,20 @@ impl LruCache { } /// Remove and return a value corresponding to the key from the cache. + /// + /// # Example + /// + /// ``` + /// use std::collections::LruCache; + /// let mut cache = LruCache::new(2u); + /// + /// cache.put(2i, "a"); + /// + /// assert_eq!(cache.pop(&1), None); + /// assert_eq!(cache.pop(&2), Some("a")); + /// assert_eq!(cache.pop(&2), None); + /// assert_eq!(cache.len(), 0); + /// ``` pub fn pop(&mut self, k: &K) -> Option { match self.map.pop(&KeyRef{k: k}) { None => None, @@ -164,12 +212,50 @@ impl LruCache { } /// Return the maximum number of key-value pairs the cache can hold. + /// + /// # Example + /// + /// ``` + /// use std::collections::LruCache; + /// let mut cache: LruCache = LruCache::new(2u); + /// assert_eq!(cache.capacity(), 2); + /// ``` pub fn capacity(&self) -> uint { self.max_size } /// Change the number of key-value pairs the cache can hold. Remove /// least-recently-used key-value pairs if necessary. + /// + /// # Example + /// + /// ``` + /// use std::collections::LruCache; + /// let mut cache = LruCache::new(2u); + /// + /// cache.put(1i, "a"); + /// cache.put(2, "b"); + /// cache.put(3, "c"); + /// + /// assert_eq!(cache.get(&1), None); + /// assert_eq!(cache.get(&2), Some(&"b")); + /// assert_eq!(cache.get(&3), Some(&"c")); + /// + /// cache.change_capacity(3u); + /// cache.put(1i, "a"); + /// cache.put(2, "b"); + /// cache.put(3, "c"); + /// + /// assert_eq!(cache.get(&1), Some(&"a")); + /// assert_eq!(cache.get(&2), Some(&"b")); + /// assert_eq!(cache.get(&3), Some(&"c")); + /// + /// cache.change_capacity(1u); + /// + /// assert_eq!(cache.get(&1), None); + /// assert_eq!(cache.get(&2), None); + /// assert_eq!(cache.get(&3), Some(&"c")); + /// ``` pub fn change_capacity(&mut self, capacity: uint) { for _ in range(capacity, self.len()) { self.remove_lru();