qbit/Array.pm

filter: Statementstmt 30.9% 17 / 55 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.7% 5 / 14 Podpod 100% 9 / 9 totaltotal 34.4% 31 / 90 CC 15 SCAR 23.1
CC
15
Cov
25%
arrays_intersection
20.0
array_uniq
12.0
array_n_min
12.0
CRAP 10.0
« qbit/File.pm ↑ Summary qbit/Hash.pm »
1 =head1 Name
2  
3 qbit::Array - Functions to manipulate arrays.
4  
5 =cut
6  
7 package qbit::Array;
8 8 $qbit::Array::VERSION = '2.5';
9 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 BEGIN {
16 8 ▶ our (@EXPORT, @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 @EXPORT = qw(
19 in_array arrays_intersection arrays_difference array_uniq
20 array_n_min array_min array_n_max array_max
21 array_avg
22 );
23 8 @EXPORT_OK = @EXPORT;
24 }
25  
26 =head1 Functions
27  
28 =head2 in_array
29  
30 B<Arguments:>
31  
32 =over
33  
34 =item
35  
36 B<$elem> - scalar;
37  
38 =item
39  
40 B<$array> - array ref.
41  
42 =back
43  
44 B<Return value:> boolean.
45  
46 =cut
47  
48 sub in_array($$) {
49 0 ▶ my ($elem, $array) = @_;
Subroutine in_array: not called CC=1 SCAR=6.9
Pod documented
50  
51 0 my %hs;
52 0 @hs{@$array} = ();
53  
54 0 return exists $hs{$elem};
55 }
56  
57 =head2 arrays_intersection
58  
59 B<Arguments:>
60  
61 =over
62  
63 =item
64  
65 B<$array_ref1>;
66  
67 =item
68  
69 B<$array_ref2>;
70  
71 =item
72  
73 B<...>;
74  
75 =item
76  
77 B<$array_refN>.
78  
79 =back
80  
81 B<Return value:> array ref, intersection of all arrays (unique values).
82  
83 =cut
84  
85 sub arrays_intersection(@) {
86 0 ▶ my %hs = ();
Subroutine arrays_intersection: not called CC=4 SCAR=30.0
Pod documented
87 0 foreach my $array (map {array_uniq($_)} @_) {
88 0 ▶ exists($hs{$_}) ? ($hs{$_}++) : ($hs{$_} = 1) for @$array;
Branch: 1 branch
BranchTrueFalseTotal
exists $hs{$_} ? : 0 0 0
89 }
90  
91 0 return [grep {$hs{$_} == @_} keys %hs];
92 }
93  
94 =head2 arrays_difference
95  
96 B<Arguments:>
97  
98 =over
99  
100 =item
101  
102 B<$array1> - array ref, minuend;
103  
104 =item
105  
106 B<$array2> - array ref, subtrahend.
107  
108 =back
109  
110 B<Return value:> array ref.
111  
112 =cut
113  
114 sub arrays_difference($$) {
115 0 ▶ my ($array1, $array2) = @_;
Subroutine arrays_difference: not called CC=1 SCAR=6.9
Pod documented
116  
117 0 my %hs;
118 0 @hs{@$array2} = ();
119  
120 0 return [grep {!exists($hs{$_})} @$array1];
121 }
122  
123 =head2 array_uniq
124  
125 B<Arguments:>
126  
127 =over
128  
129 =item
130  
131 B<@array> - each element may be array ref or scalar.
132  
133 =back
134  
135 B<Return value:> array ref, unique values from all arrays.
136  
137 =cut
138  
139 sub array_uniq(@) {
140 0 ▶ my %hs;
Subroutine array_uniq: not called CC=3 SCAR=24.8
Pod documented
141 0 ▶ @hs{ref($_) eq 'ARRAY' ? grep {defined($_)} @$_ : ($_)} = () for @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
142 0 return [keys %hs];
143 }
144  
145 =head2 array_n_min
146  
147 B<Arguments:>
148  
149 =over
150  
151 =item
152  
153 B<@array> - array of numbers.
154  
155 =back
156  
157 B<Return value:> number, min value (numeric comparasion).
158  
159 =cut
160  
161 sub array_n_min(@) {
162 0 ▶ my $min = $_[0];
Subroutine array_n_min: not called CC=3 SCAR=24.8
Pod documented
163 0 foreach (@_) {
164 0 ▶ $min = $_ if $min > $_;
Branch: 1 branch
BranchTrueFalseTotal
if $min > $_ 0 0 0
165 }
166 0 return $min;
167 }
168  
169 =head2 array_min
170  
171 B<Arguments:>
172  
173 =over
174  
175 =item
176  
177 B<@array> - array of strings.
178  
179 =back
180  
181 B<Return value:> string, min value (string comparasion).
182  
183 =cut
184  
185 sub array_min(@) {
186 0 ▶ my $min = $_[0];
Subroutine array_min: not called CC=3 SCAR=24.8
Pod documented
187 0 foreach (@_) {
188 0 ▶ $min = $_ if $min gt $_;
Branch: 1 branch
BranchTrueFalseTotal
if $min gt $_ 0 0 0
189 }
190 0 return $min;
191 }
192  
193 =head2 array_n_max
194  
195 B<Arguments:>
196  
197 =over
198  
199 =item
200  
201 B<@array> - array of numbers.
202  
203 =back
204  
205 B<Return value:> number, max value (numeric comparasion).
206  
207 =cut
208  
209 sub array_n_max(@) {
210 0 ▶ my $max = $_[0];
Subroutine array_n_max: not called CC=3 SCAR=24.8
Pod documented
211 0 foreach (@_) {
212 0 ▶ $max = $_ if $max < $_;
Branch: 1 branch
BranchTrueFalseTotal
if $max < $_ 0 0 0
213 }
214 0 return $max;
215 }
216  
217 =head2 array_max
218  
219 B<Arguments:>
220  
221 =over
222  
223 =item
224  
225 B<@array> - array of strings.
226  
227 =back
228  
229 B<Return value:> string, max value (string comparasion).
230  
231 =cut
232  
233 sub array_max(@) {
234 0 ▶ my $max = $_[0];
Subroutine array_max: not called CC=3 SCAR=24.8
Pod documented
235 0 foreach (@_) {
236 0 ▶ $max = $_ if $max lt $_;
Branch: 1 branch
BranchTrueFalseTotal
if $max lt $_ 0 0 0
237 }
238 0 return $max;
239 }
240  
241 =head2 array_avg
242  
243 B<Arguments:>
244  
245 =over
246  
247 =item
248  
249 B<@array> - array of numbers.
250  
251 =back
252  
253 B<Return value:> number, average value.
254  
255 =cut
256  
257 sub array_avg(@) {
258 0 ▶ my $sum = 0;
Subroutine array_avg: not called CC=2 SCAR=17.9
Pod documented
259 0 $sum += $_ foreach @_;
260 0 return $sum / @_;
261 }
262  
263 8 1;
« qbit/File.pm ↑ Summary qbit/Hash.pm »