Dec-29-2022, 05:09 AM
I'm trying to implement a priority queue which allows me to update details of items already in queue.
I found a simple implementation in the Python documentation, which I'm working through to understand (https://docs.python.org/2/library/heapq....tion-notes).
However, there's a portion of this example which the understanding of escapes me; and I'm hoping someone can help me clarify?
The specific example is:
The remove_task(task) pops the item in the dictionary to a variable, and the last components of the variable's content is set to REMOVED.
However, that variable only lives in the scope of the remove_task() function, and nowhere does the add_task() or remove_task() functions update the queue when the item is marked for removal.
So I do not understand how this line: [1, 1, '<removed-task>'], comes into existence when printing the queue.
I only see reference to the dictionalry "entry_finder", but nowhere to I see code which updates the item in the queue to "<removed-task>", so how did it happen?
Can anyone explain?
Much appreciated
I found a simple implementation in the Python documentation, which I'm working through to understand (https://docs.python.org/2/library/heapq....tion-notes).
However, there's a portion of this example which the understanding of escapes me; and I'm hoping someone can help me clarify?
The specific example is:
import itertools import heapq pq = [] # list of entries arranged in a heap entry_finder = {} # mapping of tasks to entries REMOVED = '<removed-task>' # placeholder for a removed task counter = itertools.count() # unique sequence count def add_task(task, priority=0): 'Add a new task or update the priority of an existing task' if task in entry_finder: remove_task(task) count = next(counter) entry = [priority, count, task] entry_finder[task] = entry heappush(pq, entry) def remove_task(task): 'Mark an existing task as REMOVED. Raise KeyError if not found.' entry = entry_finder.pop(task) entry[-1] = REMOVED def pop_task(): 'Remove and return the lowest priority task. Raise KeyError if empty.' while pq: priority, count, task = heappop(pq) if task is not REMOVED: del entry_finder[task] return task raise KeyError('pop from an empty priority queue') #Testing the above: add_task('Planning') add_task('Design',1) add_task('Building',2) print(pq) #output is: [[0, 0, 'Planning'], [1, 1, 'Design'], [2, 2, 'Building']] add_task('Design',1) #this should update an existing entry print(pq) ''' output: [[0, 0, 'Planning'], [1, 1, '<removed-task>'], [2, 2, 'Building'], [1, 3, 'Design']] '''When I work through the example I'm struggling to understand how the second item in the queue gets updated when the second add_task('Design',1) is executed.
The remove_task(task) pops the item in the dictionary to a variable, and the last components of the variable's content is set to REMOVED.
However, that variable only lives in the scope of the remove_task() function, and nowhere does the add_task() or remove_task() functions update the queue when the item is marked for removal.
So I do not understand how this line: [1, 1, '<removed-task>'], comes into existence when printing the queue.
I only see reference to the dictionalry "entry_finder", but nowhere to I see code which updates the item in the queue to "<removed-task>", so how did it happen?
Can anyone explain?
Much appreciated