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
No known key found for this signature in database
GPG Key ID: 7234E476BF21C61A
2 changed files with 29 additions and 25 deletions

View File

@ -69,14 +69,15 @@ 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 { }
None => {
let mut np_bt = BallotTree::new(); let mut np_bt = BallotTree::new();
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 {
@ -85,6 +86,7 @@ impl<'t, N: Number> BallotTree<'t, N> {
}); });
next_preferences.insert(candidate, np_bt); next_preferences.insert(candidate, np_bt);
} }
}
} else { } else {
// Exhausted // Exhausted
next_exhausted.num_ballots += &bit.ballot.orig_value; next_exhausted.num_ballots += &bit.ballot.orig_value;

View File

@ -823,17 +823,19 @@ 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 { }
None => {
let entry = NextPreferencesEntry { let entry = NextPreferencesEntry {
num_ballots: vote.ballot.orig_value.clone(), num_ballots: vote.ballot.orig_value.clone(),
votes: vec![vote], 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;
result.exhausted.votes.push(vote); result.exhausted.votes.push(vote);