Example usage for com.google.common.collect Queues newPriorityQueue

List of usage examples for com.google.common.collect Queues newPriorityQueue

Introduction

In this page you can find the example usage for com.google.common.collect Queues newPriorityQueue.

Prototype

public static <E extends Comparable> PriorityQueue<E> newPriorityQueue() 

Source Link

Document

Creates an empty PriorityQueue with the ordering given by its elements' natural ordering.

Usage

From source file:com.tdunning.scan.Scanner.java

public PriorityQueue<Score> scan(int n, long[] q) {

    List<Callable<PriorityQueue<Score>>> tasks = Lists.newArrayList();
    int batches = threads * 2;
    int batchSize = (size + batches - 1) / batches;
    for (int i = 0; i < batches; i++) {
        final int ix = i;
        tasks.add(() -> {/*www  .j a v  a2  s  .c  o  m*/
            PriorityQueue<Score> r = Queues.newPriorityQueue();
            int offset = ix * batchSize;
            int worst = stride * 64;
            int ourBatch = Math.min(batchSize, size - offset);
            for (int j = 0; j < ourBatch; j++) {
                int match = 0;
                for (int k = 0; k < stride; k++) {
                    match += Long.bitCount(q[k] ^ bits[(j + offset) * stride + k]);
                }
                if (match < worst) {
                    r.add(new Score(j, match));
                }
                while (r.size() > n) {
                    worst = r.poll().matchingBits;
                }
            }
            return r;
        });
    }
    try {
        List<Future<PriorityQueue<Score>>> taskResults = pool.invokeAll(tasks);
        PriorityQueue<Score> r = Queues.newPriorityQueue();
        for (Future<PriorityQueue<Score>> rx : taskResults) {
            r.addAll(rx.get());
            while (r.size() > n) {
                r.poll();
            }
        }
        return r;
    } catch (InterruptedException e) {
        throw new RuntimeException("Aborted execution", e);
    } catch (ExecutionException e) {
        throw new RuntimeException("Error during execution", e);
    }
}

From source file:playground.michalm.zone.ZoneFinder.java

@SuppressWarnings("unchecked")
public Zone findZone(Coord coord) {
    point = MGC.coord2Point(coord);
    queue = Queues.newPriorityQueue();

    Envelope env = point.getEnvelopeInternal();
    queueByArea(quadTree.query(env));/*from  w  w w .jav  a2 s  . co m*/

    if (queue.size() > 1) {
        Zone result = queue.peek().zone;
        printOutQueue();
        return result;
    }

    if (queue.isEmpty() && maxDistance > 0) {
        env.expandBy(maxDistance);
        queueByDistance(quadTree.query(env));
    }

    if (queue.isEmpty()) {
        return null;
    }

    return queue.peek().zone;
}

From source file:cosmos.sql.DataTable.java

public DataTable(final TableSchema<? extends SchemaDefiner<?>> schema, final String tableName,
        JavaTypeFactory typeFactory) {//from w w  w  . j  a  v  a2 s .com
    this.schema = schema;
    this.tableName = tableName;
    plans = Queues.newPriorityQueue();
    aggregationPlans = Queues.newPriorityQueue();
    resultSet = new BaseIterable<T>();
    javaFactory = typeFactory;
    this.metadata = schema.metaData;

}

From source file:edu.cmu.cs.lti.ark.fn.parsing.Decoding.java

/**
 * Decode, respecting the constraint that arguments do not overlap.
 * Find the k (approximately) best configurations of non-overlapping role-filling spans using beam search.
 *
 * @param frameFeatures features for the given frame
 * @param kBestOutput the number of top configurations we should return
 * @return a list of Strings encoding the best k configurations of spans for all roles of the given frame
 *//*from w  w w  . jav  a 2  s.  c  om*/
public List<Scored<RoleAssignments>> getPredictions(FrameFeatures frameFeatures, int kBestOutput) {
    // group by role
    final Map<String, CandidatesForRole> candidatesAndScoresByRole = scoreCandidatesForRoles(
            frameFeatures.fElements, frameFeatures.fElementSpansAndFeatures);

    // run beam search to find the (approximately) k-best non-overlapping configurations
    // our beam
    List<Scored<RoleAssignments>> currentBeam = Lists.newArrayList(scored(new RoleAssignments(), 0.0));
    // run beam search
    for (String roleName : candidatesAndScoresByRole.keySet()) {
        final PriorityQueue<Scored<RoleAssignments>> newBeam = Queues.newPriorityQueue();
        for (Scored<Span> candidate : candidatesAndScoresByRole.get(roleName)) {
            for (Scored<RoleAssignments> partialAssignment : currentBeam) {
                final double newScore = partialAssignment.score + candidate.score; // multiply in log-space
                if (newBeam.size() >= DEFAULT_BEAM_WIDTH && newScore <= newBeam.peek().score)
                    break;
                if (!partialAssignment.value.overlaps(candidate.value)) {
                    final RoleAssignments newAssignment = partialAssignment.value.plus(roleName,
                            candidate.value);
                    newBeam.add(scored(newAssignment, newScore));
                }
                if (newBeam.size() > DEFAULT_BEAM_WIDTH)
                    newBeam.poll();
            }
        }
        currentBeam = copyOf(newBeam);
        //System.out.println("Considering " + roleName);
        //System.out.println("Beam grew to " + newBeam.size());
        //System.out.println("Current best: " + newBeam.first().value + " " + newBeam.first().score);
    }
    return safeTruncate(currentBeam, kBestOutput);
}