blib/lib/List/Permutor/Repeat.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 6 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 61 CC 4 SCAR 22.2
CC
4
Cov
0%
next
20.0
new
2.0
reset
2.0
CRAP 9.2
1 package List::Permutor::Repeat;
2
3 use strict;
4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
5
6 require Exporter;
7
8 @ISA = qw(Exporter);
9 @EXPORT = qw();
10 $VERSION = '0.01';
11
12 sub new {
13 my $class = shift;
14 my $items = [ @_ ];
15 bless [ $items, [ (0) x scalar(@$items) ] ], $class;
16 }
17
18 sub reset {
19 my $self = shift;
20 my $items = $self->[0];
21 $self->[1] = [ (0) x scalar(@$items) ];
22 1; # No useful return value
23 }
24
25 sub peek {
26 my $self = shift;
27 my $items = $self->[0];
28 my $rv = $self->[1];
29 @$items[ @$rv ];
30 }
31
32 sub next {
33 my $self = shift;
34 my $items = $self->[0];
35 my $rv = $self->[1];
36 return unless @$rv;
37
38 my @next=@$rv;
39 my @tail= pop @next;
40
41 while(1){
42 if($tail[0]<$#$items){ ## +1
43 $tail[0]++;
44 $self->[1] = [ @next, @tail ];
45 last;
46 }elsif(! @next){ ## is max
47 $self->[1] = [];
48 last;
49 }else{ ## this item -> 1 , next item +1
50 $tail[0]=0;
51 my $n = pop @next;
52 unshift @tail, $n;
53 }
54 }
55 return @$items[ @$rv ];
56 }
57
58 1;
59 =pod
60 =encoding utf8
61
62 =head1 NAME
63
64 List::Permutor::Repeat - Process all possible repeat permutations of a list
65
66 组合数学,可重复排列
67
68 =head1 SYNOPSIS
69
70 use List::Permutor::Repeat;
71 my $perm = new List::Permutor::Repeat qw/ a b /;
72 while (my @set = $perm->next) {
73 print @set, "\n";
74 }
75
76 #aa
77 #ab
78 #ba
79 #bb
80
81 =head1 DESCRIPTION
82
83 This is repeat permutation.
84
85 Not repeat elem permutation see Tom Phoenix's L<List::Permutor>.
86
87 =head1 METHODS
88
89 =over 4
90
91 =item new LIST
92
93 初始化,传入一个数组
94
95 Returns a permutor for the given items.
96
97 =item next
98
99 取出下一个可重复的排列
100
101 Returns a list of the items in the next permutation.
102
103 for example, the repeat permutations of (1..5)
104 first: (1, 1, 1, 1, 1)
105 last: (5, 5, 5, 5, 5)
106
107 =item peek
108
109 取回当前的排列
110
111 Returns the list of items which would be returned by next()
112
113 =item reset
114
115 重置排列,从头开始
116
117 Resets the iterator to the start.
118
119 =back
120
121 =head1 AUTHOR
122
123 Abby Pan <abbypan@gmail.com>
124
125 The object oriented interface/method is taken from Tom Phoenix's L<List::Permutor>.
126
127 =cut