Example usage for org.apache.commons.collections15 Buffer remove

List of usage examples for org.apache.commons.collections15 Buffer remove

Introduction

In this page you can find the example usage for org.apache.commons.collections15 Buffer remove.

Prototype

E remove();

Source Link

Document

Gets and removes the next object from the buffer.

Usage

From source file:edu.uci.ics.jung.algorithms.cluster.WeakComponentClusterer.java

/**
  * Extracts the weak components from a graph.
  * @param graph the graph whose weak components are to be extracted
  * @return the list of weak components/*  w ww  . j  a  v a2s .c  o  m*/
  */
public Set<Set<V>> transform(Graph<V, E> graph) {

    Set<Set<V>> clusterSet = new HashSet<Set<V>>();

    HashSet<V> unvisitedVertices = new HashSet<V>(graph.getVertices());

    while (!unvisitedVertices.isEmpty()) {
        Set<V> cluster = new HashSet<V>();
        V root = unvisitedVertices.iterator().next();
        unvisitedVertices.remove(root);
        cluster.add(root);

        Buffer<V> queue = new UnboundedFifoBuffer<V>();
        queue.add(root);

        while (!queue.isEmpty()) {
            V currentVertex = queue.remove();
            Collection<V> neighbors = graph.getNeighbors(currentVertex);

            for (V neighbor : neighbors) {
                if (unvisitedVertices.contains(neighbor)) {
                    queue.add(neighbor);
                    unvisitedVertices.remove(neighbor);
                    cluster.add(neighbor);
                }
            }
        }
        clusterSet.add(cluster);
    }
    return clusterSet;
}

From source file:edu.uci.ics.jung.algorithms.importance.BetweennessCentrality.java

protected void computeBetweenness(Graph<V, E> graph) {

    Map<V, BetweennessData> decorator = new HashMap<V, BetweennessData>();
    Map<V, Number> bcVertexDecorator = vertexRankScores.get(getRankScoreKey());
    bcVertexDecorator.clear();/*from  ww  w.  j a v a2 s.  c  o m*/
    Map<E, Number> bcEdgeDecorator = edgeRankScores.get(getRankScoreKey());
    bcEdgeDecorator.clear();

    Collection<V> vertices = graph.getVertices();

    for (V s : vertices) {

        initializeData(graph, decorator);

        decorator.get(s).numSPs = 1;
        decorator.get(s).distance = 0;

        Stack<V> stack = new Stack<V>();
        Buffer<V> queue = new UnboundedFifoBuffer<V>();
        queue.add(s);

        while (!queue.isEmpty()) {
            V v = queue.remove();
            stack.push(v);

            for (V w : getGraph().getSuccessors(v)) {

                if (decorator.get(w).distance < 0) {
                    queue.add(w);
                    decorator.get(w).distance = decorator.get(v).distance + 1;
                }

                if (decorator.get(w).distance == decorator.get(v).distance + 1) {
                    decorator.get(w).numSPs += decorator.get(v).numSPs;
                    decorator.get(w).predecessors.add(v);
                }
            }
        }

        while (!stack.isEmpty()) {
            V w = stack.pop();

            for (V v : decorator.get(w).predecessors) {

                double partialDependency = (decorator.get(v).numSPs / decorator.get(w).numSPs);
                partialDependency *= (1.0 + decorator.get(w).dependency);
                decorator.get(v).dependency += partialDependency;
                E currentEdge = getGraph().findEdge(v, w);
                double edgeValue = bcEdgeDecorator.get(currentEdge).doubleValue();
                edgeValue += partialDependency;
                bcEdgeDecorator.put(currentEdge, edgeValue);
            }
            if (w != s) {
                double bcValue = bcVertexDecorator.get(w).doubleValue();
                bcValue += decorator.get(w).dependency;
                bcVertexDecorator.put(w, bcValue);
            }
        }
    }

    if (graph instanceof UndirectedGraph) {
        for (V v : vertices) {
            double bcValue = bcVertexDecorator.get(v).doubleValue();
            bcValue /= 2.0;
            bcVertexDecorator.put(v, bcValue);
        }
        for (E e : graph.getEdges()) {
            double bcValue = bcEdgeDecorator.get(e).doubleValue();
            bcValue /= 2.0;
            bcEdgeDecorator.put(e, bcValue);
        }
    }

    for (V vertex : vertices) {
        decorator.remove(vertex);
    }
}

