priority-queue

Java find k closest points to a given 2D point, quickly

有些话、适合烂在心里 提交于 2019-12-11 11:54:56
问题 I function that takes a set of points, checks their distance against a given point and returns an ordered set with the closest points first. In case 2 points have the same distance from the given point, both will be kept, no problem. I use that function to pick the k closest points to any user-given point with 2D coordinates. It's just a matter of picking the first k points from the ordered set. Right now it's something like this, and I guess the distance calculation is called again and again

Accessing reference wrapper elements in vector c++11

淺唱寂寞╮ 提交于 2019-12-11 11:28:36
问题 In Graph class: typedef std::pair<double, Node&> PIRV; In another class that uses graph: typedef std::priority_queue<Graph::PIRV&, vector<std::reference_wrapper<Graph::PIRV>>, compareEdge> PECMP; Now I am trying to access the first element in the priority queue ( PECMP someQueue ) by doing double a = someQueue.top().first However I get the following error: error: ‘const value_type’ has no member named ‘first’ What is the better way to access elements stored in reference wrapper? Thanks 回答1:

How to write a sortRemove method for MaxHeapPriorityQueue in Java?

做~自己de王妃 提交于 2019-12-11 07:34:33
问题 I am working on a helper method private E sortRemove(), for my HeapSort static method. Let me note, that this heap is a MaxHeapPriorityQueue, which all the elements have a child that has a value that is smaller than the parent. I am trying to Call remove repeatedly until the heap is empty. But make it so that when an element is "removed", it is moved to the end of the array instead of completely evicted from the array. When you are done, voila! The array is sorted. I'm trying to figure out

Bad_alloc exception when using new for a struct c++

久未见 提交于 2019-12-11 07:26:02
问题 I am writing a query processor which allocates large amounts of memory and tries to find matching documents. Whenever I find a match, I create a structure to hold two variables describing the document and add it to a priority queue. Since there is no way of knowing how many times I will do this, I tried creating my structs dynamically using new. When I pop a struct off the priority queue, the queue (STL priority queue implementation) is supposed to call the object's destructor. My struct code

Find minimum in array of numbers using Verilog for Priority Queue implementation

心已入冬 提交于 2019-12-11 07:19:03
问题 I'm quite a novice to Verilog, but I have an array of 16-elements (each element is 16-bits long) and I wish to find the minimum entry the array, return the minimum, and re-arrange all the entries in the array that come after the minimum so that the array is one contiguous block of entries. I know I have to use a comparator, but I really have no idea where to start with regards to comparing a large group of numbers and determining the minimum. EDIT: What I'm actually making is a priority queue

Microsoft Collections for .NET [closed]

 ̄綄美尐妖づ 提交于 2019-12-11 04:07:26
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . .NET comes with some built in collections (stack, queue, dictionary, list etc.) but others common collections, like priority queues, are missing. There are many third party collection libraries on NuGet but I was wondering if there is an official Microsoft one (like the BCL immutable collections now called

Why do I get the following result? [duplicate]

走远了吗. 提交于 2019-12-10 15:19:00
问题 This question already has an answer here : Why does my PriorityBlockingQueue in Java not sort properly? (1 answer) Closed 5 years ago . I have done the following test to see how a PriorityBlockingQueue<String> pq = new PriorityBlockingQueue<>(2); pq.put("Sing"); pq.put("Sing2"); pq.put("Sing3"); pq.put("Sing4"); pq.put("Sing10"); pq.put("Sing11"); pq.put("Sing12"); pq.put("Sing13"); for (String s1: pq) { System.out.print(s1 +" "); } The result I get is: Sing Sing10 Sing11 Sing13 Sing2 Sing3

Free implementation of “bounded priority queue” in C++

…衆ロ難τιáo~ 提交于 2019-12-10 14:25:26
问题 I'm looking for a free software implementation of the bounded priority queue abstraction in C++. Basically, I need a data structure that will behave just like std::priority_queue but will at all times hold the "best" n elements at most. Example: std::vector<int> items; // many many input items bounded_priority_queue<int> smallest_items(5); for(vector<int>::const_iterator it=items.begin(); it!=items.end(); it++) { smallest_items.push(*it); } // now smallest_items holds the 5 smallest integers

Time Complexity of Java PriorityQueue (heap) insertion of n elements? [duplicate]

醉酒当歌 提交于 2019-12-10 13:14:58
问题 This question already has answers here : How can building a heap be O(n) time complexity? (16 answers) Closed 2 years ago . I would like to know what the time complexity of Java PriorityQueue.Add() is for n elements. I understand that potential worse case insertion a single element is O(log(n)) , but it is not clear to me what the time complexity is for inserting a collection of n elements? I've seen claims from various sources (with no proofs) that the time to build a priority queue heap of

How to compare generic nodes in a linked list using Comparable?

扶醉桌前 提交于 2019-12-10 13:09:39
问题 I am implementing a sorted list using linked lists. My node class looks like this public class Node<E>{ E elem; Node<E> next, previous; } In the sorted list class I have the add method, where I need to compare generic objects based on their implementation of compareTo() methods, but I get this syntax error "The method compareTo(E) is undefined for type E". I have tried implemnting the compareTo method in Node, but then I can't call any of object's methods, because E is generic type. Here is