K-d tree: Difference between revisions

Content deleted Content added
Updated first D entry
Updated D entry
Line 222: Line 222:
struct Point(size_t k, F) if (isFloatingPoint!F) {
struct Point(size_t k, F) if (isFloatingPoint!F) {
F[k] data;
F[k] data;
alias data this; // Kills DMD std.algorithm.swap inlining.

// Define opIndexAssign and opIndex for dmd.
// alias data this; // kills DMD std.algorithm.swap inlining
F opIndex(in size_t i) const pure nothrow { return data[i]; }

void opIndexAssign(in F x, in size_t i) pure nothrow {
data[i] = x;
}

enum size_t length = k;
enum size_t length = k;


Line 428: Line 422:
{{out|Output, using the ldc2 compiler}}
{{out|Output, using the ldc2 compiler}}
<pre>Wikipedia example data:
<pre>Wikipedia example data:
Point: const(Point!(2, double))([9, 2])
Point: [9, 2]
Nearest neighbor: immutable(Point!(2, double))([8, 1])
Nearest neighbor: [8, 1]
Distance: 1.41421
Distance: 1.41421
Nodes visited: 3
Nodes visited: 3


k-d tree with 400000 random 3D float points (construction time: 250 ms):
k-d tree with 400000 random 3D float points (construction time: 250 ms):
Point: const(Point!(3, float))([0.22012, 0.984514, 0.698782])
Point: [0.22012, 0.984514, 0.698782]
Nearest neighbor: immutable(Point!(3, float))([0.225766, 0.978981, 0.69885])
Nearest neighbor: [0.225766, 0.978981, 0.69885]
Distance: 0.00790531
Distance: 0.00790531
Nodes visited: 54
Nodes visited: 54