From source file:edu.uci.ics.jung.algorithms.flows.EdmondsKarpMaxFlow.java

protected boolean hasAugmentingPath() {

    mSinkPartitionNodes.clear();// ww  w . ja va2s. c  o  m
    mSourcePartitionNodes.clear();
    mSinkPartitionNodes.addAll(mFlowGraph.getVertices());

    Set<E> visitedEdgesMap = new HashSet<E>();
    Buffer<V> queue = new UnboundedFifoBuffer<V>();
    queue.add(source);

    while (!queue.isEmpty()) {
        V currentVertex = queue.remove();
        mSinkPartitionNodes.remove(currentVertex);
        mSourcePartitionNodes.add(currentVertex);
        Number currentCapacity = parentCapacityMap.get(currentVertex);

        Collection<E> neighboringEdges = mFlowGraph.getOutEdges(currentVertex);

        for (E neighboringEdge : neighboringEdges) {

            V neighboringVertex = mFlowGraph.getDest(neighboringEdge);

            Number residualCapacity = residualCapacityMap.get(neighboringEdge);
            if (residualCapacity.intValue() <= 0 || visitedEdgesMap.contains(neighboringEdge))
                continue;

            V neighborsParent = parentMap.get(neighboringVertex);
            Number neighborCapacity = parentCapacityMap.get(neighboringVertex);
            int newCapacity = Math.min(residualCapacity.intValue(), currentCapacity.intValue());

            if ((neighborsParent == null) || newCapacity > neighborCapacity.intValue()) {
                parentMap.put(neighboringVertex, currentVertex);
                parentCapacityMap.put(neighboringVertex, new Integer(newCapacity));
                visitedEdgesMap.add(neighboringEdge);
                if (neighboringVertex != target) {
                    queue.add(neighboringVertex);
                }
            }
        }
    }

    boolean hasAugmentingPath = false;
    Number targetsParentCapacity = parentCapacityMap.get(target);
    if (targetsParentCapacity != null && targetsParentCapacity.intValue() > 0) {
        updateResidualCapacities();
        hasAugmentingPath = true;
    }
    clearParentValues();
    return hasAugmentingPath;
}

From source file:edu.brown.benchmark.seats.SEATSClient.java

protected Pair<Object[], ProcedureCallback> getUpdateReservationParams() {
    if (trace.val)
        LOG.trace("Let's look for a Reservation that we can update");

    // Pull off the first pending seat change and throw that ma at the server
    Buffer<Reservation> cache = CACHE_RESERVATIONS.get(CacheType.PENDING_UPDATES);
    assert (cache != null) : "Unexpected " + CacheType.PENDING_UPDATES;
    Reservation r = null;//from  w ww  .  j av  a2s.  co m
    synchronized (cache) {
        if (cache.isEmpty() == false)
            r = cache.remove();
    } // SYNCH
    if (r == null)
        return (null);

    long value = rng.number(1, 1 << 20);
    long attribute_idx = rng.nextInt(UpdateReservation.NUM_UPDATES);
    long seatnum = rng.nextInt(SEATSConstants.FLIGHTS_RESERVED_SEATS);
    if (debug.val)
        LOG.debug(String.format("UpdateReservation: FlightId:%d / CustomerId:%d / SeatNum:%d", r.flight_id,
                r.customer_id, seatnum));

    Object params[] = new Object[] { r.id, r.customer_id, r.flight_id, seatnum, attribute_idx, value };

    if (trace.val)
        LOG.trace("Calling " + Transaction.UPDATE_RESERVATION.getExecName());
    return new Pair<Object[], ProcedureCallback>(params, new UpdateReservationCallback(r));
}

From source file:edu.brown.benchmark.seats.SEATSClient.java

