Skip to content

Commit 8c34a97

Browse files
committed
doc: TreeMap methods with examples.
Small corrections for TreeSet examples.
1 parent 034ef07 commit 8c34a97

File tree

1 file changed

+248
-17
lines changed

1 file changed

+248
-17
lines changed

src/libcollections/treemap.rs

Lines changed: 248 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ use std::hash::{Writer, Hash};
4343
use {Collection, Mutable, Set, MutableSet, MutableMap, Map, MutableSeq};
4444
use vec::Vec;
4545

46-
// This is implemented as an AA tree, which is a simplified variation of
47-
// a red-black tree where red (horizontal) nodes can only be added
48-
// as a right child. The time complexity is the same, and re-balancing
49-
// operations are more frequent but also cheaper.
46+
/// This is implemented as an AA tree, which is a simplified variation of
47+
/// a red-black tree where red (horizontal) nodes can only be added
48+
/// as a right child. The time complexity is the same, and re-balancing
49+
/// operations are more frequent but also cheaper.
5050
5151
// Future improvements:
5252

@@ -59,7 +59,6 @@ use vec::Vec;
5959
// * union: |
6060
// These would be convenient since the methods work like `each`
6161

62-
#[allow(missing_doc)]
6362
#[deriving(Clone)]
6463
pub struct TreeMap<K, V> {
6564
root: Option<Box<TreeNode<K, V>>>,
@@ -139,20 +138,73 @@ impl<K: Ord, V> Default for TreeMap<K,V> {
139138

140139
impl<K: Ord, V> TreeMap<K, V> {
141140
/// Create an empty `TreeMap`.
141+
///
142+
/// # Example
143+
///
144+
/// ```
145+
/// use std::collections::TreeMap;
146+
/// let mut map: TreeMap<&str, int> = TreeMap::new();
147+
/// ```
142148
pub fn new() -> TreeMap<K, V> { TreeMap{root: None, length: 0} }
143149

144-
/// Get a lazy iterator over the keys in the map.
150+
/// Get a lazy iterator over the keys in the map, in ascending order.
151+
///
152+
/// # Example
153+
///
154+
/// ```
155+
/// use std::collections::TreeMap;
156+
/// let mut map = TreeMap::new();
157+
/// map.insert("a", 1i);
158+
/// map.insert("c", 3i);
159+
/// map.insert("b", 2i);
160+
///
161+
/// // Print "a", "b", "c" in order.
162+
/// for x in map.keys() {
163+
/// println!("{}", x);
164+
/// }
165+
/// ```
145166
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
146167
self.iter().map(|(k, _v)| k)
147168
}
148169

149-
/// Get a lazy iterator over the values in the map.
170+
/// Get a lazy iterator over the values in the map, in ascending order
171+
/// with respect to the corresponding keys.
172+
///
173+
/// # Example
174+
///
175+
/// ```
176+
/// use std::collections::TreeMap;
177+
/// let mut map = TreeMap::new();
178+
/// map.insert("a", 1i);
179+
/// map.insert("c", 3i);
180+
/// map.insert("b", 2i);
181+
///
182+
/// // Print 1, 2, 3 ordered by keys.
183+
/// for x in map.values() {
184+
/// println!("{}", x);
185+
/// }
186+
/// ```
150187
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
151188
self.iter().map(|(_k, v)| v)
152189
}
153190

154-
/// Get a lazy iterator over the key-value pairs in the map.
191+
/// Get a lazy iterator over the key-value pairs in the map, in ascending order.
155192
/// Requires that it be frozen (immutable).
193+
///
194+
/// # Example
195+
///
196+
/// ```
197+
/// use std::collections::TreeMap;
198+
/// let mut map = TreeMap::new();
199+
/// map.insert("a", 1i);
200+
/// map.insert("c", 3i);
201+
/// map.insert("b", 2i);
202+
///
203+
/// // Print contents in ascending order
204+
/// for (key, value) in map.iter() {
205+
/// println!("{}: {}", key, value);
206+
/// }
207+
/// ```
156208
pub fn iter<'a>(&'a self) -> Entries<'a, K, V> {
157209
Entries {
158210
stack: vec!(),
@@ -162,14 +214,49 @@ impl<K: Ord, V> TreeMap<K, V> {
162214
}
163215
}
164216

