blib/lib/Math/Vector/Real/kdTree.pm

filter: Statementstmt 57.7% 454 / 786 Branchbran 42.5% 155 / 364 Conditioncond 10.8% 10 / 92 MC/DCmcdc 5.2% 3 / 57 Subroutinesub 66.2% 49 / 74 Podpod 71.4% 20 / 28 totaltotal 49.3% 691 / 1401 CC 250 SCAR 41.7
CC
250
Cov
50%
k_means_seed_pp
342.0
_find_nearest_vector_in_box
240.0
_find_nearest_vector_in_box_chebyshev
240.0
CRAP 64.6
↑ Summary
1 package Math::Vector::Real::kdTree;
2  
3 3 our $VERSION = '0.15';
4  
5 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Math::Vector::Real;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Sort::Key::Top qw(nkeypartref nhead ntail nkeyhead);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Hash::Util::FieldHash qw(idhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $max_per_pole = 12;
15 3 our $recommended_per_pole = 6;
16  
17 3 ▶use constant _n => 0; # elements on subtree
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use constant _c0 => 1; # corner 0
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use constant _c1 => 2; # corner 1
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use constant _sum => 3; # centroid * n
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use constant _s0 => 4; # subtree 0
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use constant _s1 => 5; # subtree 1
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use constant _axis => 6; # cut axis
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use constant _cut => 7; # cut point (mediam)
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # on leaf nodes:
27 3 ▶use constant _ixs => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use constant _leaf_size => _ixs + 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 683 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
31 683 my $class = shift;
32 683 my @v = map V(@$_), @_;
33 683 ▶ my $self = { vs => \@v,
Branch: 1 branch
BranchTrueFalseTotal
@v ? : 642 41 683
34 tree => (@v ? _build(\@v, [0..$#v]) : undef) };
35 683 bless $self, $class;
36 }
37  
38 0 ▶sub clone {
Subroutine clone: not called CC=2 SCAR=17.9
Pod documented
39 0 my $self = shift;
40 0 require Storable;
41 0 my $clone = { vs => [@{$self->{vs}}],
42 0 tree => Storable::dclone($self->{tree}) };
43 0 ▶ $clone->{hidden} = { %{$self->{hidden}} } if $self->{hidden};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hidden"} 0 0 0
44 0 bless $clone, ref $self;
45 }
46  
47 38559 ▶sub _build {
Subroutine _build: called CC=2 SCAR=6.9
48 38559 my ($v, $ixs) = @_;
49 38559 ▶ if (@$ixs > $recommended_per_pole) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ixs > $Math::Vector::Real::kdTree::recommended_per_pole) { } 18748 19811 38559
50 18748 my ($b, $t) = Math::Vector::Real->box(@$v[@$ixs]);
51 18748 my $axis = ($t - $b)->max_component_index;
52 18748 my $bstart = @$ixs >> 1;
53 18748 ▶ my ($p0, $p1) = nkeypartref { $v->[$_][$axis] } $bstart => @$ixs;
Subroutine __ANON__: called CC=1 SCAR=0.0
54 18748 my $s0 = _build($v, $p0);
55 18748 my $s1 = _build($v, $p1);
56 18748 my ($c0, $c1) = Math::Vector::Real->box(@{$s0}[_c0, _c1], @{$s1}[_c0, _c1]);
57 18748 my $cut = 0.5 * ($s0->[_c1][$axis] + $s1->[_c0][$axis]);
58 # warn "b: $b, t: $t, axis: $axis, p0: $p0, p1: $p1, s0: $s0, s1: $s1, c0: $c0, c1: $c1, cut: $cut\n";
59 # [n sum s0 s1 axis cut]
60 18748 [scalar(@$ixs), $c0, $c1, $s0->[_sum] + $s1->[_sum], $s0, $s1, $axis, $cut];
61 }
62 else {
63 # [n, sum, ixs]
64 19811 my @vs = @{$v}[@$ixs];
65 19811 my ($c0, $c1) = Math::Vector::Real->box(@vs);
66 19811 [scalar(@$ixs), $c0, $c1, Math::Vector::Real->sum(@vs), $ixs];
67 }
68 }
69  
70 0 ▶sub size { scalar @{shift->{vs}} }
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
71  
72 38976 ▶sub at {
Subroutine at: called CC=1 SCAR=0.0
Pod documented
73 38976 my ($self, $ix) = @_;
74 38976 Math::Vector::Real::clone($self->{vs}[$ix]);
75 }
76  
77 6536 ▶sub insert {
Subroutine insert: called CC=4 SCAR=13.9
Pod documented
78 6536 my $self = shift;
79 6536 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 6536 0 6536
80 6536 my $vs = $self->{vs};
81 6536 my $ix = @$vs;
82 6536 ▶ if (my $tree = $self->{tree}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tree = $self->{'tree'}) { } 6495 41 6536
83 6495 for (@_) {
84 6495 my $v = V(@$_);
85 6495 push @$vs, $v;
86 6495 _insert($vs, $self->{tree}, $#$vs)
87 }
88 }
89 else {
90 41 @$vs = map V(@$_), @_;
91 41 $self->{tree} = _build($vs, [0..$#$vs]);
92 }
93 6536 return $ix;
94 }
95  
96 # _insert does not return anything but modifies its $t argument in
97 # place. This is really ugly but done to improve performance.
98  
99 35128 ▶sub _insert {
Subroutine _insert: called CC=6 SCAR=17.9
100 35128 my ($vs, $t, $ix) = @_;
101 35128 my $v = $vs->[$ix];
102  
103 # update aggregated values
104 35128 my $n = $t->[_n]++;
105 35128 @{$t}[_c0, _c1] = Math::Vector::Real->box($v, @{$t}[_c0, _c1]);
106 35128 $t->[_sum] += $v;
107  
108 35128 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 28679 6449 35128
109 28679 my $cut = $t->[_cut];
110 28679 my $c = $v->[$axis];
111  
112 28679 my $n0 = $t->[_s0][_n];
113 28679 my $n1 = $t->[_s1][_n];
114  
115 28679 ▶ if ($c <= $cut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c <= $cut) { } 14590 14089 28679
116 14590 ▶ if (2 * $n1 + $max_per_pole >= $n0) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $n1 + $Math::Vector::Real::kdTree::max_per_pole >= $n0) 14569 21 14590
117 14569 _insert($vs, $t->[_s0], $ix);
118 14569 return;
119 }
120 }
121 else {
122 14089 ▶ if (2 * $n0 + $max_per_pole >= $n1) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 * $n0 + $Math::Vector::Real::kdTree::max_per_pole >= $n1) 14064 25 14089
123 14064 _insert($vs, $t->[_s1], $ix);
124 14064 return;
125 }
126 }
127  
128 # tree needs rebalancing
129 46 my @store;
130 46 $#store = $n; # preallocate space
131 46 @store = ($ix);
132 46 _push_all($t, \@store);
133 46 $_[1] = _build($vs, \@store);
134 }
135 else {
136 6449 my $ixs = $t->[_ixs];
137 6449 push @$ixs, $ix;
138 6449 ▶ if ($n > $max_per_pole) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $Math::Vector::Real::kdTree::max_per_pole) 334 6115 6449
139 334 $_[1] = _build($vs, $ixs);
140 }
141 }
142 }
143  
144 0 ▶sub move {
Subroutine move: not called CC=3 SCAR=24.8
Pod documented
145 0 my ($self, $ix, $v) = @_;
146 0 my $vs = $self->{vs};
147 0 ▶ ($ix >= 0 and $ix < @$vs) or croak "index out of range";
Branch: 1 branch
BranchTrueFalseTotal
$ix >= 0 and $ix < @$vs or croak("index out of range") 0 0 0
Condition0%0/3
$ix >= 0 and $ix < @$vs
!ll&&!rl&&r
000
MC/DC0%0/2
$ix >= 0 and $ix < @$vs
$ix >= 0 $ix < @$vs
Truth table0%0/3
A and B
A= $ix >= 0B= $ix < @$vs
ABresult
0X0
100
111
148 0 _delete($vs, $self->{tree}, $ix);
149 0 $vs->[$ix] = Math::Vector::Real::clone($v);
150 0 _insert($vs, $self->{tree}, $ix);
151 }
152  
153 0 ▶sub _delete {
Subroutine _delete: not called CC=10 SCAR=47.0
154 0 my ($vs, $t, $ix) = @_;
155 0 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 0 0 0
156 0 my $v = $vs->[$ix];
157 0 my $c = $v->[$axis];
158 0 my ($s0, $s1, $cut) = @{$t}[_s0, _s1, _cut];
159 0 ▶ if ($c <= $cut and _delete($vs, $s0, $ix)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c <= $cut and _delete($vs, $s0, $ix)) { } 0 0 0
elsif ($c >= $cut and _delete($vs, $s1, $ix)) { } 0 0 0
Condition0%0/3
$c <= $cut and _delete($vs, $s0, $ix)
!ll&&!rl&&r
000
MC/DC0%0/2
$c <= $cut and _delete($vs, $s0, $ix)
$c <= $cut _delete($vs, $s0, $ix)
Truth table0%0/3
A and B
A= $c <= $cutB= _delete($vs, $s0, $ix)
ABresult
0X0
100
111
160 0 ▶ if ($s0->[_n]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s0->[0]) { } 0 0 0
161 0 $t->[_n]--;
162 0 $t->[_sum] -= $v;
163 }
164 else {
165 # when one subnode becomes empty, the other gets promoted up:
166 0 @$t = @$s1;
167 }
168 0 return 1;
169 }
170 ▶ elsif ($c >= $cut and _delete($vs, $s1, $ix)) {
Condition0%0/3
$c >= $cut and _delete($vs, $s1, $ix)
!ll&&!rl&&r
000
MC/DC0%0/2
$c >= $cut and _delete($vs, $s1, $ix)
$c >= $cut _delete($vs, $s1, $ix)
Truth table0%0/3
A and B
A= $c >= $cutB= _delete($vs, $s1, $ix)
ABresult
0X0
100
111
171 0 ▶ if ($s1->[_n]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s1->[0]) { } 0 0 0
172 0 $t->[_n]--;
173 0 $t->[_sum] -= $v;
174 }
175 else {
176 0 @$t = @$s0;
177 }
178 0 return 1;
179 }
180 }
181 else {
182 0 my $ixs = $t->[_ixs];
183 0 for (0..$#$ixs) {
184 0 ▶ if ($ixs->[$_] == $ix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ixs->[$_] == $ix) 0 0 0
185 0 splice(@$ixs, $_, 1);
186 0 $t->[_n]--;
187 0 $t->[_sum] -= $vs->[$ix];
188 0 return 1;
189 }
190 }
191 }
192 0 return 0;
193 }
194  
195 0 ▶sub hide {
Subroutine hide: not called CC=4 SCAR=30.0
Pod undocumented
196 0 my ($self, $ix) = @_;
197 0 my $vs = $self->{vs};
198 0 ▶ ($ix >= 0 and $ix < @$vs) or croak "index out of range";
Branch: 1 branch
BranchTrueFalseTotal
$ix >= 0 and $ix < @$vs or croak("index out of range") 0 0 0
Condition0%0/3
$ix >= 0 and $ix < @$vs
!ll&&!rl&&r
000
MC/DC0%0/2
$ix >= 0 and $ix < @$vs
$ix >= 0 $ix < @$vs
Truth table0%0/3
A and B
A= $ix >= 0B= $ix < @$vs
ABresult
0X0
100
111
199 0 _delete($vs, $self->{tree}, $ix);
200 0 ▶ ($self->{hidden} //= {})->{$ix} = 1;
Condition0%0/2
$self->{"hidden"} //= {}
l!l
00
MC/DC0%0/1
$self->{"hidden"} //= {}
$self->{"hidden"}
Truth table0%0/2
A
A= $self->{"hidden"}
Aresult
11
00
201 }
202  
203 46 ▶sub _push_all {
Subroutine _push_all: called CC=3 SCAR=11.0
204 46 my ($t, $store) = @_;
205 46 my @q;
206 46 ▶ while ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 722 46 768
207 722 ▶ if (defined $t->[_axis]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->[6]) { } 338 384 722
208 338 push @q, $t->[_s1];
209 338 $t = $t->[_s0];
210 }
211 else {
212 384 push @$store, @{$t->[_ixs]};
213 384 $t = pop @q;
214 }
215 }
216 }
217  
218 0 ▶sub path {
Subroutine path: not called CC=2 SCAR=17.9
Pod undocumented
219 0 my ($self, $ix) = @_;
220 0 my $p = _path($self->{vs}, $self->{tree}, $ix);
221 0 my $l = 1;
222 0 $l = (($l << 1) | $_) for @$p;
223 0 $l
224 }
225  
226 0 ▶sub _path {
Subroutine _path: not called CC=7 SCAR=40.3
227 0 my ($vs, $t, $ix) = @_;
228 0 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 0 0 0
229 0 my $v = $vs->[$ix];
230 0 my $c = $v->[$axis];
231 0 my $cut = $t->[_cut];
232 0 my $p;
233 0 ▶ if ($c <= $cut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c <= $cut) 0 0 0
234 0 ▶ if ($p = _path($vs, $t->[_s0], $ix)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p = _path($vs, $t->[4], $ix)) 0 0 0
235 0 unshift @$p, 0;
236 0 return $p;
237 }
238 }
239 0 ▶ if ($c >= $cut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c >= $cut) 0 0 0
240 0 ▶ if ($p = _path($vs, $t->[_s1], $ix)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p = _path($vs, $t->[5], $ix)) 0 0 0
241 0 unshift @$p, 1;
242 0 return $p;
243 }
244 }
245 }
246 else {
247 0 ▶ return [] if grep $_ == $ix, @{$t->[_ixs]}
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_ == $ix), @{$$t[4];}) 0 0 0
248 }
249 ()
250 0 }
251  
252 0 ▶sub find {
Subroutine find: not called CC=1 SCAR=6.9
Pod undocumented
253 0 my ($self, $v) = @_;
254 0 _find($self->{vs}, $self->{tree}, $v);
255 }
256  
257 0 ▶sub _find {
Subroutine _find: not called CC=7 SCAR=40.3
258 0 my ($vs, $t, $v) = @_;
259 0 ▶ while (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) 0 0 0
260 0 my $cut = $t->[_cut];
261 0 my $c = $v->[$axis];
262 0 ▶ if ($c < $cut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $cut) { } 0 0 0
263 0 $t = $t->[_s0];
264 }
265 else {
266 0 ▶ if ($c == $cut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c == $cut) 0 0 0
267 0 my $ix = _find($vs, $t->[_s0], $v);
268 0 ▶ return $ix if defined $ix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ix 0 0 0
269 }
270 0 $t = $t->[_s1];
271 }
272 }
273  
274 0 for (@{$t->[_ixs]}) {
275 0 ▶ return $_ if $vs->[$_] == $v;
Branch: 1 branch
BranchTrueFalseTotal
if $vs->[$_] == $v 0 0 0
276 }
277 ()
278 0 }
279  
280 67728 ▶sub find_nearest_vector {
Subroutine find_nearest_vector: called CC=8 SCAR=22.3
Pod documented
281 67728 my ($self, $v, $d, @but) = @_;
282 67728 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 67728 0 67728
283 67728 my $vs = $self->{vs};
284 67728 ▶ my $d2 = (defined $d ? $d * $d : 'inf');
Branch: 1 branch
BranchTrueFalseTotal
defined $d ? : 0 67728 67728
285  
286 67728 my $but;
287 67728 ▶ if (@but) {
Branch: 1 branch
BranchTrueFalseTotal
if (@but) 12992 54736 67728
288 12992 ▶ if (@but == 1 and ref $but[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@but == 1 and ref $but[0] eq 'HASH') { } 0 12992 12992
Condition33%1/3
@but == 1 and ref $but[0] eq "HASH"
!ll&&!rl&&r
0129920
MC/DC0%0/2
@but == 1 and ref $but[0] eq "HASH"
@but == 1 ref $but[0] eq "HASH"
Truth table33%1/3
A and B
A= @but == 1B= ref $but[0] eq "HASH"
ABresult
0X0
100
111
289 0 $but = $but[0];
290 }
291 else {
292 12992 my %but = map { $_ => 1 } @but;
293 12992 $but = \%but;
294 }
295 }
296  
297 67728 my ($rix, $rd2) = _find_nearest_vector($vs, $t, $v, $d2, undef, $but);
298 67728 ▶ $rix // return;
Condition50%1/2
$rix // (return)
l!l
677280
MC/DC0%0/1
$rix // (return)
$rix
Truth table50%1/2
A
A= $rix
Aresult
11
00
299 67728 ▶ wantarray ? ($rix, sqrt($rd2)) : $rix;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 67728 67728
300 }
301  
302 3 *find_nearest_neighbor = \&find_nearest_vector; # for backwards compatibility
303  
304 12992 ▶sub find_nearest_vector_internal {
Subroutine find_nearest_vector_internal: called CC=2 SCAR=7.1
Pod undocumented
305 12992 my ($self, $ix, $d) = @_;
306 12992 ▶ $ix >= 0 or croak "index out of range";
Branch: 1 branch
BranchTrueFalseTotal
$ix >= 0 or croak("index out of range") 12992 0 12992
307 12992 $self->find_nearest_vector($self->{vs}[$ix], $d, $ix);
308 }
309  
310 3 *find_nearest_neighbor_internal = \&find_nearest_vector_internal; # for backwards compatibility
311  
312 92539 ▶sub _find_nearest_vector {
Subroutine _find_nearest_vector: called CC=12 SCAR=24.8
313 92539 my ($vs, $t, $v, $best_d2, $best_ix, $but) = @_;
314  
315 92539 my @queue;
316 my @queue_d2;
317  
318 92539 while (1) {
319 984632 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 629205 355427 984632
320 # substitute the current one by the best subtree and queue
321 # the worst for later
322 629205 ▶ ($t, my ($q)) = @{$t}[($v->[$axis] <= $t->[_cut]) ? (_s0, _s1) : (_s1, _s0)];
Branch: 1 branch
BranchTrueFalseTotal
$v->[$axis] <= $t->[7] ? : 326715 302490 629205
323 629205 my $q_d2 = $v->dist2_to_box(@{$q}[_c0, _c1]);
324 629205 ▶ if ($q_d2 <= $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q_d2 <= $best_d2) 406670 222535 629205
325 406670 my $j;
326 406670 ▶ for ($j = $#queue_d2; $j >= 0; $j--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 1151490 130669 1282159
327 1151490 ▶ last if $queue_d2[$j] >= $q_d2;
Branch: 1 branch
BranchTrueFalseTotal
if $queue_d2[$j] >= $q_d2 276001 875489 1151490
328 }
329 406670 splice @queue, ++$j, 0, $q;
330 406670 splice @queue_d2, $j, 0, $q_d2;
331 }
332 }
333 else {
334 355427 for (@{$t->[_ixs]}) {
335 1676740 ▶ next if $but and $but->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $but and $but->{$_} 12992 1663748 1676740
Condition100%3/3
$but and $but->{$_}
!ll&&!rl&&r
98009068365812992
MC/DC100%2/2
$but and $but->{$_}
$but $but->{$_}
Truth table100%3/3
A and B
A= $butB= $but->{$_}
ABresult
0X0
100
111
336 1663748 my $d21 = $vs->[$_]->dist2($v);
337 1663748 ▶ if ($d21 <= $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d21 <= $best_d2) 263307 1400441 1663748
338 263307 $best_d2 = $d21;
339 263307 $best_ix = $_;
340 }
341 }
342  
343 355427 ▶ if ($t = pop @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t = pop @queue) 306368 49059 355427
344 306368 ▶ if ($best_d2 >= pop @queue_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_d2 >= pop @queue_d2) 262888 43480 306368
345 262888 next;
346 }
347 }
348  
349 92539 return ($best_ix, $best_d2);
350 }
351 }
352 }
353  
354 0 ▶sub find_nearest_vector_in_box {
Subroutine find_nearest_vector_in_box: not called CC=8 SCAR=42.8
Pod documented
355 0 my ($self, $v, $a, $b, $d, @but) = @_;
356  
357 0 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 0 0 0
358 0 my $vs = $self->{vs};
359 0 my ($a1, $b1) = Math::Vector::Real->box($a, $b);
360 0 ▶ my $d2 = (defined $d ? $d * $d : $v->max_dist2_to_box($a1, $b1));
Branch: 1 branch
BranchTrueFalseTotal
defined $d ? : 0 0 0
361 0 my $but;
362 0 ▶ if (@but) {
Branch: 1 branch
BranchTrueFalseTotal
if (@but) 0 0 0
363 0 ▶ if (@but == 1 and ref $but[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@but == 1 and ref $but[0] eq 'HASH') { } 0 0 0
Condition0%0/3
@but == 1 and ref $but[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@but == 1 and ref $but[0] eq "HASH"
@but == 1 ref $but[0] eq "HASH"
Truth table0%0/3
A and B
A= @but == 1B= ref $but[0] eq "HASH"
ABresult
0X0
100
111
364 0 $but = $but[0];
365 }
366 else {
367 0 my %but = map { $_ => 1 } @but;
368 0 $but = \%but;
369 }
370 }
371 0 my ($rix, $rd2) = _find_nearest_vector_in_box($vs, $t, $v, $a1, $b1, $d2, $but);
372 0 ▶ $rix // return;
Condition0%0/2
$rix // (return)
l!l
00
MC/DC0%0/1
$rix // (return)
$rix
Truth table0%0/2
A
A= $rix
Aresult
11
00
373 0 ▶ wantarray ? ($rix, sqrt($rd2)) : $rix;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
374 }
375  
376 0 ▶sub _find_nearest_vector_in_box {
Subroutine _find_nearest_vector_in_box: not called CC=15 SCAR=54.8
377 0 my ($vs, $t, $v, $a, $b, $best_d2, $but) = @_;
378 0 my $best_ix;
379 0 my @queue = $t;
380 0 my @queue_d2 = 0;
381  
382 0 ▶ while (my $t = pop @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = pop @queue) 0 0 0
383 0 ▶ last if $best_d2 < pop @queue_d2;
Branch: 1 branch
BranchTrueFalseTotal
if $best_d2 < pop @queue_d2 0 0 0
384 0 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 0 0 0
385 0 my @sides;
386 0 ▶ push @sides, $t->[_s0] if $a->[$axis] <= $t->[_cut];
Branch: 1 branch
BranchTrueFalseTotal
if $a->[$axis] <= $t->[7] 0 0 0
387 0 ▶ push @sides, $t->[_s1] if $b->[$axis] >= $t->[_cut];
Branch: 1 branch
BranchTrueFalseTotal
if $b->[$axis] >= $t->[7] 0 0 0
388 0 for my $s (@sides) {
389 0 my $d2 = $v->dist2_to_box(@$s[_c0, _c1]);
390 0 ▶ if ($d2 <= $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 <= $best_d2) 0 0 0
391 0 my $j;
392 0 ▶ for ($j = $#queue_d2; $j >= 0; $j--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 0 0 0
393 0 ▶ last if $queue_d2[$j] >= $d2;
Branch: 1 branch
BranchTrueFalseTotal
if $queue_d2[$j] >= $d2 0 0 0
394 }
395 0 splice @queue, ++$j, 0, $s;
396 0 splice @queue_d2, $j, 0, $d2;
397 }
398 }
399 }
400 else {
401 0 for (@{$t->[_ixs]}) {
402 0 ▶ next if $but and $but->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $but and $but->{$_} 0 0 0
Condition0%0/3
$but and $but->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$but and $but->{$_}
$but $but->{$_}
Truth table0%0/3
A and B
A= $butB= $but->{$_}
ABresult
0X0
100
111
403 0 my $v1 = $vs->[$_];
404 0 my $d2 = $v1->dist2($v);
405 0 ▶ if ($d2 <= $best_d2 and $v1->dist2_to_box($a, $b) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 <= $best_d2 and $v1->dist2_to_box($a, $b) == 0) 0 0 0
Condition0%0/3
$d2 <= $best_d2 and $v1->dist2_to_box($a, $b) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$d2 <= $best_d2 and $v1->dist2_to_box($a, $b) == 0
$d2 <= $best_d2 $v1->dist2_to_box($a, $b) == 0
Truth table0%0/3
A and B
A= $d2 <= $best_d2B= $v1->dist2_to_box($a, $b) == 0
ABresult
0X0
100
111
406 0 $best_d2 = $d2;
407 0 $best_ix = $_;
408 }
409 }
410 }
411 }
412 0 return ($best_ix, $best_d2);
413 }
414  
415 0 ▶sub find_nearest_vector_in_box_chebyshev {
Subroutine find_nearest_vector_in_box_chebyshev: not called CC=8 SCAR=42.8
Pod documented
416 0 my ($self, $v, $a, $b, $d, @but) = @_;
417  
418 0 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 0 0 0
419 0 my $vs = $self->{vs};
420 0 my ($a1, $b1) = Math::Vector::Real->box($a, $b);
421 0 ▶ my $d2 = (defined $d ? $d * $d : $v->max_dist2_to_box($a1, $b1));
Branch: 1 branch
BranchTrueFalseTotal
defined $d ? : 0 0 0
422 0 my $but;
423 0 ▶ if (@but) {
Branch: 1 branch
BranchTrueFalseTotal
if (@but) 0 0 0
424 0 ▶ if (@but == 1 and ref $but[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@but == 1 and ref $but[0] eq 'HASH') { } 0 0 0
Condition0%0/3
@but == 1 and ref $but[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@but == 1 and ref $but[0] eq "HASH"
@but == 1 ref $but[0] eq "HASH"
Truth table0%0/3
A and B
A= @but == 1B= ref $but[0] eq "HASH"
ABresult
0X0
100
111
425 0 $but = $but[0];
426 }
427 else {
428 0 my %but = map { $_ => 1 } @but;
429 0 $but = \%but;
430 }
431 }
432 0 my ($rix, $rd2) = _find_nearest_vector_in_box($vs, $t, $v, $a1, $b1, $d2, $but);
433 0 ▶ $rix // return;
Condition0%0/2
$rix // (return)
l!l
00
MC/DC0%0/1
$rix // (return)
$rix
Truth table0%0/2
A
A= $rix
Aresult
11
00
434 0 ▶ wantarray ? ($rix, sqrt($rd2)) : $rix;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
435 }
436  
437 0 ▶sub _find_nearest_vector_in_box_chebyshev {
Subroutine _find_nearest_vector_in_box_chebyshev: not called CC=15 SCAR=54.8
438 0 my ($vs, $t, $v, $a, $b, $best_d, $but) = @_;
439 0 my $best_ix;
440 0 my @queue = $t;
441 0 my @queue_d = 0;
442  
443 0 ▶ while (my $t = pop @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = pop @queue) 0 0 0
444 0 ▶ last if $best_d < pop @queue_d;
Branch: 1 branch
BranchTrueFalseTotal
if $best_d < pop @queue_d 0 0 0
445 0 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 0 0 0
446 0 my @sides;
447 0 ▶ push @sides, $t->[_s0] if $a->[$axis] <= $t->[_cut];
Branch: 1 branch
BranchTrueFalseTotal
if $a->[$axis] <= $t->[7] 0 0 0
448 0 ▶ push @sides, $t->[_s1] if $b->[$axis] >= $t->[_cut];
Branch: 1 branch
BranchTrueFalseTotal
if $b->[$axis] >= $t->[7] 0 0 0
449 0 for my $s (@sides) {
450 0 my $d = $v->chebyshev_dist_to_box(@$s[_c0, _c1]);
451 0 ▶ if ($d <= $best_d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d <= $best_d) 0 0 0
452 0 my $j;
453 0 ▶ for ($j = $#queue_d; $j >= 0; $j--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 0 0 0
454 0 ▶ last if $queue_d[$j] >= $d;
Branch: 1 branch
BranchTrueFalseTotal
if $queue_d[$j] >= $d 0 0 0
455 }
456 0 splice @queue, ++$j, 0, $s;
457 0 splice @queue_d, $j, 0, $d;
458 }
459 }
460 }
461 else {
462 0 for (@{$t->[_ixs]}) {
463 0 ▶ next if $but and $but->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $but and $but->{$_} 0 0 0
Condition0%0/3
$but and $but->{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$but and $but->{$_}
$but $but->{$_}
Truth table0%0/3
A and B
A= $butB= $but->{$_}
ABresult
0X0
100
111
464 0 my $v1 = $vs->[$_];
465 0 my $d = $v1->chebyshev_dist($v);
466 0 ▶ if ($d <= $best_d and $v1->chebyshev_dist_to_box($a, $b) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d <= $best_d and $v1->chebyshev_dist_to_box($a, $b) == 0) 0 0 0
Condition0%0/3
$d <= $best_d and $v1->chebyshev_dist_to_box($a, $b) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$d <= $best_d and $v1->chebyshev_dist_to_box($a, $b) == 0
$d <= $best_d $v1->chebyshev_dist_to_box($a, $b) == 0
Truth table0%0/3
A and B
A= $d <= $best_dB= $v1->chebyshev_dist_to_box($a, $b) == 0
ABresult
0X0
100
111
467 0 $best_d = $d;
468 0 $best_ix = $_;
469 }
470 }
471 }
472 }
473 0 return ($best_ix, $best_d);
474 }
475  
476 80 ▶sub find_nearest_vector_all_internal {
Subroutine find_nearest_vector_all_internal: called CC=3 SCAR=11.1
Pod documented
477 80 my ($self, $d) = @_;
478 80 my $vs = $self->{vs};
479 80 ▶ return unless @$vs > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$vs > 1 0 80 80
480 80 ▶ my $d2 = (defined $d ? $d * $d : 'inf');
Branch: 1 branch
BranchTrueFalseTotal
defined $d ? : 0 80 80
481  
482 80 my @best = ((undef) x @$vs);
483 80 my @d2 = (($d2) x @$vs);
484 80 _find_nearest_vector_all_internal($vs, $self->{tree}, \@best, \@d2);
485 80 return @best;
486 }
487  
488 3 *find_nearest_neighbor_all_internal = \&find_nearest_vector_all_internal; # for backwards compatibility
489  
490 5470 ▶sub _find_nearest_vector_all_internal {
Subroutine _find_nearest_vector_all_internal: called CC=10 SCAR=23.0
491 5470 my ($vs, $t, $bests, $d2s) = @_;
492 5470 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 2695 2775 5470
493 2695 my @all_leafs;
494 2695 for my $side (0, 1) {
495 5390 my @leafs = _find_nearest_vector_all_internal($vs, $t->[_s0 + $side], $bests, $d2s);
496 5390 my $other = $t->[_s1 - $side];
497 5390 my ($c0, $c1) = @{$other}[_c0, _c1];
498 5390 for my $leaf (@leafs) {
499 17408 for my $ix (@{$leaf->[_ixs]}) {
500 79612 my $v = $vs->[$ix];
501 79612 ▶ if ($v->dist2_to_box($c0, $c1) < $d2s->[$ix]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->dist2_to_box($c0, $c1) < $d2s->[$ix]) 24811 54801 79612
502 24811 ($bests->[$ix], $d2s->[$ix]) =
503 _find_nearest_vector($vs, $other, $v, $d2s->[$ix], $bests->[$ix]);
504 }
505 }
506 }
507 5390 push @all_leafs, @leafs;
508 }
509 2695 return @all_leafs;
510 }
511 else {
512 2775 my $ixs = $t->[_ixs];
513 2775 for my $i (1 .. $#$ixs) {
514 10217 my $ix_i = $ixs->[$i];
515 10217 my $v_i = $vs->[$ix_i];
516 10217 for my $ix_j (@{$ixs}[0 .. $i - 1]) {
517 29091 my $d2 = $v_i->dist2($vs->[$ix_j]);
518 29091 ▶ if ($d2 < $d2s->[$ix_i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < $d2s->[$ix_i]) 15769 13322 29091
519 15769 $d2s->[$ix_i] = $d2;
520 15769 $bests->[$ix_i] = $ix_j;
521 }
522 29091 ▶ if ($d2 < $d2s->[$ix_j]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < $d2s->[$ix_j]) 8689 20402 29091
523 8689 $d2s->[$ix_j] = $d2;
524 8689 $bests->[$ix_j] = $ix_i;
525 }
526 }
527 }
528 2775 return $t;
529 }
530 }
531  
532 418 ▶sub find_two_nearest_vectors {
Subroutine find_two_nearest_vectors: called CC=4 SCAR=15.2
Pod documented
533 418 my $self = shift;
534 418 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 418 0 418
535 418 my $vs = $self->{vs};
536 418 ▶ if (my ($rix0, $rix1, $rd2) = _find_two_nearest_vectors($vs, $t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = _find_two_nearest_vectors($vs, $t)) 418 0 418
537 418 ▶ return wantarray ? ($rix0, $rix1, sqrt($rd2)) : sqrt($rd2)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 418 0 418
538 }
539 ()
540 0 }
541  
542 0 ▶sub _pole_id {
Subroutine _pole_id: not called CC=1 SCAR=6.9
543 0 my ($id, $deep) = __pole_id(@_);
544 0 "$id/$deep";
545 }
546  
547 0 ▶sub __pole_id {
Subroutine __pole_id: not called CC=2 SCAR=17.9
548 0 my ($vs, $t) = @_;
549 0 ▶ if (defined $t->[_axis]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->[6]) 0 0 0
550 0 my ($id, $deep) = __pole_id($vs, $t->[_s0]);
551 0 return ($id, $deep+1);
552 }
553 0 return ($t->[_ixs][0], 0)
554 }
555  
556 418 ▶sub _find_two_nearest_vectors {
Subroutine _find_two_nearest_vectors: called CC=16 SCAR=27.7
557 418 my ($vs, $t) = @_;
558  
559 418 my @best_ixs = (undef, undef);
560 418 my $best_d2 = 'inf' + 0;
561  
562 418 my @inner;
563 my @queue_t1;
564 418 my @queue_t2;
565 418 ▶ while ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 31000 418 31418
566 31000 ▶ if (defined $t->[_axis]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->[6]) { } 15291 15709 31000
567 15291 my ($s0, $s1) = @{$t}[_s0, _s1];
568 15291 push @inner, $s1;
569 15291 push @queue_t1, $s0;
570 15291 push @queue_t2, $s1;
571 15291 $t = $s0;
572 }
573 else {
574 15709 my $ixs = $t->[_ixs];
575 15709 for my $i (1 .. $#$ixs) {
576 52254 my $ix1 = $ixs->[$i];
577 52254 my $v1 = $vs->[$ix1];
578 52254 for my $j (0 .. $i - 1) {
579 122406 my $ix2 = $ixs->[$j];
580 122406 my $d2 = Math::Vector::Real::dist2($v1, $vs->[$ix2]);
581 122406 ▶ if ($d2 < $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < $best_d2) 1935 120471 122406
582 1935 $best_d2 = $d2;
583 1935 @best_ixs = ($ix1, $ix2);
584 }
585 }
586 }
587 15709 $t = pop @inner;
588 }
589 }
590  
591 418 my @queue_d2 = (0) x @queue_t1;
592 418 ▶ while (my $t1 = pop @queue_t1) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t1 = pop @queue_t1) 132744 418 133162
593 132744 my $t2 = pop @queue_t2;
594 132744 my $d2 = pop @queue_d2;
595 132744 ▶ if ($d2 < $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < $best_d2) 129144 3600 132744
596 129144 ▶ unless (defined $t1->[_axis]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t1->[6]) 41398 87746 129144
597 41398 ▶ unless (defined $t2->[_axis]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t2->[6]) 39180 2218 41398
598 39180 for my $ix1 (@{$t1->[_ixs]}) {
599 179152 my $v1 = $vs->[$ix1];
600 179152 for my $ix2 (@{$t2->[_ixs]}) {
601 822400 my $d2 = Math::Vector::Real::dist2($v1, $vs->[$ix2]);
602 822400 ▶ if ($d2 < $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < $best_d2) 96 822304 822400
603 96 $best_d2 = $d2;
604 96 @best_ixs = ($ix1, $ix2);
605 }
606 }
607 }
608 39180 next;
609 }
610 2218 ($t1, $t2) = ($t2, $t1);
611 }
612 89964 for my $s (@{$t1}[_s0, _s1]) {
613 179928 my $d2 = Math::Vector::Real->dist2_between_boxes(@{$s}[_c0, _c1], @{$t2}[_c0, _c1]);
614 179928 ▶ if ($d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2) { } 170058 9870 179928
615 170058 ▶ if ($d2 < $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < $best_d2) 107583 62475 170058
616 107583 unshift @queue_t1, $t2;
617 107583 unshift @queue_t2, $s;
618 107583 unshift @queue_d2, $d2;
619 }
620 }
621 else {
622 9870 push @queue_t1, $t2;
623 9870 push @queue_t2, $s;
624 9870 push @queue_d2, 0;
625 }
626 }
627 }
628 }
629 418 (@best_ixs, $best_d2)
630 }
631  
632 6496 ▶sub find_in_ball {
Subroutine find_in_ball: called CC=3 SCAR=12.0
Pod documented
633 6496 my ($self, $z, $d, $but) = @_;
634 6496 ▶ if (defined $but and ref $but ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $but and ref $but ne "HASH") 6496 0 6496
Condition33%1/3
defined $but and ref $but ne "HASH"
!ll&&!rl&&r
006496
MC/DC0%0/2
defined $but and ref $but ne "HASH"
defined $but ref $but ne "HASH"
Truth table33%1/3
A and B
A= defined $butB= ref $but ne "HASH"
ABresult
0X0
100
111
635 6496 $but = { $but => 1 };
636 }
637 6496 _find_in_ball($self->{vs}, $self->{tree}, $z, $d * $d, $but);
638 }
639  
640 6496 ▶sub _find_in_ball {
Subroutine _find_in_ball: called CC=10 SCAR=25.5
641 6496 my ($vs, $t, $z, $d2, $but) = @_;
642 6496 my (@queue, @r);
643 6496 my $r = 0;
644  
645 6496 while (1) {
646 285359 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 161674 123685 285359
647 161674 my $c = $z->[$axis];
648 161674 my $cut = $t->[_cut];
649 161674 ▶ ($t, my ($q)) = @{$t}[$c <= $cut ? (_s0, _s1) : (_s1, _s0)];
Branch: 1 branch
BranchTrueFalseTotal
$c <= $cut ? : 82931 78743 161674
650 161674 ▶ push @queue, $q if $z->dist2_to_box(@{$q}[_c0, _c1]) <= $d2;
Branch: 1 branch
BranchTrueFalseTotal
if $z->dist2_to_box(@{$q;}[1, 2]) <= $d2 117189 44485 161674
651 }
652 else {
653 123685 my $ixs = $t->[_ixs];
654 123685 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 123685 0 123685
655 123685 push @r, grep { $vs->[$_]->dist2($z) <= $d2 } @$ixs;
656 }
657 else {
658 $r += ( $but
659 0 ▶ ? grep { !$but->{$_} and $vs->[$_]->dist2($z) <= $d2 } @$ixs
Condition0%0/3
!$but->{$_} && $vs->[$_]->dist2($z) <= $d2
!ll&&!rl&&r
000
MC/DC0%0/2
!$but->{$_} && $vs->[$_]->dist2($z) <= $d2
!$but->{$_} $vs->[$_]->dist2($z) <= $d2
Truth table0%0/3
not(A) && B
A= !$but->{$_}B= $vs->[$_]->dist2($z) <= $d2
ABresult
0X0
100
111
660 0 ▶ : grep { $vs->[$_]->dist2($z) <= $d2 } @$ixs );
Branch: 1 branch
BranchTrueFalseTotal
$but ? : 0 0 0
661 }
662  
663 123685 ▶ $t = pop @queue or last;
Branch: 1 branch
BranchTrueFalseTotal
$t = pop @queue or last 117189 6496 123685
664 }
665 }
666  
667 6496 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 6496 0 6496
668 6496 ▶ if ($but) {
Branch: 1 branch
BranchTrueFalseTotal
if ($but) 6496 0 6496
669 6496 return grep !$but->{$_}, @r;
670 }
671 0 return @r;
672 }
673 0 return $r;
674 }
675  
676 0 ▶sub find_in_box {
Subroutine find_in_box: not called CC=3 SCAR=24.8
Pod documented
677 0 my ($self, $a, $b, $but) = @_;
678 0 my ($a1, $b1) = Math::Vector::Real->box($a, $b);
679 0 ▶ if (defined $but and ref $but ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $but and ref $but ne "HASH") 0 0 0
Condition0%0/3
defined $but and ref $but ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $but and ref $but ne "HASH"
defined $but ref $but ne "HASH"
Truth table0%0/3
A and B
A= defined $butB= ref $but ne "HASH"
ABresult
0X0
100
111
680 0 $but = { $but => 1 };
681 }
682 0 _find_in_box($self->{vs}, $self->{tree}, $a1, $b1, $but);
683 }
684  
685 0 ▶sub _find_in_box {
Subroutine _find_in_box: not called CC=10 SCAR=47.0
686 0 my ($vs, $t, $a, $b, $but) = @_;
687 0 my (@r, $r);
688 0 my @queue;
689 0 while (1) {
690 0 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 0 0 0
691 0 my $cut = $t->[_cut];
692 0 ▶ push @queue, $t->[_s0] if $cut >= $a->[$axis];
Branch: 1 branch
BranchTrueFalseTotal
if $cut >= $a->[$axis] 0 0 0
693 0 ▶ push @queue, $t->[_s1] if $cut <= $b->[$axis];
Branch: 1 branch
BranchTrueFalseTotal
if $cut <= $b->[$axis] 0 0 0
694 }
695 else {
696 0 my $ixs = $t->[_ixs];
697 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
698 0 push @r, grep { $vs->[$_]->dist2_to_box($a, $b) <= 0 } @$ixs;
699 }
700 else {
701 $r += ( $but
702 0 ▶ ? grep { !$but->{$_} and $vs->[$_]->dist2_to_box($a, $b) <= 0 } @$ixs
Condition0%0/3
!$but->{$_} && $vs->[$_]->dist2_to_box($a, $b) <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
!$but->{$_} && $vs->[$_]->dist2_to_box($a, $b) <= 0
!$but->{$_} $vs->[$_]->dist2_to_box($a, $b) <= 0
Truth table0%0/3
not(A) && B
A= !$but->{$_}B= $vs->[$_]->dist2_to_box($a, $b) <= 0
ABresult
0X0
100
111
703 0 ▶ : grep { $vs->[$_]->dist2_to_box($a, $b) <= 0 } @$ixs );
Branch: 1 branch
BranchTrueFalseTotal
$but ? : 0 0 0
704 }
705 }
706 0 ▶ $t = pop @queue or last;
Branch: 1 branch
BranchTrueFalseTotal
$t = pop @queue or last 0 0 0
707 }
708  
709 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
710 0 ▶ if ($but) {
Branch: 1 branch
BranchTrueFalseTotal
if ($but) 0 0 0
711 0 return grep !$but->{$_}, @r;
712 }
713 0 return @r;
714 }
715 0 return $r;
716 }
717  
718 6496 ▶sub find_farthest_vector {
Subroutine find_farthest_vector: called CC=8 SCAR=22.9
Pod documented
719 6496 my ($self, $v, $d, @but) = @_;
720 6496 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 6496 0 6496
721 6496 my $vs = $self->{vs};
722 6496 ▶ my $d2 = ($d ? $d * $d : -1);
Branch: 1 branch
BranchTrueFalseTotal
$d ? : 0 6496 6496
723 6496 my $but;
724 6496 ▶ if (@but) {
Branch: 1 branch
BranchTrueFalseTotal
if (@but) 6496 0 6496
725 6496 ▶ if (@but == 1 and ref $but[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@but == 1 and ref $but[0] eq 'HASH') { } 0 6496 6496
Condition33%1/3
@but == 1 and ref $but[0] eq "HASH"
!ll&&!rl&&r
064960
MC/DC0%0/2
@but == 1 and ref $but[0] eq "HASH"
@but == 1 ref $but[0] eq "HASH"
Truth table33%1/3
A and B
A= @but == 1B= ref $but[0] eq "HASH"
ABresult
0X0
100
111
726 0 $but = $but[0];
727 }
728 else {
729 6496 my %but = map { $_ => 1 } @but;
730 6496 $but = \%but;
731 }
732 }
733  
734 6496 my ($rix, $rd2) = _find_farthest_vector($vs, $t, $v, $d2, undef, $but);
735 6496 ▶ $rix // return;
Condition50%1/2
$rix // (return)
l!l
64960
MC/DC0%0/1
$rix // (return)
$rix
Truth table50%1/2
A
A= $rix
Aresult
11
00
736 6496 ▶ wantarray ? ($rix, sqrt($d2)) : $rix;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6496 6496
737 }
738  
739 6496 ▶sub find_farthest_vector_internal {
Subroutine find_farthest_vector_internal: called CC=2 SCAR=7.1
Pod documented
740 6496 my ($self, $ix, $d) = @_;
741 6496 ▶ $ix >= 0 or croak "index out of range";
Branch: 1 branch
BranchTrueFalseTotal
$ix >= 0 or croak("index out of range") 6496 0 6496
742 6496 $self->find_farthest_vector($self->{vs}[$ix], $d, $ix);
743 }
744  
745 6496 ▶sub _find_farthest_vector {
Subroutine _find_farthest_vector: called CC=12 SCAR=24.9
746 6496 my ($vs, $t, $v, $best_d2, $best_ix, $but) = @_;
747  
748 6496 my @queue;
749 my @queue_d2;
750  
751 6496 while (1) {
752 191906 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 123549 68357 191906
753 # substitute the current one by the best subtree and queue
754 # the worst for later
755 123549 ▶ ($t, my ($q)) = @{$t}[($v->[$axis] >= $t->[_cut]) ? (_s0, _s1) : (_s1, _s0)];
Branch: 1 branch
BranchTrueFalseTotal
$v->[$axis] >= $t->[7] ? : 67125 56424 123549
756 123549 my $q_d2 = $v->max_dist2_to_box(@{$q}[_c0, _c1]);
757 123549 ▶ if ($q_d2 >= $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q_d2 >= $best_d2) 87561 35988 123549
758 87561 my $j;
759 87561 ▶ for ($j = $#queue_d2; $j >= 0; $j--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 466514 17930 484444
760 466514 ▶ last if $queue_d2[$j] <= $q_d2;
Branch: 1 branch
BranchTrueFalseTotal
if $queue_d2[$j] <= $q_d2 69631 396883 466514
761 }
762 87561 splice @queue, ++$j, 0, $q;
763 87561 splice @queue_d2, $j, 0, $q_d2;
764 }
765 }
766 else {
767 68357 for (@{$t->[_ixs]}) {
768 456270 ▶ next if $but and $but->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $but and $but->{$_} 215 456055 456270
Condition66%2/3
$but and $but->{$_}
!ll&&!rl&&r
0456055215
MC/DC50%1/2
$but and $but->{$_}
$but $but->{$_}
Truth table66%2/3
A and B
A= $butB= $but->{$_}
ABresult
0X0
100
111
769 456055 my $d21 = $vs->[$_]->dist2($v);
770 456055 ▶ if ($d21 >= $best_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d21 >= $best_d2) 55837 400218 456055
771 55837 $best_d2 = $d21;
772 55837 $best_ix = $_;
773 }
774 }
775  
776 68357 ▶ if ($t = pop @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t = pop @queue) 67333 1024 68357
777 67333 ▶ if ($best_d2 <= pop @queue_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_d2 <= pop @queue_d2) 61861 5472 67333
778 61861 next;
779 }
780 }
781 6496 return ($best_ix, $best_d2);
782 }
783 }
784 }
785  
786 0 ▶sub find_random_vector {
Subroutine find_random_vector: not called CC=6 SCAR=37.4
Pod undocumented
787 0 my $self = shift;
788 0 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 0 0 0
789 0 my $vs = $self->{vs};
790 0 my $hidden = $self->{hidden};
791 0 ▶ if (not $hidden or @$vs > 20 * keys(%$hidden)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hidden or @$vs > 20 * keys(%$hidden)) 0 0 0
Condition0%0/3
not $hidden or @$vs > 20 * keys(%$hidden)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $hidden or @$vs > 20 * keys(%$hidden)
not $hidden @$vs > 20 * keys(%$hidden)
Truth table0%0/3
not(A) or B
A= not $hiddenB= @$vs > 20 * keys(%$hidden)
ABresult
1X1
011
000
792 # pick directly when the hidden elements are less than 5% of the total
793 0 while (1) {
794 0 my $ix = int rand @$vs;
795 0 ▶ return $ix unless $hidden and $hidden->{$ix};
Branch: 1 branch
BranchTrueFalseTotal
unless $hidden and $hidden->{$ix} 0 0 0
Condition0%0/3
$hidden and $hidden->{$ix}
!ll&&!rl&&r
000
MC/DC0%0/2
$hidden and $hidden->{$ix}
$hidden $hidden->{$ix}
Truth table0%0/3
A and B
A= $hiddenB= $hidden->{$ix}
ABresult
0X0
100
111
796 }
797 }
798 0 _find_random_vector($vs, $t);
799 }
800  
801 0 ▶sub _find_random_vector {
Subroutine _find_random_vector: not called CC=3 SCAR=24.8
802 0 my ($vs, $t) = @_;
803 0 ▶ while (defined $t->[_axis]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->[6]) 0 0 0
804 0 ▶ $t = $t->[rand($t->[_n]) < $t->[_s0][_n] ? _s0 : _s1];
Branch: 1 branch
BranchTrueFalseTotal
rand $t->[0] < $t->[4][0] ? : 0 0 0
805 }
806 0 $t->[_ixs][rand $t->[_n]]
807 }
808  
809 224 ▶sub k_means_seed {
Subroutine k_means_seed: called CC=3 SCAR=11.3
Pod documented
810 224 my ($self, $n_req) = @_;
811 224 ▶ $n_req = int($n_req) or return;
Branch: 1 branch
BranchTrueFalseTotal
$n_req = int $n_req or return 224 0 224
812 224 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 224 0 224
813 224 my $vs = $self->{vs};
814 224 _k_means_seed($vs, $t, $n_req);
815 }
816  
817 3 *k_means_start = \&k_means_seed;
818  
819 7536 ▶sub _k_means_seed {
Subroutine _k_means_seed: called CC=7 SCAR=19.5
820 7536 my ($vs, $t, $n_req) = @_;
821 7536 ▶ if ($n_req <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_req <= 1) { } 2312 5224 7536
822 2312 ▶ return if $n_req < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n_req < 1 28 2284 2312
823 # print STDERR "returning centroid\n";
824 2284 return $t->[_sum] / $t->[_n];
825 }
826 else {
827 5224 my $n = $t->[_n];
828 5224 ▶ if (defined $t->[_axis]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->[6]) { } 3656 1568 5224
829 3656 my ($s0, $s1) = @{$t}[_s0, _s1];
830 3656 my $n0 = $s0->[_n];
831 3656 my $n1 = $s1->[_n];
832 3656 my $n0_req = int(0.5 + $n_req * ($n0 / $n));
833 3656 ▶ $n0_req = $n0 if $n0_req > $n0;
Branch: 1 branch
BranchTrueFalseTotal
if $n0_req > $n0 0 3656 3656
834 3656 return (_k_means_seed($vs, $s0, $n0_req),
835 _k_means_seed($vs, $s1, $n_req - $n0_req));
836 }
837 else {
838 1568 my $ixs = $t->[_ixs];
839 1568 my @out;
840 1568 for (0..$#$ixs) {
841 10675 ▶ push @out, $vs->[$ixs->[$_]]
Branch: 1 branch
BranchTrueFalseTotal
if rand $n - $_ < $n_req - @out 4476 6199 10675
842 if rand($n - $_) < ($n_req - @out);
843 }
844 # print STDERR "asked for $n_req elements, returning ".scalar(@out)."\n";
845  
846 1568 return @out;
847 }
848 }
849 }
850  
851 3 our $k_means_seed_pp_test;
852  
853 0 ▶sub _k_means_seed_pp_test {
Subroutine _k_means_seed_pp_test: not called CC=7 SCAR=40.3
854 0 my ($self, $err, $kms, $players, $weights) = @_;
855 0 my @w;
856 0 my $last = 0;
857 0 for my $i (0..$#$players) {
858 0 my $p = $players->[$i];
859 0 my $w = $weights->[$i] - $last;
860 0 $last = $weights->[$i];
861  
862 0 my @store;
863 0 ▶ if (ref $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p) { } 0 0 0
864 0 _push_all($p, \@store);
865 }
866 else {
867 0 @store = $p
868 }
869 0 ▶ if (@store) {
Branch: 1 branch
BranchTrueFalseTotal
if (@store) 0 0 0
870 0 $w /= @store;
871 0 $w[$_] = $w for @store;
872 }
873 }
874 0 my $vs = $self->{vs};
875 0 ▶ $w[$_] //= 0 for 0..$#$vs;
Condition0%0/2
$w[$_] //= 0
l!l
00
MC/DC0%0/1
$w[$_] //= 0
$w[$_]
Truth table0%0/2
A
A= $w[$_]
Aresult
11
00
876  
877 0 $k_means_seed_pp_test->($self, $err, [map $self->{vs}[$_], @$kms], \@w);
878 }
879  
880 0 ▶sub k_means_seed_pp {
Subroutine k_means_seed_pp: not called CC=18 SCAR=58.3
Pod undocumented
881 0 my ($self, $n_req, $err) = @_;
882 0 ▶ $n_req = int($n_req) or return;
Branch: 1 branch
BranchTrueFalseTotal
$n_req = int $n_req or return 0 0 0
883 0 ▶ $err ||= 0.5;
Condition0%0/2
$err ||= "0.5"
l!l
00
MC/DC0%0/1
$err ||= "0.5"
$err
Truth table0%0/2
A
A= $err
Aresult
11
00
884 0 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 0 0 0
885 0 my $vs = $self->{vs};
886 0 my $km = $self->find_random_vector;
887  
888 0 my (@km, @d2);
889 0 idhash my %extra; # [$min_d2, $max_d2]
890  
891 # my (@player, @weight, @queue);
892 # $#player = @$vs; # preallocate memory
893  
894 0 my (@weight, @queue);
895 0 $#weight = @$vs; # preallocate memory
896  
897 0 while (1) {
898 0 push @km, $km;
899 0 ▶ last unless @km < $n_req;
Branch: 1 branch
BranchTrueFalseTotal
unless @km < $n_req 0 0 0
900  
901 # update distances
902 0 @queue = $t;
903 0 ▶ while (my $p = pop @queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = pop @queue) 0 0 0
904 0 my $kmv = $vs->[$km];
905 0 my ($c0, $c1) = @{$p}[_c0, _c1];
906 0 ▶ my $extra = $extra{$p} //= ['inf', 'inf'];
Condition0%0/2
$extra{$p} //= ["inf", "inf"]
l!l
00
MC/DC0%0/1
$extra{$p} //= ["inf", "inf"]
$extra{$p}
Truth table0%0/2
A
A= $extra{$p}
Aresult
11
00
907 0 my ($min_d2, $max_d2) = @$extra;
908 0 my $min_d2_to_box = $kmv->dist2_to_box($c0, $c1);
909 0 ▶ if ($max_d2 > $min_d2_to_box) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_d2 > $min_d2_to_box) 0 0 0
910 0 ▶ if (defined $p->[_axis]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->[6]) { } 0 0 0
911 0 push @queue, @{$p}[_s0, _s1];
912 }
913 else {
914 0 for (@{$p->[_ixs]}) {
915 0 my $d2 = $kmv->dist2($vs->[$_]);
916 0 ▶ if ($d2 < ($d2[$_] //= $d2)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < ($d2[$_] //= $d2)) 0 0 0
Condition0%0/3
$d2[$_] //= $d2
l!l&&r!l&&!r
000
MC/DC0%0/2
$d2[$_] //= $d2
$d2[$_] $d2
Truth table0%0/3
A //= B
A= $d2[$_]B= $d2
ABresult
1X1
011
000
917 0 $d2[$_] = $d2;
918 }
919 }
920 }
921  
922 0 ▶ if ($min_d2_to_box < $min_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_d2_to_box < $min_d2) 0 0 0
923 0 $extra->[0] = $min_d2_to_box;
924 }
925  
926 0 my $max_d2_to_box = $kmv->max_dist2_to_box($c0, $c1);
927 0 ▶ if ($max_d2_to_box < $max_d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_d2_to_box < $max_d2) 0 0 0
928 0 $extra->[1] = $max_d2_to_box;
929 }
930 }
931 }
932  
933 # find players and weight them
934 0 my $weight = 0;
935 # @player = ();
936 0 @weight = ();
937  
938 # @queue = $t;
939 # while (my $p = pop @queue) {
940 # my $extra = $extra{$p} or die "internal error: extra information missing for $p";
941 # my ($min_d2, $max_d2) = @$extra;
942  
943 # if ($max_d2 * $err < $min_d2) {
944 # $weight += $p->[_n] * ($min_d2 + $max_d2) * 0.5;
945 # push @weight, $weight;
946 # push @player, $p;
947 # }
948 # else {
949 # if (defined $p->[_axis]) {
950 # push @queue, @{$p}[_s0, _s1];
951 # }
952 # else {
953 # for (@{$p->[_ixs]}) {
954 # if (my $d2 = $d2[$_]) {
955 # $weight += $d2;
956 # push @weight, $weight;
957 # push @player, $_;
958 # }
959 # }
960 # }
961 # }
962 # }
963  
964 0 for my $ix (0..@$vs) {
965 0 ▶ $weight += $d2[$ix] // 0;
Condition0%0/2
$d2[$ix] // 0
l!l
00
MC/DC0%0/1
$d2[$ix] // 0
$d2[$ix]
Truth table0%0/2
A
A= $d2[$ix]
Aresult
11
00
966 0 $weight[$ix] += $weight;
967 }
968  
969 # in order to check the algorithm we have to tap it here
970 # $k_means_seed_pp_test and @km > 1 and
971 # $self->_k_means_seed_pp_test($err, \@km, \@player, \@weight);
972  
973 # to many k-means requested?
974 # @player or last;
975  
976 # select a position on the weight queue:
977 0 my $dice = rand($weight);
978  
979 # and use binary search to look for it:
980 0 my $i = 0;
981 0 my $j = @weight;
982 0 ▶ while ($i < $j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $j) 0 0 0
983 0 my $pivot = (($i + $j) >> 1);
984 0 ▶ if ($weight[$pivot] < $dice) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weight[$pivot] < $dice) { } 0 0 0
985 0 $i = $pivot + 1;
986 }
987 else {
988 0 $j = $pivot;
989 }
990 }
991 #my $player = $player[$i];
992 #$km = (ref $player ? _find_random_vector($vs, $player) : $player);
993 0 $km = $i;
994 }
995 0 return @{$vs}[@km];
996 }
997  
998 224 ▶sub k_means_loop {
Subroutine k_means_loop: called CC=8 SCAR=20.9
Pod documented
999 224 my ($self, @k) = @_;
1000 224 ▶ @k or next;
Branch: 1 branch
BranchTrueFalseTotal
@k or next 224 0 224
1001 224 ▶ my $t = $self->{tree} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or next 224 0 224
1002 224 my $vs = $self->{vs};
1003 224 while (1) {
1004 1120 my $diffs;
1005 1120 my @n = ((0) x @k);
1006 1120 my @sum = ((undef) x @k);
1007  
1008 1120 _k_means_step($vs, $t, \@k, [0..$#k], \@n, \@sum);
1009  
1010 1120 for (0..$#k) {
1011 33612 ▶ if (my $n = $n[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $n[$_]) 32014 1598 33612
1012 32014 my $k = $sum[$_] / $n;
1013 32014 ▶ $diffs++ if $k != $k[$_];
Branch: 1 branch
BranchTrueFalseTotal
if $k != $k[$_] 9051 22963 32014
1014 32014 $k[$_] = $k;
1015 }
1016 }
1017 1120 ▶ unless ($diffs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($diffs) 224 896 1120
1018 224 ▶ return (wantarray ? @k : $k[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 224 0 224
1019 }
1020 }
1021 }
1022  
1023 0 ▶sub k_means_step {
Subroutine k_means_step: not called CC=6 SCAR=37.4
Pod documented
1024 0 my $self = shift;
1025 0 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 0 0 0
1026 0 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 0 0 0
1027 0 my $vs = $self->{vs};
1028  
1029 0 my @n = ((0) x @_);
1030 0 my @sum = ((undef) x @_);
1031  
1032 0 _k_means_step($vs, $t, \@_, [0..$#_], \@n, \@sum);
1033  
1034 0 for (0..$#n) {
1035 0 ▶ if (my $n = $n[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $n[$_]) { } 0 0 0
1036 0 $sum[$_] /= $n;
1037 }
1038 else {
1039 # otherwise let the original value stay
1040 0 $sum[$_] = $_[$_];
1041 }
1042 }
1043 0 ▶ wantarray ? @sum : $sum[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1044 }
1045  
1046 107412 ▶sub _k_means_step {
Subroutine _k_means_step: called CC=7 SCAR=19.5
1047 107412 my ($vs, $t, $centers, $cixs, $ns, $sums) = @_;
1048 107412 my ($n, $sum, $c0, $c1) = @{$t}[_n, _sum, _c0, _c1];
1049 107412 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 107412 0 107412
1050 107412 my $centroid = $sum/$n;
1051 107412 ▶ my $best = nkeyhead { $centroid->dist2($centers->[$_]) } @$cixs;
Subroutine __ANON__: called CC=1 SCAR=0.0
1052 107412 my $max_d2 = Math::Vector::Real::max_dist2_to_box($centers->[$best], $c0, $c1);
1053 107412 my @down = grep { Math::Vector::Real::dist2_to_box($centers->[$_], $c0, $c1) <= $max_d2 } @$cixs;
1054 107412 ▶ if (@down <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@down <= 1) { } 9708 97704 107412
1055 9708 $ns->[$best] += $n;
1056 # FIXME: M::V::R objects should support this undef + vector logic natively!
1057 9708 ▶ if (defined $sums->[$best]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sums->[$best]) { } 8027 1681 9708
1058 8027 $sums->[$best] += $sum;
1059 }
1060 else {
1061 1681 $sums->[$best] = V(@$sum);
1062 }
1063 }
1064 else {
1065 97704 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 53146 44558 97704
1066 53146 my ($s0, $s1) = @{$t}[_s0, _s1];
1067 53146 _k_means_step($vs, $t->[_s0], $centers, \@down, $ns, $sums);
1068 53146 _k_means_step($vs, $t->[_s1], $centers, \@down, $ns, $sums);
1069 }
1070 else {
1071 44558 for my $ix (@{$t->[_ixs]}) {
1072 278593 my $v = $vs->[$ix];
1073 278593 ▶ my $best = nkeyhead { $v->dist2($centers->[$_]) } @down;
Subroutine __ANON__: called CC=1 SCAR=0.0
1074 278593 $ns->[$best]++;
1075 278593 ▶ if (defined $sums->[$best]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sums->[$best]) { } 248260 30333 278593
1076 248260 $sums->[$best] += $v;
1077 }
1078 else {
1079 30333 $sums->[$best] = V(@$v);
1080 }
1081 }
1082 }
1083 }
1084 }
1085 }
1086  
1087 224 ▶sub k_means_assign {
Subroutine k_means_assign: called CC=3 SCAR=11.2
Pod documented
1088 224 my $self = shift;
1089 224 ▶ @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return 224 0 224
1090 224 ▶ my $t = $self->{tree} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->{"tree"} or return 224 0 224
1091 224 my $vs = $self->{vs};
1092  
1093 224 my @out = ((undef) x @$vs);
1094 224 _k_means_assign($vs, $t, \@_, [0..$#_], \@out);
1095 224 @out;
1096 }
1097  
1098 13844 ▶sub _k_means_assign {
Subroutine _k_means_assign: called CC=5 SCAR=16.1
1099 13844 my ($vs, $t, $centers, $cixs, $outs) = @_;
1100 13844 my ($n, $sum, $c0, $c1) = @{$t}[_n, _sum, _c0, _c1];
1101 13844 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 13844 0 13844
1102 13844 my $centroid = $sum/$n;
1103 13844 ▶ my $best = nkeyhead { $centroid->dist2($centers->[$_]) } @$cixs;
Subroutine __ANON__: called CC=1 SCAR=0.0
1104 13844 my $max_d2 = Math::Vector::Real::max_dist2_to_box($centers->[$best], $c0, $c1);
1105 13844 my @down = grep { Math::Vector::Real::dist2_to_box($centers->[$_], $c0, $c1) <= $max_d2 } @$cixs;
1106 13844 ▶ if (@down <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@down <= 1) { } 1443 12401 13844
1107 1443 _k_means_assign_1($t, $best, $outs);
1108 }
1109 else {
1110 12401 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 6810 5591 12401
1111 6810 my ($s0, $s1) = @{$t}[_s0, _s1];
1112 6810 _k_means_assign($vs, $t->[_s0], $centers, \@down, $outs);
1113 6810 _k_means_assign($vs, $t->[_s1], $centers, \@down, $outs);
1114 }
1115 else {
1116 5591 for my $ix (@{$t->[_ixs]}) {
1117 33908 my $v = $vs->[$ix];
1118 33908 ▶ my $best = nkeyhead { $v->dist2($centers->[$_]) } @down;
Subroutine __ANON__: called CC=1 SCAR=0.0
1119 33908 $outs->[$ix] = $best;
1120 }
1121 }
1122 }
1123 }
1124 }
1125  
1126 6411 ▶sub _k_means_assign_1 {
Subroutine _k_means_assign_1: called CC=3 SCAR=11.0
1127 6411 my ($t, $best, $outs) = @_;
1128 6411 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 2484 3927 6411
1129 2484 _k_means_assign_1($t->[_s0], $best, $outs);
1130 2484 _k_means_assign_1($t->[_s1], $best, $outs);
1131 }
1132 else {
1133 3927 $outs->[$_] = $best for @{$t->[_ixs]};
1134 }
1135 }
1136  
1137 6536 ▶sub ordered_by_proximity {
Subroutine ordered_by_proximity: called CC=1 SCAR=0.0
Pod documented
1138 6536 my $self = shift;
1139 6536 my @r;
1140 6536 $#r = $#{$self->{vs}}; $#r = -1; # preallocate
1141 6536 _ordered_by_proximity($self->{tree}, \@r);
1142 6536 return @r;
1143 }
1144  
1145 424432 ▶sub _ordered_by_proximity {
Subroutine _ordered_by_proximity: called CC=2 SCAR=6.9
1146 424432 my $t = shift;
1147 424432 my $r = shift;
1148 424432 ▶ if (defined $t->[_axis]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->[6]) { } 208948 215484 424432
1149 208948 _ordered_by_proximity($t->[_s0], $r);
1150 208948 _ordered_by_proximity($t->[_s1], $r);
1151 }
1152 else {
1153 215484 push @$r, @{$t->[_ixs]}
1154 }
1155 }
1156  
1157 0 ▶sub _dump_to_string {
Subroutine _dump_to_string: not called CC=9 SCAR=45.0
1158 0 my ($vs, $t, $indent, $opts) = @_;
1159 0 my ($n, $c0, $c1, $sum) = @{$t}[_n, _c0, _c1, _sum];
1160 0 ▶ my $id = ($opts->{pole_id} ? _pole_id($vs, $t)." " : '');
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'pole_id'} ? : 0 0 0
1161 0 ▶ if (defined (my $axis = $t->[_axis])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $axis = $t->[6])) { } 0 0 0
1162 0 my ($s0, $s1, $cut) = @{$t}[_s0, _s1, _cut];
1163 0 return ( "${indent}${id}n: $n, c0: $c0, c1: $c1, sum: $sum, axis: $axis, cut: $cut\n" .
1164 _dump_to_string($vs, $s0, "$indent$opts->{tab}", $opts) .
1165 _dump_to_string($vs, $s1, "$indent$opts->{tab}", $opts) );
1166 }
1167 else {
1168 0 ▶ my $remark = $opts->{remark} // [];
Condition0%0/2
$opts->{"remark"} // []
l!l
00
MC/DC0%0/1
$opts->{"remark"} // []
$opts->{"remark"}
Truth table0%0/2
A
A= $opts->{"remark"}
Aresult
11
00
1169 0 my $o = ( "${indent}${id}n: $n, c0: $c0, c1: $c1, sum: $sum\n" .
1170 "${indent}$opts->{tab}ixs: [" );
1171 0 my @str;
1172 0 for my $ix (@{$t->[_ixs]}) {
1173 0 ▶ my $colored_ix = (@$remark and grep($ix == $_, @$remark)
Branch: 1 branch
BranchTrueFalseTotal
grep(($ix == $_), @$remark) ? : 0 0 0
Condition0%0/3
@$remark && (grep(($ix == $_), @$remark) ? Term::ANSIColor::colored($ix, 'red') : $ix)
!ll&&!rl&&r
000
MC/DC0%0/2
@$remark && (grep(($ix == $_), @$remark) ? Term::ANSIColor::colored($ix, 'red') : $ix)
@$remark (grep(($ix == $_), @$remark) ? Term::ANSIColor::colored($ix, 'red') : $ix)
Truth table0%0/3
A && B
A= @$remarkB= (grep(($ix == $_), @$remark) ? Term::ANSIColor::colored($ix, 'red') : $ix)
ABresult
0X0
100
111
1174 ? Term::ANSIColor::colored($ix, 'red')
1175 : $ix);
1176 0 ▶ if ($opts->{dump_vectors} // 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'dump_vectors'} // 1) { } 0 0 0
Condition0%0/2
$opts->{"dump_vectors"} // 1
l!l
00
MC/DC0%0/1
$opts->{"dump_vectors"} // 1
$opts->{"dump_vectors"}
Truth table0%0/2
A
A= $opts->{"dump_vectors"}
Aresult
11
00
1177 0 push @str, "$colored_ix $vs->[$ix]";
1178 }
1179 else {
1180 0 push @str, $colored_ix;
1181 }
1182 }
1183 0 return $o . join(', ', @str) . "]\n";
1184 }
1185 }
1186  
1187 0 ▶sub dump_to_string {
Subroutine dump_to_string: not called CC=5 SCAR=34.0
Pod undocumented
1188 0 my ($self, %opts) = @_;
1189 0 ▶ my $tab = $opts{tab} //= ' ';
Condition0%0/2
$opts{"tab"} //= " "
l!l
00
MC/DC0%0/1
$opts{"tab"} //= " "
$opts{"tab"}
Truth table0%0/2
A
A= $opts{"tab"}
Aresult
11
00
1190 0 my $vs = $self->{vs};
1191 0 my $nvs = @$vs;
1192 0 ▶ my $hidden = join ", ", keys %{$self->{hidden} || {}};
Condition0%0/2
$self->{'hidden'} || {}
l!l
00
MC/DC0%0/1
$self->{'hidden'} || {}
$self->{'hidden'}
Truth table0%0/2
A
A= $self->{'hidden'}
Aresult
11
00
1193 0 my $o = "tree: n: $nvs, hidden: {$hidden}\n";
1194 0 ▶ if (my $t = $self->{tree}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $self->{'tree'}) { } 0 0 0
1195 0 ▶ require Term::ANSIColor if $opts{remark};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"remark"} 0 0 0
1196 0 return $o . _dump_to_string($vs, $t, $tab, \%opts);
1197 }
1198 else {
1199 0 return "$o${tab}(empty)\n";
1200 }
1201 }
1202  
1203 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
1204 0 my $self = shift;
1205 0 print $self->dump_to_string(@_);
1206 }
1207  
1208 3 1;
1209 __END__
↑ Summary