@@ -92,6 +92,13 @@ impl<K, V> LruEntry<K, V> {
92
92
93
93
impl < K : Hash + Eq , V > LruCache < K , V > {
94
94
/// 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
+ /// ```
95
102
pub fn new ( capacity : uint ) -> LruCache < K , V > {
96
103
let cache = LruCache {
97
104
map : HashMap :: new ( ) ,
@@ -106,6 +113,18 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
106
113
}
107
114
108
115
/// 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
+ /// ```
109
128
pub fn put ( & mut self , k : K , v : V ) {
110
129
let ( node_ptr, node_opt) = match self . map . find_mut ( & KeyRef { k : & k} ) {
111
130
Some ( node) => {
@@ -137,6 +156,21 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
137
156
}
138
157
139
158
/// 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
+ /// ```
140
174
pub fn get < ' a > ( & ' a mut self , k : & K ) -> Option < & ' a V > {
141
175
let ( value, node_ptr_opt) = match self . map . find_mut ( & KeyRef { k : k} ) {
142
176
None => ( None , None ) ,
@@ -156,6 +190,20 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
156
190
}
157
191
158
192
/// 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
+ /// ```
159
207
pub fn pop ( & mut self , k : & K ) -> Option < V > {
160
208
match self . map . pop ( & KeyRef { k : k} ) {
161
209
None => None ,
@@ -164,12 +212,49 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
164
212
}
165
213
166
214
/// 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
+ /// ```
167
223
pub fn capacity ( & self ) -> uint {
168
224
self . max_size
169
225
}
170
226
171
227
/// Change the number of key-value pairs the cache can hold. Remove
172
228
/// 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
+ /// ```
173
258
pub fn change_capacity ( & mut self , capacity : uint ) {
174
259
for _ in range ( capacity, self . len ( ) ) {
175
260
self . remove_lru ( ) ;
0 commit comments