Data-SpatialHash-Shared
view release on metacpan or search on metacpan
t/16-pairs.t view on Meta::CPAN
my $maxr = 3;
my %got; $s->each_pair_within($maxr, sub { $got{ key($_[0], $_[1]) }++ });
my %want;
for my $i (0..$#p) { for my $j ($i+1..$#p) {
my $dx = $p[$i][0]-$p[$j][0]; my $dy = $p[$i][1]-$p[$j][1];
$want{ key($p[$i][2], $p[$j][2]) } = 1 if $dx*$dx + $dy*$dy < $maxr*$maxr;
}}
is_deeply { map { $_ => 1 } keys %got }, \%want, 'each_pair_within matches brute force';
ok !(grep { $_ > 1 } values %got), 'each pair emitted exactly once';
# max_r == 0 is documented-valid: fires zero callbacks without croaking
my $n0 = 0; eval { $s->each_pair_within(0, sub { $n0++ }) };
is $@, '', 'each_pair_within(0) does not croak (0 is a valid max_r)';
is $n0, 0, 'each_pair_within(0) fires zero callbacks';
}
# --- each_colliding_pair vs brute force (heterogeneous radii) ---
{
my $s = Data::SpatialHash::Shared->new(undef, 1000, 0, 2.0); # small cell vs big radii
my @q; my $v = 0;
for (1..250) {
my @xy = (rand()*40, rand()*40); my $r = 0.5 + rand()*6;
my $h = $s->insert(@xy, $v); $s->set_radius($h, $r);
push @q, [@xy, $v, $r]; $v++;
( run in 0.675 second using v1.01-cache-2.11-cpan-7fcb06a456a )