Struct tallystick::score::ScoreTally [−][src]
pub struct ScoreTally<T, C = u64> where
T: Eq + Clone + Hash,
C: Copy + PartialOrd + AddAssign + Num + NumCast, { /* fields omitted */ }
Expand description
A generic score tally.
Generics:
T
: The candidate type.C
: The count and score type.u64
is recommended, but can be modified to use a different type for counting votes (egf64
for fractional scores).
Example
use tallystick::score::ScoreTally; // An election for Judge using floats as the score type. let mut tally = ScoreTally::<&str, f64>::new(1); tally.add(vec![("Judge Judy", 3.5), ("Notorious RBG", 2.5)]); tally.add(vec![("Judge Dredd", 0.5)]); tally.add(vec![("Abe Vigoda", 6.1), ("Notorious RBG", 3.2)]); tally.add(vec![("Judge Dredd", 1.0), ("Notorious RBG", 4.1)]); let winners = tally.winners().into_unranked(); assert!(winners[0] == "Notorious RBG");
Implementations
impl<T, C> ScoreTally<T, C> where
T: Eq + Clone + Hash,
C: Copy + PartialOrd + AddAssign + Num + NumCast,
impl<T, C> ScoreTally<T, C> where
T: Eq + Clone + Hash,
C: Copy + PartialOrd + AddAssign + Num + NumCast,
Create a new ScoreTally
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 ScoreTally
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 score voting, the winning candidate(s) is the one with the highest total score.
Get vote totals for this tally.
Each candidate has a total thhat is equal to the sum of all scores for that candidate.
Example
use tallystick::score::DefaultScoreTally; let mut tally = DefaultScoreTally::new(1); tally.add(vec![("Alice", 30), ("Bob", 10)]); tally.add(vec![("Bob", 10), ("Carol", 5)]); for (candidate, score) in tally.totals().iter() { println!("{} got a score of {}", candidate, score); } // Prints: // Alice got a score of 30 // Bob got a score of 20 // Carol got a score of 5
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::score::DefaultScoreTally; let mut tally = DefaultScoreTally::new(1); tally.add(vec![("Alice", 30), ("Bob", 10)]); tally.add(vec![("Bob", 10), ("Carol", 5)]); for ranked in tally.ranked().iter() { println!("{} has a rank of {}", ranked.candidate, ranked.rank); } // Prints: // Alice has a rank of 0 // Bob has a rank of 1 // Carol has a rank of 2
Auto Trait Implementations
impl<T, C> RefUnwindSafe for ScoreTally<T, C> where
C: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, C> Send for ScoreTally<T, C> where
C: Send,
T: Send,
impl<T, C> Sync for ScoreTally<T, C> where
C: Sync,
T: Sync,
impl<T, C> Unpin for ScoreTally<T, C> where
C: Unpin,
T: Unpin,
impl<T, C> UnwindSafe for ScoreTally<T, C> where
C: UnwindSafe,
T: UnwindSafe,