Count/RankCount.pm

filter: Statementstmt 98.9% 98 / 99 Branchbran 93.7% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod 92.3% 12 / 13 totaltotal 98.0% 146 / 150 CC 15 SCAR 11.4
CC
15
Cov
98%
_RankResult
8.0
Leader
3.0
RankTable
3.0
CRAP 3.1
« Count/Approval.pm ↑ Summary Count/Method/WIGM.pm »
1 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 39 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Vote::Count::RankCount;
6  
7 39 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use List::Util qw( min max sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 39 ▶use Vote::Count::TextTableTiny qw/generate_table/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use Sort::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 39 our $VERSION='2.02';
14  
15 =head1 NAME
16  
17 Vote::Count::RankCount
18  
19 =head1 VERSION 2.02
20  
21 =cut
22  
23 # ABSTRACT: RankCount object for Vote::Count. Toolkit for vote counting.
24  
25 ▶sub _RankResult ( $rawcount ) {
Subroutine _RankResult: called CC=8 SCAR=20.8
26 798 my %rc = ( $rawcount->%* ); # destructive process needs to use a copy.
27 798 my %ordered = ();
28 798 my %byrank = ();
29 798 my $pos = 0;
30 798 my $maxpos = scalar( keys %rc );
31 798 ▶ while ( 0 < scalar( keys %rc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < scalar keys %rc) 3769 798 4567
32 3769 $pos++;
33 3769 my @vrc = values %rc;
34 3769 my $max = max @vrc;
35 3769 for my $k ( keys %rc ) {
36 19774 ▶ if ( $rc{$k} == $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc{$k} == $max) 5249 14525 19774
37 5249 $ordered{$k} = $pos;
38 5249 delete $rc{$k};
39 5249 ▶ if ( defined $byrank{$pos} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $byrank{$pos}) { } 1480 3769 5249
40 1480 push @{ $byrank{$pos} }, $k;
41 }
42 else {
43 3769 $byrank{$pos} = [$k];
44 }
45 }
46 }
47 # uncoverable branch true
48 3769 ▶ die "Vote::Count::RankCount::Rank in infinite loop\n"
Branch: 1 branch
BranchTrueFalseTotal
if $pos > $maxpos 0 3769 3769
49 if $pos > $maxpos;
50 }
51 # %byrank[1] is arrayref of 1st position,
52 # $pos still has last position filled, %byrank{$pos} is the last place.
53 # sometimes byranks came in as var{byrank...} deref and reref fixes this
54 # although it would be better if I understood why it happened.
55 # It is useful to sort the arrays anyway, for display they would likely be
56 # sorted anyway. For testing it makes the element order predictable.
57  
58 798 for my $O ( keys %byrank ) {
59 3769 $byrank{$O} = [ sort $byrank{$O}->@* ];
60 }
61 798 my @top = @{ $byrank{1} };
62 798 my @bottom = @{ $byrank{$pos} };
63 798 ▶ my $tie = scalar(@top) > 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @top > 1 ? : 173 625 798
64 return {
65 798 'rawcount' => $rawcount,
66 'ordered' => \%ordered,
67 'byrank' => \%byrank,
68 'top' => \@top,
69 'bottom' => \@bottom,
70 'tie' => $tie,
71 };
72 }
73  
74 =head2 Rank
75  
76 Takes a single argument of a hashref containing Choices as Keys and Votes as Values. Returns an Object. This method is also aliased as new.
77  
78 =head2 newFromList
79  
80 Takes an ordered list and returns a RankCount Object where the RawCount values are zero minus the position: Item 3 in the list will have -3 votes while Item 1 will have -1.
81  
82 my $ordered_rank_count = Vote::Count::RankCount->newFromList( @ordered_list );
83  
84 =cut
85  
86 ▶sub Rank ( $class, $rawcount ) {
Subroutine Rank: called CC=1 SCAR=0.0
Pod documented
87 797 my $I = _RankResult($rawcount);
88 797 return bless $I, $class;
89 }
90  
91 ▶sub new ( $class, $rawcount ) {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
92 1 my $I = _RankResult($rawcount);
93 1 return bless $I, $class;
94 }
95  
96 ▶sub newFromList ( @list ) {
Subroutine newFromList: called CC=1 SCAR=0.0
Pod documented
97 40 shift @list;
98 40 my $pos = 0;
99 my $newobj = Vote::Count::RankCount->Rank({
100 40 map { $_ => --$pos } @list} );
101 40 $newobj->{'orderedlist'} = \@list;
102 40 return $newobj;
103 }
104  
105 =head2 Methods
106  
107 The following Methods are available from RankCount Objects.
108  
109 =head3 RawCount
110  
111 Returns the original HashRef used for Object Creation.
112  
113 =head3 HashWithOrder
114  
115 Returns a HashRef with the Choices as Keys and the position of the choice, the value for the Leader would be 1 and the Third Place Choice would be 3. If choices are tied they will share the same value for their position.
116  
117 =head3 HashByRank
118  
119 Returns a HashRef where the keys are numbers and the values an ArrayRef of the Choices in that position. The ArrayRefs are sorted alphanumerically.
120  
121 =head3 ArrayTop, ArrayBottom
122  
123 Returns an ArrayRef of the Choices in the Top or Bottom Positions.
124  
125 =head3 OrderedList
126  
127 Returns the array that was to create the RankCount object if it was created from a List. Returns an exception if the object was created from a HashRef, because RankCount does not deal with ties. Returning a list with ties resolved by randomness or a sort would not be correct.
128  
129 =head3 CountVotes
130  
131 Returns the number of votes in the RawCount. This is not the same as the votes in the BallotSet from which that was derived. For TopCount it is the number of non-exhausted ballots in the round that generated RawCount, for Approval and Borda it is probably not useful.
132  
133 =head3 Leader
134  
135 Returns a HashRef with the keys tie, tied, winner where winner is the winner, tie is true or false and tied is an array ref of the choices in the tie.
136  
137 =head3 RankTable
138  
139 Generates a MarkDown formatted table.
140  
141 say $Election->TopCount->RankTable;
142  
143 | Rank | Choice | Votes |
144 |------|------------|-------|
145 | 1 | VANILLA | 7 |
146 | 2 | MINTCHIP | 5 |
147  
148 =head3 RankTableWeighted ($votevalue)
149  
150 Ranktable for use with weighted votes. Displays both the Vote Value and the Vote Total (rounded to two places). Requires Vote Value as an argument.
151  
152 say $WeightedElection->TopCount->RankTableWeighted( 100 );
153  
154 | Rank | Choice | Votes | VoteValue |
155 |:-----|:-----------|------:|----------:|
156 | 1 | VANILLA | 7.00 | 700 |
157 | 2 | MINTCHIP | 5.00 | 500 |
158  
159 =cut
160  
161 502 ▶sub RawCount ( $I ) { return $I->{'rawcount'} }
Subroutine RawCount: called CC=1 SCAR=0.0
Pod documented
162 52 ▶sub HashWithOrder ( $I ) { return $I->{'ordered'} }
Subroutine HashWithOrder: called CC=1 SCAR=0.0
Pod documented
163 59 ▶sub HashByRank ( $I ) { return $I->{'byrank'} }
Subroutine HashByRank: called CC=1 SCAR=0.0
Pod documented
164 35 ▶sub ArrayTop ( $I ) { return $I->{'top'} }
Subroutine ArrayTop: called CC=1 SCAR=0.0
Pod documented
165 194 ▶sub ArrayBottom ( $I ) { return $I->{'bottom'} }
Subroutine ArrayBottom: called CC=1 SCAR=0.0
Pod documented
166 318 ▶sub CountVotes ($I) { return sum( values $I->{'rawcount'}->%* ) }
Subroutine CountVotes: called CC=1 SCAR=0.0
Pod documented
167  
168 ▶sub OrderedList ($I) {
Subroutine OrderedList: called CC=2 SCAR=6.9
Pod documented
169 6 ▶ return $I->{'orderedlist'}->@* if defined $I->{'orderedlist'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $I->{"orderedlist"} 5 1 6
170 1 die "OrderedList may only be used if the RankCount object was created from an ordered list.\n";
171 }
172  
173 ▶sub Leader ( $I ) {
Subroutine Leader: called CC=3 SCAR=11.1
Pod documented
174 27 my @leaders = $I->ArrayTop()->@*;
175 27 my %return = ( 'tie' => 0, 'winner' => '', 'tied' => [] );
176 27 ▶ if ( 1 == @leaders ) { $return{'winner'} = $leaders[0] }
Branch: 2 branches
BranchTrueFalseTotal
if (1 == @leaders) { } 22 5 27
elsif (1 < @leaders) { } 5 0 5
177 5 elsif ( 1 < @leaders ) { $return{'tie'} = 1; $return{'tied'} = \@leaders }
178 0 else { die "Does not compute in sub RankCount->Leader\n" }
179 27 return \%return;
180 }
181  
182 ▶sub RankTable( $self ) {
Subroutine RankTable: called CC=3 SCAR=11.0
Pod documented
183 395 my @rows = ( [ 'Rank', 'Choice', 'Votes' ] );
184 395 my %rc = $self->{'rawcount'}->%*;
185 395 my %byrank = $self->{'byrank'}->%*;
186 395 for my $r ( sort { $a <=> $b } ( keys %byrank ) ) {
187 1335 my @choice = sort $byrank{$r}->@*;
188 1335 for my $choice (@choice) {
189 1734 my $votes = $rc{$choice};
190 1734 my @row = ( $r, $choice, $votes );
191 1734 push @rows, ( \@row );
192 }
193 }
194 395 return generate_table( rows => \@rows, style => 'markdown' ) . "\n";
195 }
196  
197 ▶sub RankTableWeighted( $self, $votevalue ) {
Subroutine RankTableWeighted: called CC=3 SCAR=11.0
Pod documented
198 1 my @rows = ( [ 'Rank', 'Choice', 'Votes', 'VoteValue' ] );
199 1 my %rc = $self->{'rawcount'}->%*;
200 1 my %byrank = $self->{'byrank'}->%*;
201 1 for my $r ( sort { $a <=> $b } ( keys %byrank ) ) {
202 5 my @choice = sort $byrank{$r}->@*;
203 5 for my $choice (@choice) {
204 8 my $votes = $rc{$choice};
205 8 my @row = ( $r, $choice, sprintf("%.2f", $votes/$votevalue), $votes );
206 8 push @rows, ( \@row );
207 }
208 }
209 1 return generate_table(
210 rows => \@rows,
211 style => 'markdown',
212 align => [qw/ l l r r /]
213 ) . "\n";
214 }
215 39 1;
216  
217 #FOOTER
218  
219 =pod
220  
221 BUG TRACKER
222  
223 L<https://github.com/brainbuz/Vote-Count/issues>
224  
225 AUTHOR
226  
227 John Karr (BRAINBUZ) brainbuz@cpan.org
228  
229 CONTRIBUTORS
230  
231 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
232  
233 LICENSE
234  
235 This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>.
236  
237 SUPPORT
238  
239 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
240  
241 =cut
242  
« Count/Approval.pm ↑ Summary Count/Method/WIGM.pm »