Fix error with forwards/backwards tiebreaking on first stage
This commit is contained in:
parent
85b695c133
commit
e7bae376e9
30
src/ties.rs
30
src/ties.rs
@ -67,32 +67,42 @@ impl TieStrategy {
|
||||
pub fn choose_highest<'c, N: Number>(&self, state: &mut CountState<N>, opts: &STVOptions, candidates: &Vec<&'c Candidate>, prompt_text: &str) -> Result<&'c Candidate, STVError> {
|
||||
match self {
|
||||
Self::Forwards => {
|
||||
match &state.forwards_tiebreak {
|
||||
Some(tb) => {
|
||||
let mut candidates = candidates.clone();
|
||||
candidates.sort_unstable_by(|a, b|
|
||||
// Compare b to a to sort high-to-low
|
||||
state.forwards_tiebreak.as_ref().unwrap()[b]
|
||||
.cmp(&state.forwards_tiebreak.as_ref().unwrap()[a])
|
||||
);
|
||||
if state.forwards_tiebreak.as_ref().unwrap()[candidates[0]] == state.forwards_tiebreak.as_ref().unwrap()[candidates[1]] {
|
||||
candidates.sort_unstable_by(|a, b| tb[b].cmp(&tb[a]));
|
||||
if tb[candidates[0]] == tb[candidates[1]] {
|
||||
return Err(STVError::UnresolvedTie);
|
||||
} else {
|
||||
state.logger.log_literal(format!("Tie between {} broken forwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
|
||||
return Ok(candidates[0]);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// First stage
|
||||
return Err(STVError::UnresolvedTie);
|
||||
}
|
||||
}
|
||||
}
|
||||
Self::Backwards => {
|
||||
match &state.backwards_tiebreak {
|
||||
Some(tb) => {
|
||||
let mut candidates = candidates.clone();
|
||||
candidates.sort_unstable_by(|a, b|
|
||||
state.backwards_tiebreak.as_ref().unwrap()[b]
|
||||
.cmp(&state.backwards_tiebreak.as_ref().unwrap()[a])
|
||||
);
|
||||
if state.backwards_tiebreak.as_ref().unwrap()[candidates[0]] == state.backwards_tiebreak.as_ref().unwrap()[candidates[1]] {
|
||||
candidates.sort_unstable_by(|a, b| tb[b].cmp(&tb[a]));
|
||||
if tb[candidates[0]] == tb[candidates[1]] {
|
||||
return Err(STVError::UnresolvedTie);
|
||||
} else {
|
||||
state.logger.log_literal(format!("Tie between {} broken backwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
|
||||
return Ok(candidates[0]);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// First stage
|
||||
return Err(STVError::UnresolvedTie);
|
||||
}
|
||||
}
|
||||
}
|
||||
Self::Random(_) => {
|
||||
state.logger.log_literal(format!("Tie between {} broken at random.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
|
||||
return Ok(candidates[state.random.as_mut().unwrap().next(candidates.len())]);
|
||||
|
Loading…
Reference in New Issue
Block a user