-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityqueue.py
More file actions
53 lines (44 loc) · 1.51 KB
/
priorityqueue.py
File metadata and controls
53 lines (44 loc) · 1.51 KB
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
'''
class PriorityQueue
Created on 2010-10-14
@author: http://swinbrain.ict.swin.edu.au/wiki/Python_Samples_-_Priority_Queue
'''
import heapq
class PriorityQueue:
'''
classdocs
'''
#----------------------------------------------------------------------
def __init__(self):
self.heap = []
#----------------------------------------------------------------------
def clear(self):
self.heap = []
#----------------------------------------------------------------------
def insert(self, object):
heapq.heappush(self.heap, object)
#----------------------------------------------------------------------
def top(self):
assert(not self.isEmpty())
return self.heap[0]
#----------------------------------------------------------------------
def isEmpty(self):
return self.heap == []
#----------------------------------------------------------------------
def size(self):
return len(self.heap)
#----------------------------------------------------------------------
def pop(self):
assert(not self.isEmpty())
return heapq.heappop(self.heap)
#----------------------------------------------------------------------
def remove(self, object):
i = -1
for j in range(len(self.heap)):
if self.heap[j] == object:
i = j
break
if i == -1:
return
self.heap = self.heap[:i] + self.heap[i+1:]
heapq.heapify(self.heap)