List of usage examples for java.util TreeSet pollLast
public E pollLast()
From source file:Main.java
public static void main(String[] args) { TreeSet<Integer> treeadd = new TreeSet<Integer>(); treeadd.add(1);//from w w w .j a va 2 s. c om treeadd.add(3); treeadd.add(17); treeadd.add(2); System.out.println("Last element is: " + treeadd.pollLast()); Iterator<Integer> iterator = treeadd.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } }
From source file:Main.java
public static <T> SortedSet<T> sortTopN(Iterable<T> iterable, int n, Comparator<T> comparator) { TreeSet<T> r = Sets.newTreeSet(comparator); for (T t : iterable) { r.add(t);//from www . j ava2 s . c om if (r.size() > n) { r.pollLast(); } } return r; }
From source file:com.offbynull.voip.kademlia.FindSubcoroutine.java
@Override public List<Node> run(Continuation cnt) throws Exception { Context ctx = (Context) cnt.getContext(); ctx.addOutgoingMessage(subAddress, logAddress, info("Finding {}", findId)); // Set up subcoroutine router Address routerAddress = subAddress.appendSuffix("finderreq" + idGenerator.generate()); SubcoroutineRouter msgRouter = new SubcoroutineRouter(routerAddress, ctx); Controller msgRouterController = msgRouter.getController(); // Get initial set of nodes to query from routing table List<Node> startNodes = router.find(findId, maxResults, false); // do not include stale nodes, we only want to contact alive nodes ctx.addOutgoingMessage(subAddress, logAddress, info("Route table entries closest to {}: {}", findId, startNodes)); // Create sorted set of nodes to contact IdXorMetricComparator idClosenessComparator = new IdXorMetricComparator(findId); TreeSet<Node> contactSet = new TreeSet<>((x, y) -> idClosenessComparator.compare(x.getId(), y.getId())); contactSet.addAll(startNodes);//www . j a v a 2 s . c o m // Create a sorted set of nodes to retain closest nodes in TreeSet<Node> closestSet = new TreeSet<>((x, y) -> idClosenessComparator.compare(x.getId(), y.getId())); // Execute requests Map<Subcoroutine<?>, Node> requestSubcoroutineToNodes = new HashMap<>(); // executing requests Set<Id> queriedSet = new HashSet<>(); // ids that have already been queried while (true) { // If there's room left to query more contacts that are closer to findId, do so... while (msgRouterController.size() < maxConcurrentRequests && !contactSet.isEmpty()) { // Get next farthest away node to contact Node contactNode = contactSet.pollLast(); // Add it to set of set of ids that have already been queried.. if it's already there, it means that it's already been // queried by this find, so skip it... boolean added = queriedSet.add(contactNode.getId()); if (!added) { continue; } // Add it to the set of closest nodes (will be removed if node fails to respond) closestSet.add(contactNode); // If we already have maxResult closer nodes to findId, skip this node if (closestSet.size() > maxResults) { Node removedNode = closestSet.pollLast(); if (removedNode == contactNode) { continue; } } // Initialize query Address destinationAddress = addressTransformer.toAddress(contactNode.getLink()) .appendSuffix(ROUTER_EXT_HANDLER_RELATIVE_ADDRESS); RequestSubcoroutine<FindResponse> reqSubcoroutine = new RequestSubcoroutine.Builder<FindResponse>() .sourceAddress(routerAddress, idGenerator).destinationAddress(destinationAddress) .timerAddress(timerAddress) .request(new FindRequest(advertiseSelf ? baseId : null, findId, maxResults)) .addExpectedResponseType(FindResponse.class).attemptInterval(Duration.ofSeconds(2L)) .maxAttempts(5).throwExceptionIfNoResponse(false).build(); ctx.addOutgoingMessage(subAddress, logAddress, info("Querying node {}", contactNode)); // Add query to router msgRouterController.add(reqSubcoroutine, AddBehaviour.ADD_PRIME_NO_FINISH); requestSubcoroutineToNodes.put(reqSubcoroutine, contactNode); } // If there are no more requests running, it means we're finished if (msgRouterController.size() == 0) { ctx.addOutgoingMessage(subAddress, logAddress, info("Find complete: {}", closestSet)); return new ArrayList<>(closestSet); } // Wait for next messange forward to the router cnt.suspend(); ForwardResult fr = msgRouter.forward(); // If a request completed from the forwarded message if (fr.isForwarded() && fr.isCompleted()) { // calling isCompleted by itself may throw an exception, check isForwarded first // Get response FindResponse findResponse = (FindResponse) fr.getResult(); if (findResponse == null) { // If failure, then mark as stale and remove from closest // DONT BOTHER WITH TRYING TO CALCULATE LOCKING/UNLOCKING LOGIC. THE LOGIC WILL BECOME EXTREMELY CONVOLUTED. THE QUERY // DID 5 REQUEST. IF NO ANSWER WAS GIVEN IN THE ALLOTED TIME, THEN MARK AS STALE! Node contactedNode = requestSubcoroutineToNodes.remove(fr.getSubcoroutine()); try { // not allowed to mark self as stale -- we may want to find self, but if we do and it's not responsive dont try to // mark it as stale if (!contactedNode.getId().equals(baseId)) { router.stale(contactedNode); } } catch (NodeNotFoundException nnfe) { // may have been removed (already marked as stale) / may not be in routing tree // Do nothing } closestSet.remove(contactedNode); } else { // If success, then add returned nodes to contacts Node[] nodes = findResponse.getNodes(); contactSet.addAll(Arrays.asList(nodes)); // If we don't want to find our own ID / query ourselves... remove any reference to our own ID in the contactSet // TODO: optimize this by removing before it's added to contactSet if (ignoreSelf) { contactSet.removeIf(x -> x.getId().equals(baseId)); } } } } }
From source file:darks.learning.word2vec.Word2Vec.java
/** * Calculate specify word's nearest or relate words * //from ww w . ja va2 s. c o m * @param word Specify word * @param topCount Result size * @return Nearest or relate words */ public Set<WordEntry> distance(String word, int topCount) { int resultSize = FastMath.min(topCount, wordNodes.size()); TreeSet<WordEntry> result = new TreeSet<WordEntry>(); WordNode node = wordNodes.get(word); if (node != null) { double minSim = Double.MIN_VALUE; for (WordNode target : wordNodes.values()) { if (target.name.equals(word)) { continue; } double sim = target.feature.dot(node.feature); if (sim > minSim) { result.add(new WordEntry(target.name, sim)); if (result.size() > resultSize) { result.pollLast(); } minSim = result.last().similar; } } } return result; }
From source file:org.callimachusproject.management.CalliServer.java
@Override public synchronized void setWebappOrigins(String[] origins) throws Exception { Map<String, String> previously = conf.getOriginRepositoryIDs(); final Map<String, String> newOrigins = new LinkedHashMap<String, String>(); for (String origin : origins) { if (!previously.containsKey(origin)) { verifyOrigin(origin);/* w ww . j ava 2s .c om*/ newOrigins.put(origin, getRepositoryId(origin)); } } final Map<String, String> oldOrigins = new LinkedHashMap<String, String>(); oldOrigins.putAll(previously); oldOrigins.keySet().removeAll(Arrays.asList(origins)); final Map<String, String> subsequently = new LinkedHashMap<String, String>(previously); subsequently.keySet().removeAll(oldOrigins.keySet()); conf.setOriginRepositoryIDs(subsequently); submit(new Callable<Void>() { public Void call() throws Exception { for (Map.Entry<String, String> e : oldOrigins.entrySet()) { String id = e.getValue(); if (!subsequently.values().contains(id) && manager.hasRepositoryConfig(id)) { String prefix = id + "/"; TreeSet<String> subset = new TreeSet<String>(); for (String other : manager.getRepositoryIDs()) { if (other.startsWith(prefix)) { subset.add(other); } } while (!subset.isEmpty()) { manager.removeRepository(subset.pollLast()); } manager.removeRepository(id); } } for (Map.Entry<String, String> e : newOrigins.entrySet()) { String origin = e.getKey(); String id = e.getValue(); CalliRepository repository = getSetupRepository(id, origin); try { SetupTool tool = new SetupTool(id, repository, conf); tool.setupWebappOrigin(origin); } finally { refreshRepository(id); } serveRealm(origin, id); } return null; } }); }
From source file:com.offbynull.voip.kademlia.model.RouteTreeNode.java
public void dumpAllNodesUnderTreeNode(Id id, TreeSet<Activity> output, int max, boolean includeStale, Set<BitString> skipPrefixes) { Validate.notNull(id);// ww w . j av a 2s. c o m Validate.notNull(output); // technically shouldn't contain any null elements, but we don't care since we're just adding to this Validate.notNull(skipPrefixes); Validate.noNullElements(skipPrefixes); Validate.isTrue(max >= 0); // why would anyone want 0 here? let thru anwyways // No more room in bucket? just leave right away. if (output.size() >= max) { return; } // Sort branches at this treenode by how close the are to the ID we're searching for... Go through the sorted branches in // order... // // If it's a bucket: dump it. // If it's a branch: recurse in to the branch and repeat // ArrayList<RouteTreeBranch> sortedBranches = new ArrayList<>(branches); Collections.sort(sortedBranches, new PrefixClosenessComparator(id, prefix.getBitLength(), suffixLen)); // What is the point of taking in an ID and sorting the branches in this tree node such that the we access the "closer" prefixes // first? We want to access the branches that are closer to the suffix of the ID first because ... // // // 1. Given the same prefix, we don't end up accessing the exact same set of nodes given. For example... // // 0/\1 // / EMPTY // 0/\1 // / FULL // 0/\1 // ME FULL // // Assume the routing tree above. We want to route to node 111, but bucket 1xx is empty. We then go down the other branch and // start grabbing nodes starting with prefix 0xx. We then use the suffix of 111 (x11) to determine which branches to traverse // down first for our 0xx nodes to return. We do this because we don't want to return the same set of nodes everytime someone // tries to access a 1xx node and we have an empty branch. // // For example... // if someone wanted 111 and 1xx was empty, path to search under 0xx would be 011, then 001, then 000. // if someone wanted 101 and 1xx was empty, path to search under 0xx would be 001, then 000, then 011. // // If we did something like a depth-first search, we'd always target 000 first, then 001, then 011. We don't want to do that // because we don't want to return the same set of nodes everytime. It would end up being an undue burden on those nodes. // // // // 2. Remember our notion of closeness: XOR and normal integer less-than to see which is closer. So for example, lets say we're // looking for ID 111110 and the prefix at this point in the tree is is 110xxx. Even though the prefix 110 doesn't match, we // still want to match as closely to the remaining suffix as possible, because when we XOR those extra 0's at the beginning of // the suffix mean that we're closer. // // For example... // // This tree node has the prefix 110xxx and the ID we're searching for is 111110. There are 2 branches at this tree node: // 1100xx and 1101xx // // 110xxx // /\ // / \ // / \ // 0 / \ 1 // / \ // 1100xx 1101xx // // We know that for ID 111110, the IDs under 1101xx WILL ALWAYS BE CLOSER than the IDs at 1100xx. // // XORing with the 1100xx bucket ... XOR(111110, 1100xx) = 0011xx // // XORing with the 1101xx bucket ... XOR(111110, 1101xx) = 0010xx // // // Remember how < works... go compare each single bit from the beginning until you come across a pair of bits that aren't // equal (one is 0 and the other is 1). The ID with 0 at that position is less-than the other one. // // // The one on the bottom (1101xx) will ALWAYS CONTAIN CLOSER IDs... // // An example ID in top: 110011 ... XOR(111110, 110011) = 001101 = 13 // An exmaple ID in bottom: 110100 ... XOR(111110, 110100) = 001010 = 9 // for (RouteTreeBranch sortedBranch : sortedBranches) { if (skipPrefixes.contains(sortedBranch.getPrefix())) { continue; } if (sortedBranch instanceof RouteTreeNodeBranch) { RouteTreeNode node = sortedBranch.getItem(); node.dumpAllNodesUnderTreeNode(id, output, max, includeStale, emptySet()); // dont propogate skipPrefixes (not relevant) // Bucket's full after dumping nodes in that branch. No point in continued processing. if (output.size() >= max) { return; } } else if (sortedBranch instanceof RouteTreeBucketBranch) { KBucket bucket = sortedBranch.getItem(); // don't bother with locked nodes for now, we're not supporting them output.addAll(bucket.dumpBucket(true, includeStale, false)); // Bucket's full after that add. No point in continued processing. if (output.size() >= max) { // If we have more than max elements from that last add, start evicting farthest away nodes while (output.size() > max) { output.pollLast(); } return; } } else { throw new IllegalStateException(); // should never happen } } }
From source file:org.apache.bookkeeper.stream.storage.impl.sc.DefaultStorageContainerController.java
@Override public ClusterAssignmentData computeIdealState(ClusterMetadata clusterMetadata, ClusterAssignmentData currentState, Set<BookieSocketAddress> currentCluster) { if (currentCluster.isEmpty()) { log.info("Current cluster is empty. No alive server is found."); return currentState; }/*from w w w.j a va 2s.c o m*/ // 1. get current server assignments Map<BookieSocketAddress, Set<Long>> currentServerAssignments; try { currentServerAssignments = currentState.getServersMap().entrySet().stream() .collect(Collectors.toMap(e1 -> { try { return new BookieSocketAddress(e1.getKey()); } catch (UnknownHostException uhe) { log.error("Invalid cluster "); throw new UncheckedExecutionException( "Invalid server found in current assignment map" + e1.getKey(), uhe); } }, e2 -> e2.getValue().getContainersList().stream().collect(Collectors.toSet()))); } catch (UncheckedExecutionException uee) { log.warn("Invalid cluster assignment data is found : {} - {}. Recompute assignment from empty state", currentState, uee.getCause().getMessage()); currentServerAssignments = Maps.newHashMap(); } Set<BookieSocketAddress> currentServersAssigned = currentServerAssignments.keySet(); // 2. if no servers is assigned, initialize the ideal state if (currentServersAssigned.isEmpty()) { return initializeIdealState(clusterMetadata, currentCluster); } // 3. get the cluster diffs Set<BookieSocketAddress> serversAdded = Sets.difference(currentCluster, currentServersAssigned) .immutableCopy(); Set<BookieSocketAddress> serversRemoved = Sets.difference(currentServersAssigned, currentCluster) .immutableCopy(); if (serversAdded.isEmpty() && serversRemoved.isEmpty()) { // cluster is unchanged, assuming the current state is ideal, no re-assignment is required. return currentState; } log.info( "Storage container controller detects cluster changed:\n" + "\t {} servers added: {}\n\t {} servers removed: {}", serversAdded.size(), serversAdded, serversRemoved.size(), serversRemoved); // 4. compute the containers that owned by servers removed. these containers are needed to be reassigned. Set<Long> containersToReassign = currentServerAssignments.entrySet().stream() .filter(serverEntry -> !currentCluster.contains(serverEntry.getKey())) .flatMap(serverEntry -> serverEntry.getValue().stream()).collect(Collectors.toSet()); // 5. use an ordered set as priority deque to sort the servers by the number of assigned containers TreeSet<Pair<BookieSocketAddress, LinkedList<Long>>> assignmentQueue = new TreeSet<>( new ServerAssignmentDataComparator()); for (Map.Entry<BookieSocketAddress, Set<Long>> entry : currentServerAssignments.entrySet()) { BookieSocketAddress host = entry.getKey(); if (!currentCluster.contains(host)) { if (log.isTraceEnabled()) { log.trace("Host {} is not in current cluster anymore", host); } continue; } else { if (log.isTraceEnabled()) { log.trace("Adding host {} to assignment queue", host); } assignmentQueue.add(Pair.of(host, Lists.newLinkedList(entry.getValue()))); } } // 6. add new servers for (BookieSocketAddress server : serversAdded) { assignmentQueue.add(Pair.of(server, Lists.newLinkedList())); } // 7. assign the containers that are needed to be reassigned. for (Long containerId : containersToReassign) { Pair<BookieSocketAddress, LinkedList<Long>> leastLoadedServer = assignmentQueue.pollFirst(); leastLoadedServer.getValue().add(containerId); assignmentQueue.add(leastLoadedServer); } // 8. rebalance the containers if needed int diffAllowed; if (assignmentQueue.size() > clusterMetadata.getNumStorageContainers()) { diffAllowed = 1; } else { diffAllowed = clusterMetadata.getNumStorageContainers() % assignmentQueue.size() == 0 ? 0 : 1; } Pair<BookieSocketAddress, LinkedList<Long>> leastLoaded = assignmentQueue.first(); Pair<BookieSocketAddress, LinkedList<Long>> mostLoaded = assignmentQueue.last(); while (mostLoaded.getValue().size() - leastLoaded.getValue().size() > diffAllowed) { leastLoaded = assignmentQueue.pollFirst(); mostLoaded = assignmentQueue.pollLast(); // move container from mostLoaded to leastLoaded Long containerId = mostLoaded.getValue().removeFirst(); // add the container to the end to avoid balancing this container again. leastLoaded.getValue().addLast(containerId); assignmentQueue.add(leastLoaded); assignmentQueue.add(mostLoaded); leastLoaded = assignmentQueue.first(); mostLoaded = assignmentQueue.last(); } // 9. the new ideal state is computed, finalize it Map<String, ServerAssignmentData> newAssignmentMap = Maps.newHashMap(); assignmentQueue.forEach(assignment -> newAssignmentMap.put(assignment.getKey().toString(), ServerAssignmentData.newBuilder().addAllContainers(assignment.getValue()).build())); return ClusterAssignmentData.newBuilder().putAllServers(newAssignmentMap).build(); }
From source file:org.apache.pig.impl.logicalLayer.LOLoad.java
public RequiredFieldResponse pushProjection(RequiredFieldList requiredFieldList) throws FrontendException { RequiredFieldResponse response = new RequiredFieldResponse(false); if (mSchema == null) return response; if (requiredFieldList.getFields() == null) return response; if (requiredFieldList.getFields() == null) { return response; }/*from www . j ava 2 s. c o m*/ this.requiredFieldList = requiredFieldList; if (mLoadFunc instanceof LoadPushDown) { response = ((LoadPushDown) mLoadFunc).pushProjection(requiredFieldList); } else { // loadfunc does not support pushing projections response = new RequiredFieldResponse(false); } if (!response.getRequiredFieldResponse()) return response; // Change LOLoad schema to reflect this pruning TreeSet<Integer> prunedIndexSet = new TreeSet<Integer>(); for (int i = 0; i < mSchema.size(); i++) prunedIndexSet.add(i); for (int i = 0; i < requiredFieldList.getFields().size(); i++) { RequiredField requiredField = requiredFieldList.getFields().get(i); if (requiredField.getIndex() >= 0) prunedIndexSet.remove(requiredField.getIndex()); else { try { int index = mSchema.getPosition(requiredField.getAlias()); if (index > 0) prunedIndexSet.remove(index); } catch (FrontendException e) { return new RequiredFieldResponse(false); } } } Integer index; while ((index = prunedIndexSet.pollLast()) != null) mSchema.getFields().remove(index.intValue()); mIsProjectionMapComputed = false; getProjectionMap(); return response; }
From source file:org.apache.solr.client.solrj.io.eval.KnnEvaluator.java
@Override public Object doWork(Object... values) throws IOException { if (values.length < 3) { throw new IOException("knn expects three parameters a Matrix, numeric array and k"); }/* www .ja v a2 s. c om*/ Matrix matrix = null; double[] vec = null; int k = 0; if (values[0] instanceof Matrix) { matrix = (Matrix) values[0]; } else { throw new IOException("The first parameter for knn should be a matrix."); } if (values[1] instanceof List) { List<Number> nums = (List<Number>) values[1]; vec = new double[nums.size()]; for (int i = 0; i < nums.size(); i++) { vec[i] = nums.get(i).doubleValue(); } } else { throw new IOException("The second parameter for knn should be a numeric array."); } if (values[2] instanceof Number) { k = ((Number) values[2]).intValue(); } else { throw new IOException("The third parameter for knn should be k."); } double[][] data = matrix.getData(); TreeSet<Neighbor> neighbors = new TreeSet(); for (int i = 0; i < data.length; i++) { double distance = distanceMeasure.compute(vec, data[i]); neighbors.add(new Neighbor(i, distance)); if (neighbors.size() > k) { neighbors.pollLast(); } } double[][] out = new double[neighbors.size()][]; List<String> rowLabels = matrix.getRowLabels(); List<String> newRowLabels = new ArrayList(); List<Number> distances = new ArrayList(); int i = -1; while (neighbors.size() > 0) { Neighbor neighbor = neighbors.pollFirst(); int rowIndex = neighbor.getRow(); if (rowLabels != null) { newRowLabels.add(rowLabels.get(rowIndex)); } out[++i] = data[rowIndex]; distances.add(neighbor.getDistance()); } Matrix knn = new Matrix(out); if (rowLabels != null) { knn.setRowLabels(newRowLabels); } knn.setColumnLabels(matrix.getColumnLabels()); knn.setAttribute("distances", distances); return knn; }
From source file:org.cloudata.core.commitlog.pipe.BufferPool.java
public ByteBuffer[] getBuffer(int size) { ByteBuffer retBuffer = null;//from www. jav a 2 s. c om synchronized (bufferMap) { TreeSet<PoolEntry> entrySet = bufferMap.get(size); if (entrySet != null) { PoolEntry entry = entrySet.pollLast(); if (entry != null) { retBuffer = entry.buffer; } } } if (retBuffer == null) { retBuffer = ByteBuffer.allocateDirect(size); poolMonitor.increaseAllocated(size); } else { poolMonitor.increaseUsed(size); } return new ByteBuffer[] { retBuffer }; }