about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorJonas Hietala <tradet.h@gmail.com>2014-07-24 12:51:42 +0200
committerJonas Hietala <tradet.h@gmail.com>2014-07-24 12:51:42 +0200
commit3c45fe9e1d4ad4c27dd443bdb523befd3f0b96c2 (patch)
treee0a8b6678290a0a88f131037c4faafc42fbfdccf /src/libstd
parent9e2bb9d67bb47b9a3a6c7389efa45cefc33206a6 (diff)
downloadrust-3c45fe9e1d4ad4c27dd443bdb523befd3f0b96c2.tar.gz
rust-3c45fe9e1d4ad4c27dd443bdb523befd3f0b96c2.zip
Documentation examples for LruCache.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/collections/lru_cache.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/libstd/collections/lru_cache.rs b/src/libstd/collections/lru_cache.rs
index 45301737adb..e2aff529b79 100644
--- a/src/libstd/collections/lru_cache.rs
+++ b/src/libstd/collections/lru_cache.rs
@@ -92,6 +92,13 @@ impl<K, V> LruEntry<K, V> {
 
 impl<K: Hash + Eq, V> LruCache<K, V> {
     /// Create an LRU Cache that holds at most `capacity` items.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// use std::collections::LruCache;
+    /// let mut cache: LruCache<int, &str> = LruCache::new(10u);
+    /// ```
     pub fn new(capacity: uint) -> LruCache<K, V> {
         let cache = LruCache {
             map: HashMap::new(),
@@ -106,6 +113,18 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
     }
 
     /// 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<K: Hash + Eq, V> LruCache<K, V> {
     }
 
     /// 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<K: Hash + Eq, V> LruCache<K, V> {
     }
 
     /// 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<V> {
         match self.map.pop(&KeyRef{k: k}) {
             None => None,
@@ -164,12 +212,50 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
     }
 
     /// Return the maximum number of key-value pairs the cache can hold.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// use std::collections::LruCache;
+    /// let mut cache: LruCache<int, &str> = 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();