Example usage for org.apache.commons.collections BinaryHeap add

List of usage examples for org.apache.commons.collections BinaryHeap add

Introduction

In this page you can find the example usage for org.apache.commons.collections BinaryHeap add.

Prototype

public boolean add(Object object) 

Source Link

Document

Adds an object to this heap.

Usage

From source file:com.opendoorlogistics.core.scripts.formulae.FmLookupNearest.java

@Override
public Object execute(FunctionParameters parameters) {
    TableParameters tp = (TableParameters) parameters;
    ODLTableReadOnly table = (ODLTableReadOnly) tp.getTableById(refs.datastoreIndx, refs.tableId);

    if (type == LCType.LL) {
        return executeLL(parameters, table);
    }//  w  ww  . j  a va 2  s  .  co m

    CachedProcessedGeom searchObject = getSearchGeom(parameters);
    if (searchObject == null || searchObject.geometry == null) {
        return Functions.EXECUTION_ERROR;
    }

    class RowElement implements Comparable<RowElement> {
        int row;
        CachedProcessedGeom geom;
        double minDistance;

        @Override
        public int compareTo(RowElement o) {
            int ret = Double.compare(minDistance, o.minDistance);
            if (ret == 0) {
                ret = Integer.compare(row, o.row);
            }
            return ret;
        }
    }

    // Get all geometries, transformed into the coord system and with bounding circles.
    // Place them in a binary heap, sorted by their minimum possible distance according to bounding circle
    int nr = table.getRowCount();
    BinaryHeap sortedHeap = new BinaryHeap();
    for (int row = 0; row < nr; row++) {
        CachedProcessedGeom otherGeom = null;
        switch (type) {
        case GL:
            Pair<LatLong, Boolean> result = getLatLongFromRow(table, row);
            if (result.getSecond() == false) {
                // critical error
                return Functions.EXECUTION_ERROR;
            } else if (result.getFirst() != null) {

                LatLong ll = result.getFirst();

                // put into our comparison object and convert
                otherGeom = new CachedProcessedGeom(ll, transform);
            }
            break;

        case GG:
        case LG: {
            Object val = table.getValueAt(row, refs.columnIndices[0]);
            if (val != null) {

                ODLGeomImpl odlGeom = (ODLGeomImpl) ColumnValueProcessor.convertToMe(ODLColumnType.GEOM, val);
                if (odlGeom == null) {
                    // critical error
                    return Functions.EXECUTION_ERROR;
                }

                otherGeom = toCoordSystem(odlGeom);
                if (otherGeom == null || otherGeom.geometry == null) {
                    // critical error
                    return Functions.EXECUTION_ERROR;
                }
            }
        }
        default:
            break;
        }

        if (otherGeom != null) {
            RowElement rowElement = new RowElement();
            rowElement.row = row;
            rowElement.minDistance = searchObject.boundingCircle.minimumSeparation(otherGeom.boundingCircle);
            rowElement.geom = otherGeom;
            sortedHeap.add(rowElement);
        }
    }

    // loop over the table
    RowElement closest = null;
    double closestDistance = Double.MAX_VALUE;
    while (sortedHeap.size() > 0) {
        RowElement row = (RowElement) sortedHeap.pop();
        if (row.minDistance > closestDistance) {
            // We can stop searching now as the minimum possible distance is greater than our closest
            break;
        }

        // Explicitly get the distance
        double distance = searchObject.geometry.distance(row.geom.geometry);
        if (distance < closestDistance) {
            closestDistance = distance;
            closest = row;
        }
    }

    if (closest != null) {
        return getReturnObject(table, closest.row);
    }

    return null;
}

From source file:org.drools.util.BinaryHeapPriorityQueueTest.java

public void xxxtestBasic() {
    final Random random = new Random();
    final List items = new LinkedList();

    final BinaryHeap queue = new BinaryHeap();

    for (int i = 0; i < 100000; ++i) {
        items.add(new LongQueueable(random.nextLong()));
    }//from  w w  w. j a  va  2s  .co m

    final long startEnqueue = System.currentTimeMillis();

    for (final Iterator i = items.iterator(); i.hasNext();) {
        queue.add(i.next());
    }

    final long elapsedEnqueue = System.currentTimeMillis() - startEnqueue;

    final long startDequeue = System.currentTimeMillis();

    for (final Iterator i = items.iterator(); i.hasNext();) {
        queue.remove(i.next());
    }

    //        while (!queue.isEmpty()) {
    //            queue.pop();
    //        }

    final long elapsedDequeue = System.currentTimeMillis() - startDequeue;

    System.out.println("elapsedEnqueue = " + elapsedEnqueue);
    System.out.println("elapsedDequeue = " + elapsedDequeue);
}