priority-queue

A priority queue which allows efficient priority update?

喜你入骨 提交于 2019-11-29 19:59:24
UPDATE : Here's my implementation of Hashed Timing Wheels . Please let me know if you have an idea to improve the performance and concurrency. (20-Jan-2009) // Sample usage: public static void main(String[] args) throws Exception { Timer timer = new HashedWheelTimer(); for (int i = 0; i < 100000; i ++) { timer.newTimeout(new TimerTask() { public void run(Timeout timeout) throws Exception { // Extend another second. timeout.extend(); } }, 1000, TimeUnit.MILLISECONDS); } } UPDATE : I solved this problem by using Hierarchical and Hashed Timing Wheels . (19-Jan-2009) I'm trying to implement a

Bounded PriorityBlockingQueue

风格不统一 提交于 2019-11-29 16:58:07
问题 PriorityBlockingQueue is unbounded, but I need to bound it somehow. What is the best way to achieve that? For information, the bounded PriorityBlockingQueue will be used in a ThreadPoolExecutor . NB: By bounded I don't want to throw Exception if that happens, I want to put the object in the queue and then cut it based on its priority value. Is there any good way to do this cut thingie? 回答1: I actually wouldn't subclass it. While I can't put together example code right now, I'd suggest a

Getting Message by priority from MSMQ

与世无争的帅哥 提交于 2019-11-29 14:13:19
问题 i am sending messages in MSMQ by setting its priority. using C# can i get the message from MSMQ having high priority first? just like we get in Priority Queue. and one thing more.. suppose there are three priority level 0 - high 1- medium 2 - low the sequence in queue is 2001122221111100 now if i send message with high priority(0) where it will be placed?? by setting priority in MSMQ. will it behave like actual Priority Queue? 回答1: MSMQ does support priority queuing of messages, however

Is there an alternative to Dictionary/SortedList that allows duplicates? [duplicate]

和自甴很熟 提交于 2019-11-29 13:22:58
Possible Duplicate: C# Sortable collection which allows duplicate keys Basically I'd like to make a Dictionary work with duplicate keys without going into custom comparer implementations. There is an idea of: Dictionary<key, List<value>> but it still has some overhead. I wish Dictionary had "AllowDuplicates". If you're using .NET 3.5 then Lookup is probably what you're after. .NET 2.0: PowerCollections contains the OrderedMultiDictionary . You still can use SortedList and try to make a unique key by combining your value and a Guid into a class. In this case, you must implement the IComparer

What is the time complexity of constructing a PriorityQueue from a collection?

微笑、不失礼 提交于 2019-11-29 10:48:52
What is the complexity of Java's PriorityQueue constructor with a Collection ? I used the constructor: PriorityQueue(Collection<? extends E> c) Is the complexity O(n) or O(n*log(n))? Stuart Marks The time complexity to initialize a PriorityQueue from a collection, even an unsorted one, is O(n). Internally this uses a procedure called siftDown() to "heapify" an array in-place. (This is also called pushdown in the literature.) This is counterintuitive. It seems like inserting an element into a heap is O(log n) so inserting n elements results in O(n log n) complexity. This is true if you insert

Priority Queue remove complexity time

依然范特西╮ 提交于 2019-11-29 09:05:44
What is the complexity (big-oh) for the remove() function on the Priority Queue class in Java? I can't find anything documented anywhere, I think it's O(n), considering you have to find the element before you remove it and then reshuffle the tree. but I've seen others that disagree and think it's O(logn). Any ideas? The confusion is actually caused by your "remove" function. In java, there are two remove functions. remove() -> This is to remove the head/root, it takes O(logN) time. remove(Object o) -> This is to remove an arbitrary object. Finding this object takes O(N) time, and removing it

Sorting PriorityQueue

别来无恙 提交于 2019-11-29 07:56:52
I am having a problem with PriorityQueues, as I am lead to believe it orders on priority however I am not sure what the priority is (I mean what the value is and where it comes from). A priorityQueue can be made with a comparator in the constructor and I have tried this but it does not work. Queue class: public JavaPriorityFlightQueue() { super(); flights = new PriorityQueue(5, new SortQueueViaPriority()); } Comparator: import java.util.Comparator; public class SortQueueViaPriority implements Comparator { public int compare(Object o1, Object o2){ Flight f1 = (Flight) o1; Flight f2 = (Flight)

Testing PriorityBlockingQueue in ThreadPoolExecutor

我是研究僧i 提交于 2019-11-29 04:54:24
I realized my ThreadPoolExecutor with PriorityBlockingQueue like in this example: https://stackoverflow.com/a/12722648/2206775 and wrote a test: PriorityExecutor executorService = (PriorityExecutor) PriorityExecutor.newFixedThreadPool(16); executorService.submit(new Runnable() { @Override public void run() { try { Thread.sleep(1000); Thread.sleep(1000); System.out.println("1"); } catch (InterruptedException e) { e.printStackTrace(); } } }, 1); executorService.submit(new Runnable() { @Override public void run() { try { Thread.sleep(1000); Thread.sleep(1000); System.out.println("3"); } catch

PriorityQueue has objects with the same priority

为君一笑 提交于 2019-11-29 03:59:04
I'm using a priority queue to sort and use a large number of custom objects. The objects have a "weight" that is their natural ordering. However, different objects that are inserted into the priority queue may have the same "weight". In such cases, I want the priority queue to order them in the same order in which they were put into the queue. For example, if I add in CustomObjects A,B,C,D in that order, all with the same "weight", than the priority queue should return them in that order as well - even if I poll one or more of the objects before adding in the others. Here is the CompareTo for

How to tell a std::priority_queue to refresh its ordering?

戏子无情 提交于 2019-11-29 01:41:05
I have a priority queue of pointers to a struct city . I modify the objects pointed by these pointers outside the priority queue, and want to tell the priority queue to "reorder" itself according to the new values. What should I do? Example: #include <iostream> #include <queue> using namespace std; struct city { int data; city *previous; }; struct Compare { bool operator() ( city *lhs, city *rhs ) { return ( ( lhs -> data ) >= ( rhs -> data ) ); } }; typedef priority_queue< city *, vector< city * >, Compare > pqueue; int main() { pqueue cities; city *city1 = new city; city1 -> data = 5; city1