Skip to content
Snippets Groups Projects
Verified Commit a7fed238 authored by David Procházka's avatar David Procházka
Browse files

ADD: improved nearest child retrieval

parent dd40600b
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,9 @@ import messif.objects.LocalAbstractObject;
import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
......@@ -42,19 +44,11 @@ class InternalNode extends Node implements Serializable {
* @return the nearest child to the {@code object}
*/
protected Node getNearestChild(LocalAbstractObject object) {
Node nearestChild = children.get(0);
double minDistance = nearestChild.getDistance(object);
for (int i = 1; i < children.size(); i++) {
double distance = children.get(i).getDistance(object);
if (distance < minDistance) {
minDistance = distance;
nearestChild = children.get(i);
}
}
Optional<Node> nearestChild = children
.stream()
.min(Comparator.comparing(child -> child.getDistance(object)));
return nearestChild;
return nearestChild.orElseThrow(() -> new IllegalStateException("Internal node has no children"));
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment