Skip to content

Commit

Permalink
Power of 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaeschke committed Jul 31, 2024
1 parent 6eaf712 commit 0cf38d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main/java/org/tinspin/index/qthypercube/QuadTreeKD.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.function.Predicate;

import org.tinspin.index.*;
import org.tinspin.index.util.MathTools;
import org.tinspin.index.util.StringBuilderLn;

/**
Expand Down Expand Up @@ -94,7 +95,9 @@ public void insert(double[] key, T value) {
PointEntry<T> e = new PointEntry<>(key, value);
if (root == null) {
// We calculate a better radius when adding a second point.
root = new QNode<>(key.clone(), INITIAL_RADIUS);
// We align the center to a power of two. That reduces precision problems when
// creating subnode centers.
root = new QNode<>(MathTools.floorPowerOfTwoCopy(key), INITIAL_RADIUS);
}
if (root.getRadius() == INITIAL_RADIUS) {
adjustRootSize(key);
Expand All @@ -113,10 +116,17 @@ private void adjustRootSize(double[] key) {
return;
}
if (root.getRadius() == INITIAL_RADIUS) {
// We just use Euclidean here, that should be good enough in all cases.
double dist = PointDistance.L2.dist(key, root.getCenter());
if (dist > 0) {
root.adjustRadius(2 * dist);
// Root size has not been initialized yet.
// We start by getting the maximum horizontal distance between the node center and any point in the node
double dMax = MathTools.maxDelta(key, root.getCenter());
for (int i = 0; i < root.getEntries().size(); i++) {
dMax = Math.max(dMax, MathTools.maxDelta(root.getEntries().get(i).point(), root.getCenter()));
}
// We calculate the minimum required radius that is also a power of two.
// This radius can be divided by 2 many times without precision problems.
double radius = MathTools.ceilPowerOfTwo(dMax + QUtil.EPS_MUL);
if (radius > 0) {
root.adjustRadius(radius);
} else if (root.getEntries().size() >= maxNodeSize - 1) {
// we just set an arbitrary radius here
root.adjustRadius(1000);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/tinspin/index/qthypercube2/QuadTreeKD2.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public void insert(double[] key, T value) {
PointEntry<T> e = new PointEntry<>(key, value);
if (root == null) {
// We calculate a better radius when adding a second point.
// We align the center to a power of two. That reduces precision problems when
// creating subnode centers.
root = new QNode<>(MathTools.floorPowerOfTwoCopy(key), INITIAL_RADIUS);
}
if (root.getRadius() == INITIAL_RADIUS) {
Expand All @@ -128,10 +130,14 @@ private void adjustRootSize(double[] key) {
return;
}
if (root.getRadius() == INITIAL_RADIUS) {
// Root size has not been initialized yet.
// We start by getting the maximum horizontal distance between the node center and any point in the node
double dMax = MathTools.maxDelta(key, root.getCenter());
for (int i = 0; i < root.getValueCount(); i++) {
dMax = Math.max(dMax, MathTools.maxDelta(root.getValues()[i].point(), root.getCenter()));
}
// We calculate the minimum required radius that is also a power of two.
// This radius can be divided by 2 many times without precision problems.
double radius = MathTools.ceilPowerOfTwo(dMax + QUtil.EPS_MUL);
if (radius > 0) {
root.adjustRadius(radius);
Expand Down

0 comments on commit 0cf38d2

Please sign in to comment.