165-
/// Get a lazy reverse iterator over the key-value pairs in the map.
217+
/// Get a lazy reverse iterator over the key-value pairs in the map, in descending order.
166218
/// Requires that it be frozen (immutable).
219+
///
220+
/// # Example
221+
///
222+
/// ```
223+
/// use std::collections::TreeMap;
224+
/// let mut map = TreeMap::new();
225+
/// map.insert("a", 1i);
226+
/// map.insert("c", 3i);
227+
/// map.insert("b", 2i);
228+
///
229+
/// // Print contents in descending order
230+
/// for (key, value) in map.rev_iter() {
231+
/// println!("{}: {}", key, value);
232+
/// }
233+
/// ```
167234
pub fn rev_iter<'a>(&'a self) -> RevEntries<'a, K, V> {
168235
RevEntries{iter: self.iter()}
169236
}
170237

171238
/// Get a lazy forward iterator over the key-value pairs in the
172239
/// map, with the values being mutable.
240+
///
241+
/// # Example
242+
///
243+
/// ```
244+
/// use std::collections::TreeMap;
245+
/// let mut map = TreeMap::new();
246+
/// map.insert("a", 1i);
247+
/// map.insert("c", 3i);
248+
/// map.insert("b", 2i);
249+
///
250+
/// // Add 10 until we find "b"
251+
/// for (key, value) in map.mut_iter() {
252+
/// *value += 10;
253+
/// if key == &"b" { break }
254+
/// }
255+
///
256+
/// assert_eq!(map.find(&"a"), Some(&11));
257+
/// assert_eq!(map.find(&"b"), Some(&12));
258+
/// assert_eq!(map.find(&"c"), Some(&3));
259+
/// ```
173260
pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> {
174261
MutEntries {
175262
stack: vec!(),
@@ -180,12 +267,47 @@ impl<K: Ord, V> TreeMap<K, V> {
180267
}
181268
/// Get a lazy reverse iterator over the key-value pairs in the
182269
/// map, with the values being mutable.
270+
///
271+
/// # Example
272+
///
273+
/// ```
274+
/// use std::collections::TreeMap;
275+
/// let mut map = TreeMap::new();
276+
/// map.insert("a", 1i);
277+
/// map.insert("c", 3i);
278+
/// map.insert("b", 2i);
279+
///
280+
/// // Add 10 until we find "b"
281+
/// for (key, value) in map.mut_rev_iter() {
282+
/// *value += 10;
283+
/// if key == &"b" { break }
284+
/// }
285+
///
286+
/// assert_eq!(map.find(&"a"), Some(&1));
287+
/// assert_eq!(map.find(&"b"), Some(&12));
288+
/// assert_eq!(map.find(&"c"), Some(&13));
289+
/// ```
183290
pub fn mut_rev_iter<'a>(&'a mut self) -> RevMutEntries<'a, K, V> {
184291
RevMutEntries{iter: self.mut_iter()}
185292
}
186293

187294