protected Pair<Object[], ProcedureCallback> getDeleteReservationParams() {
    // Pull off the first cached reservation and drop it on the cluster...
    Buffer<Reservation> cache = CACHE_RESERVATIONS.get(CacheType.PENDING_DELETES);
    assert (cache != null) : "Unexpected " + CacheType.PENDING_DELETES;
    Reservation r = null;//from  w w  w.  j  a v a  2  s. c  o  m
    synchronized (cache) {
        if (cache.isEmpty() == false)
            r = cache.remove();
    } // SYNCH
    if (r == null) {
        return (null);
    }
    int rand;
    if (config.force_all_distributed) {
        rand = SEATSConstants.PROB_DELETE_WITH_CUSTOMER_ID_STR + 1;
    } else if (config.force_all_singlepartition) {
        rand = 100;
    } else {
        rand = rng.number(1, 100);
    }

    // Parameters
    long f_id = r.flight_id;
    long c_id = VoltType.NULL_BIGINT;
    String c_id_str = "";
    String ff_c_id_str = "";

    // Delete with the Customer's id as a string 
    if (rand <= SEATSConstants.PROB_DELETE_WITH_CUSTOMER_ID_STR) {
        c_id_str = String.format(SEATSConstants.CUSTOMER_ID_STR, r.customer_id);
    }
    // Delete using their FrequentFlyer information
    else if (rand <= SEATSConstants.PROB_DELETE_WITH_CUSTOMER_ID_STR
            + SEATSConstants.PROB_DELETE_WITH_FREQUENTFLYER_ID_STR) {
        ff_c_id_str = String.format(SEATSConstants.CUSTOMER_ID_STR, r.customer_id);
    }
    // Delete using their Customer id
    else {
        c_id = r.customer_id;
    }

    Object params[] = new Object[] { f_id, // [0] f_id
            c_id, // [1] c_id
            c_id_str, // [2] c_id_str
            ff_c_id_str, // [3] ff_c_id_str
    };

    if (trace.val)
        LOG.trace("Calling " + Transaction.DELETE_RESERVATION.getExecName());

    return new Pair<Object[], ProcedureCallback>(params, new DeleteReservationCallback(r));
}

From source file:edu.brown.benchmark.seats.SEATSClient.java

protected Pair<Object[], ProcedureCallback> getNewReservationParams() {
    Reservation reservation = null;//  w w w  .  ja  v  a 2 s.c  om
    BitSet seats = null;
    Buffer<Reservation> cache = CACHE_RESERVATIONS.get(CacheType.PENDING_INSERTS);
    assert (cache != null) : "Unexpected " + CacheType.PENDING_INSERTS;

    if (debug.val)
        LOG.debug(String.format("Attempting to get a new pending insert Reservation [totalPendingInserts=%d]",
                cache.size()));
    while (reservation == null) {
        Reservation r = null;
        synchronized (cache) {
            if (cache.isEmpty() == false)
                r = cache.remove();
        } // SYNCH
        if (r == null) {
            if (debug.val)
                LOG.warn("Unable to execute " + Transaction.DELETE_RESERVATION
                        + " - No available reservations to insert");
            break;
        }

        seats = this.getSeatsBitSet(r.flight_id);

        if (this.isFlightFull(seats)) {
            if (debug.val)
                LOG.debug(String.format("%s is full", r.flight_id));
            continue;
        }
        // PAVLO: Not sure why this is always coming back as reserved? 
        //            else if (seats.get(r.seatnum)) {
        //                if (debug.val)
        //                    LOG.debug(String.format("Seat #%d on %s is already booked", r.seatnum, r.flight_id));
        //                continue;
        //            }
        else if (this.isCustomerBookedOnFlight(r.customer_id, r.flight_id)) {
            if (debug.val)
                LOG.debug(String.format("%s is already booked on %s", r.customer_id, r.flight_id));
            continue;
        }
        reservation = r;
    } // WHILE
    if (reservation == null) {
        if (debug.val)
            LOG.debug("Failed to find a valid pending insert Reservation\n" + this.toString());
        return (null);
    }

    // Generate a random price for now
    double price = 2.0 * rng.number(SEATSConstants.RESERVATION_PRICE_MIN, SEATSConstants.RESERVATION_PRICE_MAX);

    // Generate random attributes
    long attributes[] = new long[SEATSConstants.NEW_RESERVATION_ATTRS_SIZE];
    for (int i = 0; i < attributes.length; i++) {
        attributes[i] = rng.nextLong();
    } // FOR

    // boolean updateCustomer = (rng.nextInt(100) < SEATSConstants.PROB_UPDATE_CUSTOMER_NEW_RESERVATION);

    Object params[] = new Object[] { reservation.id, reservation.customer_id, reservation.flight_id,
            reservation.seatnum, price, attributes, new TimestampType() };
    if (trace.val)
        LOG.trace("Calling " + Transaction.NEW_RESERVATION.getExecName());
    return new Pair<Object[], ProcedureCallback>(params, new NewReservationCallback(reservation));
}