blib/lib/Array/DeepUtils.pm

filter: Statementstmt 98.5% 265 / 269 Branchbran 74.1% 129 / 174 Conditioncond 43.9% 29 / 66 MC/DCmcdc 12.2% 5 / 41 Subroutinesub 100% 20 / 20 Podpod 100% 15 / 15 totaltotal 79.1% 463 / 585 CC 120 SCAR 25.0
CC
120
Cov
83%
binary
29.9
reshape
16.4
unary
13.2
CRAP 12.2
↑ Summary
1 package Array::DeepUtils;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Storable qw/dclone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 1 require Exporter;
10 1 our @ISA = qw(Exporter);
11 1 our @EXPORT_OK = qw/
12 binary collapse dcopy idx
13 purge remove reshape rotate scatter shape subscript
14 transpose unary value_by_path vector_iterator
15 /;
16 1 our %EXPORT_TAGS = (
17 'all' => [ @EXPORT_OK ],
18 );
19
20 1 our $VERSION = 0.2;
21 1 our $DEBUG = 0;
22 1 our $LastError = '';
23
24 1 my $NaV = bless(\my $dummy, 'NaV');
25
26
27 =pod
28
29 =head1 NAME
30
31 Array::DeepUtils - utilities for the manipulation of nested arrays
32
33 =head1 VERSION
34
35 This document refers to version 0.1 of Array::DeepUtils
36
37 =head1 SYNOPSIS
38
39
40 use Array::DeepUtils qw/:all/;
41
42 binary(
43 [1,2,3,4,5,6,7,8],
44 [[1,1][2,2][3,3][4,4]],
45 sub { $_[0] + $_[1] }
46 );
47
48 yields:
49
50 [
51 [ 2, 3 ],
52 [ 5, 6 ],
53 [ 8, 9 ],
54 [ 11, 12 ],
55 ]
56
57 A more complex example:
58
59 my $x = [1..9];
60
61 my $y = reshape($x, [3,3,3,3], $x);
62
63 $y is now:
64
65 [
66 [
67 [[1,2,3],[4,5,6],[7,8,9]],
68 [[1,2,3],[4,5,6],[7,8,9]],
69 [[1,2,3],[4,5,6],[7,8,9]],
70 ],
71 [
72 [[1,2,3],[4,5,6],[7,8,9]],
73 [[1,2,3],[4,5,6],[7,8,9]],
74 [[1,2,3],[4,5,6],[7,8,9]],
75 ],
76 [
77 [[1,2,3],[4,5,6],[7,8,9]],
78 [[1,2,3],[4,5,6],[7,8,9]],
79 [[1,2,3],[4,5,6],[7,8,9]],
80 ]
81 ];
82
83
84 my $z = dcopy($y, [[1,1,1,1],[2,2,2,2]]);
85
86 $z is now:
87
88 [
89 [
90 [[5,6],[8,9]],
91 [[5,6],[8,9]],
92 ],
93 [
94 [[5,6],[8,9]],
95 [[5,6],[8,9]],
96 ]
97 ];
98
99 my $c = reshape([], [2,2], collapse($z));
100
101 resulting in $c being:
102
103 [[5,6],[8,9]]
104
105
106 =head1 DESCRIPTION
107
108 This module is a collection of subroutines for the manipulation of
109 deeply nested arrays. It provides routines for iterating along
110 coordinates and for setting, retrieving and deleting values.
111 The functions binary and unary are provided for applying arbitrary
112 operators as code references to deeply nested arrays. With shape() and
113 reshape() there are methods to determine and change the dimensions.
114
115 By default nothing is exported. The subroutines can be imported all at
116 once via the ':all' tag.
117
118 =head2 Subroutine short description
119
120 L</"binary()"> - appply a binary operator between two nested arrays
121
122 L</"collapse()"> - flatten a nested array to a one dimensional vector
123
124 L</"dcopy()"> - extract part of a nested array between two vectors
125
126 L</"idx()"> - build an index vector for values of another vector
127
128 L</"purge()"> - remove elements by value from a nested array
129
130 L</"remove()"> - remove elements by index
131
132 L</"reshape()"> - transform nested array by dimension vector
133
134 L</"rotate()"> - rotate a data structure along its axes
135
136 L</"scatter()"> - build a new data structure with data and index vector.
137
138 L</"shape()"> - get nested array dimension vector
139
140 L</"subscript()"> - extract nested array values by index vector
141
142 L</"transpose()"> - transpose a nested array
143
144 L</"unary()"> - appply a unary operator to all values of a nested array
145
146 L</"value_by_path()"> - extract nested array values by coordinate vector
147
148 L</"vector_iterator()"> - creates a subroutine for iterating between two coordinates
149
150 =cut
151
152
153 =pod
154
155 =head1 SUBROUTINES
156
157 =head2 binary()
158
159 B<binary($aref1, $aref2, $subref, $neutral_element [, $object, $fill_aref])>
160
161 Recursively apply a binary operator represented by a subroutine
162 reference to all elements of two nested data structures given in $aref1
163 and $aref2 and set the resulting values in $aref2. $aref2 will also be
164 returned.
165
166 If these structures differ in shape they will be reshaped according to
167 the larger structure. The value of $neutral_element will be used if one
168 of the operands is undefined or does not exist ($neutral_element can
169 also be a subroutine reference; it will be called on value retrieval and
170 given $aref1 respectively $aref2 as only parameter). To be able to use
171 methods as subroutines $object will be passed to the subroutine as first
172 parameter when specified. Since binary() calls reshape() a given
173 $fill_aref will be passed as the third parameter to reshape().
174
175 A simple example, after:
176
177 my $v1 = [1,2,3];
178 my $v2 = [9,8,7];
179 my $func = sub { $_[0] * $_[1] }
180 binary($v1, $v2, $func);
181
182 $v2 will have a value of
183
184 [9, 16, 21]
185
186 Making it a bit more complicated:
187
188 my $v1 = [1,2,3,4,5,6];
189 my $v2 = [9,8,7];
190 my $func = sub { $_[0] * $_[1] }
191 binary($v1, $v2, $func);
192
193 results in:
194
195 [9,16,21,36,40,42]
196
197 because missing values will be filled with the flattened structure
198 repeated as often as it is needed, so the above is exactly the same as:
199
200 my $v1 = [1,2,3,4,5,6];
201 my $v2 = [9,8,7,9,8,7];
202 my $func = sub { $_[0] * $_[1] }
203 binary($v1, $v2, $func);
204
205 Using the fill parameter gives the opportunity to assign the values
206 used for filling. It will also be repeated when necessary.
207
208 my $v1 = [1,2,3,4,5,6];
209 my $v2 = [9,8,7];
210 my $fill = [1,2];
211 my $func = sub { $_[0] * $_[1] };
212 binary($v1, $v2, $func, 1, undef, $fill);
213
214 results in:
215
216 [9,16,21,4,10,6];
217
218 because $v2 will have been reshaped to [9,8,7,1,2,1] before the
219 multiplication.
220
221 This works for vectors of arbitrary depth, so that:
222
223 my $v1 = [[1,2,3], [4,5,6], [7,8,9]];
224 my $v2 = [[11,12], [13,14]];
225 my $fill = [1, -1];
226 my $func = sub { $_[0] * $_[1] };
227 binary($v1, $v2, $func, 1, undef, $fill);
228
229 yields:
230
231 [[11,24,3], [52,70,-6], [7,-8,9]]
232
233 =cut
234
235 1 ▶sub binary {
Subroutine binary: called CC=19 SCAR=34.0
Pod documented
236 1 my($func, $neutral, $obj, $fill) = @_[2..5];
237
238 # param checks
239 1 ▶ croak $LastError = 'binary: not a code ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $func eq "CODE" 0 1 1
240 unless ref($func) eq 'CODE';
241 1 ▶ croak $LastError = 'binary: not an object'
Branch: 1 branch
BranchTrueFalseTotal
if $obj and not ref $obj 0 1 1
Condition33%1/3
$obj and not ref $obj
!ll&&!rl&&r
100
MC/DC0%0/2
$obj and not ref $obj
$obj not ref $obj
Truth table33%1/3
A and not(B)
A= $objB= not ref $obj
ABresult
0X0
100
111
242 if $obj and !ref($obj);
243
244 # determine the "bigger" vector
245 # (run 'shape '* reduce' and compare)
246 1 my @dims;
247 my @inner;
248 1 for my $i ( 0 .. 1 ) {
249 2 $dims[$i] = shape($_[$i]);
250 2 ▶ $dims[$i] = [1] unless @{ $dims[$i] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$dims[$i];} 0 2 2
251 2 $inner[$i] = 1;
252 2 $inner[$i] *= $_ for @{ $dims[$i] };
253 }
254
255 1 ▶ my $reshape_dim = $inner[0] >= $inner[1] ? $dims[0] : $dims[1];
Branch: 1 branch
BranchTrueFalseTotal
$inner[0] >= $inner[1] ? : 0 1 1
256
257 # reshape both with reshape_dim vector
258 1 for my $i ( 0 .. 1 ) {
259 2 ▶ $_[$i] = [$_[$i]] unless ref($_[$i]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[$i] eq "ARRAY" 0 2 2
260 2 ▶ $_[$i] = reshape($_[$i], $reshape_dim, $fill ? $fill : ());
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 2 0 2
261 }
262
263 # create start and end vector
264 1 my $start = [ map { 0 } @$reshape_dim ];
265 1 my $end = [ map { $_ - 1 } @$reshape_dim ];
266
267 # shortcut for empty arrays
268 1 ▶ if ( !@$start or !@$end ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$start and @$end) 0 1 1
Condition33%1/3
@$start and @$end
!ll&&!rl&&r
001
MC/DC0%0/2
@$start and @$end
@$start @$end
Truth table33%1/3
A and B
A= @$startB= @$end
ABresult
0X0
100
111
269 0 $_[1] = [];
270 0 return $_[1];
271 }
272
273 # iterate over the arrays, call function and store
274 # the value in second array
275 1 my $iterator = vector_iterator($start, $end);
276
277 1 ▶ while ( my ($vec) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($vec) = &$iterator()) 9 1 10
278
279 # get values with value_by_path()
280 9 my @vals;
281 9 for my $i ( 0 .. 1 ) {
282 18 $vals[$i] = value_by_path($_[$i], $vec);
283 18 ▶ $vals[$i] = (ref($neutral) eq 'CODE' ? $neutral->($_[$i]) : $neutral)
Branch: 2 branches
BranchTrueFalseTotal
if not defined $vals[$i] or ref $vals[$i] eq "NaV" 0 18 18
ref $neutral eq 'CODE' ? : 0 0 0
Condition33%1/3
not defined $vals[$i] or ref $vals[$i] eq "NaV"
l!l&&r!l&&!r
0018
MC/DC0%0/2
not defined $vals[$i] or ref $vals[$i] eq "NaV"
not defined $vals[$i] ref $vals[$i] eq "NaV"
Truth table33%1/3
not(A) or B
A= not defined $vals[$i]B= ref $vals[$i] eq "NaV"
ABresult
1X1
011
000
284 if !defined($vals[$i]) or ref($vals[$i]) eq 'NaV';
285 }
286
287 # call fuction and set value
288 value_by_path(
289 9 ▶ $_[1],
Branch: 1 branch
BranchTrueFalseTotal
$obj ? : 0 9 9
290 $vec,
291 $func->($obj ? ($obj, @vals) : @vals),
292 );
293 }
294
295 1 return $_[1];
296 }
297
298
299 =pod
300
301 =head2 collapse()
302
303 B<collapse($aref1)>
304
305 Collapse the referenced array of arrays of arbitrary depth, i.e
306 flatten it to a simple array and return a reference to it.
307
308 Example:
309
310 collapse([[1,2,3],4,[5,[6,7,8,[9,0]]]]);
311
312 will return:
313
314 [1,2,3,4,5,6,7,8,9,0]
315
316 =cut
317
318 5 ▶sub collapse {
Subroutine collapse: called CC=7 SCAR=19.5
Pod documented
319 5 my($struct) = @_;
320
321 5 ▶ croak $LastError = 'collapse: not an array reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct eq "ARRAY" 0 5 5
322 unless ref($struct) eq 'ARRAY';
323
324 5 my @result;
325
326 # simply travel the array iteratively and store
327 # every value in @result
328
329 # element and index stack
330 5 my @estack = ( $struct );
331 5 my @istack = ( 0 );
332
333 5 ▶ while ( @estack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@estack) 89 5 94
334
335 # always opereate on the top of the stacks
336 89 my $e = $estack[-1];
337 89 my $i = $istack[-1];
338
339 89 ▶ if ( $i <= $#$e ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $e->$#*) 78 11 89
340
341 # in currrent array, if value is array ref
342 # push next reference and a new index onto stacks
343 78 ▶ if ( ref($e->[$i]) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e->[$i] eq "ARRAY") 26 52 78
344 26 push @estack, $e->[$i];
345 26 push @istack, 0;
346 26 next;
347 }
348
349 # push value into result array
350 52 push @result, $e->[$i];
351 }
352
353 # after last item, pop last item and last index from stacks
354 63 ▶ if ( $i >= $#$e ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $e->$#*) 31 32 63
355 31 pop @estack;
356 31 pop @istack;
357 }
358
359 # increment index for next fetch
360 63 ▶ $istack[-1]++ if @istack;
Branch: 1 branch
BranchTrueFalseTotal
if @istack 58 5 63
361 }
362
363 5 return \@result;
364 }
365
366
367 =pod
368
369 =head2 dcopy()
370
371 B<dcopy($aref, $coord_aref)>
372
373 Extract a part of an deeply nested array between two vectors given in
374 the array referenced by $coord_ref. This is done via an iterator
375 generated with vector_iterator() running from the first to the second
376 coordinate given.
377
378 Example:
379
380 dcopy([[1,2,3], [4,5,6], [7,8,9]], [[1,0], [2,1]]);
381
382 will return
383
384 [ [4,5], [7,8] ]
385
386 This will work in either direction, so:
387
388 dcopy([[1,2,3], [4,5,6], [7,8,9]], [[2,1], [1,0]]);
389
390 will give:
391
392 [ [8,7], [5,4] ]
393
394 as expected.
395
396 =cut
397
398 2 ▶sub dcopy {
Subroutine dcopy: called CC=8 SCAR=22.2
Pod documented
399 2 my($struct, $coord) = @_;
400
401 # param checks
402 2 ▶ croak $LastError = 'dcopy: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct eq "ARRAY" and ref $coord eq "ARRAY" 0 2 2
Condition33%1/3
ref $struct eq "ARRAY" and ref $coord eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $struct eq "ARRAY" and ref $coord eq "ARRAY"
ref $struct eq "ARRAY" ref $coord eq "ARRAY"
Truth table33%1/3
A and B
A= ref $struct eq "ARRAY"B= ref $coord eq "ARRAY"
ABresult
0X0
100
111
403 unless ref($struct) eq 'ARRAY' and ref($coord) eq 'ARRAY';
404
405 2 ▶ croak $LastError = 'dcopy: coordinate vector with element count != 2!'
Branch: 1 branch
BranchTrueFalseTotal
unless @$coord == 2 0 2 2
406 unless @$coord == 2;
407
408 croak $LastError = 'dcopy: coordinate vector elements have different length!'
409 2 ▶ unless @{$coord->[0]} == @{$coord->[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$coord->[0];} == @{$coord->[1];} 0 2 2
410
411 # simply iterate and set values in $dest
412 2 ▶ my $iterator = vector_iterator(
Branch: 2 branches
BranchTrueFalseTotal
ref $coord->[0] eq 'ARRAY' ? : 2 0 2
ref $coord->[1] eq 'ARRAY' ? : 2 0 2
413 ref($coord->[0]) eq 'ARRAY' ? $coord->[0] : [$coord->[0]],
414 ref($coord->[1]) eq 'ARRAY' ? $coord->[1] : [$coord->[1]]
415 );
416 2 my $dest = [];
417 2 ▶ while ( my ($svec, $dvec) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 22 2 24
418 22 value_by_path(
419 $dest,
420 $dvec,
421 value_by_path($struct, $svec)
422 );
423 }
424
425 2 return $dest;
426 }
427
428
429 =pod
430
431 =head2 idx()
432
433 B<idx($aref1, $aref2)>
434
435 Return an index vector that contains the indices of the elements of the
436 first argument vector with respect to the second index vector.
437
438 Example:
439
440 idx([[1,3],[4,5]], [[1,2,3], [4,5,6], [7,8,9]]);
441
442 will return:
443
444 [[[0,0],[0,2]],[[1,0],[1,1]]]
445
446 =cut
447
448 1 ▶sub idx {
Subroutine idx: called CC=9 SCAR=22.8
Pod documented
449 1 my ($aref1, $aref2) = @_;
450
451 # param checks
452 1 ▶ croak $LastError = 'idx: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aref1 eq "ARRAY" and ref $aref2 eq "ARRAY" 0 1 1
Condition33%1/3
ref $aref1 eq "ARRAY" and ref $aref2 eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $aref1 eq "ARRAY" and ref $aref2 eq "ARRAY"
ref $aref1 eq "ARRAY" ref $aref2 eq "ARRAY"
Truth table33%1/3
A and B
A= ref $aref1 eq "ARRAY"B= ref $aref2 eq "ARRAY"
ABresult
0X0
100
111
453 unless ref($aref1) eq 'ARRAY' and ref($aref2) eq 'ARRAY';
454
455 1 my ($dim1, $dim2) = (shape($aref1), shape($aref2));
456 1 my ($start1, $end1) = ([ map { 0 } @$dim1 ], [ map { $_ - 1 } @$dim1 ]);
457 1 my ($start2, $end2) = ([ map { 0 } @$dim2 ], [ map { $_ - 1 } @$dim2 ]);
458 1 my ($iterator1, $iterator2) = (vector_iterator($start1, $end1),
459 vector_iterator($start2, $end2));
460
461 1 ▶ return [] unless scalar @$aref1;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$aref1 0 1 1
462
463 # Create a hash with indices of the elements of $aref2, making sure
464 # that multiple occurrences of an element don't destroy the first
465 # index of this element:
466 1 my %lookup;
467 1 ▶ while ( my($index) = $iterator2->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($index) = &$iterator2()) 9 1 10
468 9 my $value = value_by_path($aref2, $index);
469 9 ▶ $lookup{$value} = $index if $value and !$lookup{$value};
Branch: 1 branch
BranchTrueFalseTotal
if $value and not $lookup{$value} 9 0 9
Condition33%1/3
$value and not $lookup{$value}
!ll&&!rl&&r
009
MC/DC0%0/2
$value and not $lookup{$value}
$value not $lookup{$value}
Truth table33%1/3
A and not(B)
A= $valueB= not $lookup{$value}
ABresult
0X0
100
111
470 }
471
472 # Now lookup every single element from $aref1 in the lookup hash:
473 1 ▶ while ( my($index) = $iterator1->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($index) = &$iterator1()) 4 1 5
474 4 my $position = $lookup{value_by_path($aref1, $index)};
475 4 ▶ value_by_path($aref1, $index, $position ? $position : []);
Branch: 1 branch
BranchTrueFalseTotal
$position ? : 4 0 4
476 }
477
478 1 return $aref1;
479 }
480
481
482 =pod
483
484 =head2 purge()
485
486 B<purge($aref, $what)>
487
488 Remove all values from the array referenced by $aref that equal $what in
489 a string comparison.
490
491 Example:
492
493 $v = [1,0,1,0,1,0,1,0];
494 purge($v, '0');
495
496 will have $v reduced to:
497
498 [1,1,1,1]
499
500 =cut
501
502 4 ▶sub purge {
Subroutine purge: called CC=7 SCAR=19.5
Pod documented
503 4 my $what = pop;
504
505 4 ▶ croak $LastError = 'purge: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 4 4
506 unless ref($_[0]) eq 'ARRAY';
507
508 4 my @estack = ($_[0]);
509 4 my @istack = ( $#{ $estack[-1] } );
510
511 4 ▶ while ( @estack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@estack) 51 4 55
512
513 51 my $e = $estack[-1];
514 51 my $i = $istack[-1];
515
516 51 ▶ if ( $i >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) { } 38 13 51
517
518 # push next reference and a new index onto stacks
519 38 ▶ if ( ref($e->[$i]) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e->[$i] eq "ARRAY") 9 29 38
520 9 push @estack, $e->[$i];
521 9 push @istack, $#{ $e->[$i] };
522 9 next;
523 }
524
525 29 ▶ splice(@$e, $i, 1) if $e->[$i] eq $what;
Branch: 1 branch
BranchTrueFalseTotal
if $e->[$i] eq $what 7 22 29
526
527 } else {
528
529 13 pop @estack;
530 13 pop @istack;
531
532 }
533
534 42 ▶ $istack[-1]-- if @istack;
Branch: 1 branch
BranchTrueFalseTotal
if @istack 38 4 42
535
536 }
537 }
538
539
540 =pod
541
542 =head2 remove()
543
544 B<remove($aref, $index|$coordinate_aref)>
545
546 Remove all values with indices or coordinates given by $index or by the
547 array referenced by $coordinate_aref from an array referenced by $aref.
548
549 Example:
550
551 my $v = [1,2,3,4,5,6,7,8,9,0];
552 remove($v, [1,2,3]);
553
554 will have $v reduced to:
555
556 [1,5,6,7,8,9,0]
557
558 and:
559
560 my $aref = [[1,2,3],[4,5,6],[7,8,9]];
561
562 remove($aref, [[0,1], [1,2], 2]);
563
564 will leave:
565
566 [[1,3],[4,5]]
567
568 in $aref.
569
570 =cut
571
572 3 ▶sub remove {
Subroutine remove: called CC=6 SCAR=18.1
Pod documented
573 3 my $coords = pop;
574
575 3 ▶ croak $LastError = 'remove: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 3 3
576 unless ref($_[0]) eq 'ARRAY';
577
578 3 ▶ $coords = [$coords]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $coords eq "ARRAY" 1 2 3
579 unless ref($coords) eq 'ARRAY';
580
581 3 for ( @$coords ) {
582 6 ▶ $_ = [$_] unless ref($_) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 4 2 6
583 6 ▶ value_by_path($_[0], $_, $NaV)
Branch: 1 branch
BranchTrueFalseTotal
unless ref value_by_path($_[0], $_) eq "NaV" 6 0 6
584 unless ref(value_by_path($_[0], $_)) eq 'NaV';
585 }
586
587 3 purge($_[0], $NaV);
588 }
589
590
591 =pod
592
593 =head2 reshape()
594
595 B<reshape($aref, $dims_aref [, $fill_aref])>
596
597 Create an array with the dimension vector given in $dims_aref and take
598 the values from $aref provided there is a value at the given position.
599 Additional values will be taken from the array referenced by $fill_aref
600 or - if it is not provided - from a flattened (call to collapse())
601 version of the original array referenced by $aref. If the fill source is
602 exhausted, reshape will start from index 0 again. This will be repeated
603 until the destination array is filled.
604
605 Example:
606
607 reshape([[1,2,3]], [3, 3]);
608
609 will return:
610
611 [ [1,2,3], [1,2,3], [1,2,3] ]
612
613 and:
614
615 reshape([[1,2,3]], [3, 3], ['x']);
616
617 will return:
618
619 [ [1,2,3], ['x','x','x'], ['x','x','x'] ]
620
621 =cut
622
623 9 ▶sub reshape {
Subroutine reshape: called CC=13 SCAR=27.9
Pod documented
624 9 my($struct, $dims, $fill) = @_;
625
626 9 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $struct ne "ARRAY" or ref $dims ne "ARRAY" or $fill and ref $fill ne "ARRAY") 0 9 9
Condition44%4/9
ref $struct ne "ARRAY" or ref $dims ne "ARRAY" or $fill and ref $fill ne "ARRAY"
l!l&&r!l&&!r
009
ref $struct ne "ARRAY" or ref $dims ne "ARRAY"
l!l&&r!l&&!r
009
$fill and ref $fill ne "ARRAY"
!ll&&!rl&&r
360
MC/DC0%0/4
ref $struct ne "ARRAY" or ref $dims ne "ARRAY" or $fill and ref $fill ne "ARRAY"
ref $struct ne "ARRAY" ref $dims ne "ARRAY" $fill ref $fill ne "ARRAY"
Truth table40%2/5
A or B or C and D
A= ref $struct ne "ARRAY"B= ref $dims ne "ARRAY"C= $fillD= ref $fill ne "ARRAY"
ABCDresult
1XXX1
01XX1
00111
000X0
00100
627 ref($struct) ne 'ARRAY' or
628 ref($dims) ne 'ARRAY' or
629 ( $fill and ref($fill) ne 'ARRAY' )
630 ) {
631 0 $LastError = "usage: reshape(AREF, AREF[, AREF])";
632 0 croak $LastError;
633 }
634
635 9 ▶ return undef unless @$dims;
Branch: 1 branch
BranchTrueFalseTotal
unless @$dims 0 9 9
636 9 ▶ return [] if $dims->[0] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dims->[0] == 0 0 9 9
637
638 # get a flattened copy of the source
639 9 ▶ $fill = collapse(dclone($struct))
Branch: 1 branch
BranchTrueFalseTotal
unless $fill 3 6 9
640 unless $fill;
641 9 ▶ @$fill = ( undef ) unless @$fill;
Branch: 1 branch
BranchTrueFalseTotal
unless @$fill 0 9 9
642
643 9 my $start = [ map { 0 } @$dims ];
644 9 my $end = [ map { $_ - 1 } @$dims ];
645
646 9 my $iterator = vector_iterator($start, $end);
647
648 9 my $i = 0;
649 9 my $dest = [];
650 9 ▶ while ( my ($vec) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($vec) = &$iterator()) 146 9 155
651 146 my $val = value_by_path($struct, $vec);
652 146 ▶ value_by_path(
Branch: 1 branch
BranchTrueFalseTotal
$val && ref $val eq 'NaV' || ref $val eq 'ARRAY' ? : 94 52 146
Condition66%4/6
$val and ref $val eq "NaV" or ref $val eq "ARRAY"
l!l&&r!l&&!r
94052
$val and ref $val eq "NaV"
!ll&&!rl&&r
05294
MC/DC33%1/3
$val and ref $val eq "NaV" or ref $val eq "ARRAY"
$val ref $val eq "NaV" ref $val eq "ARRAY"
Truth table40%2/5
A and B or C
A= $valB= ref $val eq "NaV"C= ref $val eq "ARRAY"
ABCresult
11X1
0X11
1011
0X00
1000
653 $dest,
654 $vec,
655 ( ($val and ref($val) eq 'NaV') or ref($val) eq 'ARRAY' )
656 ? $fill->[$i++ % @$fill]
657 : $val,
658 1,
659 );
660 }
661
662 9 return $dest;
663 }
664
665
666 =pod
667
668 =head2 rotate()
669
670 B<rotate($aref1, $aref2 [, $fill_aref])>
671
672 Rotate a data structure along its axes. It is possible to perform more
673 than one rotation at once, so rotating a two dimensional matrix along
674 its x- and y-axes by +1 and -1 positions is no problem.
675
676 Example:
677
678 rotate([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [1, -1]);
679
680 will return:
681
682 [[8,9,7],[2,3,1],[5,6,4]]
683
684 Using the optional third parameter it is possible to fill previously
685 empty array elements with a given value via L</"reshape()">.
686
687 =cut
688
689 2 ▶sub rotate {
Subroutine rotate: called CC=5 SCAR=16.3
Pod documented
690 2 my($struct, $rotvec, $fill) = @_;
691
692 # param checks
693 2 ▶ croak $LastError = 'rotate: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct eq "ARRAY" and ref $rotvec eq "ARRAY" 0 2 2
Condition33%1/3
ref $struct eq "ARRAY" and ref $rotvec eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $struct eq "ARRAY" and ref $rotvec eq "ARRAY"
ref $struct eq "ARRAY" ref $rotvec eq "ARRAY"
Truth table33%1/3
A and B
A= ref $struct eq "ARRAY"B= ref $rotvec eq "ARRAY"
ABresult
0X0
100
111
694 unless ref($struct) eq 'ARRAY' and ref($rotvec) eq 'ARRAY';
695
696 2 my $dim = shape($struct);
697
698 2 ▶ croak "rotate: rotation vector does not fit array dimensions"
Branch: 1 branch
BranchTrueFalseTotal
unless @$rotvec == @$dim 0 2 2
699 unless @$rotvec == @$dim;
700
701 2 $struct = reshape($struct, $dim, $fill);
702
703 2 my $start = [ map { 0 } @$dim ];
704 2 my $end = [ map { $_ - 1 } @$dim ];
705
706 2 my $iterator = vector_iterator($start, $end);
707
708 2 my $dest = [];
709 2 ▶ while ( my($svec) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($svec) = &$iterator()) 18 2 20
710 my $dvec = [ map {
711 18 ( $svec->[$_] + $rotvec->[$_] ) % $dim->[$_]
712 } 0 .. $#$svec ];
713 18 value_by_path($dest, $dvec, value_by_path($struct, $svec));
714 }
715
716 2 return $dest;
717 }
718
719
720 =pod
721
722 =head2 scatter()
723
724 B<scatter($aref, $struct)>
725
726 This function behaves inverse to subscript. While subscript selects
727 values from a nested data structure, controlled by an index vector,
728 scatter will distribute elements into a new data structure, controlled
729 by an index vector.
730
731 Example:
732
733 scatter([1, 2, 3, 4, 5, 6, 7], [[0,0], [0,1], [1,0], [1,1]]);
734
735 will return:
736
737 [[1, 2], [3, 4]]
738
739 =cut
740
741 1 ▶sub scatter {
Subroutine scatter: called CC=7 SCAR=22.1
Pod documented
742 1 my ($aref, $struct) = @_;
743
744 # param checks
745 1 ▶ croak $LastError = 'scatter: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aref eq "ARRAY" and ref $struct eq "ARRAY" 0 1 1
Condition33%1/3
ref $aref eq "ARRAY" and ref $struct eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $aref eq "ARRAY" and ref $struct eq "ARRAY"
ref $aref eq "ARRAY" ref $struct eq "ARRAY"
Truth table33%1/3
A and B
A= ref $aref eq "ARRAY"B= ref $struct eq "ARRAY"
ABresult
0X0
100
111
746 unless ref($aref) eq 'ARRAY' and ref($struct) eq 'ARRAY';
747
748 # Make sure that the raw data to be scattered will not be exhausted
749 # by the indices contained in $struct:
750 1 my $source = reshape($aref, [scalar @$struct], $aref);
751
752 # Built new data structure (possibly containing empty elements):
753 1 my $result = [];
754 1 for my $position (@$struct) {
755 4 ▶ $position = [$position] unless ref($position) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $position eq "ARRAY" 0 4 4
756 4 ▶ value_by_path($result, $position, shift(@$source))
Branch: 1 branch
BranchTrueFalseTotal
if ref $position eq "ARRAY" and ref $position->[0] ne "NaV" 4 0 4
Condition33%1/3
ref $position eq "ARRAY" and ref $position->[0] ne "NaV"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $position eq "ARRAY" and ref $position->[0] ne "NaV"
ref $position eq "ARRAY" ref $position->[0] ne "NaV"
Truth table33%1/3
A and B
A= ref $position eq "ARRAY"B= ref $position->[0] ne "NaV"
ABresult
0X0
100
111
757 if ref($position) eq 'ARRAY' and ref($position->[0]) ne 'NaV';
758 }
759
760 1 return $result;
761 }
762
763
764 =pod
765
766 =head2 shape()
767
768 B<shape($aref)>
769
770 Determine the dimensions of an array and return it as
771 a vector (an array reference)
772
773 Example:
774
775 shape([[1,2,3], [4,5,6], [7,8,9]]);
776
777 will return:
778
779 [3,3]
780
781 and:
782
783 shape([[1,2,3],4,[5,[6,7,8,[9,0]]]]);
784
785 will return:
786
787 [3,3,4,2]
788
789 A combination of shape() and reshape() will effectively turn an
790 "irregular" array into a regular one.
791
792 For example:
793
794 $aref = [[1,2,3],4,[5,6],[7,8,9]];
795
796 reshape($aref, shape($aref), [0]);
797
798 will return:
799
800 [[1,2,3],[0,0,0],[5,6,0],[7,8,9]]
801
802 =cut
803
804 10 ▶sub shape {
Subroutine shape: called CC=8 SCAR=20.8
Pod documented
805 10 my($struct) = @_;
806
807 10 ▶ return [] unless ref($struct) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct eq "ARRAY" 0 10 10
808
809 10 my @out = ( 0 );
810 10 my @idx = ( 0 );
811 10 my @vstack = ( $struct );
812
813 10 my $depth = 0;
814 10 ▶ while ( $depth >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth >= 0) 144 10 154
815
816 # get the top reference from the stack
817 144 my $aref = $vstack[-1];
818
819 144 ▶ if ( ref($aref->[$idx[$depth]]) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $$aref[$idx[$depth]] eq 'ARRAY') { } 27 117 144
elsif ($idx[$depth] <= $aref->$#*) { } 80 37 117
820
821 # found a reference push it on the stack and increase depth
822 27 push @vstack, $aref->[$idx[$depth++]];
823 # push a new index on the index stack
824 27 push @idx, 0;
825 # initialize the counter on the new level on first entry
826 27 ▶ $out[$depth] = 0 unless defined $out[$depth];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out[$depth] 12 15 27
827
828 } elsif ( $idx[$depth] <= $#$aref ) {
829
830 # no reference and we still have elements in the array
831 # --> increase index for the current level
832 80 ++$idx[$depth];
833
834 } else {
835
836 # reached the end of the array
837 # --> remove it from the stack
838 37 pop @vstack;
839
840 # remove last index from the index stack
841 37 pop @idx;
842
843 # save the number of elements of the level
844 # if it is bigger than before
845 37 ▶ $out[$depth] = @$aref if @$aref > $out[$depth];
Branch: 1 branch
BranchTrueFalseTotal
if @$aref > $out[$depth] 22 15 37
846
847 # decrease the current level
848 37 $depth--;
849
850 # increase the index for the current level
851 37 ▶ ++$idx[$depth] if $depth >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth >= 0 27 10 37
852
853 }
854 }
855
856 10 return \@out;
857 }
858
859
860 =pod
861
862 =head2 subscript()
863
864 B<subscript($aref, $index|$coord_aref)>
865
866 Retrieve and return values of a deeply nested array for a single index a
867 list of indices or a list of coordinate vectors.
868
869 Example:
870
871 my $aref = [[1,2,3],[4,5,6],[7,8,9]];
872
873 subscript($aref, 1);
874
875 returns:
876
877 [[4,5,6]]
878
879 whereas:
880
881 subscript($aref, [[0,1], [1,2], 2]);
882
883 returns:
884
885 [2,6,[7,8,9]]
886
887 =cut
888
889 3 ▶sub subscript {
Subroutine subscript: called CC=8 SCAR=21.4
Pod documented
890 3 my($struct, $coords) = @_;
891
892 3 ▶ croak $LastError = 'subscript: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 3 3
893 unless ref($_[0]) eq 'ARRAY';
894
895 3 ▶ $coords = [$coords]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $coords eq "ARRAY" 1 2 3
896 unless ref($coords) eq 'ARRAY';
897
898 3 for ( @$coords ) {
899 6 ▶ $_ = [$_] unless ref($_) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 4 2 6
900 }
901
902 3 my @out;
903 3 for my $position (@$coords) {
904 6 ▶ push @out, value_by_path($struct, $position)
Branch: 1 branch
BranchTrueFalseTotal
if ref $position eq "ARRAY" and ref $position->[0] ne "NaV" 6 0 6
Condition33%1/3
ref $position eq "ARRAY" and ref $position->[0] ne "NaV"
!ll&&!rl&&r
006
MC/DC0%0/2
ref $position eq "ARRAY" and ref $position->[0] ne "NaV"
ref $position eq "ARRAY" ref $position->[0] ne "NaV"
Truth table33%1/3
A and B
A= ref $position eq "ARRAY"B= ref $position->[0] ne "NaV"
ABresult
0X0
100
111
905 if ref($position) eq 'ARRAY' and ref($position->[0]) ne 'NaV';
906 }
907
908 3 return \@out;
909 }
910
911
912 =pod
913
914 =head2 transpose()
915
916 B<transpose($aref1, $control [, $fill_aref])>
917
918 Transpose a nested data structure. In the easiest two-dimensional case
919 this is the traditional transposition operation.
920
921 Example:
922
923 transpose([[1,2,3], [4,5,6], [7,8,9]], 1);
924
925 will return:
926
927 [[1,4,7],[2,5,8],[3,6,9]]
928
929 Using the optional third parameter, it is possible to fill previously
930 empty array elements with a given value via L</"reshape()">.
931
932 =cut
933
934 1 ▶sub transpose {
Subroutine transpose: called CC=3 SCAR=11.0
Pod documented
935 1 my($struct, $control, $fill) = @_;
936
937 1 ▶ croak $LastError = 'transpose: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct eq "ARRAY" 0 1 1
938 unless ref($struct) eq 'ARRAY';
939
940 1 my $dim = shape($struct);
941
942 1 $struct = reshape($struct, $dim, $fill);
943
944 1 my $start = [ map { 0 } @$dim ];
945 1 my $end = [ map { $_ - 1 } @$dim ];
946
947 1 my $iterator = vector_iterator($start, $end);
948
949 1 my $dest = [];
950 1 ▶ while ( my($svec) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($svec) = &$iterator()) 9 1 10
951 my $dvec = [
952 map {
953 9 $svec->[($_ + $control) % scalar(@$svec)]
954 } 0 .. $#$svec
955 ];
956 9 value_by_path($dest, $dvec, value_by_path($struct, $svec));
957 }
958
959 1 return $dest;
960 }
961
962
963 =pod
964
965 =head2 unary()
966
967 B<unary($aref1, $subref, $neutral_element [, $object])>
968
969 Recursively apply a unary operator represented by a subroutine
970 reference to all elements of a nested data structure given in $aref
971 and set the resulting values in the referenced array itself.
972 The reference will also be returned.
973
974 The value of $neutral_element will be used if the original is
975 undefined or does not exist. To be able to use methods as subroutines
976 $object will be passed to the subroutine as first parameter when
977 specified.
978
979 A simple example, after:
980
981 my $v = [1,0,2,0,3,[1,0,3]];
982 my $func = sub { ! $_[0] + 0 };
983
984 unary($v, $func);
985
986 will return:
987
988 [1,0,2,0,3,[0,1,0]]
989
990 =cut
991
992 1 ▶sub unary {
Subroutine unary: called CC=11 SCAR=25.8
Pod documented
993 1 my($func, $neutral, $obj) = @_[1..3];
994
995 # param checks
996 1 ▶ croak $LastError = 'unary: not a code ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $func eq "CODE" 0 1 1
997 unless ref($func) eq 'CODE';
998 1 ▶ croak $LastError = 'unary: not an object'
Branch: 1 branch
BranchTrueFalseTotal
if $obj and not ref $obj 0 1 1
Condition33%1/3
$obj and not ref $obj
!ll&&!rl&&r
100
MC/DC0%0/2
$obj and not ref $obj
$obj not ref $obj
Truth table33%1/3
A and not(B)
A= $objB= not ref $obj
ABresult
0X0
100
111
999 if $obj and !ref($obj);
1000
1001 return $_[0]
1002 1 ▶ if ref($_[0]) eq 'ARRAY' and @{ $_[0] } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "ARRAY" and @{$_[0];} == 0 0 1 1
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];} == 0
!ll&&!rl&&r
010
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];} == 0
ref $_[0] eq "ARRAY" @{$_[0];} == 0
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];} == 0
ABresult
0X0
100
111
1003
1004 1 my $dim = shape($_[0]);
1005
1006 1 my $start = [ map { 0 } @$dim ];
1007 1 my $end = [ map { $_ - 1 } @$dim ];
1008
1009 1 my $iterator = vector_iterator($start, $end);
1010
1011 1 ▶ while ( my ($vec) = $iterator->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($vec) = &$iterator()) 18 1 19
1012 18 my $val = value_by_path($_[0], $vec);
1013 18 ▶ value_by_path(
Branch: 3 branches
BranchTrueFalseTotal
!defined($val) || ref $val eq 'NaV' ? : 15 3 18
ref $neutral eq 'CODE' ? : 0 15 15
$obj ? : 0 3 3
Condition66%2/3
not defined $val or ref $val eq "NaV"
l!l&&r!l&&!r
0153
MC/DC50%1/2
not defined $val or ref $val eq "NaV"
not defined $val ref $val eq "NaV"
Truth table66%2/3
not(A) or B
A= not defined $valB= ref $val eq "NaV"
ABresult
1X1
011
000
1014 $_[0],
1015 $vec,
1016 (!defined($val) or ref($val) eq 'NaV')
1017 ? (ref($neutral) eq 'CODE' ? $neutral->($_[0]) : $neutral)
1018 : $func->($obj ? ($obj, $val) : $val),
1019 );
1020 }
1021
1022 1 return($_[0]);
1023 }
1024
1025
1026 =pod
1027
1028 =head2 value_by_path()
1029
1030 B<value_by_path($aref, $coordinate [, $value [, $force]])>
1031
1032 Get or set a value in a deeply nested array by a coordinate vector.
1033
1034 Example:
1035
1036 my $vec = [[1,2,3], [4,5,6], [7,8,9]];
1037
1038 value_by_path($vec, [1,1], 99);
1039
1040 will give:
1041
1042 [[1,2,3], [4,99,6], [7,8,9]];
1043
1044 in $vec. This is not spectacular since one could easily write:
1045
1046 $vec->[1][1] = 99;
1047
1048 but value_by_path() will be needed if the coordinate vector is created
1049 dynamically and can be of arbitrary length.
1050 If you explicitly want to set an undefined value, you have to set
1051 $force to a true value.
1052
1053 =cut
1054
1055 496 ▶sub value_by_path {
Subroutine value_by_path: called CC=13 SCAR=25.8
Pod documented
1056 496 my($aref, $coordinate, $value, $force) = @_;
1057
1058 496 ▶ croak $LastError = 'value_by_path: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $aref eq "ARRAY" 0 496 496
1059 unless ref($aref) eq 'ARRAY';
1060
1061 496 my $vref = $aref;
1062 496 ▶ my $vec = ref($coordinate) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $coordinate eq 'ARRAY' ? : 496 0 496
1063 ? $coordinate
1064 : [$coordinate];
1065
1066 496 my $end = @$vec - 1;
1067
1068 496 my $i = 0;
1069 496 ▶ while ( $i < $end ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 702 375 1077
1070
1071 702 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 407 295 702
1072 407 ▶ $vref->[$vec->[$i]] = []
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$vref[$vec->[$i]] and ref $$vref[$vec->[$i]] eq "ARRAY" 88 319 407
Condition66%2/3
defined $$vref[$vec->[$i]] and ref $$vref[$vec->[$i]] eq "ARRAY"
!ll&&!rl&&r
880319
MC/DC50%1/2
defined $$vref[$vec->[$i]] and ref $$vref[$vec->[$i]] eq "ARRAY"
defined $$vref[$vec->[$i]] ref $$vref[$vec->[$i]] eq "ARRAY"
Truth table66%2/3
A and B
A= defined $$vref[$vec->[$i]]B= ref $$vref[$vec->[$i]] eq "ARRAY"
ABresult
0X0
100
111
1073 unless defined($vref->[$vec->[$i]])
1074 and
1075 ref($vref->[$vec->[$i]]) eq 'ARRAY';
1076 } else {
1077 295 ▶ return $NaV unless ref($vref->[$vec->[$i]]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$vref[$vec->[$i]] eq "ARRAY" 121 174 295
1078 }
1079
1080 581 $vref = $vref->[$vec->[$i++]];
1081 }
1082
1083 375 ▶ if ( defined($value) or $force ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value or $force) { } 221 154 375
Condition66%2/3
defined $value or $force
l!l&&r!l&&!r
2210154
MC/DC50%1/2
defined $value or $force
defined $value $force
Truth table66%2/3
A or B
A= defined $valueB= $force
ABresult
1X1
011
000
1084 221 ▶ $vref->[$vec->[$i]]
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 4 217 221
1085 = ref($value) eq 'ARRAY'
1086 ? dclone($value)
1087 : $value;
1088 } else {
1089 154 ▶ return $NaV
Branch: 1 branch
BranchTrueFalseTotal
if $vec->[$i] > $vref->$#* 3 151 154
1090 if $vec->[$i] > $#$vref;
1091 return(
1092 151 ▶ ref($vref->[$vec->[$i]]) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $$vref[$vec->[$i]] eq 'ARRAY' ? : 8 143 151
1093 ? dclone($vref->[$vec->[$i]])
1094 : $vref->[$vec->[$i]]
1095 );
1096 }
1097 }
1098
1099
1100 =pod
1101
1102 =head2 vector_iterator()
1103
1104 B<vector_iterator($from_aref, $to_aref)>
1105
1106 This routine returns a subroutine reference to an iterator which
1107 is used to generate successive coordinate vectors starting with the
1108 coordinates in $from_aref to those in $to_aref.
1109
1110 The resulting subroutine will return a pair of coordinate vectors on
1111 each successive call or an empty list if the iterator has reached the
1112 last coordinate. The first coordinate returned is related to the given
1113 coordinate pair, the second one to a corresponding zero based array.
1114
1115 Example:
1116
1117 my $aref = [[1,2,3], [4,5,6], [7,8,9]];
1118
1119 my $iterator = vector_iterator([0,1], [1,2]);
1120
1121 while ( my($svec, $dvec) = $iterator->() ) {
1122 my $val = value_by_path($aref, $svec);
1123 print "[$svec->[0] $svec->[1]] [$dvec->[0] $dvec->[1]] -> $val\n";
1124 }
1125
1126 will print:
1127
1128 [0 1] [0 0] -> 2
1129 [0 2] [0 1] -> 3
1130 [1 1] [1 0] -> 5
1131 [1 2] [1 1] -> 6
1132
1133 =cut
1134
1135 20 ▶sub vector_iterator {
Subroutine vector_iterator: called CC=3 SCAR=11.0
Pod documented
1136 20 my($from, $to) = @_;
1137
1138 20 ▶ croak $LastError = 'value_by_path: not an array ref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $from eq "ARRAY" and ref $to eq "ARRAY" 0 20 20
Condition33%1/3
ref $from eq "ARRAY" and ref $to eq "ARRAY"
!ll&&!rl&&r
0020
MC/DC0%0/2
ref $from eq "ARRAY" and ref $to eq "ARRAY"
ref $from eq "ARRAY" ref $to eq "ARRAY"
Truth table33%1/3
A and B
A= ref $from eq "ARRAY"B= ref $to eq "ARRAY"
ABresult
0X0
100
111
1139 unless ref($from) eq 'ARRAY' and ref($to) eq 'ARRAY';
1140
1141 20 my @start = @$from;
1142 20 my @current = @$from;
1143 20 my @end = @$to;
1144 20 my @dir = map { $end[$_] <=> $start[$_] } 0 .. $#end;
1145 20 my @diff = map { abs($end[$_] - $start[$_]) + 1 } 0 .. $#end;
1146 20 my @dvec = map { 0 } 0 .. $#end;
1147
1148 20 my $end_reached = 0;
1149
1150 263 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
1151
1152 263 ▶ return if $end_reached;
Branch: 1 branch
BranchTrueFalseTotal
if $end_reached 20 243 263
1153
1154 243 $end_reached = 1;
1155 243 for my $i ( 0 .. $#end ) {
1156 342 ▶ $end_reached &&= $current[$i] == $end[$i];
Condition66%2/3
$end_reached &&= $current[$i] == $end[$i]
!ll&&!rl&&r
0223119
MC/DC50%1/2
$end_reached &&= $current[$i] == $end[$i]
$end_reached $current[$i] == $end[$i]
Truth table66%2/3
A &&= B
A= $end_reachedB= $current[$i] == $end[$i]
ABresult
0X0
100
111
1157 342 ▶ last unless $end_reached;
Branch: 1 branch
BranchTrueFalseTotal
unless $end_reached 223 119 342
1158 }
1159
1160 243 my $sretvec = [ @current ];
1161 243 my $dretvec = [ @dvec ];
1162
1163 243 for my $i ( reverse 0 .. $#end ) {
1164
1165 347 $current[$i] += $dir[$i];
1166 347 $dvec[$i]++;
1167 347 ▶ if ( $current[$i] == $end[$i] + $dir[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current[$i] == $end[$i] + $dir[$i]) 124 223 347
1168 124 $current[$i] = $start[$i];
1169 124 $dvec[$i] = 0;
1170 }
1171
1172 347 ▶ last if $current[$i] != $start[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $current[$i] != $start[$i] 223 124 347
1173 }
1174
1175 243 return($sretvec, $dretvec);
1176 20 };
1177 }
1178
1179
1180 =pod
1181
1182 =head1 SEE ALSO
1183
1184 Array::DeepUtils was developed during the implementation of lang5 a
1185 stack based array language. The source will be maintained in the source
1186 repository of lang5.
1187
1188 =head2 Links
1189
1190 =over
1191
1192 =item *
1193
1194 L<The lang5 Home Page|http://lang5.svn.sourceforge.net/>.
1195
1196 =item *
1197
1198 L<The lang5 SVN repository|https://lang5.svn.sourceforge.net/svnroot/lang5/>.
1199
1200 =back
1201
1202 =head2 Bug Reports and Feature Requests
1203
1204 =over
1205
1206 =item *
1207
1208 L<Bugs|https://sourceforge.net/tracker/?group_id=299543&atid=1263531>
1209
1210 =item *
1211
1212 L<Feature Requests|https://sourceforge.net/tracker/?group_id=299543&atid=1263534>
1213
1214 =back
1215
1216 =head1 AUTHOR
1217
1218 Thomas Kratz E<lt>tomk@cpan.orgE<gt>
1219
1220 Bernd Ulmann E<lt>ulmann@vaxman.deE<gt>
1221
1222 =head1 COPYRIGHT
1223
1224 Copyright (C) 2011 by Thomas Kratz, Bernd Ulmann
1225
1226 This library is free software; you can redistribute it and/or
1227 modify it under the same terms as Perl itself, either Perl version
1228 5.8.8 or, at your option, any later version of Perl 5 you may
1229 have available.
1230
1231 =cut
1232
1233 1 1;
↑ Summary