in Java/core/src/main/java/com/amazon/randomcutforest/store/PointStore.java [387:444]
public void compact() {
Vector<Integer[]> reverseReference = new Vector<>();
for (int i = 0; i < locationListLength(); i++) {
int locn = getLocation(i);
if (locn < currentStoreCapacity * dimensions && locn >= 0) {
reverseReference.add(new Integer[] { locn, i });
}
}
reverseReference.sort((o1, o2) -> o1[0].compareTo(o2[0]));
int freshStart = 0;
int jStatic = 0;
int jDynamic = 0;
int jEnd = reverseReference.size();
while (jStatic < jEnd) {
int blockStart = reverseReference.get(jStatic)[0];
int blockEnd = blockStart + dimensions;
int initial = 0;
if (rotationEnabled) {
initial = (dimensions - freshStart + blockStart) % dimensions;
}
int k = jStatic + 1;
jDynamic = jStatic + 1;
while (k < jEnd) {
int newElem = reverseReference.get(k)[0];
if (blockEnd >= newElem) {
k += 1;
jDynamic += 1;
blockEnd = max(blockEnd, newElem + dimensions);
} else {
k = jEnd;
}
}
alignBoundaries(initial, freshStart);
freshStart += initial;
int start = freshStart;
for (int i = blockStart; i < blockEnd; i++) {
assert (!rotationEnabled || freshStart % dimensions == i % dimensions);
if (jStatic < jEnd) {
int locn = reverseReference.get(jStatic)[0];
if (i == locn) {
int newIdx = reverseReference.get(jStatic)[1];
setLocation(newIdx, freshStart);
jStatic += 1;
}
}
freshStart += 1;
}
copyTo(start, blockStart, blockEnd - blockStart);
if (jStatic != jDynamic) {
throw new IllegalStateException("There is discepancy in indices");
}
}
startOfFreeSegment = freshStart;
}