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; ...@@ -5,7 +5,9 @@ import messif.objects.LocalAbstractObject;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -42,19 +44,11 @@ class InternalNode extends Node implements Serializable { ...@@ -42,19 +44,11 @@ class InternalNode extends Node implements Serializable {
* @return the nearest child to the {@code object} * @return the nearest child to the {@code object}
*/ */
protected Node getNearestChild(LocalAbstractObject object) { protected Node getNearestChild(LocalAbstractObject object) {
Node nearestChild = children.get(0); Optional<Node> nearestChild = children
double minDistance = nearestChild.getDistance(object); .stream()
.min(Comparator.comparing(child -> child.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);
}
}
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