88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
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).
1513
1614#![ allow( missing_doc) ]
1715
@@ -26,18 +24,50 @@ use {Collection, Mutable, Map, MutableMap, MutableSeq};
2624use { vec, slice} ;
2725use vec:: Vec ;
2826
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+ /// ```
3060pub struct SmallIntMap < T > {
3161 v : Vec < Option < T > > ,
3262}
3363
3464impl < V > Collection for SmallIntMap < V > {
35- /// Return the number of elements in the map
65+ /// Return the number of elements in the map.
3666 fn len ( & self ) -> uint {
3767 self . v . iter ( ) . filter ( |elt| elt. is_some ( ) ) . count ( )
3868 }
3969
40- /// Return true if there are no elements in the map
70+ /// Return ` true` if there are no elements in the map.
4171 fn is_empty ( & self ) -> bool {
4272 self . v . iter ( ) . all ( |elt| elt. is_none ( ) )
4373 }
@@ -49,7 +79,7 @@ impl<V> Mutable for SmallIntMap<V> {
4979}
5080
5181impl < 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.
5383 fn find < ' a > ( & ' a self , key : & uint ) -> Option < & ' a V > {
5484 if * key < self . v . len ( ) {
5585 match * self . v . get ( * key) {
@@ -63,7 +93,7 @@ impl<V> Map<uint, V> for SmallIntMap<V> {
6393}
6494
6595impl < 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.
6797 fn find_mut < ' a > ( & ' a mut self , key : & uint ) -> Option < & ' a mut V > {
6898 if * key < self . v . len ( ) {
6999 match * self . v . get_mut ( * key) {
@@ -76,7 +106,7 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
76106 }
77107
78108 /// 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
80110 /// not already exist in the map.
81111 fn insert ( & mut self , key : uint , value : V ) -> bool {
82112 let exists = self . contains_key ( & key) ;
@@ -88,14 +118,14 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
88118 !exists
89119 }
90120
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` .
93123 fn remove ( & mut self , key : & uint ) -> bool {
94124 self . pop ( key) . is_some ( )
95125 }
96126
97127 /// 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.
99129 fn swap ( & mut self , key : uint , value : V ) -> Option < V > {
100130 match self . find_mut ( & key) {
101131 Some ( loc) => { return Some ( replace ( loc, value) ) ; }
@@ -121,20 +151,67 @@ impl<V> Default for SmallIntMap<V> {
121151}
122152
123153impl < 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+ /// ```
125162 pub fn new ( ) -> SmallIntMap < V > { SmallIntMap { v : vec ! ( ) } }
126163
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+ /// ```
128173 pub fn with_capacity ( capacity : uint ) -> SmallIntMap < V > {
129174 SmallIntMap { v : Vec :: with_capacity ( capacity) }
130175 }
131176
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+ /// ```
132193 pub fn get < ' a > ( & ' a self , key : & uint ) -> & ' a V {
133194 self . find ( key) . expect ( "key not present" )
134195 }
135196
136197 /// 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+ /// ```
138215 pub fn iter < ' r > ( & ' r self ) -> Entries < ' r , V > {
139216 Entries {
140217 front : 0 ,
@@ -145,7 +222,26 @@ impl<V> SmallIntMap<V> {
145222
146223 /// An iterator visiting all key-value pairs in ascending order by the keys,
147224 /// 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+ /// ```
149245 pub fn mut_iter < ' r > ( & ' r mut self ) -> MutEntries < ' r , V > {
150246 MutEntries {
151247 front : 0 ,
@@ -154,7 +250,23 @@ impl<V> SmallIntMap<V> {
154250 }
155251 }
156252
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+ /// ```
158270 pub fn move_iter ( & mut self )
159271 -> FilterMap < ( uint , Option < V > ) , ( uint , V ) ,
160272 Enumerate < vec:: MoveItems < Option < V > > > >
@@ -249,6 +361,7 @@ macro_rules! double_ended_iterator {
249361 }
250362}
251363
364+ /// Forward iterator over a map.
252365pub struct Entries < ' a , T > {
253366 front : uint ,
254367 back : uint ,
@@ -258,6 +371,8 @@ pub struct Entries<'a, T> {
258371iterator ! ( impl Entries -> ( uint, & ' a T ) , get_ref)
259372double_ended_iterator ! ( impl Entries -> ( uint, & ' a T ) , get_ref)
260373
374+ /// Forward iterator over the key-value pairs of a map, with the
375+ /// values being mutable.
261376pub struct MutEntries < ' a , T > {
262377 front : uint ,
263378 back : uint ,
0 commit comments