-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxheap.py
265 lines (207 loc) · 8.15 KB
/
xheap.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from heapq import heapify, heappushpop, heapreplace, heappop, heappush
__version__ = '0.17'
__version_info__ = (0, 17)
__all__ = ['Heap', 'OrderHeap', 'RemovalHeap', 'XHeap', 'InvalidHeapError']
class Heap(list):
"""
Heap shamelessly built upon heapq providing the following benefits:
- object orientation
- check_invariant
It uses __lt__ for comparison (except for python 2 in case you don't define a __lt__ method, then its __le__).
Heap Invariant: a[k] <= a[2*k+1] and a[k] <= a[2*k+2]
"""
def __init__(self, iterable=[]):
super(Heap, self).__init__(iterable)
self.heapify()
def peek(self):
return self[0]
def push(self, item):
heappush(self, item)
def pop(self):
return heappop(self)
def remove(self, item):
raise NotImplementedError
def heapify(self):
heapify(self)
def poppush(self, item):
"""Because I always forget what replace means."""
return heapreplace(self, item)
replace = poppush
def pushpop(self, item):
return heappushpop(self, item)
def check(self):
self.check_invariant()
def check_invariant(self):
for index in range(super(Heap, self).__len__()-1, 0, -1):
parent_index = (index-1) >> 1
if self[index] < self[parent_index]:
raise InvalidHeapError('heap invariant (heap[{parent_index}] <= heap[{index}]) violated: {parent} !<= {item}'.format(parent=self[parent_index], parent_index=parent_index, item=self[index], index=index))
def __repr__(self):
return 'Heap({content})'.format(content=super(Heap, self).__repr__())
class OrderHeap(Heap):
"""
OrderHeap is a heap that allows you to specify the sorting criteria which might come in handy for
- several heaps for the same set of items but different orders
- reversing the heap order aka max-heap
"""
def __init__(self, iterable=[], key=None):
if not key:
raise RuntimeError('specify key when using OrderHeap; otherwise, just use Heap')
self.key = key
super(OrderHeap, self).__init__((key(item), item) for item in iterable)
def peek(self):
return self[0][1]
def push(self, item):
super(OrderHeap, self).push((self.key(item), item))
def pop(self):
return super(OrderHeap, self).pop()[1]
def poppush(self, item):
return heapreplace(self, (self.key(item), item))[1]
replace = poppush
def pushpop(self, item):
return heappushpop(self, (self.key(item), item))[1]
def __iter__(self):
return (item_tuple[1] for item_tuple in super(Heap, self).__iter__())
def __contains__(self, item):
return item in iter(self)
def __repr__(self):
return 'OrderHeap({content}, key={key})'.format(content=list(self), key=self.key)
class RemovalHeap(Heap):
"""
RemovalHeap is a heap that allows you to remove an item without knowing its index in the heap; useful when
- users want cancel tasks from a task queue
- you have two queues of same items, pop an item from one and you want to remove it from the other, too
"""
def __init__(self, iterable=[]):
_list = list(iterable)
self._item_set = set(_list)
if len(_list) != len(self._item_set):
raise RuntimeError('duplicate items not allowed: {_list}'.format(_list=_list))
super(RemovalHeap, self).__init__(_list)
def peek(self):
return_item = self[0]
while return_item not in self._item_set:
heappop(self)
return_item = self[0]
return return_item
def push(self, item):
if item in self._item_set:
raise RuntimeError('duplicate item not allowed: {item}'.format(item=item))
heappush(self, item)
self._item_set.add(item)
def pop(self):
return_item = heappop(self)
while return_item not in self._item_set:
return_item = heappop(self)
self._item_set.remove(return_item)
self.sweep()
return return_item
def remove(self, item):
self._item_set.remove(item)
self.sweep()
def poppush(self, item):
if item in self._item_set:
raise RuntimeError('duplicate item not allowed: {item}'.format(item=item))
self._item_set.add(item)
while self[0] not in self._item_set:
heappop(self)
return_item = heapreplace(self, item)
self._item_set.remove(return_item)
return return_item
replace = poppush
def pushpop(self, item):
if item in self._item_set:
raise RuntimeError('duplicate item not allowed: {item}'.format(item=item))
self._item_set.add(item)
return_item = heappushpop(self, item)
while return_item not in self._item_set:
return_item = heappop(self)
self._item_set.remove(return_item)
return return_item
def sweep(self):
if 2*len(self._item_set) < super(RemovalHeap, self).__len__():
self[:] = list(self)
self.heapify()
def __iter__(self):
return iter(self._item_set)
def __contains__(self, item):
return item in self._item_set
def __len__(self):
return len(self._item_set)
def __repr__(self):
return 'RemovalHeap({content})'.format(content=list(self))
class XHeap(Heap):
"""Hybrid of OrderHeap and RemovalHeap."""
# order + removal
def __init__(self, iterable=[], key=None):
if not key:
raise RuntimeError('specify key when using XHeap; otherwise, just use RemovalHeap')
self.key = key
_list = list(iterable)
self._item_set = set(_list)
if len(_list) != len(self._item_set):
raise RuntimeError('duplicate items not allowed: {_list}'.format(_list=_list))
super(XHeap, self).__init__((key(item), item) for item in _list)
# order
def peek(self):
return_item = self[0][1]
while return_item not in self._item_set:
heappop(self)
return_item = self[0][1]
return return_item
# order + removal
def push(self, item):
if item in self._item_set:
raise RuntimeError('duplicate item not allowed: {item}'.format(item=item))
heappush(self, (self.key(item), item))
self._item_set.add(item)
def pop(self):
return_item = heappop(self)[1]
while return_item not in self._item_set:
return_item = heappop(self)[1]
self._item_set.remove(return_item)
self.sweep()
return return_item
def remove(self, item):
self._item_set.remove(item)
self.sweep()
def sweep(self):
if 2*len(self._item_set) < super(XHeap, self).__len__():
self[:] = (item_tuple for item_tuple in super(XHeap, self).__iter__() if item_tuple[1] in self._item_set)
self.heapify()
# order + removal
def poppush(self, item):
if item in self._item_set:
raise RuntimeError('duplicate item not allowed: {item}'.format(item=item))
self._item_set.add(item)
while self[0][1] not in self._item_set:
heappop(self)
return_item = heapreplace(self, (self.key(item), item))[1]
self._item_set.remove(return_item)
return return_item
replace = poppush
# order + removal
def pushpop(self, item):
if item in self._item_set:
raise RuntimeError('duplicate item not allowed: {item}'.format(item=item))
self._item_set.add(item)
return_item = heappushpop(self, (self.key(item), item))[1]
while return_item not in self._item_set:
return_item = heappop(self)[1]
self._item_set.remove(return_item)
return return_item
# removal
def __iter__(self):
return iter(self._item_set)
# removal
def __contains__(self, item):
return item in self._item_set
# removal
def __len__(self):
return len(self._item_set)
def __repr__(self):
return 'XHeap({content}, key={key})'.format(content=list(self), key=self.key)
class InvalidHeapError(RuntimeError):
pass