List of usage examples for com.google.common.collect MinMaxPriorityQueue peekFirst
public E peekFirst()
From source file:kungfu.algdesign.ds.MovingMedian.java
public static void calculate(Queue<Integer> data, Queue<Integer> medians) { MinMaxPriorityQueue<Integer> minHeap = MinMaxPriorityQueue.create(); MinMaxPriorityQueue<Integer> maxHeap = MinMaxPriorityQueue.create(); minHeap.add(Integer.MIN_VALUE); maxHeap.add(Integer.MAX_VALUE); Integer item = null;// w ww .ja v a2s.c o m Integer median = null; while ((item = data.poll()) != null) { if (median == null) { maxHeap.add(item); } else if (item >= median) { maxHeap.add(item); } else { minHeap.add(item); } if (maxHeap.size() - minHeap.size() == 2) { minHeap.add(maxHeap.pollFirst()); } else if (minHeap.size() - maxHeap.size() == 2) { maxHeap.add(minHeap.pollLast()); } if (minHeap.size() == maxHeap.size() || minHeap.size() > maxHeap.size()) { median = minHeap.peekLast(); } else { median = maxHeap.peekFirst(); } medians.add(median); } }
From source file:co.cask.cdap.common.zookeeper.coordination.BalancedAssignmentStrategy.java
/** * Balance the assignment by spreading it across all handlers evenly. * * @param handlerQueue The priority queue for tracking number of resources assigned to a given handler. * @param assigner The assigner for changing the assignment. * @param maxDiff The maximum differences between the handlers that has the most resources assigned vs the one with * the least resources assigned. *///from w w w. jav a 2 s . c o m private <T> void balance(MinMaxPriorityQueue<HandlerSize<T>> handlerQueue, ResourceAssigner<T> assigner, int maxDiff) { HandlerSize<T> minHandler = handlerQueue.peekFirst(); HandlerSize<T> maxHandler = handlerQueue.peekLast(); // Move assignment from the handler that has the most assigned partition replica to the least one, until the // differences is within the desired range. Multimap<T, PartitionReplica> assignments = assigner.get(); while (maxHandler.getSize() - minHandler.getSize() > maxDiff) { PartitionReplica partitionReplica = assignments.get(maxHandler.getHandler()).iterator().next(); // Remove min and max from the queue, and perform the reassignment. handlerQueue.removeFirst(); handlerQueue.removeLast(); assigner.set(minHandler.getHandler(), partitionReplica); // After assignment, the corresponding size should get updated, hence put it back to the queue for next iteration. handlerQueue.add(minHandler); handlerQueue.add(maxHandler); minHandler = handlerQueue.peekFirst(); maxHandler = handlerQueue.peekLast(); } }
From source file:gobblin.source.extractor.extract.kafka.workunit.packer.KafkaWorkUnitPacker.java
/** * Pack a list of {@link WorkUnit}s into a smaller number of {@link MultiWorkUnit}s, * using the worst-fit-decreasing algorithm. * * Each {@link WorkUnit} is assigned to the {@link MultiWorkUnit} with the smallest load. *//*from w w w. j av a 2 s . co m*/ protected List<WorkUnit> worstFitDecreasingBinPacking(List<WorkUnit> groups, int numOfMultiWorkUnits) { // Sort workunit groups by data size desc Collections.sort(groups, LOAD_DESC_COMPARATOR); MinMaxPriorityQueue<MultiWorkUnit> pQueue = MinMaxPriorityQueue.orderedBy(LOAD_ASC_COMPARATOR) .expectedSize(numOfMultiWorkUnits).create(); for (int i = 0; i < numOfMultiWorkUnits; i++) { MultiWorkUnit multiWorkUnit = MultiWorkUnit.createEmpty(); setWorkUnitEstSize(multiWorkUnit, 0); pQueue.add(multiWorkUnit); } for (WorkUnit group : groups) { MultiWorkUnit lightestMultiWorkUnit = pQueue.poll(); addWorkUnitToMultiWorkUnit(group, lightestMultiWorkUnit); pQueue.add(lightestMultiWorkUnit); } logMultiWorkUnitInfo(pQueue); double minLoad = getWorkUnitEstLoad(pQueue.peekFirst()); double maxLoad = getWorkUnitEstLoad(pQueue.peekLast()); LOG.info(String.format("Min load of multiWorkUnit = %f; Max load of multiWorkUnit = %f; Diff = %f%%", minLoad, maxLoad, (maxLoad - minLoad) / maxLoad * 100.0)); this.state.setProp(MIN_MULTIWORKUNIT_LOAD, minLoad); this.state.setProp(MAX_MULTIWORKUNIT_LOAD, maxLoad); List<WorkUnit> multiWorkUnits = Lists.newArrayList(); multiWorkUnits.addAll(pQueue); return multiWorkUnits; }