Crate electosim

Source
Expand description

§ElectoSIM

ElectoSIM is a library that allows you to simulate simple elections using different methods.

§Methods

The following methods are available:

  • D’Hondt
  • Webster/Sainte-Laguë
  • Adams
  • Imperiali
  • Huntington-Hill
  • Danish
  • Hare-Niemeyer
  • Hagenbach-Bischoff
  • Imperiali - Quotient
  • Droop
  • Winner Takes All

§Usage

use electosim::*;

fn main() {
   let mut election = election!(
        vec![
            candidacy!(2010, 9),
            candidacy!(1018, 4),
            candidacy!(86, 0),
            candidacy!(77, 0),
        ],
        13,
        Method::HAGENBASCHBISCHOFF,
        0.1
   );

    election.compute().expect("Can not compute method");
    election.results.iter().for_each(|c| println!("{:?}", c));
}

The first statement in the main function creates a new SimpleElection with the candidates, the number of seats available, and the method to be used. The compute method is then called to compute the election results. Finally, the results are printed to the console.

§compute_ functions

A method is a function with type fn(&mut Vec<T>, u16) -> Result<(), &str> where T is a type that implements the WithVotes and WithSeats traits. You can use the compute_ functions directly if you want to compute the election results without using the SimpleElection struct. For example:

use electosim::*;
use electosim::methods::divisor::compute_dhondt;

fn main() {
   let mut candidacies = vec![
        candidacy!(2010, 0),
        candidacy!(1018, 0),
        candidacy!(86, 0),
        candidacy!(77, 0),
    ];

   compute_dhondt(&mut candidacies, 13).unwrap();

   candidacies.iter().for_each(|c| println!("{:?}", c));
}

There are some implementations of the compute_ functions in the methods::divisor (ex: D’hondt) and methods::remainder (ex: Hare) modules.

Re-exports§

pub use methods::Method;
pub use models::Candidacy;

Modules§

interface
macros
methods
models
utils
Utils

Macros§

candidacy
election
Creates a new [SimpleElection] struct.

Structs§

SimpleElection
Represents a simple election.