188-
/// Get a lazy iterator that consumes the treemap.
295+
/// Get a lazy iterator that consumes the treemap, it is not usable
296+
/// after calling this.
297+
///
298+
/// # Example
299+
///
300+
/// ```
301+
/// use std::collections::TreeMap;
302+
/// let mut map = TreeMap::new();
303+
/// map.insert("a", 1i);
304+
/// map.insert("c", 3i);
305+
/// map.insert("b", 2i);
306+
///
307+
/// // Not possible with a regular `.iter()`
308+
/// let vec: Vec<(&str, int)> = map.move_iter().collect();
309+
/// assert_eq!(vec, vec![("a", 1), ("b", 2), ("c", 3)]);
310+
/// ```
189311
pub fn move_iter(self) -> MoveEntries<K, V> {
190312
let TreeMap { root: root, length: length } = self;
191313
let stk = match root {
@@ -302,12 +424,44 @@ impl<K: Ord, V> TreeMap<K, V> {
302424

303425
/// Return a lazy iterator to the first key-value pair whose key is not less than `k`
304426
/// If all keys in map are less than `k` an empty iterator is returned.
427+
///
428+
/// # Example
429+
///
430+
/// ```
431+
/// use std::collections::TreeMap;
432+
///
433+
/// let mut map = TreeMap::new();
434+
/// map.insert(2i, "a");
435+
/// map.insert(4, "b");
436+
/// map.insert(6, "c");
437+
/// map.insert(8, "d");
438+
///
439+
/// assert_eq!(map.lower_bound(&4).next(), Some((&4, &"b")));
440+
/// assert_eq!(map.lower_bound(&5).next(), Some((&6, &"c")));
441+
/// assert_eq!(map.lower_bound(&10).next(), None);
442+
/// ```
305443
pub fn lower_bound<'a>(&'a self, k: &K) -> Entries<'a, K, V> {
306444
bound_setup!(self.iter_for_traversal(), k, true)
307445
}
308446

309447
/// Return a lazy iterator to the first key-value pair whose key is greater than `k`
310-
/// If all keys in map are not greater than `k` an empty iterator is returned.
448+
/// If all keys in map are less than or equal to `k` an empty iterator is returned.
449+
///
450+
/// # Example
451+
///
452+
/// ```
453+
/// use std::collections::TreeMap;
454+
///
455+
/// let mut map = TreeMap::new();
456+
/// map.insert(2i, "a");
457+
/// map.insert(4, "b");
458+
/// map.insert(6, "c");
459+
/// map.insert(8, "d");
460+
///
461+
/// assert_eq!(map.upper_bound(&4).next(), Some((&6, &"c")));
462+
/// assert_eq!(map.upper_bound(&5).next(), Some((&6, &"c")));
463+
/// assert_eq!(map.upper_bound(&10).next(), None);
464+
/// ```
311465
pub fn upper_bound<'a>(&'a self, k: &K) -> Entries<'a, K, V> {
312466
bound_setup!(self.iter_for_traversal(), k, false)
313467
}
@@ -328,15 +482,65 @@ impl<K: Ord, V> TreeMap<K, V> {
328482
///
329483
/// If all keys in map are less than `k` an empty iterator is
330484
/// returned.
485+
///
486+
/// # Example
487+
///
488+
/// ```
489+
/// use std::collections::TreeMap;
490+
///
491+
/// let mut map = TreeMap::new();
492+
/// map.insert(2i, "a");
493+
/// map.insert(4, "b");
494+
/// map.insert(6, "c");
495+
/// map.insert(8, "d");
496+
///
497+
/// assert_eq!(map.mut_lower_bound(&4).next(), Some((&4, &mut "b")));
498+
/// assert_eq!(map.mut_lower_bound(&5).next(), Some((&6, &mut "c")));
499+
/// assert_eq!(map.mut_lower_bound(&10).next(), None);
500+
///
501+
/// for (key, value) in map.mut_lower_bound(&4) {
502+
/// *value = "changed";
503+
/// }
504+
///
505+
/// assert_eq!(map.find(&2), Some(&"a"));
506+
/// assert_eq!(map.find(&4), Some(&"changed"));
507+
/// assert_eq!(map.find(&6), Some(&"changed"));
508+
/// assert_eq!(map.find(&8), Some(&"changed"));
509+
/// ```
331510
pub fn mut_lower_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
332511
bound_setup!(self.mut_iter_for_traversal(), k, true)
333512
}
334513

335514
/// Return a lazy iterator to the first key-value pair (with the
336515
/// value being mutable) whose key is greater than `k`.
337516
///
338-
/// If all keys in map are not greater than `k` an empty iterator
517+
/// If all keys in map are less than or equal to `k` an empty iterator
339518
/// is returned.
519+
///
520+
/// # Example
521+
///
522+
/// ```
523+
/// use std::collections::TreeMap;
524+
///
525+
/// let mut map = TreeMap::new();
526+
/// map.insert(2i, "a");
527+
/// map.insert(4, "b");
528+
/// map.insert(6, "c");
529+
/// map.insert(8, "d");
530+
///
531+
/// assert_eq!(map.mut_upper_bound(&4).next(), Some((&6, &mut "c")));
532+
/// assert_eq!(map.mut_upper_bound(&5).next(), Some((&6, &mut "c")));
533+
/// assert_eq!(map.mut_upper_bound(&10).next(), None);
534+
///
535+
/// for (key, value) in map.mut_upper_bound(&4) {
536+
/// *value = "changed";
537+
/// }
538+
///
539+
/// assert_eq!(map.find(&2), Some(&"a"));
540+
/// assert_eq!(map.find(&4), Some(&"b"));
541+
/// assert_eq!(map.find(&6), Some(&"changed"));
542+
/// assert_eq!(map.find(&8), Some(&"changed"));
543+
/// ```
340544
pub fn mut_upper_bound<'a>(&'a mut self, k: &K) -> MutEntries<'a, K, V> {
341545
bound_setup!(self.mut_iter_for_traversal(), k, false)
342546
}
@@ -853,13 +1057,36 @@ impl<T: Ord> TreeSet<T> {
8531057

8541058
/// Get a lazy iterator pointing to the first value not less than `v` (greater or equal).
8551059
/// If all elements in the set are less than `v` empty iterator is returned.
1060+
///
1061+
/// # Example
1062+
///
1063+
/// ```
1064+
/// use std::collections::TreeSet;
1065+
/// let set: TreeSet<int> = [2, 4, 6, 8].iter().map(|&x| x).collect();
1066+
///
1067+
/// assert_eq!(set.lower_bound(&4).next(), Some(&4));
1068+
/// assert_eq!(set.lower_bound(&5).next(), Some(&6));
1069+
/// assert_eq!(set.lower_bound(&10).next(), None);
1070+
/// ```
8561071
#[inline]
8571072
pub fn lower_bound<'a>(&'a self, v: &T) -> SetItems<'a, T> {
8581073
SetItems{iter: self.map.lower_bound(v)}
8591074
}
8601075

