All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.khelekore.prtree.LeafNode Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.khelekore.prtree;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

class LeafNode extends NodeBase {

    public LeafNode (Object[] data) {
	super (data);
    }

    public MBR getMBR (T t, MBRConverter converter) {
	return new SimpleMBR (t, converter);
    }

    @Override public MBR computeMBR (MBRConverter converter) {
	MBR ret = null;
	for (int i = 0, s = size (); i < s; i++)
	    ret = getUnion (ret, getMBR (get (i), converter));
	return ret;
    }

    public void expand (MBR mbr, NodeFilter filter, MBRConverter converter,
			List found, List> nodesToExpand) {
	find (mbr, converter, found, filter);
    }

    public void find (MBR mbr, MBRConverter converter,
		      List result, NodeFilter filter) {
	for (int i = 0, s = size (); i < s; i++) {
	    T  t = get (i);
	    if (mbr.intersects (t, converter) && filter.accept (t))
		result.add (t);
	}
    }

    public void nnExpand (DistanceCalculator dc,
			  NodeFilter filter,
			  List> drs,
			  int maxHits,
			  PriorityQueue> queue,
			  MinDistComparator> mdc) {
	for (int i = 0, s = size (); i < s; i++) {
	    T  t = get (i);
	    if (filter.accept (t)) {
		double dist = dc.distanceTo (t, mdc.p);
		int n = drs.size ();
		if (n < maxHits || dist < drs.get (n - 1).getDistance ()) {
		    add (drs, new DistanceResult<> (t, dist), maxHits);
		}
	    }
	}
    }

    private void add (List> drs,
		      DistanceResult dr,
		      int maxHits) {
	int n = drs.size ();
	if (n == maxHits)
	    drs.remove (n - 1);
	int pos = Collections.binarySearch (drs, dr, comp);
	if (pos < 0) {
	    // binarySearch return -(pos + 1) for new entries
	    pos = -(pos + 1);
	}
	drs.add (pos, dr);
    }

    private static final Comparator> comp =
	new Comparator> () {
	public int compare (DistanceResult d1, DistanceResult d2) {
	    return Double.compare (d1.getDistance (), d2.getDistance ());
	}
    };
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy