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/IRV.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.024;
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='2.02';
14  
15 =head1 NAME
16  
17 Vote::Count::Redact
18  
19 =head1 VERSION 2.02
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( $Election->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. If the later choices were left intact, they would become votes against the redacted choices in those pairings.
89  
90 my $newBallotSet = RedactPair( $VoteCountObject->BallotSet(), 'A', 'B');
91  
92 =cut
93  
94 # RedactPair only alters votes involving the two choices
95 # The other two methods truncate after the choice.
96  
97 ▶sub RedactPair ( $ballotset, $A, $B ) {
Subroutine RedactPair: called CC=9 SCAR=22.0
Pod documented
98 10 my $new = dclone($ballotset);
99 10 my %ballots = $new->{'ballots'}->%*;
100 REDACTPAIRLOOP:
101 10 for my $ballot ( keys %ballots ) {
102 1182 my @newvote = ();
103 1182 my @oldvote = $ballots{$ballot}{'votes'}->@*;
104 1182 ▶ while (@oldvote) {
Branch: 1 branch
BranchTrueFalseTotal
if (@oldvote) 2134 1182 3316
105 2134 my $v = shift @oldvote;
106 2134 push @newvote, $v;
107 2134 ▶ if ( $v eq $A ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v eq $A) { } 578 1556 2134
elsif ($v eq $B) { } 515 1041 1556
108 578 ▶ while (@oldvote) {
Branch: 1 branch
BranchTrueFalseTotal
if (@oldvote) 957 578 1535
109 957 my $u = shift @oldvote;
110 # If the other redaction member is the present vote
111 # truncate this vote from here on by setting oldvote to empty array.
112 957 ▶ if ( $u eq $B ) { @oldvote = () }
Branch: 1 branch
BranchTrueFalseTotal
if ($u eq $B) { } 386 571 957
113 571 else { push @newvote, ($u) }
114 }
115 }
116 elsif ( $v eq $B ) {
117 515 ▶ while (@oldvote) {
Branch: 1 branch
BranchTrueFalseTotal
if (@oldvote) 897 515 1412
118 897 my $u = shift @oldvote;
119 # If the other redaction member is the present vote
120 # truncate this vote from here on by setting oldvote to empty array.
121 897 ▶ if ( $u eq $A ) { @oldvote = () }
Branch: 1 branch
BranchTrueFalseTotal
if ($u eq $A) { } 391 506 897
122 506 else { push @newvote, ($u) }
123 }
124 }
125 else { }
126 2134 $ballots{$ballot}{'votes'} = \@newvote;
127 }
128 }
129 10 $new->{'ballots'} = \%ballots;
130 10 return $new;
131 }
132  
133 3 1;
134  
135 #FOOTER
136  
137 =pod
138  
139 BUG TRACKER
140  
141 L<https://github.com/brainbuz/Vote-Count/issues>
142  
143 AUTHOR
144  
145 John Karr (BRAINBUZ) brainbuz@cpan.org
146  
147 CONTRIBUTORS
148  
149 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
150  
151 LICENSE
152  
153 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>.
154  
155 SUPPORT
156  
157 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
158  
159 =cut
160  
« Count/Method/MinMax.pm ↑ Summary Count/IRV.pm »