8611076
/// Get a lazy iterator pointing to the first value greater than `v`.
862-
/// If all elements in the set are not greater than `v` empty iterator is returned.
1077+
/// If all elements in the set are less than or equal to `v` an
1078+
/// empty iterator is returned.
1079+
///
1080+
/// # Example
1081+
///
1082+
/// ```
1083+
/// use std::collections::TreeSet;
1084+
/// let set: TreeSet<int> = [2, 4, 6, 8].iter().map(|&x| x).collect();
1085+
///
1086+
/// assert_eq!(set.upper_bound(&4).next(), Some(&6));
1087+
/// assert_eq!(set.upper_bound(&5).next(), Some(&6));
1088+
/// assert_eq!(set.upper_bound(&10).next(), None);
1089+
/// ```
8631090
#[inline]
8641091
pub fn upper_bound<'a>(&'a self, v: &T) -> SetItems<'a, T> {
8651092
SetItems{iter: self.map.upper_bound(v)}
@@ -871,6 +1098,7 @@ impl<T: Ord> TreeSet<T> {
8711098
///
8721099
/// ```
8731100
/// use std::collections::TreeSet;
1101+
///
8741102
/// let a: TreeSet<int> = [1, 2, 3].iter().map(|&x| x).collect();
8751103
/// let b: TreeSet<int> = [3, 4, 5].iter().map(|&x| x).collect();
8761104
///
@@ -897,6 +1125,7 @@ impl<T: Ord> TreeSet<T> {
8971125
///
8981126
/// ```
8991127
/// use std::collections::TreeSet;
1128+
///
9001129
/// let a: TreeSet<int> = [1, 2, 3].iter().map(|&x| x).collect();
9011130
/// let b: TreeSet<int> = [3, 4, 5].iter().map(|&x| x).collect();
9021131
///
@@ -922,6 +1151,7 @@ impl<T: Ord> TreeSet<T> {
9221151
///
9231152
/// ```
9241153
/// use std::collections::TreeSet;
1154+
///
9251155
/// let a: TreeSet<int> = [1, 2, 3].iter().map(|&x| x).collect();
9261156
/// let b: TreeSet<int> = [2, 3, 4].iter().map(|&x| x).collect();
9271157
///
@@ -943,16 +1173,17 @@ impl<T: Ord> TreeSet<T> {
9431173
/// # Example
9441174
///
9451175
/// ```
946-
/// use std::collections::HashSet;
947-
/// let a: HashSet<int> = [1, 2, 3].iter().map(|&x| x).collect();
948-
/// let b: HashSet<int> = [3, 4, 5].iter().map(|&x| x).collect();
1176+
/// use std::collections::TreeSet;
1177+
///
1178+
/// let a: TreeSet<int> = [1, 2, 3].iter().map(|&x| x).collect();
1179+
/// let b: TreeSet<int> = [3, 4, 5].iter().map(|&x| x).collect();
9491180
///
9501181
/// // Print 1, 2, 3, 4, 5 in ascending order.
9511182
/// for x in a.union(&b) {
9521183
/// println!("{}", x);
9531184
/// }
9541185
///
955-
/// let diff: HashSet<int> = a.union(&b).map(|&x| x).collect();
1186+
/// let diff: TreeSet<int> = a.union(&b).map(|&x| x).collect();
9561187
/// assert_eq!(diff, [1, 2, 3, 4, 5].iter().map(|&x| x).collect());
9571188
/// ```
9581189
pub fn union<'a>(&'a self, other: &'a TreeSet<T>) -> UnionItems<'a, T> {

0 commit comments

Comments
 (0)