C ++ priority queue - reordering based on updated priorities

Some reason: I am creating a thread manager C++that allows the user to create an object AsyncJoband prioritize execution. I have a JobManagersingleton class that manages the priority queue for these AsyncJobsand assigns them to the stream when it is available.

Problem. Users should be able to change priority AFTER. For example, based on some runtime events, a file may need to be downloaded more urgently than others. The problem I am facing is that priority queues only reorder items in the internal heap when calling push()or pop(). As far as I know, there is no open interface that allows you to request reordering based on changing priorities.

What I would like to do is something like this:

  • Create hashmapin my class JobManagerthat contains pointers to objects in the priority queue
  • A user can access a given task by his key and update priority through a hash map
  • It then JobManagersignals priority queues that have changed priorities.
  • Priority queue reorders itself

What would be the best way to do this? Should I expect to create my own priority queue class? Or perhaps extends from std::priority_queue?

Thank!

+4
source share
3 answers

One option might be to allow AsyncJob to be “canceled” and simply add a new (copy) AsyncJob to your PQ every time the priority changes, after canceling the old one.

, , " " , , , "". , , .

+3

A .

, -, , / .

, , - , - .

O(log n), O(log n) ( O(1), ).


++:

, map , set < pair >, , (, , BST).

- unordered_map map pre-++ 11.

+2

, , ( , ), , . , , - , .

EDIT: , , std::priority_queue poppinh , , std::vector std::priority_queue, std::vector . , , API std::priority_queue. , , , .

+1

Source: https://habr.com/ru/post/1544590/


All Articles