Fix error with forwards/backwards tiebreaking on first stage
This commit is contained in:
parent
85b695c133
commit
e7bae376e9
52
src/ties.rs
52
src/ties.rs
|
@ -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> {
|
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 => {
|
||||||
let mut candidates = candidates.clone();
|
match &state.forwards_tiebreak {
|
||||||
candidates.sort_unstable_by(|a, b|
|
Some(tb) => {
|
||||||
// Compare b to a to sort high-to-low
|
let mut candidates = candidates.clone();
|
||||||
state.forwards_tiebreak.as_ref().unwrap()[b]
|
// Compare b to a to sort high-to-low
|
||||||
.cmp(&state.forwards_tiebreak.as_ref().unwrap()[a])
|
candidates.sort_unstable_by(|a, b| tb[b].cmp(&tb[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 => {
|
||||||
let mut candidates = candidates.clone();
|
match &state.backwards_tiebreak {
|
||||||
candidates.sort_unstable_by(|a, b|
|
Some(tb) => {
|
||||||
state.backwards_tiebreak.as_ref().unwrap()[b]
|
let mut candidates = candidates.clone();
|
||||||
.cmp(&state.backwards_tiebreak.as_ref().unwrap()[a])
|
candidates.sort_unstable_by(|a, b| tb[b].cmp(&tb[a]));
|
||||||
);
|
if tb[candidates[0]] == tb[candidates[1]] {
|
||||||
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(_) => {
|
||||||
|
|
Loading…
Reference in New Issue