8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- //! Ordered containers with integer keys, implemented as radix tries (`TrieSet` and `TrieMap` types)
11
+ //! Ordered containers with unsigned integer keys,
12
+ //! implemented as radix tries (`TrieSet` and `TrieMap` types).
12
13
13
14
use core:: prelude:: * ;
14
15
@@ -35,7 +36,7 @@ enum Child<T> {
35
36
Nothing
36
37
}
37
38
38
- # [ allow ( missing_doc ) ]
39
+ /// A map as a radix trie.
39
40
pub struct TrieMap < T > {
40
41
root : TrieNode < T > ,
41
42
length : uint
@@ -51,7 +52,7 @@ impl<T: PartialEq> PartialEq for TrieMap<T> {
51
52
impl < T : Eq > Eq for TrieMap < T > { }
52
53
53
54
impl < T > Collection for TrieMap < T > {
54
- /// Return the number of elements in the map
55
+ /// Return the number of elements in the map.
55
56
#[ inline]
56
57
fn len ( & self ) -> uint { self . length }
57
58
}
@@ -66,7 +67,7 @@ impl<T> Mutable for TrieMap<T> {
66
67
}
67
68
68
69
impl < T > Map < uint , T > for TrieMap < T > {
69
- /// Return a reference to the value corresponding to the key
70
+ /// Return a reference to the value corresponding to the key.
70
71
#[ inline]
71
72
fn find < ' a > ( & ' a self , key : & uint ) -> Option < & ' a T > {
72
73
let mut node: & ' a TrieNode < T > = & self . root ;
@@ -89,7 +90,7 @@ impl<T> Map<uint, T> for TrieMap<T> {
89
90
}
90
91
91
92
impl < T > MutableMap < uint , T > for TrieMap < T > {
92
- /// Return a mutable reference to the value corresponding to the key
93
+ /// Return a mutable reference to the value corresponding to the key.
93
94
#[ inline]
94
95
fn find_mut < ' a > ( & ' a mut self , key : & uint ) -> Option < & ' a mut T > {
95
96
find_mut ( & mut self . root . children [ chunk ( * key, 0 ) ] , * key, 1 )
@@ -122,19 +123,54 @@ impl<T> Default for TrieMap<T> {
122
123
}
123
124
124
125
impl < T > TrieMap < T > {
125
- /// Create an empty TrieMap
126
+ /// Create an empty TrieMap.
127
+ ///
128
+ /// # Example
129
+ ///
130
+ /// ```
131
+ /// use std::collections::TrieMap;
132
+ /// let mut map: TrieMap<&str> = TrieMap::new();
133
+ /// ```
126
134
#[ inline]
127
135
pub fn new ( ) -> TrieMap < T > {
128
136
TrieMap { root : TrieNode :: new ( ) , length : 0 }
129
137
}
130
138
131
- /// Visit all key-value pairs in reverse order
139
+ /// Visit all key-value pairs in reverse order. Abort traversal when f returns false.
140
+ /// Return true if f returns true for all elements.
141
+ ///
142
+ /// # Example
143
+ ///
144
+ /// ```
145
+ /// use std::collections::TrieMap;
146
+ /// let map: TrieMap<&str> = [(1, "a"), (2, "b"), (3, "c")].iter().map(|&x| x).collect();
147
+ ///
148
+ /// let mut vec = Vec::new();
149
+ /// assert_eq!(true, map.each_reverse(|&key, &value| { vec.push((key, value)); true }));
150
+ /// assert_eq!(vec, vec![(3, "c"), (2, "b"), (1, "a")]);
151
+ ///
152
+ /// // Stop when we reach 2
153
+ /// let mut vec = Vec::new();
154
+ /// assert_eq!(false, map.each_reverse(|&key, &value| { vec.push(value); key != 2 }));
155
+ /// assert_eq!(vec, vec!["c", "b"]);
156
+ /// ```
132
157
#[ inline]
133
158
pub fn each_reverse < ' a > ( & ' a self , f : |& uint , & ' a T | -> bool ) -> bool {
134
159
self . root . each_reverse ( f)
135
160
}
136
161
137
- /// Get an iterator over the key-value pairs in the map
162
+ /// Get an iterator over the key-value pairs in the map, ordered by keys.
163
+ ///
164
+ /// # Example
165
+ ///
166
+ /// ```
167
+ /// use std::collections::TrieMap;
168
+ /// let map: TrieMap<&str> = [(3, "c"), (1, "a"), (2, "b")].iter().map(|&x| x).collect();
169
+ ///
170
+ /// for (key, value) in map.iter() {
171
+ /// println!("{}: {}", key, value);
172
+ /// }
173
+ /// ```
138
174
pub fn iter < ' a > ( & ' a self ) -> Entries < ' a , T > {
139
175
let mut iter = unsafe { Entries :: new ( ) } ;
140
176
iter. stack [ 0 ] = self . root . children . iter ( ) ;
@@ -147,6 +183,21 @@ impl<T> TrieMap<T> {
147
183
148
184
/// Get an iterator over the key-value pairs in the map, with the
149
185
/// ability to mutate the values.
186
+ ///
187
+ /// # Example
188
+ ///
189
+ /// ```
190
+ /// use std::collections::TrieMap;
191
+ /// let mut map: TrieMap<int> = [(1, 2), (2, 4), (3, 6)].iter().map(|&x| x).collect();
192
+ ///
193
+ /// for (key, value) in map.mut_iter() {
194
+ /// *value = -(key as int);
195
+ /// }
196
+ ///
197
+ /// assert_eq!(map.find(&1), Some(&-1));
198
+ /// assert_eq!(map.find(&2), Some(&-2));
199
+ /// assert_eq!(map.find(&3), Some(&-3));
200
+ /// ```
150
201
pub fn mut_iter < ' a > ( & ' a mut self ) -> MutEntries < ' a , T > {
151
202
let mut iter = unsafe { MutEntries :: new ( ) } ;
152
203
iter. stack [ 0 ] = self . root . children . mut_iter ( ) ;
@@ -255,12 +306,34 @@ impl<T> TrieMap<T> {
255
306
256
307
/// Get an iterator pointing to the first key-value pair whose key is not less than `key`.
257
308
/// If all keys in the map are less than `key` an empty iterator is returned.
309
+ ///
310
+ /// # Example
311
+ ///
312
+ /// ```
313
+ /// use std::collections::TrieMap;
314
+ /// let map: TrieMap<&str> = [(2, "a"), (4, "b"), (6, "c")].iter().map(|&x| x).collect();
315
+ ///
316
+ /// assert_eq!(map.lower_bound(4).next(), Some((4, &"b")));
317
+ /// assert_eq!(map.lower_bound(5).next(), Some((6, &"c")));
318
+ /// assert_eq!(map.lower_bound(10).next(), None);
319
+ /// ```
258
320
pub fn lower_bound < ' a > ( & ' a self , key : uint ) -> Entries < ' a , T > {
259
321
self . bound ( key, false )
260
322
}
261
323
262
324
/// Get an iterator pointing to the first key-value pair whose key is greater than `key`.
263
325
/// If all keys in the map are not greater than `key` an empty iterator is returned.
326
+ ///
327
+ /// # Example
328
+ ///
329
+ /// ```
330
+ /// use std::collections::TrieMap;
331
+ /// let map: TrieMap<&str> = [(2, "a"), (4, "b"), (6, "c")].iter().map(|&x| x).collect();
332
+ ///
333
+ /// assert_eq!(map.upper_bound(4).next(), Some((6, &"c")));
334
+ /// assert_eq!(map.upper_bound(5).next(), Some((6, &"c")));
335
+ /// assert_eq!(map.upper_bound(10).next(), None);
336
+ /// ```
264
337
pub fn upper_bound < ' a > ( & ' a self , key : uint ) -> Entries < ' a , T > {
265
338
self . bound ( key, true )
266
339
}
@@ -275,12 +348,50 @@ impl<T> TrieMap<T> {
275
348
276
349
/// Get an iterator pointing to the first key-value pair whose key is not less than `key`.
277
350
/// If all keys in the map are less than `key` an empty iterator is returned.
351
+ ///
352
+ /// # Example
353
+ ///
354
+ /// ```
355
+ /// use std::collections::TrieMap;
356
+ /// let mut map: TrieMap<&str> = [(2, "a"), (4, "b"), (6, "c")].iter().map(|&x| x).collect();
357
+ ///
358
+ /// assert_eq!(map.mut_lower_bound(4).next(), Some((4, &mut "b")));
359
+ /// assert_eq!(map.mut_lower_bound(5).next(), Some((6, &mut "c")));
360
+ /// assert_eq!(map.mut_lower_bound(10).next(), None);
361
+ ///
362
+ /// for (key, value) in map.mut_lower_bound(4) {
363
+ /// *value = "changed";
364
+ /// }
365
+ ///
366
+ /// assert_eq!(map.find(&2), Some(&"a"));
367
+ /// assert_eq!(map.find(&4), Some(&"changed"));
368
+ /// assert_eq!(map.find(&6), Some(&"changed"));
369
+ /// ```
278
370
pub fn mut_lower_bound < ' a > ( & ' a mut self , key : uint ) -> MutEntries < ' a , T > {
279
371
self . mut_bound ( key, false )
280
372
}
281
373
282
374
/// Get an iterator pointing to the first key-value pair whose key is greater than `key`.
283
375
/// If all keys in the map are not greater than `key` an empty iterator is returned.
376
+ ///
377
+ /// # Example
378
+ ///
379
+ /// ```
380
+ /// use std::collections::TrieMap;
381
+ /// let mut map: TrieMap<&str> = [(2, "a"), (4, "b"), (6, "c")].iter().map(|&x| x).collect();
382
+ ///
383
+ /// assert_eq!(map.mut_upper_bound(4).next(), Some((6, &mut "c")));
384
+ /// assert_eq!(map.mut_upper_bound(5).next(), Some((6, &mut "c")));
385
+ /// assert_eq!(map.mut_upper_bound(10).next(), None);
386
+ ///
387
+ /// for (key, value) in map.mut_upper_bound(4) {
388
+ /// *value = "changed";
389
+ /// }
390
+ ///
391
+ /// assert_eq!(map.find(&2), Some(&"a"));
392
+ /// assert_eq!(map.find(&4), Some(&"b"));
393
+ /// assert_eq!(map.find(&6), Some(&"changed"));
394
+ /// ```
284
395
pub fn mut_upper_bound < ' a > ( & ' a mut self , key : uint ) -> MutEntries < ' a , T > {
285
396
self . mut_bound ( key, true )
286
397
}
@@ -310,14 +421,14 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
310
421
}
311
422
}
312
423
313
- # [ allow ( missing_doc ) ]
424
+ /// A set as a radix trie.
314
425
#[ deriving( Hash , PartialEq , Eq ) ]
315
426
pub struct TrieSet {
316
427
map : TrieMap < ( ) >
317
428
}
318
429
319
430
impl Collection for TrieSet {
320
- /// Return the number of elements in the set
431
+ /// Return the number of elements in the set.
321
432
#[ inline]
322
433
fn len ( & self ) -> uint { self . map . len ( ) }
323
434
}
@@ -368,32 +479,96 @@ impl Default for TrieSet {
368
479
}
369
480
370
481
impl TrieSet {
371
- /// Create an empty TrieSet
482
+ /// Create an empty TrieSet.
483
+ ///
484
+ /// # Example
485
+ ///
486
+ /// ```
487
+ /// use std::collections::TrieSet;
488
+ /// let mut set = TrieSet::new();
489
+ /// ```
372
490
#[ inline]
373
491
pub fn new ( ) -> TrieSet {
374
492
TrieSet { map : TrieMap :: new ( ) }
375
493
}
376
494
377
- /// Visit all values in reverse order
495
+ /// Visit all values in reverse order. Abort traversal when `f` returns false.
496
+ /// Return `true` if `f` returns `true` for all elements.
497
+ ///
498
+ /// # Example
499
+ ///
500
+ /// ```
501
+ /// use std::collections::TrieSet;
502
+ ///
503
+ /// let set: TrieSet = [1, 2, 3, 4, 5].iter().map(|&x| x).collect();
504
+ ///
505
+ /// let mut vec = Vec::new();
506
+ /// assert_eq!(true, set.each_reverse(|&x| { vec.push(x); true }));
507
+ /// assert_eq!(vec, vec![5, 4, 3, 2, 1]);
508
+ ///
509
+ /// // Stop when we reach 3
510
+ /// let mut vec = Vec::new();
511
+ /// assert_eq!(false, set.each_reverse(|&x| { vec.push(x); x != 3 }));
512
+ /// assert_eq!(vec, vec![5, 4, 3]);
513
+ /// ```
378
514
#[ inline]
379
515
pub fn each_reverse ( & self , f: |& uint| -> bool) -> bool {
380
516
self . map . each_reverse ( |k, _| f ( k) )
381
517
}
382
518
383
- /// Get an iterator over the values in the set
519
+ /// Get an iterator over the values in the set, in sorted order.
520
+ ///
521
+ /// # Example
522
+ ///
523
+ /// ```
524
+ /// use std::collections::TrieSet;
525
+ ///
526
+ /// let mut set = TrieSet::new();
527
+ /// set.insert(3);
528
+ /// set.insert(2);
529
+ /// set.insert(1);
530
+ /// set.insert(2);
531
+ ///
532
+ /// // Print 1, 2, 3
533
+ /// for x in set.iter() {
534
+ /// println!("{}", x);
535
+ /// }
536
+ /// ```
384
537
#[ inline]
385
538
pub fn iter < ' a > ( & ' a self ) -> SetItems < ' a > {
386
539
SetItems { iter : self . map . iter ( ) }
387
540
}
388
541
389
542
/// Get an iterator pointing to the first value that is not less than `val`.
390
543
/// If all values in the set are less than `val` an empty iterator is returned.
544
+ ///
545
+ /// # Example
546
+ ///
547
+ /// ```
548
+ /// use std::collections::TrieSet;
549
+ ///
550
+ /// let set: TrieSet = [2, 4, 6, 8].iter().map(|&x| x).collect();
551
+ /// assert_eq!(set.lower_bound(4).next(), Some(4));
552
+ /// assert_eq!(set.lower_bound(5).next(), Some(6));
553
+ /// assert_eq!(set.lower_bound(10).next(), None);
554
+ /// ```
391
555
pub fn lower_bound < ' a > ( & ' a self , val : uint ) -> SetItems < ' a > {
392
556
SetItems { iter : self . map . lower_bound ( val) }
393
557
}
394
558
395
559
/// Get an iterator pointing to the first value that key is greater than `val`.
396
- /// If all values in the set are not greater than `val` an empty iterator is returned.
560
+ /// If all values in the set are less than or equal to `val` an empty iterator is returned.
561
+ ///
562
+ /// # Example
563
+ ///
564
+ /// ```
565
+ /// use std::collections::TrieSet;
566
+ ///
567
+ /// let set: TrieSet = [2, 4, 6, 8].iter().map(|&x| x).collect();
568
+ /// assert_eq!(set.upper_bound(4).next(), Some(6));
569
+ /// assert_eq!(set.upper_bound(5).next(), Some(6));
570
+ /// assert_eq!(set.upper_bound(10).next(), None);
571
+ /// ```
397
572
pub fn upper_bound < ' a > ( & ' a self , val : uint ) -> SetItems < ' a > {
398
573
SetItems { iter : self . map . upper_bound ( val) }
399
574
}
@@ -526,7 +701,7 @@ fn remove<T>(count: &mut uint, child: &mut Child<T>, key: uint,
526
701
return ret;
527
702
}
528
703
529
- /// Forward iterator over a map
704
+ /// Forward iterator over a map.
530
705
pub struct Entries < ' a , T > {
531
706
stack : [ slice:: Items < ' a , Child < T > > , .. NUM_CHUNKS ] ,
532
707
length : uint ,
@@ -660,7 +835,7 @@ macro_rules! iterator_impl {
660
835
iterator_impl ! { Entries , iter = iter, mutability = }
661
836
iterator_impl ! { MutEntries , iter = mut_iter, mutability = mut }
662
837
663
- /// Forward iterator over a set
838
+ /// Forward iterator over a set.
664
839
pub struct SetItems < ' a > {
665
840
iter : Entries < ' a , ( ) >
666
841
}
0 commit comments