From 61b22b388dea7b3ee9e844e389da4c8b10a4ee4d Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sat, 20 Aug 2022 22:43:57 +1000 Subject: [PATCH] Remove unnecessary usage of HashMap.contains_key --- src/stv/meek.rs | 32 +++++++++++++++++--------------- src/stv/mod.rs | 22 ++++++++++++---------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/stv/meek.rs b/src/stv/meek.rs index f73deba..e3ceb47 100644 --- a/src/stv/meek.rs +++ b/src/stv/meek.rs @@ -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 diff --git a/src/stv/mod.rs b/src/stv/mod.rs index 4f58fbe..60fc421 100644 --- a/src/stv/mod.rs +++ b/src/stv/mod.rs @@ -823,16 +823,18 @@ pub fn next_preferences<'a, N: Number>(state: &CountState<'a, N>, votes: Vec { + 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;