/* 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 .
*/
#![allow(mutable_borrow_reservation_conflict)]
//#[cfg(target_arch = "wasm32")]
pub mod wasm;
use crate::numbers::Number;
use crate::election::{Candidate, CandidateState, CountCard, CountState, Parcel, Vote};
use wasm_bindgen::prelude::wasm_bindgen;
use std::collections::HashMap;
use std::ops;
#[wasm_bindgen]
pub struct STVOptions {
pub round_votes: Option,
pub surplus: SurplusMethod,
pub surplus_order: SurplusOrder,
pub transferable_only: bool,
pub exclusion: ExclusionMethod,
pub pp_decimals: usize,
}
#[wasm_bindgen]
impl STVOptions {
pub fn new(
round_votes: Option,
surplus: &str,
surplus_order: &str,
transferable_only: bool,
exclusion: &str,
pp_decimals: usize,
) -> Self {
return STVOptions {
round_votes: round_votes,
surplus: match surplus {
"wig" => SurplusMethod::WIG,
"uig" => SurplusMethod::UIG,
"eg" => SurplusMethod::EG,
"meek" => SurplusMethod::Meek,
_ => panic!("Invalid --surplus"),
},
surplus_order: match surplus_order {
"by_size" => SurplusOrder::BySize,
"by_order" => SurplusOrder::ByOrder,
_ => panic!("Invalid --surplus-order"),
},
transferable_only: transferable_only,
exclusion: match exclusion {
"single_stage" => ExclusionMethod::SingleStage,
"by_value" => ExclusionMethod::ByValue,
"parcels_by_order" => ExclusionMethod::ParcelsByOrder,
_ => panic!("Invalid --exclusion"),
},
pp_decimals: pp_decimals,
};
}
}
#[wasm_bindgen]
#[derive(Clone, Copy)]
pub enum SurplusMethod {
WIG,
UIG,
EG,
Meek,
}
#[wasm_bindgen]
#[derive(Clone, Copy)]
pub enum SurplusOrder {
BySize,
ByOrder,
}
#[wasm_bindgen]
#[derive(Clone, Copy)]
pub enum ExclusionMethod {
SingleStage,
ByValue,
ParcelsByOrder,
}
pub fn count_init(mut state: &mut CountState<'_, N>, opts: &STVOptions) {
distribute_first_preferences(&mut state);
calculate_quota(&mut state, opts);
elect_meeting_quota(&mut state);
}
pub fn count_one_stage(mut state: &mut CountState<'_, N>, opts: &STVOptions) -> bool
where
for<'r> &'r N: ops::Sub<&'r N, Output=N>,
for<'r> &'r N: ops::Div<&'r N, Output=N>,
for<'r> &'r N: ops::Neg