Skip to content

Commit

Permalink
Main examples for TrieSet and TrieMap.
Browse files Browse the repository at this point in the history
  • Loading branch information
treeman authored and alexcrichton committed Jul 24, 2014
1 parent a524928 commit 3685945
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions src/libcollections/trie.rs
Expand Up @@ -36,7 +36,44 @@ enum Child<T> {
Nothing
}

/// A map as a radix trie.
/// A map implemented as a radix trie.
///
/// # Example
///
/// ```
/// use std::collections::TrieMap;
///
/// let mut map = TrieMap::new();
/// map.insert(27, "Olaf");
/// map.insert(1, "Edgar");
/// map.insert(13, "Ruth");
/// map.insert(1, "Martin");
///
/// assert_eq!(map.len(), 3);
/// assert_eq!(map.find(&1), Some(&"Martin"));
///
/// if !map.contains_key(&90) {
/// println!("Nobody is keyed 90");
/// }
///
/// // Update a key
/// match map.find_mut(&1) {
/// Some(value) => *value = "Olga",
/// None => (),
/// }
///
/// map.remove(&13);
/// assert_eq!(map.len(), 2);
///
/// // Print the key value pairs, ordered by key.
/// for (key, value) in map.iter() {
/// // Prints `1: Olga` then `27: Olaf`
/// println!("{}: {}", key, value);
/// }
///
/// map.clear();
/// assert!(map.is_empty());
/// ```
pub struct TrieMap<T> {
root: TrieNode<T>,
length: uint
Expand Down Expand Up @@ -421,7 +458,35 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
}
}

/// A set as a radix trie.
/// A set implemented as a radix trie.
///
/// # Example
///
/// ```
/// use std::collections::TrieSet;
///
/// let mut set = TrieSet::new();
/// set.insert(6);
/// set.insert(28);
/// set.insert(6);
///
/// assert_eq!(set.len(), 2);
///
/// if !set.contains(&3) {
/// println!("3 is not in the set");
/// }
///
/// // Print contents in order
/// for x in set.iter() {
/// println!("{}", x);
/// }
///
/// set.remove(&6);
/// assert_eq!(set.len(), 1);
///
/// set.clear();
/// assert!(set.is_empty());
/// ```
#[deriving(Hash, PartialEq, Eq)]
pub struct TrieSet {
map: TrieMap<()>
Expand Down

0 comments on commit 3685945

Please sign in to comment.