@@ -24,7 +24,39 @@ use {Collection, Mutable, Map, MutableMap, MutableSeq};
24
24
use { vec, slice} ;
25
25
use vec:: Vec ;
26
26
27
- #[ allow( missing_doc) ]
27
+ /// A map optimized for small integer keys.
28
+ ///
29
+ /// # Example
30
+ ///
31
+ /// ```
32
+ /// use std::collections::SmallIntMap;
33
+ ///
34
+ /// let mut months = SmallIntMap::new();
35
+ /// months.insert(1, "Jan");
36
+ /// months.insert(2, "Feb");
37
+ /// months.insert(3, "Mar");
38
+ ///
39
+ /// if !months.contains_key(&12) {
40
+ /// println!("The end is near!");
41
+ /// }
42
+ ///
43
+ /// assert_eq!(months.find(&1), Some(&"Jan"));
44
+ ///
45
+ /// match months.find_mut(&3) {
46
+ /// Some(value) => *value = "Venus",
47
+ /// None => (),
48
+ /// }
49
+ ///
50
+ /// assert_eq!(months.find(&3), Some(&"Venus"));
51
+ ///
52
+ /// // Print out all months
53
+ /// for (key, value) in months.iter() {
54
+ /// println!("month {} is {}", key, value);
55
+ /// }
56
+ ///
57
+ /// months.clear();
58
+ /// assert!(months.is_empty());
59
+ /// ```
28
60
pub struct SmallIntMap < T > {
29
61
v : Vec < Option < T > > ,
30
62
}
@@ -120,19 +152,66 @@ impl<V> Default for SmallIntMap<V> {
120
152
121
153
impl < V > SmallIntMap < V > {
122
154
/// Create an empty SmallIntMap.
155
+ ///
156
+ /// # Example
157
+ ///
158
+ /// ```
159
+ /// use std::collections::SmallIntMap;
160
+ /// let mut map: SmallIntMap<&str> = SmallIntMap::new();
161
+ /// ```
123
162
pub fn new ( ) -> SmallIntMap < V > { SmallIntMap { v : vec ! ( ) } }
124
163
125
- /// Create an empty SmallIntMap with capacity `capacity`.
164
+ /// Create an empty SmallIntMap with space for at least `capacity` elements
165
+ /// before resizing.
166
+ ///
167
+ /// # Example
168
+ ///
169
+ /// ```
170
+ /// use std::collections::SmallIntMap;
171
+ /// let mut map: SmallIntMap<&str> = SmallIntMap::with_capacity(10);
172
+ /// ```
126
173
pub fn with_capacity ( capacity : uint ) -> SmallIntMap < V > {
127
174
SmallIntMap { v : Vec :: with_capacity ( capacity) }
128
175
}
129
176
177
+ /// Retrieves a value for the given key.
178
+ /// See [`find`](../trait.Map.html#tymethod.find) for a non-failing alternative.
179
+ ///
180
+ /// # Failure
181
+ ///
182
+ /// Fails if the key is not present.
183
+ ///
184
+ /// # Example
185
+ ///
186
+ /// ```
187
+ /// use std::collections::SmallIntMap;
188
+ ///
189
+ /// let mut map = SmallIntMap::new();
190
+ /// map.insert(1, "a");
191
+ /// assert_eq!(map.get(&1), &"a");
192
+ /// ```
130
193
pub fn get < ' a > ( & ' a self , key : & uint ) -> & ' a V {
131
194
self . find ( key) . expect ( "key not present" )
132
195
}
133
196
134
197
/// An iterator visiting all key-value pairs in ascending order by the keys.
135
198
/// Iterator element type is `(uint, &'r V)`.
199
+ ///
200
+ /// # Example
201
+ ///
202
+ /// ```
203
+ /// use std::collections::SmallIntMap;
204
+ ///
205
+ /// let mut map = SmallIntMap::new();
206
+ /// map.insert(1, "a");
207
+ /// map.insert(3, "c");
208
+ /// map.insert(2, "b");
209
+ ///
210
+ /// // Print `1: a` then `2: b` then `3: c`
211
+ /// for (key, value) in map.iter() {
212
+ /// println!("{}: {}", key, value);
213
+ /// }
214
+ /// ```
136
215
pub fn iter < ' r > ( & ' r self ) -> Entries < ' r , V > {
137
216
Entries {
138
217
front : 0 ,
@@ -144,6 +223,25 @@ impl<V> SmallIntMap<V> {
144
223
/// An iterator visiting all key-value pairs in ascending order by the keys,
145
224
/// with mutable references to the values
146
225
/// Iterator element type is `(uint, &'r mut V)`.
226
+ ///
227
+ /// # Example
228
+ ///
229
+ /// ```
230
+ /// use std::collections::SmallIntMap;
231
+ ///
232
+ /// let mut map = SmallIntMap::new();
233
+ /// map.insert(1, "a");
234
+ /// map.insert(2, "b");
235
+ /// map.insert(3, "c");
236
+ ///
237
+ /// for (key, value) in map.mut_iter() {
238
+ /// *value = "x";
239
+ /// }
240
+ ///
241
+ /// for (key, value) in map.iter() {
242
+ /// assert_eq!(value, &"x");
243
+ /// }
244
+ /// ```
147
245
pub fn mut_iter < ' r > ( & ' r mut self ) -> MutEntries < ' r , V > {
148
246
MutEntries {
149
247
front : 0 ,
@@ -153,6 +251,22 @@ impl<V> SmallIntMap<V> {
153
251
}
154
252
155
253
/// Empties the hash map, moving all values into the specified closure.
254
+ ///
255
+ /// # Example
256
+ ///
257
+ /// ```
258
+ /// use std::collections::SmallIntMap;
259
+ ///
260
+ /// let mut map = SmallIntMap::new();
261
+ /// map.insert(1, "a");
262
+ /// map.insert(3, "c");
263
+ /// map.insert(2, "b");
264
+ ///
265
+ /// // Not possible with .iter()
266
+ /// let vec: Vec<(uint, &str)> = map.move_iter().collect();
267
+ ///
268
+ /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
269
+ /// ```
156
270
pub fn move_iter ( & mut self )
157
271
-> FilterMap < ( uint , Option < V > ) , ( uint , V ) ,
158
272
Enumerate < vec:: MoveItems < Option < V > > > >
@@ -247,6 +361,7 @@ macro_rules! double_ended_iterator {
247
361
}
248
362
}
249
363
364
+ /// Forward iterator over a map.
250
365
pub struct Entries < ' a , T > {
251
366
front : uint ,
252
367
back : uint ,
@@ -256,6 +371,8 @@ pub struct Entries<'a, T> {
256
371
iterator ! ( impl Entries -> ( uint, & ' a T ) , get_ref)
257
372
double_ended_iterator ! ( impl Entries -> ( uint, & ' a T ) , get_ref)
258
373
374
+ /// Forward iterator over the key-value pairs of a map, with the
375
+ /// values being mutable.
259
376
pub struct MutEntries < ' a , T > {
260
377
front : uint ,
261
378
back : uint ,
0 commit comments