@@ -47,6 +47,107 @@ use vec::Vec;
47
47
/// a red-black tree where red (horizontal) nodes can only be added
48
48
/// as a right child. The time complexity is the same, and re-balancing
49
49
/// 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
+ /// ```
50
151
51
152
// Future improvements:
52
153
0 commit comments