| 1 | package Math::Permute::Array; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | require Exporter; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 11 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 12 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # This allows declaration use Math::Permute::Array ':all'; | ||||||||||||||||||||||||||||
| 15 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||
| 16 | # will save memory. | ||||||||||||||||||||||||||||
| 17 | 5 | our %EXPORT_TAGS = ( 'all' => [ qw()], | |||||||||||||||||||||||||||
| 18 | 'Permute' => [ qw(Permute) ], | ||||||||||||||||||||||||||||
| 19 | 'Apply_on_perms' => [ qw(Apply_on_perms) ] | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 5 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 25 | Permute | ||||||||||||||||||||||||||||
| 26 | Apply_on_perms | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 5 | our $VERSION = '0.043'; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub new | ||||||||||||||||||||||||||||
| 33 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 35 | 4 | my $self = {}; | |||||||||||||||||||||||||||
| 36 | 4 | $self->{array} = shift; | |||||||||||||||||||||||||||
| 37 | 4 | $self->{iterator} = 0; | |||||||||||||||||||||||||||
| 38 | 4 | $self->{cardinal} = undef; | |||||||||||||||||||||||||||
| 39 | 4 | bless($self, $class); | |||||||||||||||||||||||||||
| 40 | 4 | ▶ | return undef unless (defined $self->{array}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 3 | return $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | #nice implementation from the cookbook | ||||||||||||||||||||||||||||
| 45 | #but mine seems lightly more efficient | ||||||||||||||||||||||||||||
| 46 | #sub N2Permute | ||||||||||||||||||||||||||||
| 47 | #{ | ||||||||||||||||||||||||||||
| 48 | # my $rank = shift; | ||||||||||||||||||||||||||||
| 49 | # my $size = shift; | ||||||||||||||||||||||||||||
| 50 | # my @res; | ||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||
| 52 | # my $i=1; | ||||||||||||||||||||||||||||
| 53 | # while($i<=$size){ | ||||||||||||||||||||||||||||
| 54 | # push @res, $rank % ($i); | ||||||||||||||||||||||||||||
| 55 | # $rank = int($rank / ($i)); | ||||||||||||||||||||||||||||
| 56 | # $i++; | ||||||||||||||||||||||||||||
| 57 | # } | ||||||||||||||||||||||||||||
| 58 | # return @res; | ||||||||||||||||||||||||||||
| 59 | #} | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub Permute | ||||||||||||||||||||||||||||
| 62 | 80643 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
Permute: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 80643 | my $rest = shift; | |||||||||||||||||||||||||||
| 64 | 80643 | my $array = shift; | |||||||||||||||||||||||||||
| 65 | 80643 | ▶ | return undef unless (defined $rest and defined $array); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $rest and defined $array
MC/DC100%2/2
defined $rest and defined $array
defined $rest
defined $array
Truth table100%3/3
A and B
A= defined $restB= defined $array
| |||||||||||||||||||||||||||||
| 66 | 80640 | my @array = @{$array}; | |||||||||||||||||||||||||||
| 67 | 80640 | my @res; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # my $size = $#$array+1; | ||||||||||||||||||||||||||||
| 70 | # my @perm = N2Permute($k,$size); | ||||||||||||||||||||||||||||
| 71 | #push @res, splice(@array, (pop @perm), 1 )while @perm; | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 80640 | my $i = 0; | |||||||||||||||||||||||||||
| 74 | 80640 | ▶ | while($rest != 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 506558 | $res[$i] = splice @array, $rest % ($#array + 1), 1; | |||||||||||||||||||||||||||
| 76 | 506558 | $rest = int($rest / ($#array + 2)); | |||||||||||||||||||||||||||
| 77 | 506558 | $i++; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 80640 | push @res, @array; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 80640 | return \@res; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub permutation | ||||||||||||||||||||||||||||
| 85 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
permutation: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 87 | 7 | my $rest = shift; | |||||||||||||||||||||||||||
| 88 | 7 | ▶ | return undef unless (defined $rest); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 6 | my @array = @{$self->{array}}; | |||||||||||||||||||||||||||
| 90 | 6 | my @res; | |||||||||||||||||||||||||||
| 91 | 6 | my $i = 0; | |||||||||||||||||||||||||||
| 92 | 6 | ▶ | while($rest != 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 8 | $res[$i] = splice @array, $rest % ($#array + 1), 1; | |||||||||||||||||||||||||||
| 94 | 8 | $rest = int($rest / ($#array + 2)); | |||||||||||||||||||||||||||
| 95 | 8 | $i++; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 6 | push @res, @array; | |||||||||||||||||||||||||||
| 98 | 6 | return \@res; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub Apply_on_perms(&@) | ||||||||||||||||||||||||||||
| 102 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
Apply_on_perms: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 4 | my $func = shift; | |||||||||||||||||||||||||||
| 104 | 4 | my $array = shift; | |||||||||||||||||||||||||||
| 105 | 4 | ▶ | return undef unless (defined $func and defined $array); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $func and defined $array
MC/DC100%2/2
defined $func and defined $array
defined $func
defined $array
Truth table100%3/3
A and B
A= defined $funcB= defined $array
| |||||||||||||||||||||||||||||
| 106 | 1 | my $rest; | |||||||||||||||||||||||||||
| 107 | my $i; | ||||||||||||||||||||||||||||
| 108 | 1 | my $j; | |||||||||||||||||||||||||||
| 109 | 1 | my @array = @{$array}; | |||||||||||||||||||||||||||
| 110 | 1 | my $size = $#array+1; | |||||||||||||||||||||||||||
| 111 | 1 | my $card = factorial($size); | |||||||||||||||||||||||||||
| 112 | 1 | my @res; | |||||||||||||||||||||||||||
| 113 | 1 | ▶ | for($j=0;$j<$card;$j++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 40320 | @res = (); | |||||||||||||||||||||||||||
| 115 | 40320 | $rest = $j; | |||||||||||||||||||||||||||
| 116 | 40320 | $i = 0; | |||||||||||||||||||||||||||
| 117 | 40320 | ▶ | while($rest != 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 253279 | $res[$i] = splice @array, $rest % ($#array + 1), 1; | |||||||||||||||||||||||||||
| 119 | 253279 | $rest = int($rest / ($#array + 2)); | |||||||||||||||||||||||||||
| 120 | 253279 | $i++; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 40320 | push @res, @array; | |||||||||||||||||||||||||||
| 123 | 40320 | &$func(@res); | |||||||||||||||||||||||||||
| 124 | 40320 | @array = @{$array}; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 1 | return 0; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub cur | ||||||||||||||||||||||||||||
| 130 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cur: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 132 | 2 | return Math::Permute::Array::Permute($self->{iterator},$self->{array}); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | sub prev | ||||||||||||||||||||||||||||
| 136 | 40320 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
prev: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 40320 | my $self = shift; | |||||||||||||||||||||||||||
| 138 | 40320 | ▶ | return undef if($self->{iterator} == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 40319 | $self->{iterator}--; | |||||||||||||||||||||||||||
| 140 | 40319 | return Math::Permute::Array::Permute($self->{iterator},$self->{array}); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub next | ||||||||||||||||||||||||||||
| 144 | 40320 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 40320 | my $self = shift; | |||||||||||||||||||||||||||
| 146 | 40320 | ▶ | return undef if($self->{iterator} >= $self->cardinal() - 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 40319 | $self->{iterator}++; | |||||||||||||||||||||||||||
| 148 | 40319 | return Math::Permute::Array::Permute($self->{iterator},$self->{array}); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub cardinal | ||||||||||||||||||||||||||||
| 152 | 40322 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
cardinal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 40322 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | 40322 | ▶ | unless(defined $self->{cardinal}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 2 | $self->{cardinal} = factorial($#{$self->{array}} + 1); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 40322 | return $self->{cardinal}; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | #this part come from: | ||||||||||||||||||||||||||||
| 161 | # www.theperlreview.com/SamplePages/ThePerlReview-v5i1.p23.pdf | ||||||||||||||||||||||||||||
| 162 | # Author: Alberto Manuel Simoes | ||||||||||||||||||||||||||||
| 163 | sub factorial | ||||||||||||||||||||||||||||
| 164 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
factorial: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | 3 | my $value = shift; | |||||||||||||||||||||||||||
| 166 | 3 | my $res = 1; | |||||||||||||||||||||||||||
| 167 | 3 | ▶ | while ($value > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 16 | $res *= $value; | |||||||||||||||||||||||||||
| 169 | 16 | $value--; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 3 | return $res; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 5 | 1; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||