Implement --surplus-order

This commit is contained in:
RunasSudo 2021-06-01 18:57:56 +10:00
parent 76d69913c7
commit a4a28f3345
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 25 additions and 2 deletions

View File

@ -72,6 +72,9 @@ struct STV {
#[clap(help_heading=Some("STV VARIANTS"), long, possible_values=&["wig", "uig", "eg", "meek"], default_value="wig", value_name="method")]
surplus: String,
#[clap(help_heading=Some("STV VARIANTS"), long, possible_values=&["by_size", "by_order"], default_value="by_size", value_name="order")]
surplus_order: String,
/// Examine only transferable papers during surplus distributions
#[clap(help_heading=Some("STV VARIANTS"), long)]
transferable_only: bool,
@ -131,6 +134,11 @@ where
"meek" => stv::SurplusMethod::Meek,
_ => panic!("Invalid --surplus"),
},
surplus_order: match cmd_opts.surplus_order.as_str() {
"by_size" => stv::SurplusOrder::BySize,
"by_order" => stv::SurplusOrder::ByOrder,
_ => panic!("Invalid --surplus-order"),
},
transferable_only: cmd_opts.transferable_only,
exclusion: match cmd_opts.exclusion.as_str() {
"single_stage" => stv::ExclusionMethod::SingleStage,

View File

@ -32,6 +32,7 @@ use std::ops;
pub struct STVOptions {
pub round_votes: Option<usize>,
pub surplus: SurplusMethod,
pub surplus_order: SurplusOrder,
pub transferable_only: bool,
pub exclusion: ExclusionMethod,
pub pp_decimals: usize,
@ -46,6 +47,13 @@ pub enum SurplusMethod {
Meek,
}
#[wasm_bindgen]
#[derive(Clone, Copy)]
pub enum SurplusOrder {
BySize,
ByOrder,
}
#[wasm_bindgen]
#[derive(Clone, Copy)]
pub enum ExclusionMethod {
@ -253,8 +261,14 @@ where
.collect();
if has_surplus.len() > 0 {
// TODO: Different sorting orders
match opts.surplus_order {
SurplusOrder::BySize => {
has_surplus.sort_unstable_by(|a, b| a.1.votes.partial_cmp(&b.1.votes).unwrap());
}
SurplusOrder::ByOrder => {
has_surplus.sort_unstable_by(|a, b| a.1.order_elected.partial_cmp(&b.1.order_elected).unwrap());
}
}
// Distribute top candidate's surplus
// TODO: Handle ties

View File

@ -56,6 +56,7 @@ fn aec_tas19_rational() {
let stv_opts = stv::STVOptions {
round_votes: Some(0),
surplus: stv::SurplusMethod::UIG,
surplus_order: stv::SurplusOrder::ByOrder,
transferable_only: false,
exclusion: stv::ExclusionMethod::ByValue,
pp_decimals: 2,