[][src]Struct tallystick::borda::BordaTally

pub struct BordaTally<T, C = u64> where
    T: Eq + Clone + Hash,
    C: Copy + PartialOrd + AddAssign + Num + NumCast
{ /* fields omitted */ }

A generic borda tally.

Generics:

  • T: The candidate type.
  • C: The count type. u64 is recommended, but can be modified to use a different type for counting votes (eg f64 for fractional vote weights). If using Variant::Dowdall then a float, a rational, or anyting that implements Real must be used.

Example:

   use tallystick::borda::BordaTally;
   use tallystick::borda::Variant;

   // A tally with string candidates, one winner, `f64` counting, using the Dowall point system.
   let mut tally = BordaTally::<&str, f64>::new(1, Variant::Dowdall);
   tally.add(vec!["Alice", "Bob", "Carlos"]);
   tally.add(vec!["Bob", "Carlos", "Alice"]);
   tally.add(vec!["Alice", "Carlos", "Bob"]);
   tally.add(vec!["Alice", "Bob", "Carlos"]);

   let winners = tally.winners();

Methods

impl<T, C> BordaTally<T, C> where
    T: Eq + Clone + Hash,
    C: Copy + PartialOrd + AddAssign + Num + NumCast
[src]

pub fn new(num_winners: u32, variant: Variant<C>) -> Self[src]

Create a new BordaTally with the given number of winners.

If there is a tie, the number of winners might be more than num_winners. (See winners() for more information on ties.)

pub fn with_capacity(
    num_winners: u32,
    variant: Variant<C>,
    expected_candidates: usize
) -> Self
[src]

Create a new BordaTally with the given number of winners, and number of expected candidates.

pub fn add(&mut self, vote: Vec<T>) -> Result<(), TallyError>[src]

Add a new vote

Votes are represented as a vector of ranked candidates, ordered by preference. An error will only be returned if vote contains duplicate candidates.

pub fn add_ref(&mut self, vote: &[T]) -> Result<(), TallyError>[src]

Add a new vote by reference

pub fn add_weighted(
    &mut self,
    vote: Vec<T>,
    weight: C
) -> Result<(), TallyError>
[src]

Add a weighted vote. By default takes a weight as a usize integer, but can be customized by using BordaTally with a custom vote type.

pub fn add_weighted_ref(
    &mut self,
    vote: &[T],
    weight: C
) -> Result<(), TallyError>
[src]

Add a weighted vote by reference

pub fn winners(&self) -> RankedWinners<T>[src]

Get a ranked list of winners. Winners with the same rank are tied. The number of winners might be greater than the requested num_winners if there is a tie. In a borda count, the winners are determine by what candidate obtains the most points.

pub fn ranked(&self) -> Vec<(T, u32)>[src]

Get a ranked list of all candidates. Candidates with the same rank are tied.

pub fn totals(&self) -> Vec<(T, C)>[src]

Get point totals for this tally.

This will return a vector with the number of borda points for each candidate.

Example

   use tallystick::borda::DefaultBordaTally;
   use tallystick::borda::Variant;

   let mut tally = DefaultBordaTally::new(1, Variant::ClassicBorda);
   for _ in 0..30 { tally.add(vec!["Alice", "Bob"]).unwrap() }
   for _ in 0..10 { tally.add(vec!["Bob", "Alice"]).unwrap() }

   for (candidate, num_points) in tally.totals().iter() {
      println!("{} has {} points", candidate, num_points);
   }
   // Prints:
   //   Alice has 70 points
   //   Bob has 30 points

pub fn candidates(&self) -> Vec<T>[src]

Get a list of all candidates seen by this tally. Candidates are returned in no particular order.

Auto Trait Implementations

impl<T, C = u64> !RefUnwindSafe for BordaTally<T, C>

impl<T, C = u64> !Send for BordaTally<T, C>

impl<T, C = u64> !Sync for BordaTally<T, C>

impl<T, C> Unpin for BordaTally<T, C> where
    C: Unpin,
    T: Unpin

impl<T, C = u64> !UnwindSafe for BordaTally<T, C>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.