| 1 | 25 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 25 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 25 | ▶ | use 5.022; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | package Vote::Count::RankCount; | ||||||||||||||
| 6 | |||||||||||||||
| 7 | 25 | ▶ | use feature qw /postderef signatures/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 25 | ▶ | no warnings 'experimental'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 25 | ▶ | use List::Util qw( min max sum); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 25 | ▶ | use Vote::Count::TextTableTiny qw/generate_table/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 25 | ▶ | use Sort::Hash; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 25 | our $VERSION='1.10'; | |||||||||||||
| 14 | |||||||||||||||
| 15 | =head1 NAME | ||||||||||||||
| 16 | |||||||||||||||
| 17 | Vote::Count::RankCount | ||||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 VERSION 1.10 | ||||||||||||||
| 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 | 557 | my %rc = ( $rawcount->%* ); # destructive process needs to use a copy. | |||||||||||||
| 27 | 557 | my %ordered = (); | |||||||||||||
| 28 | 557 | my %byrank = (); | |||||||||||||
| 29 | 557 | my $pos = 0; | |||||||||||||
| 30 | 557 | my $maxpos = scalar( keys %rc ); | |||||||||||||
| 31 | 557 | ▶ | while ( 0 < scalar( keys %rc ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | 2876 | $pos++; | |||||||||||||
| 33 | 2876 | my @vrc = values %rc; | |||||||||||||
| 34 | 2876 | my $max = max @vrc; | |||||||||||||
| 35 | 2876 | for my $k ( keys %rc ) { | |||||||||||||
| 36 | 15826 | ▶ | if ( $rc{$k} == $max ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | 3919 | $ordered{$k} = $pos; | |||||||||||||
| 38 | 3919 | delete $rc{$k}; | |||||||||||||
| 39 | 3919 | ▶ | if ( defined $byrank{$pos} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 1043 | push @{ $byrank{$pos} }, $k; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | else { | ||||||||||||||
| 43 | 2876 | $byrank{$pos} = [$k]; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | } | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | # uncoverable branch true | ||||||||||||||
| 48 | 2876 | ▶ | die "Vote::Count::RankCount::Rank in infinite loop\n" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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 | 557 | for my $O ( keys %byrank ) { | |||||||||||||
| 59 | 2876 | $byrank{$O} = [ sort $byrank{$O}->@* ]; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | 557 | my @top = @{ $byrank{1} }; | |||||||||||||
| 62 | 557 | my @bottom = @{ $byrank{$pos} }; | |||||||||||||
| 63 | 557 | ▶ | my $tie = scalar(@top) > 1 ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | return { | ||||||||||||||
| 65 | 557 | 'rawcount' => $rawcount, | |||||||||||||
| 66 | 'ordered' => \%ordered, | ||||||||||||||
| 67 | 'byrank' => \%byrank, | ||||||||||||||
| 68 | 'top' => \@top, | ||||||||||||||
| 69 | 'bottom' => \@bottom, | ||||||||||||||
| 70 | 'tie' => $tie, | ||||||||||||||
| 71 | }; | ||||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | =head1 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 | =head1 newFromList | ||||||||||||||
| 79 | |||||||||||||||
| 80 | Takes an ordered list and returns a RankCount Object where the RawCount values are the position time -1: Item 3 in the list will have -3 votes while Item 1 have -1. | ||||||||||||||
| 81 | |||||||||||||||
| 82 | my $ordered_rank_count = Vote::Count::RankCount->newFromList( @ordered_list ); | ||||||||||||||
| 83 | |||||||||||||||
| 84 | |||||||||||||||
| 85 | |||||||||||||||
| 86 | =cut | ||||||||||||||
| 87 | |||||||||||||||
| 88 | ▶ | sub Rank ( $class, $rawcount ) { | |||||||||||||
|
Subroutine
Rank: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 89 | 556 | my $I = _RankResult($rawcount); | |||||||||||||
| 90 | 556 | return bless $I, $class; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | ▶ | sub new ( $class, $rawcount ) { | |||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 94 | 1 | my $I = _RankResult($rawcount); | |||||||||||||
| 95 | 1 | return bless $I, $class; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | ▶ | sub newFromList ( @list ) { | |||||||||||||
|
Subroutine
newFromList: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 99 | 5 | shift @list; | |||||||||||||
| 100 | 5 | my $pos = 0; | |||||||||||||
| 101 | my $newobj = Vote::Count::RankCount->Rank({ | ||||||||||||||
| 102 | 5 | map { $_ => --$pos } @list} ); | |||||||||||||
| 103 | 5 | $newobj->{'orderedlist'} = \@list; | |||||||||||||
| 104 | 5 | return $newobj; | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head2 Methods | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =over | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =item * RawCount | ||||||||||||||
| 112 | |||||||||||||||
| 113 | Returns the original HashRef used for Object Creation. | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =item * HashWithOrder | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 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. | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =item * HashByRank | ||||||||||||||
| 120 | |||||||||||||||
| 121 | Returns a HashRef where the keys are numbers and the values an ArrayRef of the Choices in that position. The ArrayRefs are sorted alphanumerically. | ||||||||||||||
| 122 | |||||||||||||||
| 123 | =item * ArrayTop, ArrayBottom | ||||||||||||||
| 124 | |||||||||||||||
| 125 | Returns an ArrayRef of the Choices in the Top or Bottom Positions. | ||||||||||||||
| 126 | |||||||||||||||
| 127 | =item * OrderedList | ||||||||||||||
| 128 | |||||||||||||||
| 129 | 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. | ||||||||||||||
| 130 | |||||||||||||||
| 131 | =item * CountVotes | ||||||||||||||
| 132 | |||||||||||||||
| 133 | 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 Boorda it is probably not useful. | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =item * Leader | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 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. | ||||||||||||||
| 138 | |||||||||||||||
| 139 | =back | ||||||||||||||
| 140 | |||||||||||||||
| 141 | =head3 RankTable | ||||||||||||||
| 142 | |||||||||||||||
| 143 | Generates a MarkDown formatted table. | ||||||||||||||
| 144 | |||||||||||||||
| 145 | =head3 RankTableWeighted ($votevalue) | ||||||||||||||
| 146 | |||||||||||||||
| 147 | 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. | ||||||||||||||
| 148 | |||||||||||||||
| 149 | =cut | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 391 | ▶ | sub RawCount ( $I ) { return $I->{'rawcount'} } | ||||||||||||
|
Subroutine
RawCount: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 152 | 4 | ▶ | sub HashWithOrder ( $I ) { return $I->{'ordered'} } | ||||||||||||
|
Subroutine
HashWithOrder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 153 | 18 | ▶ | sub HashByRank ( $I ) { return $I->{'byrank'} } | ||||||||||||
|
Subroutine
HashByRank: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 154 | 23 | ▶ | sub ArrayTop ( $I ) { return $I->{'top'} } | ||||||||||||
|
Subroutine
ArrayTop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 155 | 182 | ▶ | sub ArrayBottom ( $I ) { return $I->{'bottom'} } | ||||||||||||
|
Subroutine
ArrayBottom: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 156 | 286 | ▶ | sub CountVotes ($I) { return sum( values $I->{'rawcount'}->%* ) } | ||||||||||||
|
Subroutine
CountVotes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 157 | |||||||||||||||
| 158 | ▶ | sub OrderedList ($I) { | |||||||||||||
|
Subroutine
OrderedList: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 159 | 2 | ▶ | return $I->{'orderedlist'}->@* if defined $I->{'orderedlist'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | 1 | die "OrderedList may only be used if the RankCount object was created from an ordered list.\n"; | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | ▶ | sub Leader ( $I ) { | |||||||||||||
|
Subroutine
Leader: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||
| 164 | 15 | my @leaders = $I->ArrayTop()->@*; | |||||||||||||
| 165 | 15 | my %return = ( 'tie' => 0, 'winner' => '', 'tied' => [] ); | |||||||||||||
| 166 | 15 | ▶ | if ( 1 == @leaders ) { $return{'winner'} = $leaders[0] } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 167 | 5 | elsif ( 1 < @leaders ) { $return{'tie'} = 1; $return{'tied'} = \@leaders } | |||||||||||||
| 168 | 0 | else { die "Does not compute in sub RankCount->Leader\n" } | |||||||||||||
| 169 | 15 | return \%return; | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | ▶ | sub RankTable( $self ) { | |||||||||||||
|
Subroutine
RankTable: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 173 | 295 | my @rows = ( [ 'Rank', 'Choice', 'Votes' ] ); | |||||||||||||
| 174 | 295 | my %rc = $self->{'rawcount'}->%*; | |||||||||||||
| 175 | 295 | my %byrank = $self->{'byrank'}->%*; | |||||||||||||
| 176 | 295 | for my $r ( sort { $a <=> $b } ( keys %byrank ) ) { | |||||||||||||
| 177 | 1115 | my @choice = sort $byrank{$r}->@*; | |||||||||||||
| 178 | 1115 | for my $choice (@choice) { | |||||||||||||
| 179 | 1366 | my $votes = $rc{$choice}; | |||||||||||||
| 180 | 1366 | my @row = ( $r, $choice, $votes ); | |||||||||||||
| 181 | 1366 | push @rows, ( \@row ); | |||||||||||||
| 182 | } | ||||||||||||||
| 183 | } | ||||||||||||||
| 184 | 295 | return generate_table( rows => \@rows, style => 'markdown' ) . "\n"; | |||||||||||||
| 185 | } | ||||||||||||||
| 186 | |||||||||||||||
| 187 | ▶ | sub RankTableWeighted( $self, $votevalue ) { | |||||||||||||
|
Subroutine
RankTableWeighted: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 188 | 1 | my @rows = ( [ 'Rank', 'Choice', 'Votes', 'VoteValue' ] ); | |||||||||||||
| 189 | 1 | my %rc = $self->{'rawcount'}->%*; | |||||||||||||
| 190 | 1 | my %byrank = $self->{'byrank'}->%*; | |||||||||||||
| 191 | 1 | for my $r ( sort { $a <=> $b } ( keys %byrank ) ) { | |||||||||||||
| 192 | 5 | my @choice = sort $byrank{$r}->@*; | |||||||||||||
| 193 | 5 | for my $choice (@choice) { | |||||||||||||
| 194 | 8 | my $votes = $rc{$choice}; | |||||||||||||
| 195 | 8 | my @row = ( $r, $choice, sprintf("%.2f", $votes/$votevalue), $votes ); | |||||||||||||
| 196 | 8 | push @rows, ( \@row ); | |||||||||||||
| 197 | } | ||||||||||||||
| 198 | } | ||||||||||||||
| 199 | 1 | return generate_table( | |||||||||||||
| 200 | rows => \@rows, | ||||||||||||||
| 201 | style => 'markdown', | ||||||||||||||
| 202 | align => [qw/ l l r r /] | ||||||||||||||
| 203 | ) . "\n"; | ||||||||||||||
| 204 | } | ||||||||||||||
| 205 | 25 | 1; | |||||||||||||
| 206 | |||||||||||||||
| 207 | #FOOTER | ||||||||||||||
| 208 | |||||||||||||||
| 209 | =pod | ||||||||||||||
| 210 | |||||||||||||||
| 211 | BUG TRACKER | ||||||||||||||
| 212 | |||||||||||||||
| 213 | L<https://github.com/brainbuz/Vote-Count/issues> | ||||||||||||||
| 214 | |||||||||||||||
| 215 | AUTHOR | ||||||||||||||
| 216 | |||||||||||||||
| 217 | John Karr (BRAINBUZ) brainbuz@cpan.org | ||||||||||||||
| 218 | |||||||||||||||
| 219 | CONTRIBUTORS | ||||||||||||||
| 220 | |||||||||||||||
| 221 | Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org. | ||||||||||||||
| 222 | |||||||||||||||
| 223 | LICENSE | ||||||||||||||
| 224 | |||||||||||||||
| 225 | 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>. | ||||||||||||||
| 226 | |||||||||||||||
| 227 | =cut | ||||||||||||||
| 228 | |||||||||||||||