Fix bug when excluding candidate with 0 votes
This commit is contained in:
parent
a595f2ff6b
commit
5189a74010
@ -445,53 +445,57 @@ where
|
|||||||
panic!("Invalid --exclusion");
|
panic!("Invalid --exclusion");
|
||||||
}
|
}
|
||||||
|
|
||||||
let value = &votes[0].value / &votes[0].ballot.orig_value;
|
|
||||||
|
|
||||||
// Count next preferences
|
|
||||||
let result = next_preferences(state, votes);
|
|
||||||
|
|
||||||
if opts.exclusion == "one_round" {
|
|
||||||
state.logger.log_literal(format!("Transferring {:.0} ballot papers, totalling {:.2} votes.", result.total_ballots, result.total_votes));
|
|
||||||
} else if opts.exclusion == "by_value" {
|
|
||||||
state.logger.log_literal(format!("Transferring {:.0} ballot papers, totalling {:.2} votes, received at value {:.2}.", result.total_ballots, result.total_votes, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transfer candidate votes
|
|
||||||
let mut checksum = N::new();
|
let mut checksum = N::new();
|
||||||
|
|
||||||
for (candidate, entry) in result.candidates.into_iter() {
|
if votes.len() > 0 {
|
||||||
let parcel = entry.votes as Parcel<N>;
|
let value = &votes[0].value / &votes[0].ballot.orig_value;
|
||||||
let count_card = state.candidates.get_mut(candidate).unwrap();
|
|
||||||
count_card.parcels.push(parcel);
|
|
||||||
|
|
||||||
// Round transfers
|
// Count next preferences
|
||||||
let mut candidate_transfers = entry.num_votes;
|
let result = next_preferences(state, votes);
|
||||||
if let Some(dps) = opts.round_votes {
|
|
||||||
candidate_transfers.floor_mut(dps);
|
if opts.exclusion == "one_round" {
|
||||||
|
state.logger.log_literal(format!("Transferring {:.0} ballot papers, totalling {:.2} votes.", result.total_ballots, result.total_votes));
|
||||||
|
} else if opts.exclusion == "by_value" {
|
||||||
|
state.logger.log_literal(format!("Transferring {:.0} ballot papers, totalling {:.2} votes, received at value {:.2}.", result.total_ballots, result.total_votes, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transfer candidate votes
|
||||||
|
for (candidate, entry) in result.candidates.into_iter() {
|
||||||
|
let parcel = entry.votes as Parcel<N>;
|
||||||
|
let count_card = state.candidates.get_mut(candidate).unwrap();
|
||||||
|
count_card.parcels.push(parcel);
|
||||||
|
|
||||||
|
// Round transfers
|
||||||
|
let mut candidate_transfers = entry.num_votes;
|
||||||
|
if let Some(dps) = opts.round_votes {
|
||||||
|
candidate_transfers.floor_mut(dps);
|
||||||
|
}
|
||||||
|
count_card.transfer(&candidate_transfers);
|
||||||
|
checksum += candidate_transfers;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transfer exhausted votes
|
||||||
|
let parcel = result.exhausted.votes as Parcel<N>;
|
||||||
|
state.exhausted.parcels.push(parcel);
|
||||||
|
|
||||||
|
let mut exhausted_transfers = result.exhausted.num_votes;
|
||||||
|
if let Some(dps) = opts.round_votes {
|
||||||
|
exhausted_transfers.floor_mut(dps);
|
||||||
|
}
|
||||||
|
state.exhausted.transfer(&exhausted_transfers);
|
||||||
|
checksum += exhausted_transfers;
|
||||||
|
|
||||||
|
if votes_remaining > 0 {
|
||||||
|
// Subtract from candidate tally
|
||||||
|
let count_card = state.candidates.get_mut(excluded_candidate).unwrap();
|
||||||
|
checksum -= &result.total_votes;
|
||||||
|
count_card.transfer(&-result.total_votes);
|
||||||
|
|
||||||
|
// By definition, there is no loss by fraction
|
||||||
}
|
}
|
||||||
count_card.transfer(&candidate_transfers);
|
|
||||||
checksum += candidate_transfers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transfer exhausted votes
|
if votes_remaining == 0 {
|
||||||
let parcel = result.exhausted.votes as Parcel<N>;
|
|
||||||
state.exhausted.parcels.push(parcel);
|
|
||||||
|
|
||||||
let mut exhausted_transfers = result.exhausted.num_votes;
|
|
||||||
if let Some(dps) = opts.round_votes {
|
|
||||||
exhausted_transfers.floor_mut(dps);
|
|
||||||
}
|
|
||||||
state.exhausted.transfer(&exhausted_transfers);
|
|
||||||
checksum += exhausted_transfers;
|
|
||||||
|
|
||||||
if votes_remaining > 0 {
|
|
||||||
// Subtract from candidate tally
|
|
||||||
let count_card = state.candidates.get_mut(excluded_candidate).unwrap();
|
|
||||||
checksum -= &result.total_votes;
|
|
||||||
count_card.transfer(&-result.total_votes);
|
|
||||||
|
|
||||||
// By definition, there is no loss by fraction
|
|
||||||
} else {
|
|
||||||
// Finalise candidate votes
|
// Finalise candidate votes
|
||||||
let count_card = state.candidates.get_mut(excluded_candidate).unwrap();
|
let count_card = state.candidates.get_mut(excluded_candidate).unwrap();
|
||||||
checksum -= &count_card.votes;
|
checksum -= &count_card.votes;
|
||||||
|
Loading…
Reference in New Issue
Block a user