blib/lib/List/PowerSet.pm

filter: Statementstmt 100% 29 / 29 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 45 / 45 CC 6 SCAR 11.0
CC
6
Cov
100%
__ANON__
5.0
powerset
2.0
BEGIN
1.0
CRAP 3.0
↑ Summary
1 package List::PowerSet;
2  
3 # $Id: PowerSet.pm,v 1.4 2004/03/07 22:44:43 nik Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @EXPORT_OK = qw(powerset powerset_lazy);
11 1 our $VERSION = '0.01';
12  
13 =head1 NAME
14  
15 List::PowerSet - generate the power set of a list
16  
17 =head1 SYNOPSIS
18  
19 use List::PowerSet qw(powerset powerset_lazy);
20  
21 my $ps = powerset(qw(1 2 3));
22  
23 my $ps_iterator = powerset_lazy(1 .. 1_000);
24 while(my $set = $ps_iterator->()) {
25 # $set is the next powerset entry
26 }
27  
28 =head1 DESCRIPTION
29  
30 Suppose you have a list L. The power set of such a list is a list of
31 all the sublists that you can obtain from L by deleting elements from it.
32 For example, the power set of (1, 2, 3) is the list of lists ((), (1),
33 (2), (3), (1, 2), (1, 3), (2, 3), (1, 2, 3)), in some order.
34  
35 C<List::PowerSet> provides two functions (which are not exported by default,
36 you have to ask for them) to generate power sets.
37  
38 =cut
39  
40 =head1 FUNCTIONS
41  
42 =head2 B<powerset()>
43  
44 Given a list, C<powerset()> returns an array reference of array references,
45 each referring to a different subset in the powerset of the input list.
46  
47 my $ps = powerset(qw(1 2 3));
48  
49 # $ps == [ [1, 2, 3],
50 # [ 2, 3],
51 # [1, 3],
52 # [ 3],
53 # [1, 2 ],
54 # [ 2 ],
55 # [1 ],
56 # [ ] ];
57  
58 =cut
59  
60 # mjd's powerset implementation. See http://perl.plover.com/LOD/199803.html
61 # for more details
62 4 ▶sub powerset {
Subroutine powerset: called CC=2 SCAR=6.9
Pod documented
63 4 ▶ return [[]] if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 1 3 4
64 3 my $first = shift;
65 3 my $pow = &powerset;
66 3 [ map { [$first, @$_ ], [ @$_] } @$pow ];
67 }
68  
69 =head2 B<powerset_lazy()>
70  
71 Given even a moderately sized input list, C<powerset()> will have to
72 generate a huge result list, taking time and memory to generate. A 20
73 element input list to C<powerset()> will generate a result list containing
74 1,048,576 references to other arrays, on average containing 10 items.
75  
76 C<powerset_lazy()> takes the same input list as C<powerset()>, and returns
77 a subroutine reference. Every time you call through this reference an
78 array reference to a different subset of the powerset is generated and
79 returned.
80  
81 =cut
82  
83 # mjd's implementation, from personal e-mail
84 1 ▶sub powerset_lazy {
Subroutine powerset_lazy: called CC=1 SCAR=0.0
Pod documented
85 1 my @set = @_;
86 1 my @odometer = (1) x @set;
87 1 my $FINISHED;
88 9 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
89 9 ▶ return if $FINISHED;
Branch: 1 branch
BranchTrueFalseTotal
if $FINISHED 1 8 9
90 8 my @result;
91 8 my $adjust = 1;
92 8 for (0 .. $#odometer) {
93 24 ▶ push @result, $set[$_] if $odometer[$_];
Branch: 1 branch
BranchTrueFalseTotal
if $odometer[$_] 12 12 24
94 24 ▶ $adjust = $odometer[$_] = 1 - $odometer[$_] if $adjust;
Branch: 1 branch
BranchTrueFalseTotal
if $adjust 14 10 24
95 }
96 8 $FINISHED = (@result == 0);
97 8 \@result;
98 1 };
99 }
100  
101  
102 =head1 AUTHORS
103  
104 Mark Jason Dominus <mjd@plover.com>, Nik Clayton <nik@FreeBSD.org>
105  
106 The original code was written by Mark.
107  
108 The module was written by Nik, who discovered mjd's code after failing
109 to find a powerset implementation on CPAN. With mjd's permission he
110 packaged it so that others can easily make use of it.
111  
112 Copyright 2004 Mark Jason Dominus, and Nik Clayton. All Rights Reserved.
113  
114 This program is free software; you can redistribute it
115 and/or modify it under the same terms as Perl itself.
116  
117 =head1 BUGS
118  
119 None known.
120  
121 Bugs should be reported to via the CPAN RT system.
122 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List::PowerSet>.
123  
124 =cut
125  
126 1 1;
↑ Summary