Skip to content

Commit a524928

Browse files
treemanalexcrichton
authored andcommitted
Document TrieSet and TrieMap methods.
1 parent 237738f commit a524928

File tree

1 file changed

+191
-16
lines changed

1 file changed

+191
-16
lines changed

src/libcollections/trie.rs

Lines changed: 191 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

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).
1213
1314
use core::prelude::*;
1415

@@ -35,7 +36,7 @@ enum Child<T> {
3536
Nothing
3637
}
3738

38-
#[allow(missing_doc)]
39+
/// A map as a radix trie.
3940
pub struct TrieMap<T> {
4041
root: TrieNode<T>,
4142
length: uint
@@ -51,7 +52,7 @@ impl<T: PartialEq> PartialEq for TrieMap<T> {
5152
impl<T: Eq> Eq for TrieMap<T> {}
5253

5354
impl<T> Collection for TrieMap<T> {
54-
/// Return the number of elements in the map
55+
/// Return the number of elements in the map.
5556
#[inline]
5657
fn len(&self) -> uint { self.length }
5758
}
@@ -66,7 +67,7 @@ impl<T> Mutable for TrieMap<T> {
6667
}
6768

6869
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.
7071
#[inline]
7172
fn find<'a>(&'a self, key: &uint) -> Option<&'a T> {
7273
let mut node: &'a TrieNode<T> = &self.root;
@@ -89,7 +90,7 @@ impl<T> Map<uint, T> for TrieMap<T> {
8990
}
9091

9192
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.
9394
#[inline]
9495
fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut T> {
9596
find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
@@ -122,19 +123,54 @@ impl<T> Default for TrieMap<T> {
122123
}
123124

124125
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+
/// ```
126134
#[inline]
127135
pub fn new() -> TrieMap<T> {
128136
TrieMap{root: TrieNode::new(), length: 0}
129137
}
130138

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+
/// ```
132157
#[inline]
133158
pub fn each_reverse<'a>(&'a self, f: |&uint, &'a T| -> bool) -> bool {
134159
self.root.each_reverse(f)
135160
}
136161

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+
/// ```
138174
pub fn iter<'a>(&'a self) -> Entries<'a, T> {
139175
let mut iter = unsafe {Entries::new()};
140176
iter.stack[0] = self.root.children.iter();
@@ -147,6 +183,21 @@ impl<T> TrieMap<T> {
147183

148184
/// Get an iterator over the key-value pairs in the map, with the
149185
/// 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+
/// ```
150201
pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, T> {
151202
let mut iter = unsafe {MutEntries::new()};
152203
iter.stack[0] = self.root.children.mut_iter();
@@ -255,12 +306,34 @@ impl<T> TrieMap<T> {
255306

256307
/// Get an iterator pointing to the first key-value pair whose key is not less than `key`.
257308
/// 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+
/// ```
258320
pub fn lower_bound<'a>(&'a self, key: uint) -> Entries<'a, T> {
259321
self.bound(key, false)
260322
}
261323

262324
/// Get an iterator pointing to the first key-value pair whose key is greater than `key`.
263325
/// 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+
/// ```
264337
pub fn upper_bound<'a>(&'a self, key: uint) -> Entries<'a, T> {
265338
self.bound(key, true)
266339
}
@@ -275,12 +348,50 @@ impl<T> TrieMap<T> {
275348

276349
/// Get an iterator pointing to the first key-value pair whose key is not less than `key`.
277350
/// 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+
/// ```
278370
pub fn mut_lower_bound<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
279371
self.mut_bound(key, false)
280372
}
281373

282374
/// Get an iterator pointing to the first key-value pair whose key is greater than `key`.
283375
/// 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+
/// ```
284395
pub fn mut_upper_bound<'a>(&'a mut self, key: uint) -> MutEntries<'a, T> {
285396
self.mut_bound(key, true)
286397
}
@@ -310,14 +421,14 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
310421
}
311422
}
312423

313-
#[allow(missing_doc)]
424+
/// A set as a radix trie.
314425
#[deriving(Hash, PartialEq, Eq)]
315426
pub struct TrieSet {
316427
map: TrieMap<()>
317428
}
318429

319430
impl Collection for TrieSet {
320-
/// Return the number of elements in the set
431+
/// Return the number of elements in the set.
321432
#[inline]
322433
fn len(&self) -> uint { self.map.len() }
323434
}
@@ -368,32 +479,96 @@ impl Default for TrieSet {
368479
}
369480

370481
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+
/// ```
372490
#[inline]
373491
pub fn new() -> TrieSet {
374492
TrieSet{map: TrieMap::new()}
375493
}
376494

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+
/// ```
378514
#[inline]
379515
pub fn each_reverse(&self, f: |&uint| -> bool) -> bool {
380516
self.map.each_reverse(|k, _| f(k))
381517
}
382518

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+
/// ```
384537
#[inline]
385538
pub fn iter<'a>(&'a self) -> SetItems<'a> {
386539
SetItems{iter: self.map.iter()}
387540
}
388541

389542
/// Get an iterator pointing to the first value that is not less than `val`.
390543
/// 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+
/// ```
391555
pub fn lower_bound<'a>(&'a self, val: uint) -> SetItems<'a> {
392556
SetItems{iter: self.map.lower_bound(val)}
393557
}
394558

395559
/// 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+
/// ```
397572
pub fn upper_bound<'a>(&'a self, val: uint) -> SetItems<'a> {
398573
SetItems{iter: self.map.upper_bound(val)}
399574
}
@@ -526,7 +701,7 @@ fn remove<T>(count: &mut uint, child: &mut Child<T>, key: uint,
526701
return ret;
527702
}
528703

529-
/// Forward iterator over a map
704+
/// Forward iterator over a map.
530705
pub struct Entries<'a, T> {
531706
stack: [slice::Items<'a, Child<T>>, .. NUM_CHUNKS],
532707
length: uint,
@@ -660,7 +835,7 @@ macro_rules! iterator_impl {
660835
iterator_impl! { Entries, iter = iter, mutability = }
661836
iterator_impl! { MutEntries, iter = mut_iter, mutability = mut }
662837

663-
/// Forward iterator over a set
838+
/// Forward iterator over a set.
664839
pub struct SetItems<'a> {
665840
iter: Entries<'a, ()>
666841
}

0 commit comments

Comments
 (0)