Skip to content

Instantly share code, notes, and snippets.

@schmee
Last active August 29, 2015 14:01
Show Gist options
  • Save schmee/2faf6c361f6e539666a6 to your computer and use it in GitHub Desktop.
Save schmee/2faf6c361f6e539666a6 to your computer and use it in GitHub Desktop.
#[deriving(Clone)]
struct CountedSet<T> {
data: HashMap<T, uint>
}
fn subtract(&self, other: &CountedSet<T>) -> CountedSet<T> {
let mut result = self.clone();
for (key, count) in other.iter() {
match result.data.find_copy(key) {
Some(old) => {
let difference = if old > *count {old - *count} else { 0 };
result.data.insert((*key).clone(), difference);
}
_ => {}
}
}
result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment