blib/lib/Tree/R.pm

filter: Statementstmt 92.1% 236 / 256 Branchbran 74.1% 89 / 120 Conditioncond 71.4% 45 / 63 MC/DCmcdc 41.9% 13 / 31 Subroutinesub 95.6% 22 / 23 Podpod 0.0% 0 / 20 totaltotal 78.9% 405 / 513 CC 95 SCAR 24.1
CC
95
Cov
84%
dump
56.0
query_completely_within_rect
15.3
QuadraticSplit
13.9
CRAP 11.1
↑ Summary
1 package Tree::R;
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 require Exporter;
7 1 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our @ISA = qw(Exporter);
10  
11 # Items to export into callers namespace by default. Note: do not export
12 # names by default without a very good reason. Use EXPORT_OK instead.
13 # Do not simply export all your public functions/methods/constants.
14  
15 # This allows declaration use Tree::R ':all';
16 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17 # will save memory.
18 1 our %EXPORT_TAGS = ( 'all' => [ qw(
19  
20 ) ] );
21  
22 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23  
24 1 our @EXPORT = qw(
25  
26 );
27  
28 1 our $VERSION = '0.072';
29  
30 =pod
31  
32 =head1 NAME
33  
34 Tree::R - Perl extension for the R-tree data structure and algorithms
35  
36 =head1 SYNOPSIS
37  
38 use Tree::R;
39  
40 my $rtree = Tree::R->new
41  
42 for my $object (@objects) {
43 my @bbox = $object->bbox(); # (minx,miny,maxx,maxy)
44 $rtree->insert($object,@bbox);
45 }
46  
47 my @point = (123, 456); # (x,y)
48 my @results;
49 $rtree->query_point(@point,\@results);
50 for my $object (@results) {
51 # point is in object's bounding box
52 }
53  
54 my @rect = (123, 456, 789, 1234); # (minx,miny,maxx,maxy)
55 @results = ();
56 $rtree->query_completely_within_rect(@rect,\@results);
57 for my $object (@results) {
58 # object is within rectangle
59 }
60  
61 @results = ();
62 $rtree->query_partly_within_rect(@rect,\@results);
63 for my $object (@results) {
64 # object's bounding box and rectangle overlap
65 }
66  
67 =head1 DESCRIPTION
68  
69 R-tree is a data structure for storing and indexing and efficiently
70 looking up non-zero-size spatial objects.
71  
72 =head2 EXPORT
73  
74 None by default.
75  
76 =head1 SEE ALSO
77  
78 A. Guttman: R-trees: a dynamic index structure for spatial
79 indexing. ACM SIGMOD'84, Proc. of Annual Meeting (1984), 47--57.
80  
81 N. Beckmann, H.-P. Kriegel, R. Schneider & B. Seeger: The R*-tree: an
82 efficient and robust access method for points and rectangles. Proc. of
83 the 1990 ACM SIGMOD Internat. Conf. on Management of Data (1990),
84 322--331.
85  
86 The homepage of this module is on github:
87 https://github.com/ajolma/Tree-R
88  
89 =head1 AUTHOR
90  
91 Ari Jolma
92  
93 =head1 COPYRIGHT AND LICENSE
94  
95 Copyright (C) 2005- by Ari Jolma
96  
97 This library is free software; you can redistribute it and/or modify
98 it under the terms of The Artistic License 2.0.
99  
100 =head1 REPOSITORY
101  
102 L<https://github.com/ajolma/Tree-R>
103  
104 =cut
105  
106 4 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
107 4 my $package = shift;
108 4 my %opt = @_;
109 4 my $self = {};
110 4 for my $k (keys %opt) {
111 4 $self->{$k} = $opt{$k};
112 }
113 4 ▶ $self->{m} = 2 unless $self->{m};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"m"} 2 2 4
114 4 ▶ $self->{M} = 5 unless $self->{M};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"M"} 2 2 4
115 # $self->{root} = [1,$child,@rect];
116 # $child == [[0,$object,@rect],...] if leaf or [[1,$child,@rect],...] if non-leaf
117 4 ▶ bless $self => (ref($package) or $package);
Condition33%1/3
ref $package || $package
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $package || $package
ref $package $package
Truth table33%1/3
A || B
A= ref $packageB= $package
ABresult
1X1
011
000
118 4 return $self;
119 }
120  
121 88 ▶sub objects {
Subroutine objects: called CC=6 SCAR=17.9
Pod undocumented
122 88 my ($self,$objects,$N) = @_;
123 88 ▶ return unless $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root"} 1 87 88
124 87 ▶ $N = $self->{root} unless $N;
Branch: 1 branch
BranchTrueFalseTotal
unless $N 12 75 87
125 87 ▶ return unless $N;
Branch: 1 branch
BranchTrueFalseTotal
unless $N 0 87 87
126 87 ▶ unless ($N->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($N->[0]) { } 32 55 87
127 55 push @$objects,$N->[1];
128 } else {
129 # check entries
130 32 for my $entry (@{$N->[1]}) {
131 75 $self->objects($objects,$entry);
132 }
133 }
134 }
135  
136 13 ▶sub query_point {
Subroutine query_point: called CC=9 SCAR=22.0
Pod undocumented
137 13 my($self,$x,$y,$objects,$N) = @_;
138 13 ▶ return unless $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root"} 1 12 13
139 12 ▶ $N = $self->{root} unless $N;
Branch: 1 branch
BranchTrueFalseTotal
unless $N 2 10 12
140 12 ▶ return unless $x >= $N->[2] and $x <= $N->[4] and $y >= $N->[3] and $y <= $N->[5];
Branch: 1 branch
BranchTrueFalseTotal
unless $x >= $N->[2] and $x <= $N->[4] and $y >= $N->[3] and $y <= $N->[5] 4 8 12
Condition77%7/9
$x >= $N->[2] and $x <= $N->[4] and $y >= $N->[3] and $y <= $N->[5]
!ll&&!rl&&r
408
$x >= $N->[2] and $x <= $N->[4] and $y >= $N->[3]
!ll&&!rl&&r
228
$x >= $N->[2] and $x <= $N->[4]
!ll&&!rl&&r
0210
MC/DC50%2/4
$x >= $N->[2] and $x <= $N->[4] and $y >= $N->[3] and $y <= $N->[5]
$x >= $N->[2] $x <= $N->[4] $y >= $N->[3] $y <= $N->[5]
Truth table60%3/5
A and B and C and D
A= $x >= $N->[2]B= $x <= $N->[4]C= $y >= $N->[3]D= $y <= $N->[5]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
141 8 ▶ unless ($N->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($N->[0]) { } 4 4 8
142 4 push @$objects,$N->[1];
143 } else {
144 # check entries
145 4 for my $entry (@{$N->[1]}) {
146 10 $self->query_point($x,$y,$objects,$entry);
147 }
148 }
149 }
150  
151 #non-recursive from liuyi at cis.uab.edu
152 2 ▶sub query_completely_within_rect {
Subroutine query_completely_within_rect: called CC=15 SCAR=27.3
Pod undocumented
153 2 my($self,$minx,$miny,$maxx,$maxy,$objects,$Node) = @_;
154 2 ▶ return unless $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root"} 0 2 2
155  
156 2 ▶ $Node = $self->{root} unless $Node;
Branch: 1 branch
BranchTrueFalseTotal
unless $Node 2 0 2
157 2 my @entries;
158 2 push @entries,\$Node;
159  
160 2 ▶ while (@entries>0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@entries > 0) 16 2 18
161 16 my $N = pop @entries;
162 16 ▶ if (${$N}->[2] > $maxx or # right
Branch: 1 branch
BranchTrueFalseTotal
if (${$N;}->[2] > $maxx or ${$N;}->[4] < $minx or ${$N;}->[3] > $maxy or ${$N;}->[5] < $miny) { } 2 14 16
Condition66%6/9
${$N;}->[2] > $maxx or ${$N;}->[4] < $minx or ${$N;}->[3] > $maxy or ${$N;}->[5] < $miny
l!l&&r!l&&!r
2014
${$N;}->[2] > $maxx or ${$N;}->[4] < $minx or ${$N;}->[3] > $maxy
l!l&&r!l&&!r
2014
${$N;}->[2] > $maxx or ${$N;}->[4] < $minx
l!l&&r!l&&!r
0214
MC/DC25%1/4
${$N;}->[2] > $maxx or ${$N;}->[4] < $minx or ${$N;}->[3] > $maxy or ${$N;}->[5] < $miny
${$N;}->[2] > $maxx ${$N;}->[4] < $minx ${$N;}->[3] > $maxy ${$N;}->[5] < $miny
Truth table40%2/5
A or B or C or D
A= ${$N;}->[2] > $maxxB= ${$N;}->[4] < $minxC= ${$N;}->[3] > $maxyD= ${$N;}->[5] < $miny
ABCDresult
1XXX1
01XX1
001X1
00011
00000
163 16 ${$N}->[4] < $minx or # left
164 14 ${$N}->[3] > $maxy or # above
165 14 ${$N}->[5] < $miny) # below
166 {
167 2 next;
168 }
169 else {
170 14 ▶ if ((!${$N}->[0])
Branch: 1 branch
BranchTrueFalseTotal
if (not ${$N;}->[0] and ${$N;}->[2] >= $minx and ${$N;}->[4] <= $maxx and ${$N;}->[3] >= $miny and ${$N;}->[5] <= $maxy) 4 10 14
Condition83%10/12
not ${$N;}->[0] and ${$N;}->[2] >= $minx and ${$N;}->[4] <= $maxx and ${$N;}->[3] >= $miny and ${$N;}->[5] <= $maxy
!ll&&!rl&&r
1004
not ${$N;}->[0] and ${$N;}->[2] >= $minx and ${$N;}->[4] <= $maxx and ${$N;}->[3] >= $miny
!ll&&!rl&&r
1004
not ${$N;}->[0] and ${$N;}->[2] >= $minx and ${$N;}->[4] <= $maxx
!ll&&!rl&&r
824
not ${$N;}->[0] and ${$N;}->[2] >= $minx
!ll&&!rl&&r
626
MC/DC60%3/5
not ${$N;}->[0] and ${$N;}->[2] >= $minx and ${$N;}->[4] <= $maxx and ${$N;}->[3] >= $miny and ${$N;}->[5] <= $maxy
not ${$N;}->[0] ${$N;}->[2] >= $minx ${$N;}->[4] <= $maxx ${$N;}->[3] >= $miny ${$N;}->[5] <= $maxy
Truth table66%4/6
not(A) and B and C and D and E
A= not ${$N;}->[0]B= ${$N;}->[2] >= $minxC= ${$N;}->[4] <= $maxxD= ${$N;}->[3] >= $minyE= ${$N;}->[5] <= $maxy
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
171 8 and (${$N}->[2] >= $minx)
172 6 and (${$N}->[4] <= $maxx)
173 4 and (${$N}->[3] >= $miny)
174 4 and (${$N}->[5] <= $maxy))
175 {
176 4 push @$objects,${$N}->[1];
177 }
178
179 14 ▶ if (${$N}->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$N;}->[0]) 6 8 14
180 6 foreach my $e (@{${$N}->[1]}) {
181 14 push @entries,\$e;
182 }
183 }
184 }
185 }
186 2 return $objects;
187 }
188  
189 #non-recursive from liuyi at cis.uab.edu
190 2 ▶sub query_partly_within_rect {
Subroutine query_partly_within_rect: called CC=10 SCAR=23.2
Pod undocumented
191 2 my($self,$minx,$miny,$maxx,$maxy,$objects,$Node) = @_;
192 2 ▶ return unless $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root"} 0 2 2
193  
194 2 ▶ $Node = $self->{root} unless $Node;
Branch: 1 branch
BranchTrueFalseTotal
unless $Node 2 0 2
195 2 my @entries;
196 2 push @entries,\$Node;
197  
198 2 ▶ while (@entries>0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@entries > 0) 16 2 18
199 16 my $N = pop @entries;
200 16 ▶ if (${$N}->[2] > $maxx or # right
Branch: 1 branch
BranchTrueFalseTotal
if (${$N;}->[2] > $maxx or ${$N;}->[4] < $minx or ${$N;}->[3] > $maxy or ${$N;}->[5] < $miny) { } 2 14 16
Condition66%6/9
${$N;}->[2] > $maxx or ${$N;}->[4] < $minx or ${$N;}->[3] > $maxy or ${$N;}->[5] < $miny
l!l&&r!l&&!r
2014
${$N;}->[2] > $maxx or ${$N;}->[4] < $minx or ${$N;}->[3] > $maxy
l!l&&r!l&&!r
2014
${$N;}->[2] > $maxx or ${$N;}->[4] < $minx
l!l&&r!l&&!r
0214
MC/DC25%1/4
${$N;}->[2] > $maxx or ${$N;}->[4] < $minx or ${$N;}->[3] > $maxy or ${$N;}->[5] < $miny
${$N;}->[2] > $maxx ${$N;}->[4] < $minx ${$N;}->[3] > $maxy ${$N;}->[5] < $miny
Truth table40%2/5
A or B or C or D
A= ${$N;}->[2] > $maxxB= ${$N;}->[4] < $minxC= ${$N;}->[3] > $maxyD= ${$N;}->[5] < $miny
ABCDresult
1XXX1
01XX1
001X1
00011
00000
201 16 ${$N}->[4] < $minx or # left
202 14 ${$N}->[3] > $maxy or # above
203 14 ${$N}->[5] < $miny) # below
204 {
205 2 next;
206 }
207 else {
208 14 ▶ if (!${$N}->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ${$N;}->[0]) { } 8 6 14
209 8 push @$objects,${$N}->[1];
210 }
211 else {
212 6 foreach my $e (@{${$N}->[1]}) {
213 14 push @entries,\$e;
214 }
215 }
216 }
217 }
218 2 return $objects;
219 }
220  
221 22 ▶sub insert {
Subroutine insert: called CC=3 SCAR=11.0
Pod undocumented
222 22 my ($self,$object,@rect) = @_; # rect = $minX,$minY,$maxX,$maxY
223 22 my $child = [0,$object,@rect];
224 22 ▶ unless ($self->{root}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'root'}) { } 18 4 22
225 4 $self->{root} = [1,[$child],@rect];
226 } else {
227 18 my $N = $self->ChooseSubTree(@rect);
228 18 push @{$N->[1]},$child;
229 18 ▶ $self->QuadraticSplit($N->[1]) if @{$N->[1]} > $self->{M};
Branch: 1 branch
BranchTrueFalseTotal
if @{$N->[1];} > $self->{"M"} 2 16 18
230 }
231 }
232  
233 # returns the leaf which contains the object, the index of the object
234 # in the leaf, and the parent of the leaf
235  
236 23 ▶sub get_leaf {
Subroutine get_leaf: called CC=6 SCAR=17.9
Pod undocumented
237 23 my ($self,$object,$leaf,$index_of_leaf,$parent) = @_;
238 23 ▶ $leaf = $self->{root} unless $leaf;
Branch: 1 branch
BranchTrueFalseTotal
unless $leaf 11 12 23
239 23 for my $index (0..$#{$leaf->[1]}) {
240 41 my $entry = $leaf->[1]->[$index];
241 41 ▶ unless ($entry->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->[0]) { } 12 29 41
242 29 ▶ return ($parent,$index_of_leaf,$leaf,$index) if $entry->[1] == $object;
Branch: 1 branch
BranchTrueFalseTotal
if $entry->[1] == $object 11 18 29
243 } else {
244 12 my @ret = $self->get_leaf($object,$entry,$index,$leaf);
245 12 ▶ return @ret if @ret;
Branch: 1 branch
BranchTrueFalseTotal
if @ret 7 5 12
246 }
247 }
248 5 return ();
249 }
250  
251 56 ▶sub set_bboxes {
Subroutine set_bboxes: called CC=5 SCAR=16.1
Pod undocumented
252 56 my ($self,$N) = @_;
253 56 ▶ $N = $self->{root} unless $N;
Branch: 1 branch
BranchTrueFalseTotal
unless $N 11 45 56
254 56 ▶ return @$N[2..5] if $N->[0] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $N->[0] == 0 33 23 56
255 23 my @bbox;
256 23 for my $child (@{$N->[1]}) {
257 45 my @bbox_of_child = $self->set_bboxes($child);
258 45 ▶ @bbox = @bbox ? enlarged_rect(@bbox_of_child,@bbox) : @bbox_of_child;
Branch: 1 branch
BranchTrueFalseTotal
@bbox ? : 23 22 45
259 }
260 23 @$N[2..5] = @bbox;
261 23 return @bbox;
262 }
263  
264 11 ▶sub remove {
Subroutine remove: called CC=10 SCAR=23.1
Pod undocumented
265 11 my ($self,$object) = @_;
266 11 my ($parent,$index_of_leaf,$leaf,$index) = $self->get_leaf($object);
267  
268 11 ▶ return unless $leaf;
Branch: 1 branch
BranchTrueFalseTotal
unless $leaf 0 11 11
269  
270 # remove the object
271 11 splice(@{$leaf->[1]},$index,1);
272  
273 # is the leaf too small now?
274 11 ▶ if ($parent and @{$leaf->[1]} < $self->{m}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and @{$leaf->[1];} < $self->{'m'}) { } 1 10 11
Condition100%3/3
$parent and @{$leaf->[1];} < $self->{"m"}
!ll&&!rl&&r
461
MC/DC100%2/2
$parent and @{$leaf->[1];} < $self->{"m"}
$parent @{$leaf->[1];} < $self->{"m"}
Truth table100%3/3
A and B
A= $parentB= @{$leaf->[1];} < $self->{"m"}
ABresult
0X0
100
111
275  
276 # remove the leaf
277 1 splice(@{$parent->[1]},$index_of_leaf,1);
278  
279 # is the parent now too small?
280 1 ▶ if (@{$parent->[1]} < $self->{m}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$parent->[1];} < $self->{"m"}) 1 0 1
281  
282 # yes, move the children up
283 1 my @new_child_list;
284 1 for my $entry (@{$parent->[1]}) {
285 1 for my $child (@{$entry->[1]}) {
286 3 push @new_child_list,$child;
287 }
288 }
289 1 $parent->[1] = [@new_child_list];
290  
291 }
292  
293 1 $self->set_bboxes();
294  
295 # reinsert the orphans
296 1 for my $child (@{$leaf->[1]}) {
297 1 my $N = $self->ChooseSubTree(@$child[2..5]);
298 1 push @{$N->[1]},$child;
299 1 ▶ $self->QuadraticSplit($N->[1]) if @{$N->[1]} > $self->{M};
Branch: 1 branch
BranchTrueFalseTotal
if @{$N->[1];} > $self->{"M"} 1 0 1
300 }
301  
302 } else {
303  
304 10 $self->set_bboxes();
305  
306 }
307 11 ▶ delete $self->{root} unless defined $self->{root}->[2];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"root"}[2] 1 10 11
308 }
309  
310 0 ▶sub dump {
Subroutine dump: not called CC=7 SCAR=40.3
Pod undocumented
311 0 my ($self,$N,$level) = @_;
312 0 ▶ return unless $self->{root};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root"} 0 0 0
313 0 ▶ $N = $self->{root} unless $N;
Branch: 1 branch
BranchTrueFalseTotal
unless $N 0 0 0
314 0 ▶ return unless $N;
Branch: 1 branch
BranchTrueFalseTotal
unless $N 0 0 0
315 0 ▶ $level = 0 unless $level;
Branch: 1 branch
BranchTrueFalseTotal
unless $level 0 0 0
316 0 ▶ unless ($N->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($N->[0]) { } 0 0 0
317 0 print "($level) object $N $N->[1] rect @$N[2..5]\n";
318 } else {
319 0 print "($level) subtree $N $N->[1] rect @$N[2..5]\n";
320 0 for my $entry (@{$N->[1]}) {
321 0 $self->dump($entry,$level+1);
322 }
323 }
324 }
325  
326 19 ▶sub ChooseSubTree {
Subroutine ChooseSubTree: called CC=7 SCAR=19.5
Pod undocumented
327 19 my ($self,@rect) = @_;
328 # CS1
329 19 ▶ unless ($self->{root}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"root"}) 0 19 19
330 0 $self->{root} = [1,[],@rect];
331 0 return $self->{root};
332 }
333 19 my $N = $self->{root};
334 28 CS2:
335 @$N[2..5] = enlarged_rect(@$N[2..5],@rect);
336 # print STDERR "N = $N, $N->[0], @{$N->[1]}\n";
337 28 ▶ unless ($N->[1]->[0]->[0]) { # is leaf
Branch: 1 branch
BranchTrueFalseTotal
if ($N->[1][0][0]) { } 9 19 28
338 19 return $N;
339 } else {
340 9 my $chosen;
341 my $needed_enlargement_of_chosen;
342 9 my $area_of_chosen;
343 9 for my $entry (@{$N->[1]}) {
344 18 my @rect_of_entry = @$entry[2..5];
345 18 my $area = area_of_rect(@rect_of_entry);
346 18 my $needed_enlargement = area_of_rect(enlarged_rect(@rect_of_entry,@rect)) - $area;
347 18 ▶ if (!$chosen or
Branch: 1 branch
BranchTrueFalseTotal
if (not $chosen or $needed_enlargement < $needed_enlargement_of_chosen or $area < $area_of_chosen) 16 2 18
Condition100%6/6
not $chosen or $needed_enlargement < $needed_enlargement_of_chosen or $area < $area_of_chosen
l!l&&r!l&&!r
1512
not $chosen or $needed_enlargement < $needed_enlargement_of_chosen
l!l&&r!l&&!r
963
MC/DC100%3/3
not $chosen or $needed_enlargement < $needed_enlargement_of_chosen or $area < $area_of_chosen
not $chosen $needed_enlargement < $needed_enlargement_of_chosen $area < $area_of_chosen
Truth table100%4/4
not(A) or B or C
A= not $chosenB= $needed_enlargement < $needed_enlargement_of_chosenC= $area < $area_of_chosen
ABCresult
1XX1
01X1
0011
0000
348 $needed_enlargement < $needed_enlargement_of_chosen or
349 $area < $area_of_chosen)
350 {
351 16 $chosen = $entry;
352 16 $needed_enlargement_of_chosen = $needed_enlargement;
353 16 $area_of_chosen = $area;
354 }
355 }
356 # CS3
357 9 $N = $chosen;
358 9 goto CS2;
359 }
360 }
361  
362 3 ▶sub QuadraticSplit {
Subroutine QuadraticSplit: called CC=8 SCAR=26.3
Pod undocumented
363 3 my($self,$group) = @_;
364 3 my($E1,$E2) = PickSeeds($group);
365 3 $E2 = splice(@$group,$E2,1);
366 3 $E1 = splice(@$group,$E1,1);
367 3 $E1 = [1,[$E1],@$E1[2..5]];
368 3 $E2 = [1,[$E2],@$E2[2..5]];
369 do {
370 6 DistributeEntry($group,$E1,$E2);
371 } until @$group == 0 or
372 @$E1 == $self->{M}-$self->{m}+1 or
373 3 ▶ @$E2 == $self->{M}-$self->{m}+1;
Branch: 1 branch
BranchTrueFalseTotal
(@$group == 0 or @$E1 == $self->{"M"} - $self->{"m"} + 1 or @$E2 == $self->{"M"} - $self->{"m"} + 1) or do { do { DistributeEntry($group, $E1, $E2) } } 3 3 6
Condition66%4/6
@$group == 0 or @$E1 == $self->{"M"} - $self->{"m"} + 1 or @$E2 == $self->{"M"} - $self->{"m"} + 1
l!l&&r!l&&!r
303
@$group == 0 or @$E1 == $self->{"M"} - $self->{"m"} + 1
l!l&&r!l&&!r
303
MC/DC33%1/3
@$group == 0 or @$E1 == $self->{"M"} - $self->{"m"} + 1 or @$E2 == $self->{"M"} - $self->{"m"} + 1
@$group == 0 @$E1 == $self->{"M"} - $self->{"m"} + 1 @$E2 == $self->{"M"} - $self->{"m"} + 1
Truth table50%2/4
A or B or C
A= @$group == 0B= @$E1 == $self->{"M"} - $self->{"m"} + 1C= @$E2 == $self->{"M"} - $self->{"m"} + 1
ABCresult
1XX1
01X1
0011
0000
374 3 ▶ unless (@$group == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$group == 0) 0 3 3
375 0 ▶ if (@$E1 < @$E2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$E1 < @$E2) { } 0 0 0
376 0 ▶ while (@$group > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$group > 1) 0 0 0
377 0 add_to_group($E1,pop @$group);
378 }
379 } else {
380 0 ▶ while (@$group > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$group > 1) 0 0 0
381 0 add_to_group($E2,pop @$group);
382 }
383 }
384 }
385 3 push @$group,($E1,$E2);
386 }
387  
388 3 ▶sub PickSeeds {
Subroutine PickSeeds: called CC=5 SCAR=16.2
Pod undocumented
389 3 my($group) = @_;
390 3 my ($seed1,$seed2,$d,$e1);
391 3 ▶ for ($e1 = 0; $e1 < @$group-1; $e1++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e1 < @$group - 1) 9 3 12
392 9 my @rect1 = @{$group->[$e1]}[2..5];
393 9 my $a1 = area_of_rect(@rect1);
394 9 my $e2;
395 9 ▶ for ($e2 = $e1+1; $e2 < @$group; $e2++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e2 < @$group) 18 9 27
396 18 my @rect2 = @{$group->[$e2]}[2..5];
397 18 my @R = enlarged_rect(@rect1,@rect2);
398 18 my $d_test = area_of_rect(@R) - $a1 - area_of_rect(@rect2);
399 18 ▶ if (!$d or $d_test > $d) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $d or $d_test > $d) 18 0 18
Condition33%1/3
not $d or $d_test > $d
l!l&&r!l&&!r
1800
MC/DC0%0/2
not $d or $d_test > $d
not $d $d_test > $d
Truth table33%1/3
not(A) or B
A= not $dB= $d_test > $d
ABresult
1X1
011
000
400 18 $seed1 = min($e1,$e2);
401 18 $seed2 = max($e1,$e2);
402 }
403 }
404 }
405 3 return ($seed1,$seed2);
406 }
407  
408 6 ▶sub DistributeEntry {
Subroutine DistributeEntry: called CC=5 SCAR=16.7
Pod undocumented
409 6 my($from,$to1,$to2) = @_;
410 6 my $area_of_to1 = area_of_rect(@$to1[2..5]);
411 6 my $area_of_to2 = area_of_rect(@$to2[2..5]);
412 6 my ($next,$area_of_enlarged1,$area_of_enlarged2) =
413 PickNext($from,$to1,$to2,$area_of_to1,$area_of_to2);
414 6 my $cmp = $area_of_enlarged1 - $area_of_to1 <=> $area_of_enlarged2 - $area_of_to2;
415 6 ▶ $cmp = $area_of_to1 <=> $area_of_to2 if $cmp == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp == 0 0 6 6
416 0 ▶ $cmp = @{$to1->[1]} <=> @{$to2->[1]} if $cmp == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp == 0 0 6 6
417 6 ▶ if ($cmp <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp <= 0) { } 2 4 6
elsif ($cmp > 0) { } 4 0 4
418 2 add_to_group($to1,$from->[$next]);
419 2 splice(@$from,$next,1);
420 } elsif ($cmp > 0) {
421 4 add_to_group($to2,$from->[$next]);
422 4 splice(@$from,$next,1);
423 }
424 }
425  
426 6 ▶sub PickNext {
Subroutine PickNext: called CC=6 SCAR=18.0
Pod undocumented
427 6 my($from,$to1,$to2,$area_of_to1,$area_of_to2) = @_;
428 6 my $next;
429 my $max_diff;
430 6 my $area_of_enlarged1;
431 6 my $area_of_enlarged2;
432 6 my @cover_of_to1 = @$to1[2..5];
433 6 my @cover_of_to2 = @$to2[2..5];
434 6 for my $i (0..$#$from) {
435 9 my $a1 = area_of_rect(enlarged_rect(@cover_of_to1,@{$from->[$i]}[2..5]));
436 9 ▶ $area_of_enlarged1 = $a1 unless defined $area_of_enlarged1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $area_of_enlarged1 6 3 9
437 9 my $a2 = area_of_rect(enlarged_rect(@cover_of_to2,@{$from->[$i]}[2..5]));
438 9 ▶ $area_of_enlarged2 = $a2 unless defined $area_of_enlarged2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $area_of_enlarged2 6 3 9
439 9 my $diff = abs(($area_of_enlarged1 - $area_of_to1) - ($area_of_enlarged2 - $area_of_to2));
440 9 ▶ if (!$next or $diff > $max_diff) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $next or $diff > $max_diff) 9 0 9
Condition33%1/3
not $next or $diff > $max_diff
l!l&&r!l&&!r
900
MC/DC0%0/2
not $next or $diff > $max_diff
not $next $diff > $max_diff
Truth table33%1/3
not(A) or B
A= not $nextB= $diff > $max_diff
ABresult
1X1
011
000
441 9 $next = $i;
442 9 $max_diff = $diff;
443 9 $area_of_enlarged1 = $a1;
444 9 $area_of_enlarged2 = $a2;
445 }
446 }
447 6 return ($next,$area_of_enlarged1,$area_of_enlarged2);
448 }
449  
450 6 ▶sub add_to_group {
Subroutine add_to_group: called CC=1 SCAR=0.0
Pod undocumented
451 6 my($to,$entry) = @_;
452 6 push @{$to->[1]},$entry;
453 6 @$to[2..5] = enlarged_rect(@$to[2..5],@$entry[2..5]);
454 }
455  
456 111 ▶sub enlarged_rect {
Subroutine enlarged_rect: called CC=1 SCAR=0.0
Pod undocumented
457 111 return (min($_[0],$_[4]),min($_[1],$_[5]),max($_[2],$_[6]),max($_[3],$_[7]));
458 }
459  
460 111 ▶sub area_of_rect {
Subroutine area_of_rect: called CC=1 SCAR=0.0
Pod undocumented
461 111 ($_[3]-$_[1])*($_[2]-$_[0]);
462 }
463  
464 240 ▶sub min {
Subroutine min: called CC=2 SCAR=6.9
Pod undocumented
465 240 ▶ $_[0] > $_[1] ? $_[1] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $_[1] ? : 84 156 240
466 }
467  
468 240 ▶sub max {
Subroutine max: called CC=2 SCAR=6.9
Pod undocumented
469 240 ▶ $_[0] > $_[1] ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $_[1] ? : 118 122 240
470 }
471  
472 1 1;
473 __END__
↑ Summary