From 2bd71c1f40a4c21c51909a9ed1e42795365e6820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Proch=C3=A1zka?= <david@prochazka.dev> Date: Tue, 4 May 2021 20:53:39 +0200 Subject: [PATCH] ADD: utils javadoc, merge type method for getting merging objects --- src/mhtree/MergeType.java | 20 +++++++++++++++++-- src/mhtree/Node.java | 10 +--------- src/mhtree/ObjectToNodeDistance.java | 26 +++++++++---------------- src/mhtree/Utils.java | 29 +++++++++++++++++----------- 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/mhtree/MergeType.java b/src/mhtree/MergeType.java index 30d6ddb..0c7d3dd 100644 --- a/src/mhtree/MergeType.java +++ b/src/mhtree/MergeType.java @@ -1,6 +1,22 @@ package mhtree; +import messif.objects.LocalAbstractObject; + +import java.util.List; + public enum MergeType { - OBJECT_BASED, - REPRESENTATION_BASED, + OBJECT_BASED { + @Override + public List<LocalAbstractObject> getObjects(Node node) { + return node.getObjects(); + } + }, + HULL_BASED { + @Override + public List<LocalAbstractObject> getObjects(Node node) { + return node.getHullObjects(); + } + }; + + abstract public List<LocalAbstractObject> getObjects(Node node); } diff --git a/src/mhtree/Node.java b/src/mhtree/Node.java index cecf5e5..9b9494f 100644 --- a/src/mhtree/Node.java +++ b/src/mhtree/Node.java @@ -33,7 +33,7 @@ public abstract class Node implements Serializable { protected static InternalNode createParent(List<Node> nodes, PrecomputedDistances distances, InsertType insertType, ObjectToNodeDistance objectToNodeDistance, MergeType mergeType) { List<LocalAbstractObject> objects = nodes .stream() - .map(mergeType == MergeType.OBJECT_BASED ? Node::getObjects : Node::getHullObjects) + .map(mergeType::getObjects) .flatMap(Collection::stream) .collect(Collectors.toList()); @@ -76,18 +76,10 @@ public abstract class Node implements Serializable { return objectToNodeDistance.getDistance(object, this, distances); } - protected double getDistanceToNearest(LocalAbstractObject object) { - return ObjectToNodeDistance.NEAREST.getDistance(object, this); - } - protected boolean isLeaf() { return (this instanceof LeafNode); } - protected boolean isInternal() { - return !isLeaf(); - } - protected int getHullObjectCount() { return hull.getRepresentativesCount(); } diff --git a/src/mhtree/ObjectToNodeDistance.java b/src/mhtree/ObjectToNodeDistance.java index 8d11bfd..42fc7c0 100644 --- a/src/mhtree/ObjectToNodeDistance.java +++ b/src/mhtree/ObjectToNodeDistance.java @@ -15,11 +15,11 @@ public enum ObjectToNodeDistance { */ AVERAGE { @Override - protected double getDistance(Node node, ToDoubleFunction<? super LocalAbstractObject> distanceFunction) { + protected double getDistance(Node node, ToDoubleFunction<? super LocalAbstractObject> getDistance) { return node .getHullObjects() .stream() - .mapToDouble(distanceFunction) + .mapToDouble(getDistance) .sum() / node.getHullObjects().size(); } }, @@ -29,11 +29,11 @@ public enum ObjectToNodeDistance { */ FURTHEST { @Override - protected double getDistance(Node node, ToDoubleFunction<? super LocalAbstractObject> distanceFunction) { + protected double getDistance(Node node, ToDoubleFunction<? super LocalAbstractObject> getDistance) { return node .getHullObjects() .stream() - .mapToDouble(distanceFunction) + .mapToDouble(getDistance) .max() .orElse(Double.MIN_VALUE); } @@ -44,11 +44,11 @@ public enum ObjectToNodeDistance { */ NEAREST { @Override - protected double getDistance(Node node, ToDoubleFunction<? super LocalAbstractObject> distanceFunction) { + protected double getDistance(Node node, ToDoubleFunction<? super LocalAbstractObject> getDistance) { return node .getHullObjects() .stream() - .mapToDouble(distanceFunction) + .mapToDouble(getDistance) .min() .orElse(Double.MAX_VALUE); } @@ -63,11 +63,11 @@ public enum ObjectToNodeDistance { * @return the distance between {@code object} and {@code node} */ public double getDistance(LocalAbstractObject object, Node node) { - return this.getDistance(node, getDistanceFunction(object, null)); + return this.getDistance(node, object::getDistance); } /** - * Returns the distance between {@code object} and {@code node}. + * Returns the distance between {@code object} and {@code node} using {@code distances}. * * @param object an object * @param node a node @@ -75,16 +75,8 @@ public enum ObjectToNodeDistance { * @return the distance between {@code object} and {@code node} */ public double getDistance(LocalAbstractObject object, Node node, PrecomputedDistances distances) { - return this.getDistance(node, getDistanceFunction(object, distances)); + return this.getDistance(node, otherObject -> distances.getDistance(object, otherObject)); } protected abstract double getDistance(Node node, ToDoubleFunction<? super LocalAbstractObject> distanceFunction); - - private ToDoubleFunction<? super LocalAbstractObject> getDistanceFunction(LocalAbstractObject object, PrecomputedDistances distances) { - if (distances == null) { - return object::getDistance; - } - - return (o) -> distances.getDistance(object, o); - } } diff --git a/src/mhtree/Utils.java b/src/mhtree/Utils.java index 2cd8b8b..65982f6 100644 --- a/src/mhtree/Utils.java +++ b/src/mhtree/Utils.java @@ -6,11 +6,11 @@ import java.util.function.BiPredicate; public class Utils { /** - * Returns the index from {@code validIndices} with the maximum value in {@code distanceMatrix}. + * Returns index from {@code validIndices} with the maximum value in {@code distanceMatrix}. * * @param distanceMatrix a distance matrix * @param validIndices valid indices in {@code distanceMatrix} - * @return the index from {@code validIndices} + * @return index from {@code validIndices} with the maximum value in {@code distanceMatrix} */ public static int maxDistanceIndex(float[][] distanceMatrix, BitSet validIndices) { float maxDistance = Float.MIN_VALUE; @@ -30,28 +30,35 @@ public class Utils { } /** - * @param distances - * @param validIndices - * @return an index from validIndices with minimal distance in distances + * Returns index from {@code validIndices} with the minimal distance in {@code distances}. + * + * @param distances an array of distances + * @param validIndices valid indices in {@code distances} + * @return index from {@code validIndices} with the minimal distance in {@code distances} */ public static int minDistanceIndex(float[] distances, BitSet validIndices) { return getDistanceIndex(distances, validIndices, (minDistance, newDistance) -> minDistance > newDistance); } /** - * @param distances - * @param validIndices - * @return an index from validIndices with maximal distance in distances + * Returns index from {@code validIndices} with the maximal distance in {@code distances}. + * + * @param distances an array of distances + * @param validIndices valid indices in {@code distances} + * @return index from {@code validIndices} with the maximal distance in {@code distances}. */ private static int maxDistanceIndex(float[] distances, BitSet validIndices) { return getDistanceIndex(distances, validIndices, (maxDistance, newDistance) -> maxDistance < newDistance); } /** - * @param distances - * @param validIndices + * Returns index from {@code validIndices} with the specific distance in {@code distances}, + * where the distance is set by the {@code comparator}. + * + * @param distances an array of distances + * @param validIndices valid indices in {@code distances} * @param comparator specifies when to update the value of distance and index - * @return + * @return index from {@code validIndices} with the specific distance in {@code distances} */ private static int getDistanceIndex(float[] distances, BitSet validIndices, BiPredicate<Float, Float> comparator) { int index = -1; -- GitLab