/* OpenTally: Open-source election vote counting
* Copyright © 2021 Lee Yingtong Li (RunasSudo)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
use crate::logger::Logger;
use crate::numbers::Number;
use crate::sharandom::SHARandom;
use std::collections::HashMap;
/// An election to be counted
pub struct Election {
pub name: String,
pub seats: usize,
pub candidates: Vec,
pub withdrawn_candidates: Vec,
pub ballots: Vec>,
}
impl Election {
/// Parse the given BLT file and return an [Election]
pub fn from_blt>(mut lines: I) -> Self {
// Read first line
let line = lines.next().expect("Unexpected EOF");
let mut bits = line.split(" ");
let num_candidates = bits.next().expect("Syntax Error").parse().expect("Syntax Error");
let seats: usize = bits.next().expect("Syntax Error").parse().expect("Syntax Error");
// Initialise the Election object
let mut election = Election {
name: String::new(),
seats: seats,
candidates: Vec::with_capacity(num_candidates),
withdrawn_candidates: Vec::new(),
ballots: Vec::new(),
};
// Read ballots
for line in &mut lines {
if line == "0" {
break;
}
let mut bits = line.split(" ");
if line.starts_with("-") {
// Withdrawn candidates
for bit in bits.into_iter() {
let val = bit[1..bit.len()].parse::().expect("Syntax Error");
election.withdrawn_candidates.push(val - 1);
}
continue;
}
let value = N::parse(bits.next().expect("Syntax Error"));
let mut ballot = Ballot {
orig_value: value,
preferences: Vec::new(),
};
for preference in bits {
if preference != "0" {
let preference = preference.parse::().expect("Syntax Error");
ballot.preferences.push(preference - 1);
}
}
election.ballots.push(ballot);
}
// Read candidates
for line in lines.by_ref().take(num_candidates) {
let mut line = &line[..];
if line.starts_with("\"") && line.ends_with("\"") {
line = &line[1..line.len()-1];
}
election.candidates.push(Candidate { name: line.to_string() });
}
// Read name
let line = lines.next().expect("Syntax Error");
let mut line = &line[..];
if line.starts_with("\"") && line.ends_with("\"") {
line = &line[1..line.len()-1];
}
election.name.push_str(line);
return election;
}
/// Convert ballots with weight >1 to multiple ballots of weight 1
pub fn normalise_ballots(&mut self) {
let mut normalised_ballots = Vec::new();
for ballot in self.ballots.iter() {
let mut n = N::new();
let one = N::one();
while n < ballot.orig_value {
let new_ballot = Ballot {
orig_value: N::one(),
preferences: ballot.preferences.clone(),
};
normalised_ballots.push(new_ballot);
n += &one;
}
}
self.ballots = normalised_ballots;
}
}
/// A candidate in an [Election]
#[derive(PartialEq, Eq, Hash)]
pub struct Candidate {
pub name: String,
}
/// The current state of counting an [Election]
//#[derive(Clone)]
pub struct CountState<'a, N: Number> {
pub election: &'a Election,
pub candidates: HashMap<&'a Candidate, CountCard<'a, N>>,
pub exhausted: CountCard<'a, N>,
pub loss_fraction: CountCard<'a, N>,
pub ballot_tree: Option>,
pub forwards_tiebreak: Option>,
pub backwards_tiebreak: Option>,
pub random: Option>,
pub quota: Option,
pub vote_required_election: Option,
pub num_elected: usize,
pub num_excluded: usize,
pub kind: Option<&'a str>,
pub title: String,
pub logger: Logger<'a>,
}
impl<'a, N: Number> CountState<'a, N> {
/// Construct a new blank [CountState] for the given [Election]
pub fn new(election: &'a Election) -> Self {
let mut state = CountState {
election: &election,
candidates: HashMap::new(),
exhausted: CountCard::new(),
loss_fraction: CountCard::new(),
ballot_tree: None,
forwards_tiebreak: None,
backwards_tiebreak: None,
random: None,
quota: None,
vote_required_election: None,
num_elected: 0,
num_excluded: 0,
kind: None,
title: String::new(),
logger: Logger { entries: Vec::new() },
};
for candidate in election.candidates.iter() {
state.candidates.insert(candidate, CountCard::new());
}
for withdrawn_idx in election.withdrawn_candidates.iter() {
state.candidates.get_mut(&election.candidates[*withdrawn_idx]).unwrap().state = CandidateState::Withdrawn;
}
return state;
}
/// [Step](CountCard::step) every [CountCard] to prepare for the next stage
pub fn step_all(&mut self) {
for (_, count_card) in self.candidates.iter_mut() {
count_card.step();
}
self.exhausted.step();
self.loss_fraction.step();
}
}
/// Represents either a reference to a [CountState] or a clone
#[allow(dead_code)]
pub enum CountStateOrRef<'a, N: Number> {
State(CountState<'a, N>), // NYI: May be used e.g. for tie-breaking or rollback-based constraints
Ref(&'a CountState<'a, N>),
}
impl<'a, N: Number> CountStateOrRef<'a, N> {
/// Construct a [CountStateOrRef] as a reference to a [CountState]
pub fn from(state: &'a CountState) -> Self {
return Self::Ref(state);
}
/// Return a reference to the underlying [CountState]
pub fn as_ref(&self) -> &CountState {
match self {
CountStateOrRef::State(state) => &state,
CountStateOrRef::Ref(state) => state,
}
}
}
/// Result of a stage of counting
pub struct StageResult<'a, N: Number> {
pub kind: Option<&'a str>,
pub title: &'a String,
pub logs: Vec,
pub state: CountStateOrRef<'a, N>,
}
/// Current state of a [Candidate] during an election count
#[derive(Clone)]
pub struct CountCard<'a, N> {
pub state: CandidateState,
pub order_elected: isize,
//pub orig_votes: N,
pub transfers: N,
pub votes: N,
pub parcels: Vec>,
/// Candidate's keep value (Meek STV)
pub keep_value: Option,
}
impl<'a, N: Number> CountCard<'a, N> {
/// Returns a new blank [CountCard]
pub fn new() -> Self {
return CountCard {
state: CandidateState::Hopeful,
order_elected: 0,
//orig_votes: N::new(),
transfers: N::new(),
votes: N::new(),
parcels: Vec::new(),
keep_value: None,
};
}
/// Transfer the given number of votes to this [CountCard], incrementing [transfers](CountCard::transfers) and [votes](CountCard::votes)
pub fn transfer(&mut self, transfer: &'_ N) {
self.transfers += transfer;
self.votes += transfer;
}
/// Set [transfers](CountCard::transfers) to 0
pub fn step(&mut self) {
//self.orig_votes = self.votes.clone();
self.transfers = N::new();
}
}
/// Parcel of [Vote]s during a count
pub type Parcel<'a, N> = Vec>;
/// Represents a [Ballot] with an associated value
#[derive(Clone)]
pub struct Vote<'a, N> {
pub ballot: &'a Ballot,
pub value: N,
/// Index of the next preference to examine
pub up_to_pref: usize,
}
/// A record of a voter's preferences
pub struct Ballot {
pub orig_value: N,
pub preferences: Vec,
}
/// State of a [Candidate] during a count
#[allow(dead_code)]
#[derive(PartialEq)]
#[derive(Clone)]
pub enum CandidateState {
Hopeful,
Guarded,
Elected,
Doomed,
Withdrawn,
Excluded,
}