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

View File

@ -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> { 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 { match self {
Self::Forwards => { Self::Forwards => {
match &state.forwards_tiebreak {
Some(tb) => {
let mut candidates = candidates.clone(); let mut candidates = candidates.clone();
candidates.sort_unstable_by(|a, b|
// Compare b to a to sort high-to-low // Compare b to a to sort high-to-low
state.forwards_tiebreak.as_ref().unwrap()[b] candidates.sort_unstable_by(|a, b| tb[b].cmp(&tb[a]));
.cmp(&state.forwards_tiebreak.as_ref().unwrap()[a]) if tb[candidates[0]] == tb[candidates[1]] {
);
if state.forwards_tiebreak.as_ref().unwrap()[candidates[0]] == state.forwards_tiebreak.as_ref().unwrap()[candidates[1]] {
return Err(STVError::UnresolvedTie); return Err(STVError::UnresolvedTie);
} else { } else {
state.logger.log_literal(format!("Tie between {} broken forwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect()))); state.logger.log_literal(format!("Tie between {} broken forwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
return Ok(candidates[0]); return Ok(candidates[0]);
} }
} }
None => {
// First stage
return Err(STVError::UnresolvedTie);
}
}
}
Self::Backwards => { Self::Backwards => {
match &state.backwards_tiebreak {
Some(tb) => {
let mut candidates = candidates.clone(); let mut candidates = candidates.clone();
candidates.sort_unstable_by(|a, b| candidates.sort_unstable_by(|a, b| tb[b].cmp(&tb[a]));
state.backwards_tiebreak.as_ref().unwrap()[b] if tb[candidates[0]] == tb[candidates[1]] {
.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); return Err(STVError::UnresolvedTie);
} else { } else {
state.logger.log_literal(format!("Tie between {} broken backwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect()))); state.logger.log_literal(format!("Tie between {} broken backwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
return Ok(candidates[0]); return Ok(candidates[0]);
} }
} }
None => {
// First stage
return Err(STVError::UnresolvedTie);
}
}
}
Self::Random(_) => { Self::Random(_) => {
state.logger.log_literal(format!("Tie between {} broken at random.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect()))); 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())]); return Ok(candidates[state.random.as_mut().unwrap().next(candidates.len())]);