Anonymous user
K-d tree: Difference between revisions
Updated second D entry
(Updated second D entry) |
|||
Line 438:
{{trans|C}}
This version performs less lookups. Compiled with DMD this version is two times slower than the C version. Compiled with ldc2 it's a little faster than the C version compiled with gcc.
<lang d>import std.stdio, std.algorithm, std.math, std.random, std.
enum maxDim = 3;
struct KdNode {
Line 512 ⟶ 505:
ref const(KdNode)* best,
ref double bestDist,
ref size_t nVisited) pure nothrow @safe @nogc {
static double dist(in ref KdNode a, in ref KdNode b)
pure nothrow @nogc {
typeof(KdNode.x[0]) result = 0;
foreach (immutable i;
result += (a.x[i] - b.x[i]) ^^ 2;
return result;
Line 549 ⟶ 542:
void randPt(size_t dim=3)(ref KdNode v, ref Xorshift rng)
pure nothrow @safe @nogc {
foreach (immutable i;
v.x[i] = rng.uniform01;
}
Line 576 ⟶ 569:
auto bigTree = new KdNode[N];
auto rng =
foreach (ref node; bigTree)
randPt(node, rng);
|