-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_heap.py
53 lines (44 loc) · 1.47 KB
/
priority_heap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from heapq import heapify, heappush, heappop
from dataclasses import dataclass, field
from typing import Any
from itertools import count
@dataclass(order=True)
class Node:
priority: Any = field(compare=True)
task: Any = field(compare=False)
class PriorityHeap:
def __init__(self, lst):
self.counter = count()
self.size = 0
self.entry_finder = {}
self.heap = list()
for task, pri in lst.items():
node = self.create_node(task, pri)
self.entry_finder[task] = node
self.heap.append(node)
heapify(self.heap)
self.size = len(self.heap)
def create_node(self, task, priority):
return Node((priority, next(self.counter)), task)
def add_task(self, task, priority):
if task in self.entry_finder:
self.remove_task(task)
node = self.create_node(task, priority)
self.entry_finder[task] = node
heappush(self.heap, node)
self.size += 1
def remove_task(self, task):
entry = self.entry_finder.pop(task)
entry.item = None
self.size -= 1
def pop_task(self):
while self.size > 0:
node = heappop(self.heap)
if node.task is not None:
del self.entry_finder[node.task]
self.size -= 1
return node.task
self.heap = list()
raise KeyError('pop from an empty priority queue')
def __len__(self):
return self.size