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

ADD: distance measure in BuildTree, insert type as BuildTree parameter

parent 10bdf184
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,6 @@ package mhtree;
import cz.muni.fi.disa.similarityoperators.cover.AbstractRepresentation.PrecomputedDistances;
import messif.buckets.BucketDispatcher;
import messif.buckets.BucketStorageException;
import messif.buckets.LocalBucket;
import messif.objects.LocalAbstractObject;
import java.util.*;
......@@ -22,11 +21,13 @@ class BuildTree {
private final PrecomputedDistances objectDistances;
private final float[][] nodeDistances;
private final InsertType insertType;
private final DistanceMeasure distanceMeasure;
private final BucketDispatcher bucketDispatcher;
private Node root;
BuildTree(List<LocalAbstractObject> objects, int leafCapacity, int arity, InsertType insertType, BucketDispatcher bucketDispatcher) throws BucketStorageException {
BuildTree(List<LocalAbstractObject> objects, int leafCapacity, int arity, InsertType insertType, DistanceMeasure distanceMeasure, BucketDispatcher bucketDispatcher) throws BucketStorageException {
this.arity = arity;
this.leafCapacity = leafCapacity;
......@@ -38,17 +39,19 @@ class BuildTree {
objectDistances = new PrecomputedDistances(objects);
nodeDistances = new float[nodes.length][nodes.length];
this.insertType = insertType;
this.distanceMeasure = distanceMeasure;
this.bucketDispatcher = bucketDispatcher;
buildTree(insertType);
buildTree();
}
public Node getRoot() {
return root;
}
private void buildTree(InsertType insertType) throws BucketStorageException {
if (!initHullPoints(insertType)) return;
private void buildTree() throws BucketStorageException {
if (!initHullPoints()) return;
precomputeHullDistances();
......@@ -62,7 +65,7 @@ class BuildTree {
notProcessedIndices.stream().forEach(restOfTheIndices::add);
int mainIndex = restOfTheIndices.remove(0);
mergeHulls(mainIndex, restOfTheIndices, insertType);
mergeHulls(mainIndex, restOfTheIndices);
break;
}
......@@ -78,16 +81,16 @@ class BuildTree {
nnIndices.add(index);
}
mergeHulls(furthestNodeIndex, nnIndices, insertType);
mergeHulls(furthestNodeIndex, nnIndices);
}
}
root = nodes[validNodeIndices.nextSetBit(0)];
}
private boolean initHullPoints(InsertType insertType) throws BucketStorageException {
private boolean initHullPoints() throws BucketStorageException {
if (objectDistances.getObjectCount() < leafCapacity) {
root = new LeafNode(objectDistances, bucketDispatcher.createBucket(), insertType);
root = new LeafNode(objectDistances, bucketDispatcher.createBucket(), insertType, distanceMeasure);
return false;
}
......@@ -111,7 +114,7 @@ class BuildTree {
// Select the rest of the objects up to the total of leafCapacity
objects.addAll(findClosestObjects(furthestIndex, leafCapacity - 1, notProcessedObjectIndices));
nodes[nodeIndex] = new LeafNode(objectDistances.getSubset(objects), bucketDispatcher.createBucket(), insertType);
nodes[nodeIndex] = new LeafNode(objectDistances.getSubset(objects), bucketDispatcher.createBucket(), insertType, distanceMeasure);
}
return true;
......@@ -192,7 +195,7 @@ class BuildTree {
}
}
private void mergeHulls(int mainHullIndex, List<Integer> otherHullIndices, InsertType insertType) {
private void mergeHulls(int mainHullIndex, List<Integer> otherHullIndices) {
if (otherHullIndices.size() == 0) return;
List<Integer> indices = new ArrayList<>(otherHullIndices);
......@@ -200,7 +203,7 @@ class BuildTree {
List<Node> nodes = indices.stream().map(i -> this.nodes[i]).collect(Collectors.toList());
InternalNode parent = Node.createParent(nodes, objectDistances, insertType);
InternalNode parent = Node.createParent(nodes, objectDistances, insertType, distanceMeasure);
nodes.forEach(node -> node.setParent(parent));
parent.addChildren(nodes);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment