-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathheapq.py
266 lines (190 loc) · 6.04 KB
/
heapq.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
266
# A port of https://github.com/python/cpython/blob/e42b7051/Lib/heapq.py
import heapq as hq
from numba.core import types
from numba.core.errors import TypingError
from numba.core.extending import overload, register_jitable
@register_jitable
def _siftdown(heap, startpos, pos):
newitem = heap[pos]
while pos > startpos:
parentpos = (pos - 1) >> 1
parent = heap[parentpos]
if newitem < parent:
heap[pos] = parent
pos = parentpos
continue
break
heap[pos] = newitem
@register_jitable
def _siftup(heap, pos):
endpos = len(heap)
startpos = pos
newitem = heap[pos]
childpos = 2 * pos + 1
while childpos < endpos:
rightpos = childpos + 1
if rightpos < endpos and not heap[childpos] < heap[rightpos]:
childpos = rightpos
heap[pos] = heap[childpos]
pos = childpos
childpos = 2 * pos + 1
heap[pos] = newitem
_siftdown(heap, startpos, pos)
@register_jitable
def _siftdown_max(heap, startpos, pos):
newitem = heap[pos]
while pos > startpos:
parentpos = (pos - 1) >> 1
parent = heap[parentpos]
if parent < newitem:
heap[pos] = parent
pos = parentpos
continue
break
heap[pos] = newitem
@register_jitable
def _siftup_max(heap, pos):
endpos = len(heap)
startpos = pos
newitem = heap[pos]
childpos = 2 * pos + 1
while childpos < endpos:
rightpos = childpos + 1
if rightpos < endpos and not heap[rightpos] < heap[childpos]:
childpos = rightpos
heap[pos] = heap[childpos]
pos = childpos
childpos = 2 * pos + 1
heap[pos] = newitem
_siftdown_max(heap, startpos, pos)
@register_jitable
def reversed_range(x):
# analogous to reversed(range(x))
return range(x - 1, -1, -1)
@register_jitable
def _heapify_max(x):
n = len(x)
for i in reversed_range(n // 2):
_siftup_max(x, i)
@register_jitable
def _heapreplace_max(heap, item):
returnitem = heap[0]
heap[0] = item
_siftup_max(heap, 0)
return returnitem
def assert_heap_type(heap):
if not isinstance(heap, (types.List, types.ListType)):
raise TypingError('heap argument must be a list')
dt = heap.dtype
if isinstance(dt, types.Complex):
msg = ("'<' not supported between instances "
"of 'complex' and 'complex'")
raise TypingError(msg)
def assert_item_type_consistent_with_heap_type(heap, item):
if not heap.dtype == item:
raise TypingError('heap type must be the same as item type')
@overload(hq.heapify)
def hq_heapify(x):
assert_heap_type(x)
def hq_heapify_impl(x):
n = len(x)
for i in reversed_range(n // 2):
_siftup(x, i)
return hq_heapify_impl
@overload(hq.heappop)
def hq_heappop(heap):
assert_heap_type(heap)
def hq_heappop_impl(heap):
lastelt = heap.pop()
if heap:
returnitem = heap[0]
heap[0] = lastelt
_siftup(heap, 0)
return returnitem
return lastelt
return hq_heappop_impl
@overload(hq.heappush)
def heappush(heap, item):
assert_heap_type(heap)
assert_item_type_consistent_with_heap_type(heap, item)
def hq_heappush_impl(heap, item):
heap.append(item)
_siftdown(heap, 0, len(heap) - 1)
return hq_heappush_impl
@overload(hq.heapreplace)
def heapreplace(heap, item):
assert_heap_type(heap)
assert_item_type_consistent_with_heap_type(heap, item)
def hq_heapreplace(heap, item):
returnitem = heap[0]
heap[0] = item
_siftup(heap, 0)
return returnitem
return hq_heapreplace
@overload(hq.heappushpop)
def heappushpop(heap, item):
assert_heap_type(heap)
assert_item_type_consistent_with_heap_type(heap, item)
def hq_heappushpop_impl(heap, item):
if heap and heap[0] < item:
item, heap[0] = heap[0], item
_siftup(heap, 0)
return item
return hq_heappushpop_impl
def check_input_types(n, iterable):
if not isinstance(n, (types.Integer, types.Boolean)):
raise TypingError("First argument 'n' must be an integer")
# heapq also accepts 1.0 (but not 0.0, 2.0, 3.0...) but
# this isn't replicated
if not isinstance(iterable, (types.Sequence, types.Array, types.ListType)):
raise TypingError("Second argument 'iterable' must be iterable")
@overload(hq.nsmallest)
def nsmallest(n, iterable):
check_input_types(n, iterable)
def hq_nsmallest_impl(n, iterable):
if n == 0:
return [iterable[0] for _ in range(0)]
elif n == 1:
out = min(iterable)
return [out]
size = len(iterable)
if n >= size:
return sorted(iterable)[:n]
it = iter(iterable)
result = [(elem, i) for i, elem in zip(range(n), it)]
_heapify_max(result)
top = result[0][0]
order = n
for elem in it:
if elem < top:
_heapreplace_max(result, (elem, order))
top, _order = result[0]
order += 1
result.sort()
return [elem for (elem, order) in result]
return hq_nsmallest_impl
@overload(hq.nlargest)
def nlargest(n, iterable):
check_input_types(n, iterable)
def hq_nlargest_impl(n, iterable):
if n == 0:
return [iterable[0] for _ in range(0)]
elif n == 1:
out = max(iterable)
return [out]
size = len(iterable)
if n >= size:
return sorted(iterable)[::-1][:n]
it = iter(iterable)
result = [(elem, i) for i, elem in zip(range(0, -n, -1), it)]
hq.heapify(result)
top = result[0][0]
order = -n
for elem in it:
if top < elem:
hq.heapreplace(result, (elem, order))
top, _order = result[0]
order -= 1
result.sort(reverse=True)
return [elem for (elem, order) in result]
return hq_nlargest_impl