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()]; let candidate = &candidates[*preference.first().unwrap()];
if next_preferences.contains_key(candidate) { match next_preferences.get_mut(candidate) {
let np_bt = next_preferences.get_mut(candidate).unwrap(); Some(np_bt) => {
np_bt.num_ballots += &bit.ballot.orig_value; np_bt.num_ballots += &bit.ballot.orig_value;
np_bt.ballots.push(BallotInTree { np_bt.ballots.push(BallotInTree {
ballot: bit.ballot, ballot: bit.ballot,
up_to_pref: bit.up_to_pref + 1, up_to_pref: bit.up_to_pref + 1,
}); });
} else { }
let mut np_bt = BallotTree::new(); None => {
np_bt.num_ballots += &bit.ballot.orig_value; let mut np_bt = BallotTree::new();
np_bt.ballots.push(BallotInTree { np_bt.num_ballots += &bit.ballot.orig_value;
ballot: bit.ballot, np_bt.ballots.push(BallotInTree {
up_to_pref: bit.up_to_pref + 1, ballot: bit.ballot,
}); up_to_pref: bit.up_to_pref + 1,
next_preferences.insert(candidate, np_bt); });
next_preferences.insert(candidate, np_bt);
}
} }
} else { } else {
// Exhausted // 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 // Have to structure like this to satisfy Rust's borrow checker
if let Some(candidate) = next_candidate { if let Some(candidate) = next_candidate {
if result.candidates.contains_key(candidate) { match result.candidates.get_mut(candidate) {
let entry = result.candidates.get_mut(candidate).unwrap(); Some(entry) => {
entry.num_ballots += &vote.ballot.orig_value; entry.num_ballots += &vote.ballot.orig_value;
entry.votes.push(vote); entry.votes.push(vote);
} else { }
let entry = NextPreferencesEntry { None => {
num_ballots: vote.ballot.orig_value.clone(), let entry = NextPreferencesEntry {
votes: vec![vote], num_ballots: vote.ballot.orig_value.clone(),
}; votes: vec![vote],
result.candidates.insert(candidate, entry); };
result.candidates.insert(candidate, entry);
}
} }
} else { } else {
result.exhausted.num_ballots += &vote.ballot.orig_value; result.exhausted.num_ballots += &vote.ballot.orig_value;