blib/lib/Data/PowerSet.pm

filter: Statementstmt 100% 81 / 81 Branchbran 100% 54 / 54 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 6 / 6 totaltotal 100% 160 / 160 CC 30 SCAR 21.9
CC
30
Cov
100%
powerset
14.0
new
8.0
next
8.0
CRAP 8.9
↑ Summary
1 # Data::PowerSet.pm
2 #
3 # Copyright (c) 2005-2008 David Landgren
4 # All rights reserved
5  
6 package Data::PowerSet;
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use vars qw/$VERSION @ISA @EXPORT_OK/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 $VERSION = '0.05';
13 2 @ISA = ('Exporter');
14  
15 =head1 NAME
16  
17 Data::PowerSet - Generate all subsets of a list of elements
18  
19 =head1 VERSION
20  
21 This document describes version 0.05 of Data::PowerSet, released
22 2008-05-13.
23  
24 =head1 SYNOPSIS
25  
26 use Data::PowerSet 'powerset';
27  
28 my $powerset = powerset( 3, 1, 4 );
29 for my $p (@$powerset) {
30 print "@$p\n";
31 }
32  
33 # prints
34 3 1 4
35 1 4
36 3 4
37 4
38 3 1
39 1
40 3
41  
42 An object-oriented interface is also available;
43  
44 my $d = Data::PowerSet->new( 3, 1, 4 );
45 while (my $r = $d->next) {
46 print "@$r\n";
47 }
48 # produces the same output as above
49  
50 =head1 DESCRIPTION
51  
52 C<Data::PowerSet> takes a list and returns all possible
53 combinations of the elements appearing in the list without replacement.
54  
55 =head1 EXPORTABLE FUNCTIONS
56  
57 =over 8
58  
59 =item powerset
60  
61 The C<powerset> function takes an array (or a reference to an array) on
62 input and returns a reference to an array of arrays containing all the
63 possible unique combinations of elements.
64  
65 It is also possible to supply a reference to hash as the first
66 parameter to tweak the behaviour. See the C<new> method for a
67 description of what keys can be specified.
68  
69 powerset( 2, 5, 10, 17 );
70  
71 powerset( {min => 1}, qw(a b c d) );
72  
73 powerset( [qw[ bodine mondaugen gadrulfi fleische eigenvalue ]] );
74  
75 =cut
76  
77 2 push @EXPORT_OK, 'powerset';
78 6 ▶sub powerset {
Subroutine powerset: called CC=14 SCAR=26.4
Pod documented
79 6 my %args;
80 6 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 5 1 6
81 5 %args = %{shift @_};
82 }
83 6 ▶ my @list = ref($_[0]) eq 'ARRAY' ? @{shift @_} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 5 6
84  
85 6 ▶ $args{min} = exists $args{min} ? $args{min} < 0 ? 0 : $args{min} : 0;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'min'} ? : 3 3 6
$args{'min'} < 0 ? : 1 2 3
86 6 ▶ $args{max} = exists $args{max} ? $args{max} > @list ? @list : $args{max} : @list;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'max'} ? : 3 3 6
$args{'max'} > @list ? : 1 2 3
87  
88 ($args{min}, $args{max}) = ($args{max}, $args{min})
89 6 ▶ if $args{max} < $args{min};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"max"} < $args{"min"} 1 5 6
90  
91 6 my $lim = 2 ** @list - 1;
92 6 my @powerset;
93 6 ▶ while( $lim >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lim >= 0) 52 6 58
94 52 my @set;
95 52 my $mask = $lim--;
96 52 my $offset = 0;
97 52 ▶ while( $mask ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask) 122 52 174
98 122 ▶ push @set, $list[$offset] if $mask & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 1 84 38 122
99 122 $mask >>= 1;
100 122 ++$offset;
101 }
102 52 ▶ if( @set >= $args{min} and @set <= $args{max} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@set >= $args{"min"} and @set <= $args{"max"}) 43 9 52
Condition100%3/3
@set >= $args{"min"} and @set <= $args{"max"}
!ll&&!rl&&r
8143
MC/DC100%2/2
@set >= $args{"min"} and @set <= $args{"max"}
@set >= $args{"min"} @set <= $args{"max"}
Truth table100%3/3
A and B
A= @set >= $args{"min"}B= @set <= $args{"max"}
ABresult
0X0
100
111
103 push @powerset, exists $args{join}
104 43 ▶ ? join( $args{join}, @set)
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'join'} ? : 35 8 43
105 : [@set];
106 }
107 }
108 6 return \@powerset;
109 }
110  
111 =back
112  
113 =head1 METHODS
114  
115 The object-oriented interface provided by the module is implemented
116 with the following methods.
117  
118 =over 8
119  
120 =item new
121  
122 Creates a new C<Data::PowerSet> object.
123  
124 my $ps = Data::PowerSet->new( qw( foo bar grault waldo ));
125  
126 A reference to a hash may
127 be supplied, to change the way the object behaves.
128  
129 =over 8
130  
131 =item B<min>
132  
133 Minimum number of elements present in the selection.
134  
135 Note that the empty set (no elements) is quite valid, according to
136 the mathematical definition of a power set. If this is not what you
137 expect, setting C<min> to 1 will effectively cause the empty set to
138 be excluded from the result.
139  
140 my $ps = Data::PowerSet->new( {min=>2}, 2, 3, 5, 8, 11 );
141  
142 In the above object, no returned list will contain fewer
143 than 2 elements.
144  
145 =item B<max>
146  
147 Maximum number of elements present in the selection.
148  
149 my $ps = Data::PowerSet->new( {max=>3}, 2, 3, 5, 8, 11 );
150  
151 In the above object, no returned list will contain more
152 than 3 elements.
153  
154 =item B<join>
155  
156 Perform a C<join()> on each returned list using the
157 specified value.
158  
159 my $ps = Data::Powerset->new( {join=>'-'}, 'a', 'b' );
160  
161 When this attribute is used, the C<next()> method will
162 return a scalar rather than a reference to an array.
163  
164 =back
165  
166 =cut
167  
168 7 ▶sub new {
Subroutine new: called CC=8 SCAR=20.8
Pod documented
169 7 my $class = shift;
170 7 my %args;
171 7 ▶ if( ref($_[0]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 4 3 7
172 4 %args = %{shift(@_)};
173 }
174 7 ▶ if( ref($_[0]) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 2 5 7
175 2 $args{data} = shift @_;
176 }
177 else {
178 5 $args{data} = [@_],
179 }
180 7 $args{current} = 2**@{$args{data}}-1;
181  
182 $args{min} =
183 exists $args{min}
184 ? $args{min} < 0
185 ? 0 : $args{min}
186 7 ▶ : 0
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'min'} ? : 4 3 7
$args{'min'} < 0 ? : 1 3 4
187 ;
188  
189 $args{max} =
190 exists $args{max}
191 4 ? $args{max} > @{$args{data}}
192 1 ? @{$args{data}} : $args{max}
193 7 ▶ : @{$args{data}}
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'max'} ? : 4 3 7
$args{'max'} > @{$args{'data'};} ? : 1 3 4
194 ;
195  
196 ($args{min}, $args{max}) = ($args{max}, $args{min})
197 7 ▶ if $args{max} < $args{min};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"max"} < $args{"min"} 1 6 7
198 7 return bless \%args, $class;
199 }
200  
201 =item next
202  
203 Returns a reference to an array containing the next combination of
204 elements from the original list;
205  
206 my $ps = Data::PowerSet->new(qw(e t a i s o n));
207 my $first = $ps->next;
208 my $next = $ps->next;
209  
210 =cut
211  
212 59 ▶sub next {
Subroutine next: called CC=8 SCAR=20.8
Pod documented
213 59 my $self = shift;
214 59 my $ok = 0;
215 59 my @set;
216 59 ▶ until( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 93 54 147
217 93 ▶ return undef unless $self->{current} >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"current"} >= 0 5 88 93
218 88 my $mask = $self->{current}--;
219 88 my $offset = 0;
220 88 @set = ();
221 88 ▶ while( $mask ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask) 309 88 397
222 309 ▶ push @set, $self->{data}[$offset] if $mask & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $mask & 1 196 113 309
223 309 $mask >>= 1;
224 309 ++$offset;
225 }
226 88 ▶ $ok = 1 if @set >= $self->{min} and @set <= $self->{max};
Branch: 1 branch
BranchTrueFalseTotal
if @set >= $self->{"min"} and @set <= $self->{"max"} 54 34 88
Condition100%3/3
@set >= $self->{"min"} and @set <= $self->{"max"}
!ll&&!rl&&r
32254
MC/DC100%2/2
@set >= $self->{"min"} and @set <= $self->{"max"}
@set >= $self->{"min"} @set <= $self->{"max"}
Truth table100%3/3
A and B
A= @set >= $self->{"min"}B= @set <= $self->{"max"}
ABresult
0X0
100
111
227 }
228 54 ▶ return exists $self->{join} ? join($self->{join}, @set) : \@set;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'join'} ? : 15 39 54
229 }
230  
231 =item reset
232  
233 Restart from the first combination of the list.
234  
235 =cut
236  
237 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
238 1 my $self = shift;
239 1 $self->{current} = 2**@{$self->{data}}-1;
240 }
241  
242 =item data
243  
244 Accept a new list of elements from which to draw combinations.
245  
246 $ps->data( qw(all new elements to use) );
247  
248 =cut
249  
250 2 ▶sub data {
Subroutine data: called CC=3 SCAR=11.0
Pod documented
251 2 my $self = shift;
252 $self->{data} = [@_],
253 2 $self->{current} = 2**@{$self->{data}}-1;
254  
255 2 ▶ $self->{min} = @{$self->{data}} if $self->{min} > @{$self->{data}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"min"} > @{$self->{"data"};} 1 1 2
256 2 ▶ $self->{max} = @{$self->{data}} if $self->{max} > @{$self->{data}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max"} > @{$self->{"data"};} 1 1 2
257 }
258  
259 =item count
260  
261 Returns the number of elements in the set. This can be used
262 to set C<max> to the number of elements minus one, in order to
263 exclude the set of all elements, when the number of elements
264 is difficult to determine beforehand.
265  
266 =cut
267  
268 1 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
269 1 my $self = shift;
270 1 return scalar(@{$self->{data}});
271 }
272  
273 =back
274  
275 =head1 DIAGNOSTICS
276  
277 None.
278  
279 =head1 NOTES
280  
281 Power sets grow exponentially. A power set of 10 elements returns
282 a more than one thousand results. A power set of 20 elements contains
283 more than one million results. The module is not expected to be put
284 to use in larger sets.
285  
286 A power set, by definition, includes the set of no elements and
287 the set of all elements. If these results are not desired, the
288 C<min> and C<max> methods or properties can be used to exclude
289 them from the results.
290  
291 This module works with perl version 5.005_04 and above.
292  
293 =head1 SEE ALSO
294  
295 =over 8
296  
297 =item L<List::PowerSet>
298  
299 Another module that generates power sets. If I had managed to find
300 it in a search beforehand, I probably would have used it instead.
301 Nonetheless, C<Data::PowerSet> has a couple of features not
302 present in C<List::PowerSet>, but otherwise both can be used
303 pretty much interchangeably.
304  
305 =item L<Algorithm::Combinatorics>
306  
307 A fast (no stacks, no recursion) method for generating permutations
308 and combinations of a set. A power set is merely the union of all
309 combinations (of differing lengths).
310  
311 =item L<http://en.wikipedia.org/wiki/Power_set>
312  
313 The wikipedia definition of a power set.
314  
315 =back
316  
317 =head1 BUGS
318  
319 None known. Please report all bugs at
320 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-PowerSet|rt.cpan.org>
321  
322 Make sure you include the output from the following two commands:
323  
324 perl -MData::PowerSet -le 'print Data::PowerSet::VERSION'
325 perl -V
326  
327 =head1 ACKNOWLEDGEMENTS
328  
329 This module is dedicated to Estelle Souche, who pointed out the very
330 elegant and obvious algorithm. Smylers suggested the name.
331  
332 =head1 AUTHOR
333  
334 David Landgren, copyright (C) 2005-2008. All rights reserved.
335  
336 http://www.landgren.net/perl/
337  
338 If you (find a) use this module, I'd love to hear about it.
339 If you want to be informed of updates, send me a note. You
340 know my first name, you know my domain. Can you guess my
341 e-mail address?
342  
343 =head1 LICENSE
344  
345 This library is free software; you can redistribute it and/or modify
346 it under the same terms as Perl itself.
347  
348 =cut
349  
350 2 'The Lusty Decadent Delights of Imperial Pompeii';
↑ Summary