Count/Redact.pm

filter: Statementstmt 100% 70 / 70 Branchbran 100% 20 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 100% 104 / 104 CC 14 SCAR 15.6
CC
14
Cov
100%
RedactPair
9.0
RedactSingle
4.0
RedactBullet
3.0
CRAP 4.8
« Count/Method/MinMax.pm ↑ Summary Count/Borda.pm »
1 package Vote::Count::Redact;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Storable 3.15 qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION='1.10';
14  
15 =head1 NAME
16  
17 Vote::Count::Redact
18  
19 =head1 VERSION 1.10
20  
21 Methods for Redacting Ballots.
22  
23 =head2 Purpose
24  
25 Redacting Ballots is useful for what-if analysis and identifying Later Harm effects. Compound Methods seeking to reduce Later Harm effects can also be developed using this technique.
26  
27 use Vote::Count::Redact qw/RedactPair RedactBullet RedactSingle/;
28  
29 =cut
30  
31 # ABSTRACT: Methods for Redacting Vote::Count BallotSets.
32  
33 3 ▶use Exporter::Easy ( OK => [qw( RedactSingle RedactPair RedactBullet )], );
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 =head2 RedactBullet
36  
37 Takes a list (array) of choices to be converted to bullet votes. Returns a modified BallotSet where all votes that had a first choice vote for a member of the list are votes for only that choice.
38  
39 my $newBallotSet = RedactBullet( $VoteCountObject->BallotSet(), 'A', 'B', 'F');
40  
41 =cut
42  
43 ▶sub RedactBullet ( $ballotset, @choices ) {
Subroutine RedactBullet: called CC=3 SCAR=11.0
Pod documented
44 4 my $new = dclone($ballotset);
45 4 my %ballots = $new->{'ballots'}->%*;
46 REDACTBULLETLOOP:
47 4 for my $ballot ( keys %ballots ) {
48 32 my @newvote = ();
49 32 my $oldvote = $ballots{$ballot}->{'votes'}[0];
50 32 ▶ if ( grep( /^$oldvote$/, @choices ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^$oldvote$/u, @choices) 8 24 32
51 8 $ballots{$ballot}->{'votes'} = [$oldvote];
52 }
53 }
54 4 $new->{'ballots'} = \%ballots;
55 4 return $new;
56 }
57  
58 =head2 RedactSingle
59  
60 Return a new BallotSet truncating the ballots after the given choice.
61  
62 my $newBallotSet = RedactSingle( $VoteCountObject->BallotSet(), $choice);
63  
64 =cut
65  
66 ▶sub RedactSingle ( $ballotset, $A ) {
Subroutine RedactSingle: called CC=4 SCAR=13.9
Pod documented
67 2 my $new = dclone($ballotset);
68 2 my %ballots = $new->{'ballots'}->%*;
69 REDACTSINGLELOOP:
70 2 for my $ballot ( keys %ballots ) {
71 22 my @newvote = ();
72 22 my @oldvote = $ballots{$ballot}{'votes'}->@*;
73 22 ▶ while (@oldvote) {
Branch: 1 branch
BranchTrueFalseTotal
if (@oldvote) 42 22 64
74 42 my $v = shift @oldvote;
75 42 push @newvote, $v;
76 42 ▶ if ( $v eq $A ) { @oldvote = () }
Branch: 1 branch
BranchTrueFalseTotal
if ($v eq $A) { } 6 36 42
77 else { }
78 42 $ballots{$ballot}{'votes'} = \@newvote;
79 }
80 22 $ballots{$ballot}{'votes'} = \@newvote;
81 }
82 2 $new->{'ballots'} = \%ballots;
83 2 return $new;
84 }
85  
86 =head2 RedactPair
87  
88 For a Ballot Set and two choices, on each ballot where both appear it removes the later one and all subsequent choices, returning a completely independent new BallotSet. It is necessary to remove later choices, because otherwise the ballot would be voting against the target later choice, not merely not voting for.
89  
90 my $newBallotSet = RedactPair( $VoteCountObject->BallotSet(), 'A', 'B');
91  
92 =head3 Todo for RedactPair
93  
94 Add options to only apply to first choice votes, either making them bullets or only redacting the opposing choice from first choice votes.
95  
96 =cut
97  
98 # RedactPair only alters votes involving the two choices
99 # The other two methods truncate after the choice.
100  
101 ▶sub RedactPair ( $ballotset, $A, $B ) {
Subroutine RedactPair: called CC=9 SCAR=22.0
Pod documented
102 10 my $new = dclone($ballotset);
103 10 my %ballots = $new->{'ballots'}->%*;
104 REDACTPAIRLOOP:
105 10 for my $ballot ( keys %ballots ) {
106 1182 my @newvote = ();
107 1182 my @oldvote = $ballots{$ballot}{'votes'}->@*;
108 1182 ▶ while (@oldvote) {
Branch: 1 branch
BranchTrueFalseTotal
if (@oldvote) 2134 1182 3316
109 2134 my $v = shift @oldvote;
110 2134 push @newvote, $v;
111 2134 ▶ if ( $v eq $A ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v eq $A) { } 578 1556 2134
elsif ($v eq $B) { } 515 1041 1556
112 578 ▶ while (@oldvote) {
Branch: 1 branch
BranchTrueFalseTotal
if (@oldvote) 957 578 1535
113 957 my $u = shift @oldvote;
114 # If the other redaction member is the present vote
115 # truncate this vote from here on by setting oldvote to empty array.
116 957 ▶ if ( $u eq $B ) { @oldvote = () }
Branch: 1 branch
BranchTrueFalseTotal
if ($u eq $B) { } 386 571 957
117 571 else { push @newvote, ($u) }
118 }
119 }
120 elsif ( $v eq $B ) {
121 515 ▶ while (@oldvote) {
Branch: 1 branch
BranchTrueFalseTotal
if (@oldvote) 897 515 1412
122 897 my $u = shift @oldvote;
123 # If the other redaction member is the present vote
124 # truncate this vote from here on by setting oldvote to empty array.
125 897 ▶ if ( $u eq $A ) { @oldvote = () }
Branch: 1 branch
BranchTrueFalseTotal
if ($u eq $A) { } 391 506 897
126 506 else { push @newvote, ($u) }
127 }
128 }
129 else { }
130 2134 $ballots{$ballot}{'votes'} = \@newvote;
131 }
132 }
133 10 $new->{'ballots'} = \%ballots;
134 10 return $new;
135 }
136  
137 3 1;
138  
139 #FOOTER
140  
141 =pod
142  
143 BUG TRACKER
144  
145 L<https://github.com/brainbuz/Vote-Count/issues>
146  
147 AUTHOR
148  
149 John Karr (BRAINBUZ) brainbuz@cpan.org
150  
151 CONTRIBUTORS
152  
153 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
154  
155 LICENSE
156  
157 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>.
158  
159 =cut
160  
« Count/Method/MinMax.pm ↑ Summary Count/Borda.pm »