Created
May 23, 2014 16:14
-
-
Save schmee/3fe6c075a5b25e49ea78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate collections; | |
use collections::HashMap; | |
use std::hash::Hash; | |
fn main() { | |
let mut map: CountedSet<&'static str> = CountedSet::new(); | |
let strings = ["this", "is", "a", "list", "of", "of", "strings"]; | |
for k in strings.iter() { | |
map.add(*k); | |
} | |
println!("{:?}", map) | |
} | |
struct CountedSet<T> { | |
data: HashMap<T, uint> | |
} | |
impl<T: Hash + TotalEq> CountedSet<T> { | |
fn add<T>(&self, k: T) -> T { | |
self.data.insert_or_update_with(k, 1u, |_, v| *v += 1); | |
} | |
fn new() -> CountedSet<T> { | |
CountedSet{data: HashMap::new()} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment