Struct tallystick::approval::ApprovalTally [−][src]
pub struct ApprovalTally<T, C = u64> where
T: Eq + Clone + Hash,
C: Copy + PartialOrd + AddAssign + Num + NumCast, { /* fields omitted */ }
Expand description
A generic approval 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 (egf64
for fractional vote weights).
Example
use tallystick::approval::ApprovalTally; // An election for Judge using floats as the count type. let mut tally = ApprovalTally::<&str, f64>::new(1); tally.add_weighted(vec!["Judge Judy", "Notorious RBG"], 0.5); tally.add_weighted(vec!["Judge Dredd"], 2.0); tally.add_weighted(vec!["Abe Vigoda", "Notorious RBG"], 3.2); tally.add_weighted(vec!["Judge Dredd", "Notorious RBG"], 1.0); let winners = tally.winners().into_unranked(); assert!(winners[0] == "Notorious RBG");
Implementations
impl<T, C> ApprovalTally<T, C> where
T: Eq + Clone + Hash,
C: Copy + PartialOrd + AddAssign + Num + NumCast,
impl<T, C> ApprovalTally<T, C> where
T: Eq + Clone + Hash,
C: Copy + PartialOrd + AddAssign + Num + NumCast,
Create a new ApprovalTally
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.)
Create a new ApprovalTally
with the given number of winners, and number of expected candidates.
Add a weighted vote.
By default takes a weight as a usize
integer, but can be customized by using ApprovalTally
with a custom vote type.
Add a weighted vote by reference.
Get a list of all candidates seen by this tally. Candidates are returned in no particular order.
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 approval voting, the winning candidate(s) is the one most approved by all voters.
Get vote totals for this tally.
Each candidate has a total thhat is equal to the number of voters that approve of that candidate.
If vote weights are used, then each candidates total is equal to the weighted sum of the votes that include that candidate.
Example
use tallystick::approval::DefaultApprovalTally; let mut tally = DefaultApprovalTally::new(1); tally.add_weighted(vec!["Alice", "Bob"], 30); tally.add_weighted(vec!["Bob", "Carol"], 10); for (candidate, num_votes) in tally.totals().iter() { println!("{} got {} votes", candidate, num_votes); } // Prints: // Alice got 30 votes // Bob got 40 votes // Carol got 10 votes
Get a ranked list of all candidates. Candidates with the same rank are tied.
Candidates are ranked in ascending order. The highest ranked candidate has a rank of 0
.
Example
use tallystick::approval::DefaultApprovalTally; let mut tally = DefaultApprovalTally::new(1); tally.add_weighted(vec!["Alice", "Bob"], 30); tally.add_weighted(vec!["Bob", "Carol"], 10); for ranked in tally.ranked().iter() { println!("{} has a rank of {}", ranked.candidate, ranked.rank); } // Prints: // Bob has a rank of 0 // Alice has a rank of 1 // Carol has a rank of 2
Auto Trait Implementations
impl<T, C> RefUnwindSafe for ApprovalTally<T, C> where
C: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, C> Send for ApprovalTally<T, C> where
C: Send,
T: Send,
impl<T, C> Sync for ApprovalTally<T, C> where
C: Sync,
T: Sync,
impl<T, C> Unpin for ApprovalTally<T, C> where
C: Unpin,
T: Unpin,
impl<T, C> UnwindSafe for ApprovalTally<T, C> where
C: UnwindSafe,
T: UnwindSafe,