Remove unnecessary usage of HashMap.contains_key

This commit is contained in:
RunasSudo 2022-08-20 22:43:57 +10:00
parent 9a4af322ca
commit 61b22b388d
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 29 additions and 25 deletions

View File

@ -69,21 +69,23 @@ impl<'t, N: Number> BallotTree<'t, N> {
let candidate = &candidates[*preference.first().unwrap()];
if next_preferences.contains_key(candidate) {
let np_bt = next_preferences.get_mut(candidate).unwrap();
np_bt.num_ballots += &bit.ballot.orig_value;
np_bt.ballots.push(BallotInTree {
ballot: bit.ballot,
up_to_pref: bit.up_to_pref + 1,
});
} else {
let mut np_bt = BallotTree::new();
np_bt.num_ballots += &bit.ballot.orig_value;
np_bt.ballots.push(BallotInTree {
ballot: bit.ballot,
up_to_pref: bit.up_to_pref + 1,
});
next_preferences.insert(candidate, np_bt);
match next_preferences.get_mut(candidate) {
Some(np_bt) => {
np_bt.num_ballots += &bit.ballot.orig_value;
np_bt.ballots.push(BallotInTree {
ballot: bit.ballot,
up_to_pref: bit.up_to_pref + 1,
});
}
None => {
let mut np_bt = BallotTree::new();
np_bt.num_ballots += &bit.ballot.orig_value;
np_bt.ballots.push(BallotInTree {
ballot: bit.ballot,
up_to_pref: bit.up_to_pref + 1,
});
next_preferences.insert(candidate, np_bt);
}
}
} else {
// Exhausted

View File

@ -823,16 +823,18 @@ pub fn next_preferences<'a, N: Number>(state: &CountState<'a, N>, votes: Vec<Vot
// Have to structure like this to satisfy Rust's borrow checker
if let Some(candidate) = next_candidate {
if result.candidates.contains_key(candidate) {
let entry = result.candidates.get_mut(candidate).unwrap();
entry.num_ballots += &vote.ballot.orig_value;
entry.votes.push(vote);
} else {
let entry = NextPreferencesEntry {
num_ballots: vote.ballot.orig_value.clone(),
votes: vec![vote],
};
result.candidates.insert(candidate, entry);
match result.candidates.get_mut(candidate) {
Some(entry) => {
entry.num_ballots += &vote.ballot.orig_value;
entry.votes.push(vote);
}
None => {
let entry = NextPreferencesEntry {
num_ballots: vote.ballot.orig_value.clone(),
votes: vec![vote],
};
result.candidates.insert(candidate, entry);
}
}
} else {
result.exhausted.num_ballots += &vote.ballot.orig_value;