in hyracks/hyracks-storage-am-rtree/src/main/java/org/apache/hyracks/storage/am/rtree/linearize/HilbertDoubleComparator.java [97:182]
public int compare() {
boolean equal = true;
for (int i = 0; i < dim; i++) {
if (a[i] != b[i])
equal = false;
}
if (equal)
return 0;
// We keep the state of the state machine after a comparison. In most
// cases,
// the needed zoom factor is close to the old one. In this step, we
// check if we have
// to zoom out
while (true) {
if (stateStack.size() <= dim) {
resetStateMachine();
break;
}
boolean zoomOut = false;
for (int i = 0; i < dim; i++) {
if (Math.min(a[i], b[i]) <= bounds[i] - 2 * stepsize
|| Math.max(a[i], b[i]) >= bounds[i] + 2 * stepsize) {
zoomOut = true;
break;
}
}
state = stateStack.getLast();
stateStack.removeLast();
for (int j = dim - 1; j >= 0; j--) {
bounds[j] = boundsStack.getLast();
boundsStack.removeLast();
}
stepsize *= 2;
if (!zoomOut) {
state = stateStack.getLast();
stateStack.removeLast();
for (int j = dim - 1; j >= 0; j--) {
bounds[j] = boundsStack.getLast();
boundsStack.removeLast();
}
stepsize *= 2;
break;
}
}
while (true) {
stateStack.add(state);
for (int j = 0; j < dim; j++) {
boundsStack.add(bounds[j]);
}
// Find the quadrant in which A and B are
int quadrantA = 0, quadrantB = 0;
for (int i = dim - 1; i >= 0; i--) {
if (a[i] >= bounds[i])
quadrantA ^= (1 << (dim - i - 1));
if (b[i] >= bounds[i])
quadrantB ^= (1 << (dim - i - 1));
if (a[i] >= bounds[i]) {
bounds[i] += stepsize;
} else {
bounds[i] -= stepsize;
}
}
stepsize /= 2;
if (stepsize <= 2 * DoublePointable.getEpsilon())
return 0;
// avoid infinite loop due to machine epsilon problems
if (quadrantA != quadrantB) {
// find the position of A and B's quadrants
int posA = states[state].position[quadrantA];
int posB = states[state].position[quadrantB];
if (posA < posB)
return -1;
else
return 1;
}
state = states[state].nextState[quadrantA];
}
}