Function sainte_lague::distribute[][src]

pub fn distribute(
    votes: &[f64],
    seat_count: &usize,
    draw_on_tie: &bool
) -> Result<Vec<usize>, DistributionError>

Calculate the Sainte-Laguë distribution for the given votes and a parliament of size seat_count. Note that while votes are usually restricted to integers in normal elections, this function expects floating point numbers, allowing additional use cases.

The draw_on_tie flag should be used to indicate if the method should randomly assign seats in case of a draw or return an error instead.

Check DistributionError for a list of all possible error cases.

Examples

Note that, while you would usually use the absolute vote counts to calculate a distribution, as explained before, the method is not restricted to integer input, so you can also use relative vote shares, as in in this example:

use sainte_lague::distribute;

let votes_german_bundestag_2013 = [41.5, 25.7, 8.6, 8.4];
let seats_german_bundestag_2013 = 631;
let draw_on_tie = false;

let distribution = distribute(&votes_german_bundestag_2013, &seats_german_bundestag_2013, &draw_on_tie);
let parliament: Vec<usize> = vec![311, 193, 64, 63];
assert_eq!(distribution, Ok(parliament));

The draw_on_tie flag is relevant in case of a draw:

use sainte_lague::{distribute, DistributionError};

let votes = [3.0, 3.0, 1.0];
let seats = 8;

let distribution_without_draw = distribute(&votes, &seats, &false);
assert_eq!(distribution_without_draw, Err(DistributionError::Tied));

let distribution_with_draw = distribute(&votes, &seats, &true);
let parliament_draw_possibility_a: Vec<usize> = vec![4, 3, 1];
let parliament_draw_possibility_b: Vec<usize> = vec![3, 4, 1];
assert_eq!(
    [Ok(parliament_draw_possibility_a), Ok(parliament_draw_possibility_b)]
        .iter()
        .any(|x| x == &distribution_with_draw),
    true
);