Example usage for com.google.common.collect TreeMultiset tailMultiset

List of usage examples for com.google.common.collect TreeMultiset tailMultiset

Introduction

In this page you can find the example usage for com.google.common.collect TreeMultiset tailMultiset.

Prototype

@Override
    public SortedMultiset<E> tailMultiset(@Nullable E lowerBound, BoundType boundType) 

Source Link

Usage

From source file:org.apache.mahout.math.neighborhood.ProjectionSearch.java

/**
 * Returns the closest vector to the query.
 * When only one the nearest vector is needed, use this method, NOT search(query, limit) because
 * it's faster (less overhead).//  ww w .jav a  2 s .com
 *
 * @param query the vector to search for
 * @param differentThanQuery if true, returns the closest vector different than the query (this
 *                           only matters if the query is among the searched vectors), otherwise,
 *                           returns the closest vector to the query (even the same vector).
 * @return the weighted vector closest to the query
 */
@Override
public WeightedThing<Vector> searchFirst(Vector query, boolean differentThanQuery) {
    double bestDistance = Double.POSITIVE_INFINITY;
    Vector bestVector = null;

    Iterator<? extends Vector> projections = basisMatrix.iterator();
    for (TreeMultiset<WeightedThing<Vector>> v : scalarProjections) {
        Vector basisVector = projections.next();
        WeightedThing<Vector> projectedQuery = new WeightedThing<Vector>(query, query.dot(basisVector));
        for (WeightedThing<Vector> candidate : Iterables.concat(
                Iterables.limit(v.tailMultiset(projectedQuery, BoundType.CLOSED), searchSize), Iterables.limit(
                        v.headMultiset(projectedQuery, BoundType.OPEN).descendingMultiset(), searchSize))) {
            double distance = distanceMeasure.distance(query, candidate.getValue());
            if (distance < bestDistance && (!differentThanQuery || !candidate.getValue().equals(query))) {
                bestDistance = distance;
                bestVector = candidate.getValue();
            }
        }
    }

    return new WeightedThing<Vector>(bestVector, bestDistance);
}

From source file:org.apache.mahout.math.neighborhood.ProjectionSearch.java

/**
 * Searches for the query vector returning the closest limit referenceVectors.
 *
 * @param query the vector to search for.
 * @param limit the number of results to return.
 * @return a list of Vectors wrapped in WeightedThings where the "thing"'s weight is the
 * distance./* w  w  w  .  jav a2  s.  c  o m*/
 */
@Override
public List<WeightedThing<Vector>> search(Vector query, int limit) {
    Set<Vector> candidates = Sets.newHashSet();

    Iterator<? extends Vector> projections = basisMatrix.iterator();
    for (TreeMultiset<WeightedThing<Vector>> v : scalarProjections) {
        Vector basisVector = projections.next();
        WeightedThing<Vector> projectedQuery = new WeightedThing<Vector>(query, query.dot(basisVector));
        for (WeightedThing<Vector> candidate : Iterables.concat(
                Iterables.limit(v.tailMultiset(projectedQuery, BoundType.CLOSED), searchSize), Iterables.limit(
                        v.headMultiset(projectedQuery, BoundType.OPEN).descendingMultiset(), searchSize))) {
            candidates.add(candidate.getValue());
        }
    }

    // If searchSize * scalarProjections.size() is small enough not to cause much memory pressure,
    // this is probably just as fast as a priority queue here.
    List<WeightedThing<Vector>> top = Lists.newArrayList();
    for (Vector candidate : candidates) {
        top.add(new WeightedThing<Vector>(candidate, distanceMeasure.distance(query, candidate)));
    }
    Collections.sort(top);
    return top.subList(0, Math.min(limit, top.size()));
}