Fix crash on attempting segmented exclusion of candidate with no votes

This commit is contained in:
RunasSudo 2021-07-19 18:35:23 +10:00
parent d144ab0cb4
commit 7f16090395
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
1 changed files with 49 additions and 37 deletions

View File

@ -391,16 +391,22 @@ where
}
ExclusionMethod::ByValue => {
// Exclude by value
let max_value = excluded_candidates.iter()
.map(|c| state.candidates[c].parcels.iter()
let excluded_with_votes: Vec<&&Candidate> = excluded_candidates.iter().filter(|c| !state.candidates[*c].parcels.is_empty()).collect();
if excluded_with_votes.is_empty() {
votes_remain = false;
} else {
// If candidates to exclude still having votes, select only those with the greatest value
let max_value = excluded_with_votes.iter()
.map(|c| state.candidates[*c].parcels.iter()
.map(|p| p.iter().map(|v| &v.value / &v.ballot.orig_value).max().unwrap())
.max().unwrap())
.max().unwrap();
votes_remain = false;
for excluded_candidate in excluded_candidates.iter() {
let count_card = state.candidates.get_mut(excluded_candidate).unwrap();
for excluded_candidate in excluded_with_votes.iter() {
let count_card = state.candidates.get_mut(*excluded_candidate).unwrap();
// Filter out just those votes with max_value
let mut remaining_votes = Vec::new();
@ -429,6 +435,7 @@ where
count_card.transfer(&-votes_transferred);
}
}
}
ExclusionMethod::ParcelsByOrder => {
// Exclude by parcel by order
if excluded_candidates.len() > 1 {
@ -436,6 +443,10 @@ where
}
let count_card = state.candidates.get_mut(excluded_candidates[0]).unwrap();
if count_card.parcels.is_empty() {
votes_remain = false;
} else {
votes = count_card.parcels.remove(0);
votes_remain = !count_card.parcels.is_empty();
@ -444,6 +455,7 @@ where
checksum -= &votes_transferred;
count_card.transfer(&-votes_transferred);
}
}
_ => panic!()
}