blib/lib/Acme/Siteswap.pm

filter: Statementstmt 77.3% 58 / 75 Branchbran 60.5% 23 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 72.7% 8 / 11 Podpod 100% 3 / 3 totaltotal 72.4% 92 / 127 CC 26 SCAR 20.4
CC
26
Cov
72%
__ANON__
20.0
_expand_throws
12.0
_check_timing
9.0
CRAP 7.7
↑ Summary
1 package Acme::Siteswap;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use List::Util qw( max reduce );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Acme::Siteswap - Provide information about Juggling Siteswap patterns
10  
11 =head1 SYNOPSIS
12  
13 use Acme::Siteswap;
14 my $siteswap = Acme::Siteswap->new(
15 pattern => '53142',
16 balls => 3,
17 );
18 print "Awesome!\n" unless $siteswap->valid;
19  
20 =cut
21  
22 1 our $VERSION = '0.03';
23  
24 =head1 FUNCTIONS
25  
26 =head2 new
27  
28 Create a new Acme::Siteswap object.
29  
30 Options:
31  
32 =over 4
33  
34 =item pattern
35  
36 Mandatory. The siteswap pattern. Should be a series of throws.
37  
38 =item balls
39  
40 Mandatory. The number of balls in the pattern.
41  
42 =back
43  
44 =cut
45  
46 285 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
47 285 my $class = shift;
48 285 my $self = { @_ };
49 285 ▶ die "A siteswap pattern is required!" unless defined $self->{pattern};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pattern"} 0 285 285
50 285 ▶ die "The number of balls is required!" unless defined $self->{balls};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"balls"} 0 285 285
51 285 bless $self, $class;
52 285 return $self;
53 }
54  
55 =head2 valid
56  
57 Determines if the specified pattern is valid.
58  
59 =cut
60  
61 566 ▶sub valid {
Subroutine valid: called CC=6 SCAR=18.0
Pod documented
62 566 my $self = shift;
63 566 my $pattern = $self->{pattern};
64  
65 566 my @throws;
66 566 eval { @throws = _pattern_to_throws($pattern) };
67 566 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 566 566
68 0 $self->{error} = $@;
69 0 return 0;
70 }
71  
72 # Check that the numbers / throws == # of balls
73 566 my $total = 0;
74 566 for my $t (@throws) {
75 2702 ▶ if (ref $t eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $t eq 'ARRAY') { } 23 2679 2702
76 23 foreach my $m_t (@$t) {
77 46 $total += $m_t;
78 }
79 }
80 else {
81 2679 $total += $t;
82 }
83 }
84  
85 566 my $avg = $total / @throws;
86 566 ▶ unless ($avg == $self->{balls}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($avg == $self->{"balls"}) 1 565 566
87 1 $self->{error} = "sum of throws / # of throws does not equal # of balls!";
88 1 return 0;
89 }
90
91 565 return $self->_check_timing(@throws);
92 }
93  
94 565 ▶sub _check_timing {
Subroutine _check_timing: called CC=9 SCAR=22.0
95 565 my ($self, @throws) = @_;
96
97 # foreach non-zero throw, mark where the ball will next be
98 # thrown and make sure that each throw is fed.
99 565 ▶ my @throw_map = map { ref $_ eq 'ARRAY' ? scalar(@$_)
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 23 2678 2701
$_ > 0 ? : 2459 219 2678
100 : ( $_ > 0 ? 1 : 0 ) } @throws;
101 565 my @feeds = (0) x scalar @throws;
102 565 for my $i (0 .. $#throws) {
103 2701 ▶ my @subthrows = ref $throws[$i] eq 'ARRAY' ? @{$throws[$i]}
Branch: 1 branch
BranchTrueFalseTotal
ref $throws[$i] eq 'ARRAY' ? : 23 2678 2701
104 : ($throws[$i]);
105
106 2701 foreach my $throw (@subthrows) {
107 2724 ▶ next if $throws[$i] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $throws[$i] == 0 219 2505 2724
108 2505 my $next_thrown = ($i + $throw) % scalar @throws;
109 2505 $feeds[$next_thrown]++;
110 }
111 }
112  
113 565 for my $i (0 .. $#throws) {
114 2695 ▶ if ($feeds[$i] != $throw_map[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feeds[$i] != $throw_map[$i]) 3 2692 2695
115 3 $self->{error} = "Multiple throws would land at the same time.";
116 3 return 0;
117 }
118 }
119 562 return 1;
120 }
121  
122 =head2 error
123  
124 Returns an error message or empty string.
125  
126 =cut
127  
128 4 ▶sub error { $_[0]->{error} || '' }
Subroutine error: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"error"} or "" 4 0 4
129  
130 566 ▶sub _pattern_to_throws {
Subroutine _pattern_to_throws: called CC=4 SCAR=14.0
131 566 my $pattern = shift;
132  
133 566 my @throw_set = ();
134  
135 566 ▶ while ($pattern =~ m/
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ / # next block of non-multiplex throws (?: \G (\d+) ) # or the next multiplex throw | (?: \G \[(\d+)\] ) # or the end of the pattern | (?: \G \z ) /gmx) 1153 0 1153
136 # next block of non-multiplex throws
137 (?: \G (\d+) )
138 # or the next multiplex throw
139 | (?: \G \[(\d+)\] )
140 # or the end of the pattern
141 | (?: \G \z )
142 /xmg) {
143 1153 ▶ if ( defined $1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $1) { } 564 589 1153
elsif (defined $2) { } 23 566 589
144 564 push (@throw_set, split (//, $1));
145 }
146 elsif ( defined $2 ) {
147 23 push (@throw_set, [ split(//, $2) ]);
148 }
149 else {
150 # if we never get here, the pattern had an issue
151 566 return @throw_set;
152 }
153 }
154
155 0 die "unable to parse pattern: $pattern";
156 }
157  
158 0 ▶sub _max_throw {
Subroutine _max_throw: not called CC=2 SCAR=17.9
159 0 my ($throws) = @_;
160  
161 0 ▶ my $max_throw = reduce {
Subroutine __ANON__: not called CC=4 SCAR=30.0
162 0 ▶ my $a_1 = ( ref $a eq 'ARRAY' ? max(@$a) : $a );
Branch: 1 branch
BranchTrueFalseTotal
ref $a eq 'ARRAY' ? : 0 0 0
163 0 ▶ my $b_1 = ( ref $b eq 'ARRAY' ? max(@$b) : $b );
Branch: 1 branch
BranchTrueFalseTotal
ref $b eq 'ARRAY' ? : 0 0 0
164 0 ▶ $a_1 >= $b_1 ? $a_1 : $b_1;
Branch: 1 branch
BranchTrueFalseTotal
$a_1 >= $b_1 ? : 0 0 0
165 0 } @$throws;
166  
167 # if our pattern is a 1-length multiplex pattern,
168 # reduce returns the first element, so correct for
169 # that here
170 0 ▶ $max_throw = max(@$max_throw) if ref $max_throw eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $max_throw eq "ARRAY" 0 0 0
171  
172 0 return $max_throw;
173 }
174  
175 # extend the pattern by the number of throws equal to the biggest
176 # throw in the pattern, to ensure that every throw in the pattern
177 # lands at least once.
178 0 ▶sub _expand_throws {
Subroutine _expand_throws: not called CC=3 SCAR=24.8
179 0 my ($throws) = @_;
180 0 my $max_throw = _max_throw($throws);
181
182 0 foreach my $i (0 .. $max_throw) {
183 # if it's a multiplex throw, we want to copy it
184 0 ▶ my $t = ref $throws->[$i] eq 'ARRAY' ? [@{$throws->[$i]}]
Branch: 1 branch
BranchTrueFalseTotal
ref $throws->[$i] eq 'ARRAY' ? : 0 0 0
185 : $throws->[$i];
186 0 push @$throws, $t;
187 }
188 0 return $throws;
189 }
190  
191 =head1 AUTHORS
192  
193 Luke Closs, C<< <cpan at 5thplane dut com> >>
194 Multiplex support by Seamus Campbell, C<< <conform at deadgeek rot com >>
195  
196 =head1 BUGS
197  
198 Please report any bugs or feature requests to
199 C<bug-acme-siteswap at rt.cpan.org>, or through the web interface at
200 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Acme-Siteswap>.
201 I will be notified, and then you'll automatically be notified of progress on
202 your bug as I make changes.
203  
204 =head1 COPYRIGHT
205  
206 Copyright 2007 Luke Closs, all rights reserved.
207  
208 =head1 LICENSE
209  
210 This program is free software; you can redistribute it and/or modify it
211 under the same terms as Perl itself.
212  
213 =cut
214  
215 1 1;
↑ Summary