Last active
August 29, 2015 14:01
-
-
Save schmee/2faf6c361f6e539666a6 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
#[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