| 1 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use 5.024; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | package Vote::Count::Helper::Table; | ||
| 6 | 2 | ▶ | no warnings 'experimental'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use feature qw /postderef signatures/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Sort::Hash; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use Vote::Count::TextTableTiny qw/generate_table/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 2 | our $VERSION='2.02'; | |
| 12 | |||
| 13 | # ABSTRACT: Non OO Components for the Vote::Charge implementation of STV. | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Vote::Count::Helper::Table | ||
| 18 | |||
| 19 | =head1 VERSION 2.02 | ||
| 20 | |||
| 21 | =head1 Description | ||
| 22 | |||
| 23 | Table Formatting Helpers for use within Vote::Count. | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | =pod | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use Vote::Count::Helper::Table 'ChargeTable'; | ||
| 32 | # $chargesPerChoice and $chargedPerChoice are from Vote::Count::Charge::Cascade | ||
| 33 | say ChargeTable( $chargesPerChoice, $chargedPerChoice ); | ||
| 34 | |||
| 35 | use Vote::Count::Helper::Table 'WeightedTable'; | ||
| 36 | # When weighted voting is used will generate a table | ||
| 37 | # with the Top Count and Approval totals | ||
| 38 | say WeightedTable( $STV_Election ); | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | use Exporter::Easy ( | ||
| 43 | 2 | OK => [ 'WeightedTable', 'ChargeTable' ], | |
| 44 | 2 | ▶ | ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | |||
| 46 | =head2 ChargeTable | ||
| 47 | |||
| 48 | Arguments: $chargesPerChoice, $chargedPerChoice | ||
| 49 | |||
| 50 | chargesPerChoice is a HashRef with the choices as keys, and the values the charge assessed each ballot supporting the choice. | ||
| 51 | |||
| 52 | chargedPerChoice is a HashRef with the choices as keys and the values a HashRef with the keys value, count, surplus, where value is the total vote value charged for the choice, count is the number of ballots that contributed, and surplus the value above quota charged. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | ▶ | sub ChargeTable ( $chargesPerChoice, $chargedPerChoice ) { | |
|
Subroutine
ChargeTable: not called CC=2 SCAR=17.9
Pod
documented
| |||
| 57 | 0 | my @rows = (['Choice','Charge','Value Charged', 'Votes Charged','Surplus'] ); | |
| 58 | 0 | for my $c ( sort keys $chargesPerChoice->%* ) { | |
| 59 | push @rows, [ | ||
| 60 | $c, $chargesPerChoice->{$c}, | ||
| 61 | $chargedPerChoice->{$c}{'value'}, | ||
| 62 | $chargedPerChoice->{$c}{'count'}, | ||
| 63 | 0 | $chargedPerChoice->{$c}{'surplus'} | |
| 64 | ] | ||
| 65 | } | ||
| 66 | 0 | return generate_table( | |
| 67 | rows => \@rows, | ||
| 68 | style => 'markdown', | ||
| 69 | align => [qw/ l l r r r/] | ||
| 70 | ) . "\n"; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 WeightedTable | ||
| 74 | |||
| 75 | Formats the current Vote Totals by Approval and Top Count when weighted voting is in use, for STV/Vote Charge methods. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | ▶ | sub WeightedTable ( $I ) { | |
|
Subroutine
WeightedTable: called CC=4 SCAR=13.9
Pod
documented
| |||
| 80 | 1 | my $approval = $I->Approval()->RawCount(); | |
| 81 | 1 | my $tc = $I->TopCount(); | |
| 82 | 1 | my $tcr = $tc->RawCount(); | |
| 83 | 1 | my $vv = $I->VoteValue(); | |
| 84 | 1 | my %data =(); | |
| 85 | 1 | my @active = $I->GetActiveList(); | |
| 86 | 1 | for my $choice ( @active ) { | |
| 87 | $data{ $choice } = { | ||
| 88 | 'votevalue' => $tcr->{ $choice }, | ||
| 89 | 'votes' => sprintf( "%.2f",$tcr->{ $choice } / $vv), | ||
| 90 | 'approvalvalue' => $approval->{ $choice }, | ||
| 91 | 8 | 'approval' => sprintf( "%.2f", $approval->{ $choice } / $vv), | |
| 92 | }; | ||
| 93 | } | ||
| 94 | 1 | my @rows = ( [ 'Rank', 'Choice', 'Votes', 'VoteValue', 'Approval', 'Approval Value' ] ); | |
| 95 | 1 | my %byrank = $tc->HashByRank()->%*; | |
| 96 | 1 | for my $r ( sort { $a <=> $b } ( keys %byrank ) ) { | |
| 97 | 5 | my @choice = sort $byrank{$r}->@*; | |
| 98 | 5 | for my $choice (@choice) { | |
| 99 | # my $votes = $tcr->{$choice}; | ||
| 100 | 8 | my $D = $data{$choice}; | |
| 101 | my @row = ( | ||
| 102 | $r, $choice, $D->{'votes'}, $D->{'votevalue'}, | ||
| 103 | 8 | $D->{'approval'}, $D->{'approvalvalue'} ); | |
| 104 | 8 | push @rows, ( \@row ); | |
| 105 | } | ||
| 106 | } | ||
| 107 | 1 | return generate_table( | |
| 108 | rows => \@rows, | ||
| 109 | style => 'markdown', | ||
| 110 | align => [qw/ l l r r r r/] | ||
| 111 | ) . "\n"; | ||
| 112 | } | ||
| 113 | 2 | 1; | |
| 114 | |||
| 115 | #FOOTER | ||
| 116 | |||
| 117 | =pod | ||
| 118 | |||
| 119 | BUG TRACKER | ||
| 120 | |||
| 121 | L<https://github.com/brainbuz/Vote-Count/issues> | ||
| 122 | |||
| 123 | AUTHOR | ||
| 124 | |||
| 125 | John Karr (BRAINBUZ) brainbuz@cpan.org | ||
| 126 | |||
| 127 | CONTRIBUTORS | ||
| 128 | |||
| 129 | Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org. | ||
| 130 | |||
| 131 | LICENSE | ||
| 132 | |||
| 133 | 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>. | ||
| 134 | |||
| 135 | SUPPORT | ||
| 136 | |||
| 137 | This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||