Skip to content

Commit 32e521f

Browse files
committed
auto merge of #15941 : treeman/rust/doc-lru, r=alexcrichton
2 parents e6e544f + 6bbe92e commit 32e521f

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/libstd/collections/lru_cache.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ impl<K, V> LruEntry<K, V> {
9292

9393
impl<K: Hash + Eq, V> LruCache<K, V> {
9494
/// Create an LRU Cache that holds at most `capacity` items.
95+
///
96+
/// # Example
97+
///
98+
/// ```
99+
/// use std::collections::LruCache;
100+
/// let mut cache: LruCache<int, &str> = LruCache::new(10);
101+
/// ```
95102
pub fn new(capacity: uint) -> LruCache<K, V> {
96103
let cache = LruCache {
97104
map: HashMap::new(),
@@ -106,6 +113,18 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
106113
}
107114

108115
/// Put a key-value pair into cache.
116+
///
117+
/// # Example
118+
///
119+
/// ```
120+
/// use std::collections::LruCache;
121+
/// let mut cache = LruCache::new(2);
122+
///
123+
/// cache.put(1i, "a");
124+
/// cache.put(2, "b");
125+
/// assert_eq!(cache.get(&1), Some(&"a"));
126+
/// assert_eq!(cache.get(&2), Some(&"b"));
127+
/// ```
109128
pub fn put(&mut self, k: K, v: V) {
110129
let (node_ptr, node_opt) = match self.map.find_mut(&KeyRef{k: &k}) {
111130
Some(node) => {
@@ -137,6 +156,21 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
137156
}
138157

139158
/// Return a value corresponding to the key in the cache.
159+
///
160+
/// # Example
161+
///
162+
/// ```
163+
/// use std::collections::LruCache;
164+
/// let mut cache = LruCache::new(2);
165+
///
166+
/// cache.put(1i, "a");
167+
/// cache.put(2, "b");
168+
/// cache.put(2, "c");
169+
/// cache.put(3, "d");
170+
///
171+
/// assert_eq!(cache.get(&1), None);
172+
/// assert_eq!(cache.get(&2), Some(&"c"));
173+
/// ```
140174
pub fn get<'a>(&'a mut self, k: &K) -> Option<&'a V> {
141175
let (value, node_ptr_opt) = match self.map.find_mut(&KeyRef{k: k}) {
142176
None => (None, None),
@@ -156,6 +190,20 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
156190
}
157191

158192
/// Remove and return a value corresponding to the key from the cache.
193+
///
194+
/// # Example
195+
///
196+
/// ```
197+
/// use std::collections::LruCache;
198+
/// let mut cache = LruCache::new(2);
199+
///
200+
/// cache.put(2i, "a");
201+
///
202+
/// assert_eq!(cache.pop(&1), None);
203+
/// assert_eq!(cache.pop(&2), Some("a"));
204+
/// assert_eq!(cache.pop(&2), None);
205+
/// assert_eq!(cache.len(), 0);
206+
/// ```
159207
pub fn pop(&mut self, k: &K) -> Option<V> {
160208
match self.map.pop(&KeyRef{k: k}) {
161209
None => None,
@@ -164,12 +212,49 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
164212
}
165213

166214
/// Return the maximum number of key-value pairs the cache can hold.
215+
///
216+
/// # Example
217+
///
218+
/// ```
219+
/// use std::collections::LruCache;
220+
/// let mut cache: LruCache<int, &str> = LruCache::new(2);
221+
/// assert_eq!(cache.capacity(), 2);
222+
/// ```
167223
pub fn capacity(&self) -> uint {
168224
self.max_size
169225
}
170226

171227
/// Change the number of key-value pairs the cache can hold. Remove
172228
/// least-recently-used key-value pairs if necessary.
229+
///
230+
/// # Example
231+
///
232+
/// ```
233+
/// use std::collections::LruCache;
234+
/// let mut cache = LruCache::new(2);
235+
///
236+
/// cache.put(1i, "a");
237+
/// cache.put(2, "b");
238+
/// cache.put(3, "c");
239+
///
240+
/// assert_eq!(cache.get(&1), None);
241+
/// assert_eq!(cache.get(&2), Some(&"b"));
242+
/// assert_eq!(cache.get(&3), Some(&"c"));
243+
///
244+
/// cache.change_capacity(3);
245+
/// cache.put(1i, "a");
246+
/// cache.put(2, "b");
247+
///
248+
/// assert_eq!(cache.get(&1), Some(&"a"));
249+
/// assert_eq!(cache.get(&2), Some(&"b"));
250+
/// assert_eq!(cache.get(&3), Some(&"c"));
251+
///
252+
/// cache.change_capacity(1);
253+
///
254+
/// assert_eq!(cache.get(&1), None);
255+
/// assert_eq!(cache.get(&2), None);
256+
/// assert_eq!(cache.get(&3), Some(&"c"));
257+
/// ```
173258
pub fn change_capacity(&mut self, capacity: uint) {
174259
for _ in range(capacity, self.len()) {
175260
self.remove_lru();

0 commit comments

Comments
 (0)