Fix error with forwards/backwards tiebreaking on first stage

This commit is contained in:
RunasSudo 2021-08-20 02:06:45 +10:00
parent 85b695c133
commit e7bae376e9
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
1 changed files with 31 additions and 21 deletions

View File

@ -67,30 +67,40 @@ 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 => {
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]] {
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]);
match &state.forwards_tiebreak {
Some(tb) => {
let mut candidates = candidates.clone();
// Compare b to a to sort high-to-low
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 => {
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]] {
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]);
match &state.backwards_tiebreak {
Some(tb) => {
let mut candidates = candidates.clone();
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(_) => {