Skip to content

Commit 80435ee

Browse files
committed
---
yaml --- r: 153854 b: refs/heads/try2 c: 53c6391 h: refs/heads/master v: v3
1 parent 6274646 commit 80435ee

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 8c34a97b37a2f546490546c5cac97c9add8a2972
8+
refs/heads/try2: 53c639184cbbdb95d316cb36691a2f9722dfc41a
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcollections/treemap.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,107 @@ use vec::Vec;
4747
/// a red-black tree where red (horizontal) nodes can only be added
4848
/// as a right child. The time complexity is the same, and re-balancing
4949
/// operations are more frequent but also cheaper.
50+
///
51+
/// # Example
52+
///
53+
/// ```
54+
/// use std::collections::TreeMap;
55+
///
56+
/// let mut map = TreeMap::new();
57+
///
58+
/// map.insert(2i, "bar");
59+
/// map.insert(1i, "foo");
60+
/// map.insert(3i, "quux");
61+
///
62+
/// // In ascending order by keys
63+
/// for (key, value) in map.iter() {
64+
/// println!("{}: {}", key, value);
65+
/// }
66+
///
67+
/// // Print 1, 2, 3
68+
/// for key in map.keys() {
69+
/// println!("{}", key);
70+
/// }
71+
///
72+
/// // Print `foo`, `bar`, `quux`
73+
/// for key in map.values() {
74+
/// println!("{}", key);
75+
/// }
76+
///
77+
/// map.remove(&1);
78+
/// assert_eq!(map.len(), 2);
79+
///
80+
/// if !map.contains_key(&1) {
81+
/// println!("1 is no more");
82+
/// }
83+
///
84+
/// for key in range(0, 4) {
85+
/// match map.find(&key) {
86+
/// Some(val) => println!("{} has a value: {}", key, val),
87+
/// None => println!("{} not in map", key),
88+
/// }
89+
/// }
90+
///
91+
/// map.clear();
92+
/// assert!(map.is_empty());
93+
/// ```
94+
///
95+
/// The easiest way to use `TreeMap` with a custom type as keys is to implement `Ord`.
96+
/// We must also implement `PartialEq`, `Eq` and `PartialOrd`.
97+
///
98+
/// ```
99+
/// use std::collections::TreeMap;
100+
///
101+
/// // We need `Eq` and `PartialEq`, these can be derived.
102+
/// #[deriving(Eq, PartialEq)]
103+
/// struct Troll<'a> {
104+
/// name: &'a str,
105+
/// level: uint,
106+
/// }
107+
///
108+
/// // Implement `Ord` and sort trolls by level.
109+
/// impl<'a> Ord for Troll<'a> {
110+
/// fn cmp(&self, other: &Troll) -> Ordering {
111+
/// // If we swap `self` and `other`, we get descended ordering.
112+
/// self.level.cmp(&other.level)
113+
/// }
114+
/// }
115+
///
116+
/// // `PartialOrd` needs to be implemented as well.
117+
/// impl<'a> PartialOrd for Troll<'a> {
118+
/// fn partial_cmp(&self, other: &Troll) -> Option<Ordering> {
119+
/// Some(self.cmp(other))
120+
/// }
121+
/// }
122+
///
123+
/// // Use a map to store trolls, sorted by level, and track a list of
124+
/// // heroes slain.
125+
/// let mut trolls = TreeMap::new();
126+
///
127+
/// trolls.insert(Troll { name: "Orgarr", level: 2 },
128+
/// vec!["King Karl"]);
129+
/// trolls.insert(Troll { name: "Blargarr", level: 3 },
130+
/// vec!["Odd"]);
131+
/// trolls.insert(Troll { name: "Kron the Smelly One", level: 4 },
132+
/// vec!["Omar the Brave", "Peter: Slayer of Trolls"]);
133+
/// trolls.insert(Troll { name: "Wartilda", level: 1 },
134+
/// vec![]);
135+
///
136+
/// println!("You are facing {} trolls!", trolls.len());
137+
///
138+
/// // Print the trolls, ordered by level with smallest level first
139+
/// for (troll, heroes) in trolls.iter() {
140+
/// let what = if heroes.len() == 1u { "hero" }
141+
/// else { "heroes" };
142+
///
143+
/// println!("level {}: '{}' has slain {} {}",
144+
/// troll.level, troll.name, heroes.len(), what);
145+
/// }
146+
///
147+
/// // Kill all trolls
148+
/// trolls.clear();
149+
/// assert_eq!(trolls.len(), 0);
150+
/// ```
50151
51152
// Future improvements:
52153

0 commit comments

Comments
 (0)