8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- /*!
12
- * A simple map based on a vector for small integer keys. Space requirements
13
- * are O(highest integer key).
14
- */
11
+ //! A simple map based on a vector for small integer keys. Space requirements
12
+ //! are O(highest integer key).
15
13
16
14
#![ allow( missing_doc) ]
17
15
@@ -26,18 +24,50 @@ use {Collection, Mutable, Map, MutableMap, MutableSeq};
26
24
use { vec, slice} ;
27
25
use vec:: Vec ;
28
26
29
- #[ 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
+ /// ```
30
60
pub struct SmallIntMap < T > {
31
61
v : Vec < Option < T > > ,
32
62
}
33
63
34
64
impl < V > Collection for SmallIntMap < V > {
35
- /// Return the number of elements in the map
65
+ /// Return the number of elements in the map.
36
66
fn len ( & self ) -> uint {
37
67
self . v . iter ( ) . filter ( |elt| elt. is_some ( ) ) . count ( )
38
68
}
39
69
40
- /// Return true if there are no elements in the map
70
+ /// Return ` true` if there are no elements in the map.
41
71
fn is_empty ( & self ) -> bool {
42
72
self . v . iter ( ) . all ( |elt| elt. is_none ( ) )
43
73
}
@@ -49,7 +79,7 @@ impl<V> Mutable for SmallIntMap<V> {
49
79
}
50
80
51
81
impl < V > Map < uint , V > for SmallIntMap < V > {
52
- /// Return a reference to the value corresponding to the key
82
+ /// Return a reference to the value corresponding to the key.
53
83
fn find < ' a > ( & ' a self , key : & uint ) -> Option < & ' a V > {
54
84
if * key < self . v . len ( ) {
55
85
match * self . v . get ( * key) {
@@ -63,7 +93,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
63
93
}
64
94
65
95
impl < V > MutableMap < uint , V > for SmallIntMap < V > {
66
- /// Return a mutable reference to the value corresponding to the key
96
+ /// Return a mutable reference to the value corresponding to the key.
67
97
fn find_mut < ' a > ( & ' a mut self , key : & uint ) -> Option < & ' a mut V > {
68
98
if * key < self . v . len ( ) {
69
99
match * self . v . get_mut ( * key) {
@@ -76,7 +106,7 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
76
106
}
77
107
78
108
/// Insert a key-value pair into the map. An existing value for a
79
- /// key is replaced by the new value. Return true if the key did
109
+ /// key is replaced by the new value. Return ` true` if the key did
80
110
/// not already exist in the map.
81
111
fn insert ( & mut self , key : uint , value : V ) -> bool {
82
112
let exists = self . contains_key ( & key) ;
@@ -88,14 +118,14 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
88
118
!exists
89
119
}
90
120
91
- /// Remove a key-value pair from the map. Return true if the key
92
- /// was present in the map, otherwise false.
121
+ /// Remove a key-value pair from the map. Return ` true` if the key
122
+ /// was present in the map, otherwise ` false` .
93
123
fn remove ( & mut self , key : & uint ) -> bool {
94
124
self . pop ( key) . is_some ( )
95
125
}
96
126
97
127
/// Insert a key-value pair from the map. If the key already had a value
98
- /// present in the map, that value is returned. Otherwise None is returned.
128
+ /// present in the map, that value is returned. Otherwise ` None` is returned.
99
129
fn swap ( & mut self , key : uint , value : V ) -> Option < V > {
100
130
match self . find_mut ( & key) {
101
131
Some ( loc) => { return Some ( replace ( loc, value) ) ; }
@@ -121,20 +151,67 @@ impl<V> Default for SmallIntMap<V> {
121
151
}
122
152
123
153
impl < V > SmallIntMap < V > {
124
- /// Create an empty SmallIntMap
154
+ /// Create an empty SmallIntMap.
155
+ ///
156
+ /// # Example
157
+ ///
158
+ /// ```
159
+ /// use std::collections::SmallIntMap;
160
+ /// let mut map: SmallIntMap<&str> = SmallIntMap::new();
161
+ /// ```
125
162
pub fn new ( ) -> SmallIntMap < V > { SmallIntMap { v : vec ! ( ) } }
126
163
127
- /// 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
+ /// ```
128
173
pub fn with_capacity ( capacity : uint ) -> SmallIntMap < V > {
129
174
SmallIntMap { v : Vec :: with_capacity ( capacity) }
130
175
}
131
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
+ /// ```
132
193
pub fn get < ' a > ( & ' a self , key : & uint ) -> & ' a V {
133
194
self . find ( key) . expect ( "key not present" )
134
195
}
135
196
136
197
/// An iterator visiting all key-value pairs in ascending order by the keys.
137
- /// Iterator element type is (uint, &'r V)
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
+ /// ```
138
215
pub fn iter < ' r > ( & ' r self ) -> Entries < ' r , V > {
139
216
Entries {
140
217
front : 0 ,
@@ -145,7 +222,26 @@ impl<V> SmallIntMap<V> {
145
222
146
223
/// An iterator visiting all key-value pairs in ascending order by the keys,
147
224
/// with mutable references to the values
148
- /// Iterator element type is (uint, &'r mut V)
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
+ /// ```
149
245
pub fn mut_iter < ' r > ( & ' r mut self ) -> MutEntries < ' r , V > {
150
246
MutEntries {
151
247
front : 0 ,
@@ -154,7 +250,23 @@ impl<V> SmallIntMap<V> {
154
250
}
155
251
}
156
252
157
- /// Empties the hash map, moving all values into the specified closure
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
+ /// ```
158
270
pub fn move_iter ( & mut self )
159
271
-> FilterMap < ( uint , Option < V > ) , ( uint , V ) ,
160
272
Enumerate < vec:: MoveItems < Option < V > > > >
@@ -249,6 +361,7 @@ macro_rules! double_ended_iterator {
249
361
}
250
362
}
251
363
364
+ /// Forward iterator over a map.
252
365
pub struct Entries < ' a , T > {
253
366
front : uint ,
254
367
back : uint ,
@@ -258,6 +371,8 @@ pub struct Entries<'a, T> {
258
371
iterator ! ( impl Entries -> ( uint, & ' a T ) , get_ref)
259
372
double_ended_iterator ! ( impl Entries -> ( uint, & ' a T ) , get_ref)
260
373
374
+ /// Forward iterator over the key-value pairs of a map, with the
375
+ /// values being mutable.
261
376
pub struct MutEntries < ' a , T > {
262
377
front : uint ,
263
378
back : uint ,
0 commit comments