Count/Helper/FullCascadeCharge.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 95.5% 64 / 67 CC 10 SCAR 17.9
CC
10
Cov
96%
FullCascadeCharge
10.0
BEGIN
1.0
BEGIN
1.0
CRAP 6.0
« Count/TextTableTiny.pm ↑ Summary Count/Matrix.pm »
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Vote::Count::Helper::FullCascadeCharge;
6 5 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Sort::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Vote::Count::TextTableTiny qw/generate_table/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION='2.02';
12  
13 # ABSTRACT: Non OO Components for the Vote::Charge implementation of STV FullCascadeCharge.
14  
15 =head1 NAME
16  
17 Vote::Count::Helper::FullCascadeCharge
18  
19 =head1 VERSION 2.02
20  
21 =head1 SYNOPSIS
22  
23 use Vote::Count::Helper::FullCascadeCharge;
24 my $charged = FullCascadeCharge(
25 $Election->GetBallots(), $quota, $cost, $active, $votevalue );
26  
27 =head1 FullCascadeCharge
28  
29 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.
30  
31 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.
32  
33 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.
34  
35 The method is non-OO. This permits isolation of values, which may be needed for performing estimations to establish the Costs.
36  
37 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)
38  
39 =cut
40  
41 use Exporter::Easy (
42 5 EXPORT => [ 'FullCascadeCharge' ],
43 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 ▶sub FullCascadeCharge ( $ballots, $quota, $cost, $active, $votevalue ) {
Subroutine FullCascadeCharge: called CC=10 SCAR=23.1
Pod undocumented
46 34 for my $b ( keys $ballots->%* ) {
47 23414 $ballots->{$b}{'votevalue'} = $votevalue;
48 }
49 my %chargedval =
50 34 map { $_ => { value => 0, count => 0, surplus => 0 } } ( keys $cost->%* );
51 FullChargeBALLOTLOOP1:
52 34 for my $V ( values $ballots->%* ) {
53 23414 ▶ unless ( $V->{'votevalue'} > 0 ) { next FullChargeBALLOTLOOP1 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($V->{"votevalue"} > 0) 0 23414 23414
54 FullChargeBALLOTLOOP2:
55 23414 for my $C ( $V->{'votes'}->@* ) {
56 35745 ▶ if ( $active->{$C} ) { last FullChargeBALLOTLOOP2 }
Branch: 2 branches
BranchTrueFalseTotal
if ($active->{$C}) { } 17943 17802 35745
elsif ($cost->{$C}) { } 17703 99 17802
57 elsif ( $cost->{$C} ) {
58 17703 my $charge = do {
59 17703 ▶ if ( $V->{'votevalue'} >= $cost->{$C} ) { $cost->{$C} }
Branch: 1 branch
BranchTrueFalseTotal
if ($V->{'votevalue'} >= $cost->{$C}) { } 12335 5368 17703
60 5368 else { $V->{'votevalue'} }
61 };
62 17703 $V->{'votevalue'} -= $charge;
63 17703 $chargedval{$C}{'value'} += $charge * $V->{'count'};
64 17703 $chargedval{$C}{'count'} += $V->{'count'};
65 17703 ▶ unless ( $V->{'votevalue'} > 0 ) { last FullChargeBALLOTLOOP2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($V->{"votevalue"} > 0) 5370 12333 17703
66 }
67 }
68 }
69 34 for my $E ( keys %chargedval ) {
70 89 $chargedval{$E}{'surplus'} = $chargedval{$E}{'value'} - $quota;
71 }
72 34 return \%chargedval;
73 }
74  
75 5 1;
76  
77 #FOOTER
78  
79 =pod
80  
81 BUG TRACKER
82  
83 L<https://github.com/brainbuz/Vote-Count/issues>
84  
85 AUTHOR
86  
87 John Karr (BRAINBUZ) brainbuz@cpan.org
88  
89 CONTRIBUTORS
90  
91 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
92  
93 LICENSE
94  
95 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>.
96  
97 SUPPORT
98  
99 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
100  
101 =cut
102  
« Count/TextTableTiny.pm ↑ Summary Count/Matrix.pm »