blib/lib/Set/Intersection.pm

filter: Statementstmt 100% 33 / 33 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 100% 46 / 46 CC 6 SCAR 12.4
CC
6
Cov
100%
get_intersection
5.0
_intersection
2.0
BEGIN
1.0
CRAP 3.4
↑ Summary
1 package Set::Intersection;
2
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 =head1 NAME
7
8 Set::Intersection - provides an API to get intersection (of set theory) of ARRAYs.
9
10 =head1 VERSION
11
12 Version 0.01
13
14 =cut
15
16 5 our $VERSION = '0.02';
17
18
19 =head1 SYNOPSIS
20
21 use Set::Intersection;
22
23 my @arr1 = qw/3 1 4 1 5 9/;
24 my @arr2 = qw/1 7 3 2 0 5/;
25 my @intersection = get_intersection(\@arr1, \@arr2);
26 # got (1, 3, 5) in @intersection
27
28 =head1 EXPORT
29
30 get_intersection
31
32 =cut
33
34 5 require Exporter;
35 5 our @ISA = qw/Exporter/;
36
37 5 our @EXPORT = qw/get_intersection/;
38
39 =head1 FUNCTIONS
40
41 =head2 get_intersection [\%options,] [\@ARRAY[, \@ARRAY[, ...]]]
42
43 Returns intersection set (as LIST) of all ARRAYs.
44
45 =over 1
46
47 =item The result LIST is uniqized and unordered.
48
49 =item If no ARRAYs passed, the result LIST is empty.
50
51 =item If only one ARRAY passed, the result LIST is same as the passed. (elements won't be uniqized nor order-changed)
52
53 =item If you have undef in any LIST, you'll be warned.
54
55 =back
56
57 =over 1
58
59 =item %options
60
61 =over 2
62
63 =item -preordered => BOOLEAN
64
65 =over 3
66
67 =item To reduce calculation time, get_intersection sorts ARRAYs
68 by their length before calculating intersections.
69
70 =item This option tells that order of ARRAYs are well done,
71 and calculation of intersection will be based on left most ARRAY.
72
73 =back
74
75 =back
76
77 =back
78
79 =cut
80
81 5 my %_default_opts = (
82 -preordered => 0,
83 );
84
85 sub get_intersection
86 {
87 12 ▶ my %opts;
Subroutine get_intersection: called CC=5 SCAR=16.1
Pod documented
88 12 ▶ if ( ref($_[0]) =~ m{^HASH} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($_[0]) =~ /^HASH/) 1 11 12
89 1 %opts = (%_default_opts, %{$_[0]});
90 1 shift;
91 }
92
93 12 my @arrs = @_;
94 12 ▶ return () if !@arrs;
Branch: 1 branch
BranchTrueFalseTotal
unless @arrs 1 11 12
95 11 ▶ return @{$arrs[0]} if @arrs == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @arrs == 1 1 10 11
96
97 10 ▶ @arrs = sort { @$a <=> @$b } @arrs if !$opts{-preordered};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"-preordered"} 9 1 10
98
99 10 my $head = shift @arrs;
100
101 10 _intersection($head, @arrs);
102 }
103
104 sub _intersection
105 {
106 10 ▶ my ($head, @left) = @_;
Subroutine _intersection: called CC=2 SCAR=6.9
107
108 10 my %h = map { $_ => undef } @$head;
109 10 for my $l ( @left ) {
110 15 %h = map { $_ => undef } grep { exists $h{$_} } @$l;
111 }
112 10 keys %h;
113 }
114
115 =head1 SEE ALSO
116
117 List::Compare, Set::Object
118
119 =head1 AUTHOR
120
121 turugina, C<< <turugina at cpan.org> >>
122
123 =head1 BUGS
124
125 Please report any bugs or feature requests to C<bug-list-intersection at rt.cpan.org>, or through
126 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Set-Intersection>. I will be notified, and then you'll
127 automatically be notified of progress on your bug as I make changes.
128
129
130
131
132 =head1 SUPPORT
133
134 You can find documentation for this module with the perldoc command.
135
136 perldoc Set::Intersection
137
138
139 You can also look for information at:
140
141 =over 4
142
143 =item * RT: CPAN's request tracker
144
145 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Set-Intersection>
146
147 =item * AnnoCPAN: Annotated CPAN documentation
148
149 L<http://annocpan.org/dist/Set-Intersection>
150
151 =item * CPAN Ratings
152
153 L<http://cpanratings.perl.org/d/Set-Intersection>
154
155 =item * Search CPAN
156
157 L<http://search.cpan.org/dist/Set-Intersection/>
158
159 =back
160
161
162 =head1 ACKNOWLEDGEMENTS
163
164
165 =head1 COPYRIGHT & LICENSE
166
167 Copyright 2009 turugina, all rights reserved.
168
169 This program is free software; you can redistribute it and/or modify it
170 under the same terms as Perl itself.
171
172
173 =cut
174
175 5 1; # End of Set::Intersection
↑ Summary