/******************************************************************************* * Copyright (c) 2010 Haifeng Li * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package smile.neighbor; /** * The object encapsulates the results of nearest neighbor search. A returned * neighbor for nearest neighbor search contains the key of object (say weight * vector of a neuron) and the object itself (say a neuron in neural network, * which also contains other information beyond weight vector), an index of * object in the dataset, which is often useful, and the distance between * the query key to the object key. * * @param <K> the type of keys. * @param <V> the type of associated objects. * * @author Haifeng Li */ public class Neighbor<K, V> implements Comparable<Neighbor<K,V>> { /** * The key of neighbor. */ public K key; /** * The data object of neighbor. It may be same as the key object. */ public V value; /** * The index of neighbor object in the dataset. */ public int index; /** * The distance between the query and the neighbor. */ public double distance; /** * Constructor. * @param object the neighbor object. * @param index the index of neighbor object in the dataset. * @param distance the distance between the query and the neighbor. */ public Neighbor(K key, V object, int index, double distance) { this.key = key; this.value = object; this.index = index; this.distance = distance; } @Override public int compareTo(Neighbor<K,V> o) { int d = (int) Math.signum(distance - o.distance); // Sometime, the dataset contains duplicate samples. // If the distances are same, we sort by the sample index. if (d == 0) return index - o.index; else return d; } }