blib/lib/CXC/Number/Grid/Tree.pm

filter: Statementstmt 83.5% 71 / 85 Branchbran 71.4% 20 / 28 Conditioncond 58.3% 7 / 12 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 81.8% 18 / 22 Podpod 100% 8 / 8 totaltotal 78.8% 127 / 161 CC 25 SCAR 21.1
CC
25
Cov
78%
to_string
42.0
_snap_overlaid_edges
7.1
to_grid
5.0
CRAP 8.3
« blib/lib/CXC/Number/Grid/Range.pm ↑ Summary blib/lib/CXC/Number/Sequence/Utils.pm »
1 package CXC::Number::Grid::Tree;
2  
3 # ABSTRACT: A Tree representing a Grid
4  
5 13 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 our $VERSION = '0.12';
10  
11 13 ▶use parent 'Tree::Range::RB';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 13 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2795 ▶my sub _key_compare { $_[0] <=> $_[1] }
Subroutine _key_compare: called CC=1 SCAR=0.0
16  
17 sub _croak {
18 0 ▶ require Carp;
Subroutine _croak: not called CC=1 SCAR=6.9
19 0 goto \&Carp::croak;
20 }
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31 21 ▶sub new ( $class, $options = {} ) {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
32  
33 21 my %options = ( cmp => \&_key_compare, $options->%* );
34 21 return $class->SUPER::new( \%options );
35 }
36  
37  
38  
39  
40  
41  
42  
43  
44  
45 ▶sub to_string ( $self ) {
Subroutine to_string: not called CC=6 SCAR=37.4
Pod documented
46 0 my $ic = $self->range_iter_closure;
47 0 my @string;
48 0 ▶ while ( my ( $v, $lb, $ub ) = $ic->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ic()) 0 0 0
49 0 ▶ $v
Branch: 2 branches
BranchTrueFalseTotal
defined $v ? : 0 0 0
ref $v ? : 0 0 0
50 = defined $v
51 ? ref $v
52 0 ? "[ @{[ join( ', ', $v->@*) ]} ]"
53 : $v
54 : 'undef';
55 0 ▶ $lb //= 'undef';
Condition0%0/2
$lb //= "undef"
l!l
00
MC/DC0%0/1
$lb //= "undef"
$lb
Truth table0%0/2
A
A= $lb
Aresult
11
00
56 0 ▶ $ub //= 'undef';
Condition0%0/2
$ub //= "undef"
l!l
00
MC/DC0%0/1
$ub //= "undef"
$ub
Truth table0%0/2
A
A= $ub
Aresult
11
00
57 0 push @string, "( $lb, $ub )\t=> $v";
58 }
59  
60 0 return join( "\n", @string );
61 }
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74 ▶sub to_array ( $self ) {
Subroutine to_array: called CC=2 SCAR=6.9
Pod documented
75 6 my @arr;
76 6 my $ic = $self->range_iter_closure;
77 6 $ic->(); # discard lower bound
78 6 ▶ while ( my ( $v, $lb, $ub ) = $ic->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ic()) 27 6 33
79 27 push @arr, [ $lb, $ub, $v ];
80 }
81 6 pop @arr; # discard upper bound
82 6 return \@arr;
83 }
84  
85  
86  
87  
88  
89  
90  
91  
92  
93 ▶sub from_array ( $, $ranges ) {
Subroutine from_array: called CC=2 SCAR=6.9
Pod documented
94 1 my $tree = __PACKAGE__->new;
95 1 $tree->range_set( $_->@* ) for $ranges->@*;
96 1 return $tree;
97 }
98  
99  
100  
101  
102  
103  
104  
105  
106  
107 ▶sub from_grid ( $, $grid ) {
Subroutine from_grid: called CC=2 SCAR=6.9
Pod documented
108 4 my $tree = __PACKAGE__->new;
109  
110 4 my $edges = $grid->_raw_edges;
111 4 my $include = $grid->include;
112  
113 4 $tree->range_set( $edges->@[ $_, $_ + 1 ], $include->[$_] ) for 0 .. ( $grid->nbins - 1 );
114 4 return $tree;
115 }
116  
117  
118  
119  
120  
121  
122  
123  
124  
125 ▶sub to_grid ( $self ) {
Subroutine to_grid: called CC=5 SCAR=16.1
Pod documented
126  
127 15 my @edges;
128 my @include;
129 15 my $ic = $self->range_iter_closure;
130  
131  
132 # this is the lower bound
133 15 my ( $v, $lower, $upper ) = $ic->();
134 15 push @edges, $upper;
135  
136 15 ▶ while ( ( $v, $lower, $upper ) = $ic->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ic()) 91 15 106
137 91 ▶ if ( defined $upper ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $upper) 76 15 91
138 76 push @edges, $upper;
139 76 ▶ push @include, ref $v ? $v->[-1] : $v // 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 30 46 76
Condition100%2/2
$v // 0
l!l
451
MC/DC100%1/1
$v // 0
$v
Truth table100%2/2
A
A= $v
Aresult
11
00
140 }
141 }
142 15 require CXC::Number::Grid;
143 15 return CXC::Number::Grid->new( edges => \@edges, include => \@include );
144 }
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195 sub snap_overlaid ( $self, $layer, $snap_to, $snap_dist )
196 ▶{ ## no critic(Subroutines::ProhibitManyArgs)
Subroutine snap_overlaid: called CC=3 SCAR=11.0
Pod documented
197  
198 8 ▶ return if $snap_dist == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $snap_dist == 0 3 5 8
199  
200 # Tree::Range doesn't represent a range as a node with the ability
201 # to visit a predecessor. It essentially only allows one way
202 # tree traversal, so we need to traverse it forwards to handle
203 # snapping to the right, and backwards to handle snapping to the left.
204 5 $self->_snap_overlaid_edges( $layer, $snap_to, $snap_dist, $_ ) for qw( right left );
205 }
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217 sub _snap_overlaid_edges ( $self, $layer, $snap_to, $snap_dist, $scan_direction )
218 ▶{ ## no critic(Subroutines::ProhibitManyArgs)
Subroutine _snap_overlaid_edges: called CC=7 SCAR=19.6
219  
220 10 require CXC::Number::Grid::Range;
221  
222 10 ▶ defined( my $scan_reversed = { right => 0, left => 1 }->{$scan_direction} )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $scan_reversed = {"right", 0, "left", 1}->{$scan_direction}) or _croak("illegal scan direction: '${scan_direction}'") 0 10 10
223 or _croak( "illegal scan direction: '$scan_direction'" );
224  
225 16 ▶ my sub iter ( $key = undef ) {
Subroutine iter: called CC=3 SCAR=11.0
226 16 my $iter = $self->range_iter_closure( $key, $scan_reversed );
227 # first range goes from +-inf to real bound; remove
228 16 ▶ $iter->() unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 10 6 16
229 16 return $iter;
230 }
231  
232 10 my $iter = iter();
233  
234 my sub next_range {
235 68 ▶ my @r = $iter->();
Subroutine next_range: called CC=2 SCAR=6.9
236 return @r
237 68 ▶ ? CXC::Number::Grid::Range->new( {
Branch: 1 branch
BranchTrueFalseTotal
@r ? : 58 10 68
238 value => $r[0],
239 lb => $r[1],
240 ub => $r[2],
241 } )
242 : undef;
243 }
244  
245 10 my $current = next_range();
246 10 my $next;
247  
248 my %SnapTo = (
249 overlay => {
250 right => sub {
251 # would prefer
252 # $self->range_set( $prev->lb, $current->ub, $current->value );
253 # but there's no way to get $prev from Tree::Range.
254 #
255 # This code depends upon Tree::Range storing [ $lb, $value ]
256 # in each node, so deleting a node extends the previous
257 # range.
258 4 ▶ $self->delete( $current->lb );
Subroutine __ANON__: called CC=1 SCAR=0.0
259 4 $iter = iter( $current->lb );
260 },
261 left => sub {
262 0 ▶ $self->range_set( $next->lb, $current->ub, $next->value );
Subroutine __ANON__: not called CC=1 SCAR=6.9
263 0 $iter = iter( $current->ub );
264 },
265 },
266 underlay => {
267 right => sub {
268 1 ▶ $self->range_set( $current->lb, $next->ub, $next->value );
Subroutine __ANON__: called CC=1 SCAR=0.0
269 1 $iter = iter( $current->lb );
270  
271 },
272 left => sub {
273 1 ▶ $self->range_set( $next->lb, $current->ub, $next->value );
Subroutine __ANON__: called CC=1 SCAR=0.0
274 1 $iter = iter( $current->ub );
275 },
276 },
277 10 );
278  
279 10 ▶ my $snap = $SnapTo{$snap_to}{$scan_direction}
Branch: 1 branch
BranchTrueFalseTotal
my $snap = $SnapTo{$snap_to}{$scan_direction} or _croak("unknown layer to snap to: $snap_to") 0 10 10
280 or _croak( "unknown layer to snap to: $snap_to" );
281  
282 10 ▶ while ( defined( $next = next_range() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($next = next_range())) 42 10 52
283  
284 42 ▶ if ( abs( $current->ub - $current->lb ) <= $snap_dist
Branch: 1 branch
BranchTrueFalseTotal
if (abs $current->ub - $current->lb <= $snap_dist and $next->layer == $layer and $current->layer < $next->layer) { } 6 36 42
Condition83%5/6
abs $current->ub - $current->lb <= $snap_dist and $next->layer == $layer and $current->layer < $next->layer
!ll&&!rl&&r
3606
abs $current->ub - $current->lb <= $snap_dist and $next->layer == $layer
!ll&&!rl&&r
3516
MC/DC66%2/3
abs $current->ub - $current->lb <= $snap_dist and $next->layer == $layer and $current->layer < $next->layer
abs $current->ub - $current->lb <= $snap_dist $next->layer == $layer $current->layer < $next->layer
Truth table75%3/4
A and B and C
A= abs $current->ub - $current->lb <= $snap_distB= $next->layer == $layerC= $current->layer < $next->layer
ABCresult
0XX0
10X0
1100
1111
285 && $next->layer == $layer
286 && $current->layer < $next->layer )
287 {
288 6 $snap->();
289 # all of the $snap routines reset the iterator
290 6 $current = next_range();
291 }
292 else {
293 36 $current = $next;
294 }
295 }
296 }
297  
298  
299  
300  
301  
302  
303  
304  
305  
306 ▶sub clone ( $self ) {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
307 0 return __PACKAGE__->from_array( $self->to_array );
308 }
309  
310 13 1;
311  
312 #
313 # This file is part of CXC-Number
314 #
315 # This software is Copyright (c) 2019 by Smithsonian Astrophysical Observatory.
316 #
317 # This is free software, licensed under:
318 #
319 # The GNU General Public License, Version 3, June 2007
320 #
321  
322 __END__
323  
324 =pod
325  
326 =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory extrema overlaid
327  
328 =head1 NAME
329  
330 CXC::Number::Grid::Tree - A Tree representing a Grid
331  
332 =head1 VERSION
333  
334 version 0.12
335  
336 =head1 DESCRIPTION
337  
338 This is a subclass of L<Tree::Range> which is used to manipulate grids
339  
340 =head1 CONSTRUCTORS
341  
342 =head2 new
343  
344 $tree = CXC::Number::Grid::Tree->new( ?\%options )
345  
346 Construct a new tree, using a default numerical key comparison
347 function. All options recognized by L<Tree::Range::RB> are accepted.
348  
349 =head2 from_array
350  
351 $tree = CXC::Number::Grid::Tree->from_array( \@array );
352  
353 Construct a tree object from an array generated by L</to_array>.
354  
355 =head2 from_grid
356  
357 $tree = CXC::Number::Grid::Tree->from_grid( $grid );
358  
359 Construct a tree object from a L<CXC::Number::Grid> object.
360  
361 =head1 METHODS
362  
363 =head2 to_string
364  
365 $tree->to_string;
366  
367 Return a string representation of the tree.
368  
369 =head2 to_array
370  
371 \@array = $tree->to_array;
372  
373 Return an arrayref with one element per bin. Each element is an
374 arrayref and contains the lower bound, upper bound, and value stored
375 in the tree for the bin bin.
376  
377 =head2 to_grid
378  
379 $grid = $tree->to_grid;
380  
381 Return a L<CXC::Number::Grid> object represented by the tree.
382  
383 =head2 snap_overlaid
384  
385 $tree->snap_overlaid( $layer, $snap_to, $snap_dist ) {
386  
387 Snap overlaid bins' edges.
388  
389 B<Works in place!!>
390  
391 This assumes that the Tree has been
392  
393 =over
394  
395 =item 1
396  
397 loaded with ranges from two grids, one of which overlays the other; and
398  
399 =item 2
400  
401 that the range values are arrayrefs with the first value being the
402 layer id (larger number indicates the top grid); and
403  
404 =item 3
405  
406 that the top grid is contiguous (e.g. no holes through which the
407 lower grid is visible)
408  
409 =back
410  
411 In the general case, the minimum and maximum edges of the top grid
412 will intersect bins in the lower grid. If the remnants of those bins
413 (e.g. the parts not covered by the top grid) are small enough (e.g,
414 smaller than C<$snap_dist> in width), then this routine will either:
415  
416 =over
417  
418 =item *
419  
420 move the outer edge of the top grid to coincide with the remaining edge of the intersected lower bin
421 (C<$snap_to = 'underlay'>)
422  
423 =item *
424  
425 move the remaining edge of the intersected lower bin to coincide with the edge of the top grid.
426 (C<$snap_to = 'overlay'>)
427  
428 =back
429  
430 =head2 clone
431  
432 $clone = $tree->clone;
433  
434 Clone a tree, performing a shallow copy of the values associated with each bin in the tree.
435  
436 =head1 INTERNALS
437  
438 =head2 Methods
439  
440 =head3 _snap_overlaid_edges
441  
442 $self->_snap_overlaid_edges( $layer, $snap_to, $snap_dist, $scan_direction ) {
443  
444 A helper for L</snap_to>. This routine is run twice, with
445 C<$scan_direction> set to C<left> and C<right>, then to handle the
446 extrema of the overlaid grid.
447  
448 =head1 SUPPORT
449  
450 =head2 Bugs
451  
452 Please report any bugs or feature requests to bug-cxc-number@rt.cpan.org or through the web interface at: L<https://rt.cpan.org/Public/Dist/Display.html?Name=CXC-Number>
453  
454 =head2 Source
455  
456 Source is available at
457  
458 https://gitlab.com/djerius/cxc-number
459  
460 and may be cloned from
461  
462 https://gitlab.com/djerius/cxc-number.git
463  
464 =head1 SEE ALSO
465  
466 Please see those modules/websites for more information related to this module.
467  
468 =over 4
469  
470 =item *
471  
472 L<CXC::Number|CXC::Number>
473  
474 =item *
475  
476 L<CXC::Number::Grid|CXC::Number::Grid>
477  
478 =item *
479  
480 L<CXC::Number::Grid::Range|CXC::Number::Grid::Range>
481  
482 =back
483  
484 =head1 AUTHOR
485  
486 Diab Jerius <djerius@cpan.org>
487  
488 =head1 COPYRIGHT AND LICENSE
489  
490 This software is Copyright (c) 2019 by Smithsonian Astrophysical Observatory.
491  
492 This is free software, licensed under:
493  
494 The GNU General Public License, Version 3, June 2007
495  
496 =cut
« blib/lib/CXC/Number/Grid/Range.pm ↑ Summary blib/lib/CXC/Number/Sequence/Utils.pm »