From e7bae376e923937f3b62bd83a4a812aea8084a53 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Fri, 20 Aug 2021 02:06:45 +1000 Subject: [PATCH] Fix error with forwards/backwards tiebreaking on first stage --- src/ties.rs | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/ties.rs b/src/ties.rs index ab3a775..76b23c5 100644 --- a/src/ties.rs +++ b/src/ties.rs @@ -67,30 +67,40 @@ impl TieStrategy { pub fn choose_highest<'c, N: Number>(&self, state: &mut CountState, 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(_) => {