Correct handling of exhausted votes during random sample surplus distribution

This commit is contained in:
RunasSudo 2021-08-09 00:17:14 +10:00
parent ae0d1d8411
commit 46e895ee5a
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -181,8 +181,6 @@ where
count_card.votes.assign(state.quota.as_ref().unwrap()); count_card.votes.assign(state.quota.as_ref().unwrap());
checksum -= surplus; checksum -= surplus;
count_card.finalised = true; // Mark surpluses as done
// Update loss by fraction // Update loss by fraction
state.loss_fraction.transfer(&-checksum); state.loss_fraction.transfer(&-checksum);
} }
@ -209,6 +207,7 @@ where
let surplus = &state.candidates[elected_candidate].votes - state.quota.as_ref().unwrap(); let surplus = &state.candidates[elected_candidate].votes - state.quota.as_ref().unwrap();
state.exhausted.transfer(&surplus); state.exhausted.transfer(&surplus);
state.candidates.get_mut(elected_candidate).unwrap().transfer(&-surplus); state.candidates.get_mut(elected_candidate).unwrap().transfer(&-surplus);
break;
} }
} }
} }
@ -236,30 +235,34 @@ where
while &state.candidates[elected_candidate].votes > state.quota.as_ref().unwrap() { while &state.candidates[elected_candidate].votes > state.quota.as_ref().unwrap() {
// Transfer one vote to next available preference // Transfer one vote to next available preference
let vote = numbered_votes.remove(&index).unwrap(); match numbered_votes.remove(&index) {
Some(vote) => {
transfer_ballot(state, opts, elected_candidate, vote, opts.transferable_only)?; transfer_ballot(state, opts, elected_candidate, vote, opts.transferable_only)?;
if state.num_elected == state.election.seats { if state.num_elected == state.election.seats {
return Ok(()); return Ok(());
} }
}
index += skip_value; None => {
if index >= total_ballots {
iteration += 1;
index = iteration + skip_value - 1;
if iteration >= skip_value {
// We have run out of ballot papers // We have run out of ballot papers
// Remaining ballot papers exhaust // Remaining ballot papers exhaust
let surplus = &state.candidates[elected_candidate].votes - state.quota.as_ref().unwrap(); let surplus = &state.candidates[elected_candidate].votes - state.quota.as_ref().unwrap();
state.exhausted.transfer(&surplus); state.exhausted.transfer(&surplus);
state.candidates.get_mut(elected_candidate).unwrap().transfer(&-surplus); state.candidates.get_mut(elected_candidate).unwrap().transfer(&-surplus);
break; break;
} }
} }
index += skip_value;
if index >= total_ballots {
iteration += 1;
index = iteration + skip_value - 1;
} }
} }
} }
}
let count_card = state.candidates.get_mut(elected_candidate).unwrap();
count_card.finalised = true; // Mark surpluses as done
return Ok(()); return Ok(());
} }