@@ -24,7 +24,39 @@ use {Collection, Mutable, Map, MutableMap, MutableSeq};
2424use { vec, slice} ;
2525use vec:: Vec ;
2626
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+ /// ```
2860pub struct SmallIntMap < T > {
2961 v : Vec < Option < T > > ,
3062}
@@ -120,19 +152,66 @@ impl<V> Default for SmallIntMap<V> {
120152
121153impl < V > SmallIntMap < V > {
122154 /// Create an empty SmallIntMap.
155+ ///
156+ /// # Example
157+ ///
158+ /// ```
159+ /// use std::collections::SmallIntMap;
160+ /// let mut map: SmallIntMap<&str> = SmallIntMap::new();
161+ /// ```
123162 pub fn new ( ) -> SmallIntMap < V > { SmallIntMap { v : vec ! ( ) } }
124163
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+ /// ```
126173 pub fn with_capacity ( capacity : uint ) -> SmallIntMap < V > {
127174 SmallIntMap { v : Vec :: with_capacity ( capacity) }
128175 }
129176
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+ /// ```
130193 pub fn get < ' a > ( & ' a self , key : & uint ) -> & ' a V {
131194 self . find ( key) . expect ( "key not present" )
132195 }
133196
134197 /// An iterator visiting all key-value pairs in ascending order by the keys.
135198 /// 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+ /// ```
136215 pub fn iter < ' r > ( & ' r self ) -> Entries < ' r , V > {
137216 Entries {
138217 front : 0 ,
@@ -144,6 +223,25 @@ impl<V> SmallIntMap<V> {
144223 /// An iterator visiting all key-value pairs in ascending order by the keys,
145224 /// with mutable references to the values
146225 /// 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+ /// ```
147245 pub fn mut_iter < ' r > ( & ' r mut self ) -> MutEntries < ' r , V > {
148246 MutEntries {
149247 front : 0 ,
@@ -153,6 +251,22 @@ impl<V> SmallIntMap<V> {
153251 }
154252
155253 /// 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+ /// ```
156270 pub fn move_iter ( & mut self )
157271 -> FilterMap < ( uint , Option < V > ) , ( uint , V ) ,
158272 Enumerate < vec:: MoveItems < Option < V > > > >
@@ -247,6 +361,7 @@ macro_rules! double_ended_iterator {
247361 }
248362}
249363
364+ /// Forward iterator over a map.
250365pub struct Entries < ' a , T > {
251366 front : uint ,
252367 back : uint ,
@@ -256,6 +371,8 @@ pub struct Entries<'a, T> {
256371iterator ! ( impl Entries -> ( uint, & ' a T ) , get_ref)
257372double_ended_iterator ! ( impl Entries -> ( uint, & ' a T ) , get_ref)
258373
374+ /// Forward iterator over the key-value pairs of a map, with the
375+ /// values being mutable.
259376pub struct MutEntries < ' a , T > {
260377 front : uint ,
261378 back : uint ,
0 commit comments