@@ -43,10 +43,10 @@ use std::hash::{Writer, Hash};
43
43
use { Collection , Mutable , Set , MutableSet , MutableMap , Map , MutableSeq } ;
44
44
use vec:: Vec ;
45
45
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.
50
50
51
51
// Future improvements:
52
52
@@ -59,7 +59,6 @@ use vec::Vec;
59
59
// * union: |
60
60
// These would be convenient since the methods work like `each`
61
61
62
- #[ allow( missing_doc) ]
63
62
#[ deriving( Clone ) ]
64
63
pub struct TreeMap < K , V > {
65
64
root : Option < Box < TreeNode < K , V > > > ,
@@ -139,20 +138,73 @@ impl<K: Ord, V> Default for TreeMap<K,V> {
139
138
140
139
impl < K : Ord , V > TreeMap < K , V > {
141
140
/// Create an empty `TreeMap`.
141
+ ///
142
+ /// # Example
143
+ ///
144
+ /// ```
145
+ /// use std::collections::TreeMap;
146
+ /// let mut map: TreeMap<&str, int> = TreeMap::new();
147
+ /// ```
142
148
pub fn new ( ) -> TreeMap < K , V > { TreeMap { root : None , length : 0 } }
143
149
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
+ /// ```
145
166
pub fn keys < ' a > ( & ' a self ) -> Keys < ' a , K , V > {
146
167
self . iter ( ) . map ( |( k, _v) | k)
147
168
}
148
169
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
+ /// ```
150
187
pub fn values < ' a > ( & ' a self ) -> Values < ' a , K , V > {
151
188
self . iter ( ) . map ( |( _k, v) | v)
152
189
}
153
190
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 .
155
192
/// 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
+ /// ```
156
208
pub fn iter < ' a > ( & ' a self ) -> Entries < ' a , K , V > {
157
209
Entries {
158
210
stack : vec ! ( ) ,
@@ -162,14 +214,49 @@ impl<K: Ord, V> TreeMap<K, V> {
162
214
}
163
215
}
164
216
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 .
166
218
/// 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
+ /// ```
167
234
pub fn rev_iter < ' a > ( & ' a self ) -> RevEntries < ' a , K , V > {
168
235
RevEntries { iter : self . iter ( ) }
169
236
}
170
237
171
238
/// Get a lazy forward iterator over the key-value pairs in the
172
239
/// 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
+ /// ```
173
260
pub fn mut_iter < ' a > ( & ' a mut self ) -> MutEntries < ' a , K , V > {
174
261
MutEntries {
175
262
stack : vec ! ( ) ,
@@ -180,12 +267,47 @@ impl<K: Ord, V> TreeMap<K, V> {
180
267
}
181
268
/// Get a lazy reverse iterator over the key-value pairs in the
182
269
/// 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
+ /// ```
183
290
pub fn mut_rev_iter < ' a > ( & ' a mut self ) -> RevMutEntries < ' a , K , V > {
184
291
RevMutEntries { iter : self . mut_iter ( ) }
185
292
}
186
293
187
294
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
+ /// ```
189
311
pub fn move_iter ( self ) -> MoveEntries < K , V > {
190
312
let TreeMap { root : root, length : length } = self ;
191
313
let stk = match root {
@@ -302,12 +424,44 @@ impl<K: Ord, V> TreeMap<K, V> {
302
424
303
425
/// Return a lazy iterator to the first key-value pair whose key is not less than `k`
304
426
/// 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
+ /// ```
305
443
pub fn lower_bound < ' a > ( & ' a self , k : & K ) -> Entries < ' a , K , V > {
306
444
bound_setup ! ( self . iter_for_traversal( ) , k, true )
307
445
}
308
446
309
447
/// 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
+ /// ```
311
465
pub fn upper_bound < ' a > ( & ' a self , k : & K ) -> Entries < ' a , K , V > {
312
466
bound_setup ! ( self . iter_for_traversal( ) , k, false )
313
467
}
@@ -328,15 +482,65 @@ impl<K: Ord, V> TreeMap<K, V> {
328
482
///
329
483
/// If all keys in map are less than `k` an empty iterator is
330
484
/// 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
+ /// ```
331
510
pub fn mut_lower_bound < ' a > ( & ' a mut self , k : & K ) -> MutEntries < ' a , K , V > {
332
511
bound_setup ! ( self . mut_iter_for_traversal( ) , k, true )
333
512
}
334
513
335
514
/// Return a lazy iterator to the first key-value pair (with the
336
515
/// value being mutable) whose key is greater than `k`.
337
516
///
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
339
518
/// 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
+ /// ```
340
544
pub fn mut_upper_bound < ' a > ( & ' a mut self , k : & K ) -> MutEntries < ' a , K , V > {
341
545
bound_setup ! ( self . mut_iter_for_traversal( ) , k, false )
342
546
}
@@ -853,13 +1057,36 @@ impl<T: Ord> TreeSet<T> {
853
1057
854
1058
/// Get a lazy iterator pointing to the first value not less than `v` (greater or equal).
855
1059
/// 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
+ /// ```
856
1071
#[ inline]
857
1072
pub fn lower_bound < ' a > ( & ' a self , v : & T ) -> SetItems < ' a , T > {
858
1073
SetItems { iter : self . map . lower_bound ( v) }
859
1074
}
860
1075
861
1076
/// 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
+ /// ```
863
1090
#[ inline]
864
1091
pub fn upper_bound < ' a > ( & ' a self , v : & T ) -> SetItems < ' a , T > {
865
1092
SetItems { iter : self . map . upper_bound ( v) }
@@ -871,6 +1098,7 @@ impl<T: Ord> TreeSet<T> {
871
1098
///
872
1099
/// ```
873
1100
/// use std::collections::TreeSet;
1101
+ ///
874
1102
/// let a: TreeSet<int> = [1, 2, 3].iter().map(|&x| x).collect();
875
1103
/// let b: TreeSet<int> = [3, 4, 5].iter().map(|&x| x).collect();
876
1104
///
@@ -897,6 +1125,7 @@ impl<T: Ord> TreeSet<T> {
897
1125
///
898
1126
/// ```
899
1127
/// use std::collections::TreeSet;
1128
+ ///
900
1129
/// let a: TreeSet<int> = [1, 2, 3].iter().map(|&x| x).collect();
901
1130
/// let b: TreeSet<int> = [3, 4, 5].iter().map(|&x| x).collect();
902
1131
///
@@ -922,6 +1151,7 @@ impl<T: Ord> TreeSet<T> {
922
1151
///
923
1152
/// ```
924
1153
/// use std::collections::TreeSet;
1154
+ ///
925
1155
/// let a: TreeSet<int> = [1, 2, 3].iter().map(|&x| x).collect();
926
1156
/// let b: TreeSet<int> = [2, 3, 4].iter().map(|&x| x).collect();
927
1157
///
@@ -943,16 +1173,17 @@ impl<T: Ord> TreeSet<T> {
943
1173
/// # Example
944
1174
///
945
1175
/// ```
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();
949
1180
///
950
1181
/// // Print 1, 2, 3, 4, 5 in ascending order.
951
1182
/// for x in a.union(&b) {
952
1183
/// println!("{}", x);
953
1184
/// }
954
1185
///
955
- /// let diff: HashSet <int> = a.union(&b).map(|&x| x).collect();
1186
+ /// let diff: TreeSet <int> = a.union(&b).map(|&x| x).collect();
956
1187
/// assert_eq!(diff, [1, 2, 3, 4, 5].iter().map(|&x| x).collect());
957
1188
/// ```
958
1189
pub fn union < ' a > ( & ' a self , other : & ' a TreeSet < T > ) -> UnionItems < ' a , T > {
0 commit comments