blib/lib/perl5i/0/ARRAY.pm untested

filter: Statementstmt 0.0% 0 / 181 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 410 CC 46 SCAR 42.1
CC
46
Cov
0%
_are_equal
272.0
_equal_overload
56.0
ARRAY::diff
30.0
CRAP 67.5
1 # vi: set ts=4 sw=4 ht=4 et :
2 package perl5i::0::ARRAY;
3 use 5.010;
4  
5 use strict;
6 use warnings;
7 no if $] >= 5.018000, warnings => 'experimental::smartmatch';
8  
9 sub ARRAY::first {
10 my ( $array, $filter ) = @_;
11  
12 # Deep recursion and segfault (lines 90 and 91 in first.t) if we use
13 # the same elegant approach as in ARRAY::grep().
14 if ( ref $filter eq 'Regexp' ) {
15 return List::Util::first( sub { $_ ~~ $filter }, @$array );
16 }
17  
18 return List::Util::first( sub { $filter->() }, @$array );
19  
20 }
21  
22 sub ARRAY::grep {
23 my ( $array, $filter ) = @_;
24  
25 my @result = CORE::grep { $_ ~~ $filter } @$array;
26  
27 return wantarray ? @result : \@result;
28 }
29  
30 sub ARRAY::all {
31 require List::MoreUtils;
32 return List::MoreUtils::all($_[1], @{$_[0]});
33 }
34  
35 sub ARRAY::any {
36 require List::MoreUtils;
37 return List::MoreUtils::any($_[1], @{$_[0]});
38 }
39  
40 sub ARRAY::none {
41 require List::MoreUtils;
42 return List::MoreUtils::none($_[1], @{$_[0]});
43 }
44  
45 sub ARRAY::true {
46 require List::MoreUtils;
47 return List::MoreUtils::true($_[1], @{$_[0]});
48 }
49  
50 sub ARRAY::false {
51 require List::MoreUtils;
52 return List::MoreUtils::false($_[1], @{$_[0]});
53 }
54  
55 sub ARRAY::uniq {
56 require List::MoreUtils;
57 my @uniq = List::MoreUtils::uniq(@{$_[0]});
58 return wantarray ? @uniq : \@uniq;
59 }
60  
61 sub ARRAY::minmax {
62 require List::MoreUtils;
63 my @minmax = List::MoreUtils::minmax(@{$_[0]});
64 return wantarray ? @minmax : \@minmax;
65 }
66  
67 sub ARRAY::mesh {
68 require List::MoreUtils;
69 my @mesh = List::MoreUtils::zip(@_);
70 return wantarray ? @mesh : \@mesh;
71 }
72  
73  
74 # Returns the code which will run when the object is used as a string
75 require overload;
76 my $overload_type = sub {
77 return unless ref $_[0];
78 my $str = overload::Method($_[0], q[""]);
79 my $num = overload::Method($_[0], "0+");
80 return "both" if $str and $num;
81 return "" if !$str and !$num;
82 return "str" if $str;
83 return "num" if $num;
84 };
85  
86  
87 sub ARRAY::diff {
88 my ($base, @rest) = @_;
89 unless (@rest) {
90 return wantarray ? @$base : $base;
91 }
92  
93 # XXX If I use carp here, the exception is "bizarre copy of ARRAY in
94 # ssasign ... "
95 die "Arguments must be array references" if grep { ref $_ ne 'ARRAY' } @rest;
96  
97 foreach my $array (@rest) {
98 $base = _diff_two($base, $array);
99 }
100  
101 return wantarray ? @$base : $base;
102 }
103  
104 sub _diff_two {
105 # Compare differences between two arrays.
106 my ($c, $d) = @_;
107  
108 my $diff = [];
109  
110 # For each element of $c, try to find if it is equal to any of the
111 # elements of $d. If not, it's unique, and has to be pushed into
112 # $diff.
113  
114 require List::MoreUtils;
115 foreach my $item (@$c) {
116 unless (
117 List::MoreUtils::any( sub { _are_equal( $item, $_ ) }, @$d )
118 )
119 {
120 push @$diff, $item;
121 }
122 }
123  
124 return $diff;
125 }
126  
127 sub ARRAY::intersect {
128 my ($base, @rest) = @_;
129  
130 unless (@rest) {
131 return wantarray ? @$base : $base;
132 }
133  
134 # XXX If I use carp here, the exception is "bizarre copy of ARRAY in
135 # ssasign ... "
136 die "Arguments must be array references" if grep { ref $_ ne 'ARRAY' } @rest;
137  
138 foreach my $array (@rest) {
139 $base = _intersect_two($base, $array);
140 }
141  
142 return wantarray ? @$base : $base;
143 }
144  
145 sub _intersect_two {
146 # Compare differences between two arrays.
147 my ($c, $d) = @_;
148  
149 my $intersect = [];
150  
151 # For each element of $c, try to find if it is equal to any of the
152 # elements of $d. If it is, it's shared, and has to be pushed into
153 # $intersect.
154  
155 require List::MoreUtils;
156 foreach my $item (@$c) {
157 if (
158 List::MoreUtils::any( sub { _are_equal( $item, $_ ) }, @$d )
159 )
160 {
161 push @$intersect, $item;
162 }
163 }
164  
165 return $intersect;
166 }
167  
168 sub _are_equal {
169 my ($r1, $r2) = @_;
170  
171 # given two scalars, decide whether they are identical or not,
172 # recursing over deep data structures. Since it uses recursion,
173 # traversal is done depth-first.
174 # Warning: complex if-then-else decision tree ahead. It's ordered on
175 # my perceived and anecdotical take on the frequency of occurrence
176 # of each reftype: most popular on top, most rare on the bottom.
177 # This way we return as early as possible.
178  
179 # undef eq undef
180 return 1 if !defined $r1 and !defined $r2;
181  
182 # One is defined, one isn't
183 return if defined $r1 xor defined $r2;
184  
185 my( $ref1, $ref2 ) = (ref $r1, ref $r2);
186  
187 if( !$ref1 and !$ref2 ) {
188 my $is_num1 = SCALAR::is_number($r1);
189 my $is_num2 = SCALAR::is_number($r2);
190 if( $is_num1 xor $is_num2 ) {
191 # One's looks like a number, the other doesn't.
192 # Can't be equal.
193 return 0;
194 }
195 elsif( $is_num1 ) {
196 # They're both numbers
197 return $r1 == $r2;
198 }
199 else {
200 # They're both strings
201 return $r1 eq $r2;
202 }
203 }
204 elsif( $ref1 eq $ref2 ) {
205 if ( $ref1 ~~ [qw(Regexp GLOB CODE)] ) {
206 return $r1 eq $r2;
207 }
208 elsif ( $ref1 eq 'ARRAY' ) {
209 return _equal_arrays( $r1, $r2 );
210 }
211 elsif ( $ref1 eq 'HASH' ) {
212 return _equal_hashes( $r1, $r2 );
213 }
214 elsif ( $ref1 ~~ [qw(SCALAR REF)] ) {
215 return _are_equal($$r1, $$r2);
216 }
217 else {
218 # Must be an object
219 return _equal_objects( $r1, $r2 );
220 }
221 }
222 elsif( $ref1 and $ref2 ) {
223 # They're both refs, but not of the same type
224 my $is_overloaded1 = overload::Overloaded($r1);
225 my $is_overloaded2 = overload::Overloaded($r2);
226  
227 if( $is_overloaded1 and $is_overloaded2 ) {
228 # Two overloaded objects
229 return _equal_overload( $r1, $r2 );
230 }
231 else {
232 # One's an overloaded object, the other is not or
233 # Two plain refs different type or
234 # non-overloaded objects of different type.
235 return 0;
236 }
237 }
238 else {
239 # One is a ref, one is not
240 my $is_overloaded = $ref1 ? overload::Overloaded($r1)
241 : overload::Overloaded($r2);
242  
243 if( $is_overloaded ) {
244 # One's an overloaded object, one's a plain scalar
245 return $ref1 ? _equal_overload_vs_scalar($r1, $r2)
246 : _equal_overload_vs_scalar($r2, $r1);
247 }
248 else {
249 # One's a plain ref or object, one's a plain scalar
250 return 0;
251 }
252 }
253 }
254  
255  
256 # Two objects, same class
257 sub _equal_objects {
258 my($r1, $r2) = @_;
259  
260 # No need to check both, they're the same class
261 my $is_overloaded = overload::Overloaded($r1);
262  
263 if( !$is_overloaded ) {
264 # Neither are overloaded, they're the same class, are they the same object?
265 return $r1 eq $r2;
266 }
267 else {
268 return _equal_overload( $r1, $r2 );
269 }
270 }
271  
272  
273 # Two objects, possibly different classes, both overloaded.
274 sub _equal_overload {
275 my($obj1, $obj2) = @_;
276  
277 my $type1 = $overload_type->($obj1);
278 my $type2 = $overload_type->($obj2);
279  
280 # One of them is not overloaded
281 return if !$type1 or !$type2;
282  
283 if( $type1 eq 'both' and $type2 eq 'both' ) {
284 return $obj1 == $obj2 || $obj1 eq $obj2;
285 }
286 elsif(
287 ($type1 eq 'num' and $type2 eq 'str') or
288 ($type1 eq 'str' and $type2 eq 'num')
289 )
290 {
291 # They're not both numbers, not both strings, and not both both
292 # Must be str vs num.
293 return $type1 eq 'num' ? $obj1+0 eq "$obj2"
294 : $obj2+0 eq "$obj1";
295 }
296 elsif( 'num' ~~ [$type1, $type2] ) {
297 return $obj1 == $obj2;
298 }
299 elsif( 'str' ~~ [$type1, $type2] ) {
300 return $obj1 eq $obj2;
301 }
302 else {
303 die "Should never be reached";
304 }
305 }
306  
307  
308 # One overloaded object, one plain scalar
309 # STRING != OBJ
310 # NUMBER != OBJ
311 # STRING eq OBJeq
312 # STRING eq OBJboth
313 # STRING != OBJ== (using == will throw a warning)
314 # NUMBER == OBJ==
315 # NUMBER eq OBJeq
316 # NUMBER == OBJboth
317 sub _equal_overload_vs_scalar {
318 my($obj, $scalar) = @_;
319  
320 my $type = $overload_type->($obj);
321 return unless $type;
322  
323 if( SCALAR::is_number($scalar) ) {
324 if( $type eq 'str' ) {
325 $obj eq $scalar;
326 }
327 else {
328 $obj == $scalar;
329 }
330 }
331 else {
332 if( $type eq 'num' ) {
333 # Can't reliably compare
334 return;
335 }
336 else {
337 $obj eq $scalar;
338 }
339 }
340 }
341  
342 sub _equal_arrays {
343 my ($r1, $r2) = @_;
344 # They can only be equal if they have the same nÂș of elements.
345 return if @$r1 != @$r2;
346  
347 foreach my $i (0 .. @$r1 - 1) {
348 return unless _are_equal($r1->[$i], $r2->[$i]);
349 }
350  
351 return 1;
352 }
353  
354 sub _equal_hashes {
355 my ($r1, $r2) = @_;
356 # Hashes can't be equal unless their keys are equal.
357 return unless ( %$r1 ~~ %$r2 );
358  
359 # Compare the equality of the values for each key.
360 foreach my $key (keys %$r1) {
361 return unless _are_equal( $r1->{$key}, $r2->{$key} );
362 }
363  
364 return 1;
365 }
366  
367 1;