File Coverage

blib/lib/Math/Subsets/List.pm
Criterion Covered Total %
statement 24 24 100.0
branch 2 2 100.0
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 30 31 96.7


line stmt bran cond sub pod time code
1             =head1 Name
2            
3             Math::Subsets::List - Generate all the subsets of a list.
4            
5             =head1 Synopsis
6            
7             use Math::Subsets::List;
8            
9             subsets {say "@_"} qw(a b c);
10            
11             #
12             # a
13             # b
14             # c
15             # a b
16             # a c
17             # b c
18             # a b c
19            
20             =cut
21            
22             package Math::Subsets::List;
23            
24 11     11   174240 use strict;
  11         18  
  11         1667  
25            
26 10     10 0 121 sub subsets(&@) # Generate all the subsets of a list
27             {my $s = shift; # Subroutine to call to process each subset
28            
29 10         23 my $n = scalar(@_); # Size of list to be subsetted
30 10         16 my $l = 0; # Current item
31 10         26 my @p = (); # Current subset
32 10         22 my @P = @_; # List to be subsetted
33            
34 10         16 my $p; $p = sub # Generate each subset
  1096         650  
35 2200 100   2200   2069 {if ($l < $n)
36 1104         1164 {++$l;
37 1096         990 &$p();
38 1094         1279 push @p, $P[$l-1];
39 1094         966 &$p();
40 1094         1096 pop @p;
41 1094         807 --$l
42             }
43             else
44             {&$s(@p)
45             }
46 10         42 };
47            
48 10         40 &$p;
49 9         16 $p = undef; # Break memory loop per Philipp Rumpf
50            
51 9         92 2**$n;
52             }
53            
54             # Export details
55            
56             require 5;
57             require Exporter;
58            
59 11     11   54 use vars qw(@ISA @EXPORT $VERSION);
  11         13  
  11         1039  
60            
61             @ISA = qw(Exporter);
62             @EXPORT = qw(subsets);
63             $VERSION = '1.008'; # Friday 30 Jan 2015
64            
65             =head1 Description
66            
67             Generate all the subsets of a list and process them using the standard
68             Perl metaphor.
69            
70             C returns the number of subsets. Please note that this
71             includes the empty set as it is a subset of all sets.
72            
73             Please note that the order in which the subsets are generated is
74             not guaranteed, so please do not rely on it.
75            
76             C is easy to use and fast. It is written in 100% Pure Perl.
77            
78             =head1 Export
79            
80             The C function is exported.
81            
82             =head1 Installation
83            
84             Standard Module::Build process for building and installing modules:
85            
86             perl Build.PL
87             ./Build
88             ./Build test
89             ./Build install
90            
91             Or, if you're on a platform (like DOS or Windows) that doesn't require
92             the "./" notation, you can do this:
93            
94             perl Build.PL
95             Build
96             Build test
97             Build install
98            
99             =head1 Author
100            
101             PhilipRBrenan@appaapps.com
102            
103             http://www.appaapps.com
104            
105             =head1 Acknowledgments
106            
107             With lots of help and advice from Philip Rumpff to who I am most grateful.
108            
109             =head1 See Also
110            
111             =over
112            
113             =item L
114            
115             =item L
116            
117             =item L
118            
119             =back
120            
121             =head1 Copyright
122            
123             Copyright (c) 2009 Philip R Brenan.
124            
125             This module is free software. It may be used, redistributed and/or
126             modified under the same terms as Perl itself.
127            
128             =cut
129            
130             1;
131             __END__