| 1 | package Iterator::Flex::Permute; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Permute Iterator Class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use v5.28; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use experimental qw( signatures postderef declared_refs ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.33'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use parent 'Iterator::Flex::Base'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Iterator::Flex::Utils qw( :IterAttrs :IterStates throw_failure ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Iterator::Flex::Factory 'to_iterator'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Ref::Util 'is_hashref', 'is_arrayref'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub new ( $class, $array, $pars = {} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | ▶ | throw_failure( parameter => q{'array' argument must be an array} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | unless is_arrayref( $array ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | throw_failure( parameter => q{'pars' argument must be a hash} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | unless is_hashref( $pars ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | my %pars = $pars->%*; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | ▶ | my $k = delete $pars{k} // $array->@*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $pars{"k"} // @$array
MC/DC0%0/2
delete $pars{"k"} // @$array
delete $pars{"k"}
@$array
Truth table33%1/3
A // B
A= delete $pars{"k"}B= @$array
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | my $n = $array->@*; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 4 | ▶ | defined $k && Scalar::Util::looks_like_number( $k ) && int( $k ) == $k && $k > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
defined $k and &Scalar::Util::looks_like_number($k) and int $k == $k and $k > 0
defined $k and &Scalar::Util::looks_like_number($k) and int $k == $k
defined $k and &Scalar::Util::looks_like_number($k)
MC/DC0%0/4
defined $k and &Scalar::Util::looks_like_number($k) and int $k == $k and $k > 0
defined $k
&Scalar::Util::looks_like_number($k)
int $k == $k
$k > 0
Truth table20%1/5
A and B and C and D
A= defined $kB= &Scalar::Util::looks_like_number($k)C= int $k == $kD= $k > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | || throw_failure( parameter => 'k parameter is not a positive integer' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | throw_failure( parameter => "size of subset (k = $k) > size of set ( $n )" ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | if $k > $n; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | $class->SUPER::new( { k => $k, array => $array, }, \%pars ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | my sub permutations; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | ▶ | sub construct ( $class, $state ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
construct: called CC=8 SCAR=21.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 4 | ▶ | throw_failure( parameter => q{state must be a HASH reference} ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | unless Ref::Util::is_hashref( $state ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | my ( $array, $k, $idx, $value, $c, $i, $restart ) = @{$state}{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | qw[ array k idx value | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | c i restart | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | $value //= []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$value //= []
MC/DC0%0/1
$value //= []
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | ▶ | $c //= [ ( 0 ) x $k ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$c //= [(0) x $k]
MC/DC0%0/1
$c //= [(0) x $k]
$c
Truth table50%1/2
A
A= $c
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | ▶ | $i //= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$i //= 1
MC/DC0%0/1
$i //= 1
$i
Truth table50%1/2
A
A= $i
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | ▶ | $restart //= !!0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$restart //=
MC/DC0%0/1
$restart //=
$restart
Truth table50%1/2
A
A= $restart
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | my $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | my $iterator_state; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | my $n = $array->@*; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | my %params = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ( +_SELF ) => \$self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | ( +STATE ) => \$iterator_state, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | ( +CURRENT ) => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | return $idx ? [ $value->@* ] : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | ( +RESET ) => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $i = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $c = [ ( 0 ) x $k ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $restart = !!0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | $idx = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $value->@* = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | +( REWIND ) => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $i = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | $c = [ ( 0 ) x $k ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | $restart = !!0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $idx = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 4 | ▶ | if ( $n == $k ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | $params{ +NEXT } = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 10 | ▶ | return $self->signal_exhaustion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | if $iterator_state == IterState_EXHAUSTED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 10 | my \@value = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 10 | my \@array = $array; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 10 | ▶ | if ( !defined $idx ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | $idx = [ 0 .. $k - 1 ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 8 | $i = permutations( $c, $i, $n, $idx, $restart ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 8 | ▶ | return $self->signal_exhaustion if !defined $i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 6 | $restart = !!1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 8 | @value = @array[ $idx->@* ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 8 | return [@value]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | my $combination = @{$state}{'combination'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 2 | ▶ | $combination //= [ 0 .. $k - 1 ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$combination //= [0 .. $k - 1]
MC/DC0%0/1
$combination //= [0 .. $k - 1]
$combination
Truth table50%1/2
A
A= $combination
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 2 | my \@combination = $combination; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | $params{ +NEXT } = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 20 | ▶ | return $self->signal_exhaustion | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | if $iterator_state == IterState_EXHAUSTED; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 20 | my \@value = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 20 | my \@array = $array; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 20 | ▶ | if ( !defined $idx ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 2 | $idx = [ 0 .. $k - 1 ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 18 | $i = permutations( $c, $i, $k, $idx, $restart ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 18 | ▶ | if ( !defined $i ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 9 | ▶ | next_combination( \@combination, $n ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | or return $self->signal_exhaustion; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 7 | $i = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 7 | $c = [ ( 0 ) x $k ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 7 | $restart = !!0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 7 | $idx = [ 0 .. $k - 1 ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 9 | $restart = !!1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 18 | @value = @array[ @combination[ $idx->@* ] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 18 | return [@value]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 4 | return \%params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | # https://en.wikipedia.org/wiki/Heap%27s_algorithm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # procedure permutations(n : integer, A : array of any): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # // c is an encoding of the stack state. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # // c[k] encodes the for-loop counter for when permutations(k + 1, A) is called | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # c : array of int | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # for i := 0; i < n; i += 1 do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # c[i] := 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # end for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # output(A) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | # // i acts similarly to a stack pointer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | # i := 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # while i < n do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # if c[i] < i then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # if i is even then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # swap(A[0], A[i]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # swap(A[c[i]], A[i]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # output(A) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # // Swap has occurred ending the while-loop. Simulate the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # // increment of the while-loop counter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # c[i] += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # // Simulate recursive call reaching the base case by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # // bringing the pointer to the base case analog in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # // array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # i := 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | # else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # // Calling permutations(i+1, A) has ended as the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # // while-loop terminated. Reset the state and simulate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # // popping the stack by incrementing the pointer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # c[i] := 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # i += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # end if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # end while | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | ▶ | sub permutations ( $c, $i, $n, $A, $restart ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
permutations: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # these are initialized outside of here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 26 | my \@c = $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 26 | my \@A = $A; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 26 | ▶ | while ( $i < $n ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 44 | ▶ | if ( $c[$i] < $i ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 30 | ▶ | if ( $restart ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 15 | $restart = !!0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 15 | $c[$i] += 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 15 | $i = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 15 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 15 | ▶ | if ( 0 == ( $i % 2 ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | my $t = $A[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 2 | $A[$i] = $A[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | $A[0] = $t; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 13 | my $t = $A[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 13 | $A[$i] = $A[ $c[$i] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 13 | $A[ $c[$i] ] = $t; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 15 | return $i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 14 | $c[$i] = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 14 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 11 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # see https://cs.stackexchange.com/a/161542 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | ▶ | sub next_combination ( $A, $n ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_combination: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 9 | my \@A = $A; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 9 | my $k = @A; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | # for ( my $i = $k - 1 ; $i >= 0 ; $i-- ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 9 | for my $i ( reverse 0 .. $k - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 14 | ▶ | if ( $A[$i] < $n - $k + $i ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 7 | $A[$i]++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 7 | for my $j ( $i + 1 .. $k - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 3 | $A[$j] = $A[ $j - 1 ] + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 7 | return !!1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 2 | return !!0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # Brute Force, slow | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | # my \@idx = $idx; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | # my $last_slot; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | # my $overflow; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # BACKWARDS: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # for my $slot ( reverse 0 .. $k - 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # $last_slot = $slot; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # my $iarr = $idx[$slot]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | # $overflow = !!0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | # while ( $iarr++ < $n - 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # next if elem_num( $iarr, @idx[ 0 .. $slot - 1 ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # $idx[$slot] = $iarr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | # last BACKWARDS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # $overflow = !!1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # return $self->signal_exhaustion if $last_slot == 0 && $overflow; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | # FORWARDS: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # for my $slot ( $last_slot + 1 .. $k - 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # for my $iarr ( 0 .. $n - 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # next if elem_num( $iarr, @idx[ 0 .. $slot - 1 ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | # $idx[$slot] = $iarr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # next FORWARDS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 2 | __PACKAGE__->_add_roles( qw[ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | State::Closure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | Next::ClosedSelf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | Rewind::Closure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | Reset::Closure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | Current::Closure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # This file is part of Iterator-Flex | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||