priority-queue

How to get inverse of a comparator in java

匆匆过客 提交于 2019-12-01 16:13:54
In a method I receive a generic object E extends Comparable<E> as an argument. Now i want to create two priority queues.One which uses the comparator used by E and other queue which uses the opposite of comparator used by E(i.e. if E uses '<' then second queue must use '>='). Please hep me how to create two such queues. queue2=new PriorityQueue<E>(0,Collections.reverseOrder(e)); I am getting the error that reverseOrder is not applicable. please help Look at Collections.reverseOrder . Your object E extends java.lang.Comparable, but it is not a java.util.Comparator . Create your first queue w/o

Why PriorityQueue requires API 24 in Android

点点圈 提交于 2019-12-01 13:54:16
PriorityQueue was added in Java 1.5 new PriorityQueue() is enabled in Android, but new PriorityQueue(new Comparator() { @Override public int compare(Object o1, Object o2) { return 0; } }); requires API 24. Why? Because PriorityQueue(Comparator) constructor was added to the SDK in API level 24. In JDK, that constructor was added in Java 8. For compatibility with earlier API levels, you can use PriorityQueue(int,Comparator) that has been there since API level 1. That is because PriorityQueue in Java 1.5 does not have PriorityQueue (Comparator<? super E> comparator) constructor at all. It was

How to orderly traverse a Boost.Heap Priority Queue and update a given element?

拟墨画扇 提交于 2019-12-01 13:16:29
I'm looking for a good data structure that can maintain its elements sorted. Currently I'm trying Boost.Heap . I frequently need to orderly traverse the data structure and when reaching an element based on some property, update its priority. Boost.Heap priority queues provide ordered and non-ordered iterators. Element updates occurs through a node handle, a handle can be obtained from a ordinary non-ordered iterator, but not directly from a ordered one as in the following example: #include <iostream> #include <algorithm> #include <boost/heap/fibonacci_heap.hpp> using namespace boost::heap; int

Why PriorityQueue requires API 24 in Android

依然范特西╮ 提交于 2019-12-01 11:52:36
问题 PriorityQueue was added in Java 1.5 new PriorityQueue() is enabled in Android, but new PriorityQueue(new Comparator() { @Override public int compare(Object o1, Object o2) { return 0; } }); requires API 24. Why? 回答1: Because PriorityQueue(Comparator) constructor was added to the SDK in API level 24. In JDK, that constructor was added in Java 8. For compatibility with earlier API levels, you can use PriorityQueue(int,Comparator) that has been there since API level 1. 回答2: That is because

Special case scheduling [closed]

感情迁移 提交于 2019-12-01 11:19:19
So here's the question. While studying about process scheduling I came across two seemingly contradictory examples I just can't get my head around. The problem arises if for instance in the priority non-preemptive scheduling algorithm which always chooses the process with the highest priority to be run next and once running, process can only volontarily give up its CPU time, that is no other process can run until the currently running process finishes. It seems that what the solution the book proposes is that if both end of one process and arrival of the new high-priority process occur at the

how to change the value of std::priority_queue top()?

耗尽温柔 提交于 2019-12-01 10:52:33
When using std::priority_queue top() it returns a constant reference. So is there a way I can both take advantage of std::priority_queue and change the value of top()? Daniel Frey I had to clarify one point about associative containers first, but now I'm finally able to write my answer to this question. The basic strategy when modifying the key of an object that is part of an associave container has already been outlined in the comment from @Xymostech. You copy/retrieve the element, remove it from the container, modify it and finally re-insert it to the container. Your question and your idea

Java: Access local variables from anon inner class? (PriorityQueue)

放肆的年华 提交于 2019-12-01 10:36:38
I want to use a PriorityQueue to do a topological sort on a graph. For brevity, I'd like to use an anonymous inner class for the comparator. However, I need access to the graph g in order to determine the in degree of the nodes I'm looking at. Is this possible? /** * topological sort * @param g must be a dag */ public static Queue<String> topoSort(DirectedGraph<String, DefaultEdge> g) { Queue<String> result = new PriorityQueue<String>(g.vertexSet().size(), new Comparator<String>() { DirectedGraph<String, DefaultEdge> g; @Override public int compare(String arg0, String arg1) { if (g.inDegreeOf

How to orderly traverse a Boost.Heap Priority Queue and update a given element?

流过昼夜 提交于 2019-12-01 09:49:21
问题 I'm looking for a good data structure that can maintain its elements sorted. Currently I'm trying Boost.Heap. I frequently need to orderly traverse the data structure and when reaching an element based on some property, update its priority. Boost.Heap priority queues provide ordered and non-ordered iterators. Element updates occurs through a node handle, a handle can be obtained from a ordinary non-ordered iterator, but not directly from a ordered one as in the following example: #include

Special case scheduling [closed]

天涯浪子 提交于 2019-12-01 09:41:38
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . So here's the question. While studying about process scheduling I came across two seemingly contradictory examples I just can't get my head around. The problem arises if for instance in the priority non-preemptive scheduling algorithm which always chooses the process with the highest priority to be

how to change the value of std::priority_queue top()?

倖福魔咒の 提交于 2019-12-01 08:22:28
问题 When using std::priority_queue top() it returns a constant reference. So is there a way I can both take advantage of std::priority_queue and change the value of top()? 回答1: I had to clarify one point about associative containers first, but now I'm finally able to write my answer to this question. The basic strategy when modifying the key of an object that is part of an associave container has already been outlined in the comment from @Xymostech. You copy/retrieve the element, remove it from