Skip to content

Instantly share code, notes, and snippets.

@schmee
Created May 23, 2014 16:14
Show Gist options
  • Save schmee/3fe6c075a5b25e49ea78 to your computer and use it in GitHub Desktop.
Save schmee/3fe6c075a5b25e49ea78 to your computer and use it in GitHub Desktop.
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