简体   繁体   中英

PriorityQueue the right data structure?

I need to store action objects that have a perform-time property and a reference to the performer (unit) itself. When a unit performs an action it get's added to the PriorityQueue with the time it takes to perform that action. This performer is not allowed to perform again until it is first on the list again. I never worked with a priorityqueue before so I am unaware of it's capabilities.

So how to go about this? Let's have a look at the following sorted actions:

time:1600 -> unit:1
time:3700 -> unit:2
time:12000 -> unit:3

Now I want to access this data structure and lower all of these time properties by 1600 and enable unit one to perform again. If I keep adding to time I will soon run into the maximum. I could use a long but eventually I will run into the same problem. Anyway, I guess for each type I have to iterate through the complete list or set to alter object properties in it. When inserting I just compare time for fast insertion in a structure like PriorityQueue.

So what data structure should I use for this?

You can easily remove the first element from a priority queue.

I suggest you should use an additional integer value delta in which you can store the difference. With this approch you only have to calculate 3700-delta if you remove the node 3700/unit 2 from the priority queue. After removing an element, you have to change this delta again.

In fact, you do not change the order in your priority queue by adding a specific value to all entries so you don't have to change the data structure itself.

To prevent the delta from increasing to infinity, you have to update all entries after delta reaches a threshold. The advantage of this approach is, that you only have to update your data elements very infrequently.

The priority queue is one of the best approaches for this problem. The main advantage is that the element with the lowest priority is always at the top.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM