File Coverage

blib/lib/Vote/Count/Helper/FullCascadeCharge.pm
Criterion Covered Total %
statement 51 52 98.0
branch 9 10 90.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 69 72 95.8


line stmt bran cond sub pod time code
1 5     5   467 use strict;
  5         12  
  5         156  
2 5     5   28 use warnings;
  5         10  
  5         123  
3 5     5   91 use 5.024;
  5         18  
4              
5             no warnings 'experimental';
6 5     5   84 use feature qw /postderef signatures/;
  5         11  
  5         213  
7 5     5   32 use Sort::Hash;
  5         12  
  5         507  
8 5     5   41 use Vote::Count::TextTableTiny qw/generate_table/;
  5         48  
  5         270  
9 5     5   1868  
  5         15  
  5         461  
10             our $VERSION='2.02';
11              
12             # ABSTRACT: Non OO Components for the Vote::Charge implementation of STV FullCascadeCharge.
13              
14             =head1 NAME
15              
16             Vote::Count::Helper::FullCascadeCharge
17              
18             =head1 VERSION 2.02
19              
20             =head1 SYNOPSIS
21              
22             use Vote::Count::Helper::FullCascadeCharge;
23             my $charged = FullCascadeCharge(
24             $Election->GetBallots(), $quota, $cost, $active, $votevalue );
25              
26             =head1 FullCascadeCharge
27              
28             Performs a full Cascading Charge of the Ballots. It takes a list of choices to be elected, with the Vote Value to be charged for each of these. It walks through the Ballots and looks at each choice on the ballot in order. If the choice is elected the vote is charged (up to the remaining vote value) the specified charge and then continues to the next choice on the ballot. If the choice is in the active list (hopeful) it stops processing the choice on the ballot and moves on to the next ballot, otherwise it will continue until the ballot exhausts its choices or vote value.
29              
30             Parameters are Ballots, Quota, Cost (HashRef of elected choices and the charge to each), Active Set (HashRef), and the VoteValue assigned initially to the Ballots.
31              
32             Return Value is a HashRef where the keys are the Elected Choices, the values are a HashRef with the keys: value, count, surplus. The value key is the total Vote Value charged for that choice, the count is the number of Ballots which contributed any amount to that charge, and finally the surplus is the amount of value over or under (negative) the quota.
33              
34             The method is non-OO. This permits isolation of values, which may be needed for performing estimations to establish the Costs.
35              
36             The Ballots are passed as a HashRef and the votevalue will be modified, if you do not want the Ballots modified, provide a copy of them (Storable 'dclone' is recommended)
37              
38             =cut
39              
40             use Exporter::Easy (
41             EXPORT => [ 'FullCascadeCharge' ],
42 5         33 );
43 5     5   1931  
  5         6303  
44             for my $b ( keys $ballots->%* ) {
45 34     34 0 7584 $ballots->{$b}{'votevalue'} = $votevalue;
  34         61  
  34         56  
  34         51  
  34         54  
  34         48  
  34         51  
46 34         2978 }
47 23414         35992 my %chargedval =
48             map { $_ => { value => 0, count => 0, surplus => 0 } } ( keys $cost->%* );
49             FullChargeBALLOTLOOP1:
50 34         919 for my $V ( values $ballots->%* ) {
  89         396  
51             unless ( $V->{'votevalue'} > 0 ) { next FullChargeBALLOTLOOP1 }
52 34         871 FullChargeBALLOTLOOP2:
53 23414 50       41178 for my $C ( $V->{'votes'}->@* ) {
  0         0  
54             if ( $active->{$C} ) { last FullChargeBALLOTLOOP2 }
55 23414         37330 elsif ( $cost->{$C} ) {
56 35745 100       68043 my $charge = do {
  17943 100       26934  
57             if ( $V->{'votevalue'} >= $cost->{$C} ) { $cost->{$C} }
58 17703         22073 else { $V->{'votevalue'} }
59 17703 100       29929 };
  12335         18506  
60 5368         7830 $V->{'votevalue'} -= $charge;
61             $chargedval{$C}{'value'} += $charge * $V->{'count'};
62 17703         24452 $chargedval{$C}{'count'} += $V->{'count'};
63 17703         29655 unless ( $V->{'votevalue'} > 0 ) { last FullChargeBALLOTLOOP2 }
64 17703         26520 }
65 17703 100       34019 }
  5370         8654  
66             }
67             for my $E ( keys %chargedval ) {
68             $chargedval{$E}{'surplus'} = $chargedval{$E}{'value'} - $quota;
69 34         201 }
70 89         184 return \%chargedval;
71             }
72 34         176  
73             1;
74              
75             #FOOTER
76              
77             =pod
78              
79             BUG TRACKER
80              
81             L<https://github.com/brainbuz/Vote-Count/issues>
82              
83             AUTHOR
84              
85             John Karr (BRAINBUZ) brainbuz@cpan.org
86              
87             CONTRIBUTORS
88              
89             Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
90              
91             LICENSE
92              
93             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>.
94              
95             SUPPORT
96              
97             This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
98              
99             =cut
100