@@ -173,40 +173,6 @@ cdef class Stack:
173
173
# PriorityHeap data structure
174
174
# =============================================================================
175
175
176
- cdef void heapify_up(PriorityHeapRecord* heap, SIZE_t pos) nogil:
177
- """ Restore heap invariant parent.improvement > child.improvement from
178
- ``pos`` upwards. """
179
- if pos == 0 :
180
- return
181
-
182
- cdef SIZE_t parent_pos = (pos - 1 ) / 2
183
-
184
- if heap[parent_pos].improvement < heap[pos].improvement:
185
- heap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]
186
- heapify_up(heap, parent_pos)
187
-
188
-
189
- cdef void heapify_down(PriorityHeapRecord* heap, SIZE_t pos,
190
- SIZE_t heap_length) nogil:
191
- """ Restore heap invariant parent.improvement > children.improvement from
192
- ``pos`` downwards. """
193
- cdef SIZE_t left_pos = 2 * (pos + 1 ) - 1
194
- cdef SIZE_t right_pos = 2 * (pos + 1 )
195
- cdef SIZE_t largest = pos
196
-
197
- if (left_pos < heap_length and
198
- heap[left_pos].improvement > heap[largest].improvement):
199
- largest = left_pos
200
-
201
- if (right_pos < heap_length and
202
- heap[right_pos].improvement > heap[largest].improvement):
203
- largest = right_pos
204
-
205
- if largest != pos:
206
- heap[pos], heap[largest] = heap[largest], heap[pos]
207
- heapify_down(heap, largest, heap_length)
208
-
209
-
210
176
cdef class PriorityHeap:
211
177
""" A priority queue implemented as a binary heap.
212
178
@@ -240,6 +206,38 @@ cdef class PriorityHeap:
240
206
cdef bint is_empty(self ) nogil:
241
207
return self .heap_ptr <= 0
242
208
209
+ cdef void heapify_up(self , PriorityHeapRecord* heap, SIZE_t pos) nogil:
210
+ """ Restore heap invariant parent.improvement > child.improvement from
211
+ ``pos`` upwards. """
212
+ if pos == 0 :
213
+ return
214
+
215
+ cdef SIZE_t parent_pos = (pos - 1 ) / 2
216
+
217
+ if heap[parent_pos].improvement < heap[pos].improvement:
218
+ heap[parent_pos], heap[pos] = heap[pos], heap[parent_pos]
219
+ self .heapify_up(heap, parent_pos)
220
+
221
+ cdef void heapify_down(self , PriorityHeapRecord* heap, SIZE_t pos,
222
+ SIZE_t heap_length) nogil:
223
+ """ Restore heap invariant parent.improvement > children.improvement from
224
+ ``pos`` downwards. """
225
+ cdef SIZE_t left_pos = 2 * (pos + 1 ) - 1
226
+ cdef SIZE_t right_pos = 2 * (pos + 1 )
227
+ cdef SIZE_t largest = pos
228
+
229
+ if (left_pos < heap_length and
230
+ heap[left_pos].improvement > heap[largest].improvement):
231
+ largest = left_pos
232
+
233
+ if (right_pos < heap_length and
234
+ heap[right_pos].improvement > heap[largest].improvement):
235
+ largest = right_pos
236
+
237
+ if largest != pos:
238
+ heap[pos], heap[largest] = heap[largest], heap[pos]
239
+ self .heapify_down(heap, largest, heap_length)
240
+
243
241
cdef int push(self , SIZE_t node_id, SIZE_t start, SIZE_t end, SIZE_t pos,
244
242
SIZE_t depth, bint is_leaf, double improvement,
245
243
double impurity, double impurity_left,
@@ -276,7 +274,7 @@ cdef class PriorityHeap:
276
274
heap[heap_ptr].improvement = improvement
277
275
278
276
# Heapify up
279
- heapify_up(heap, heap_ptr)
277
+ self . heapify_up(heap, heap_ptr)
280
278
281
279
# Increase element count
282
280
self .heap_ptr = heap_ptr + 1
@@ -298,7 +296,7 @@ cdef class PriorityHeap:
298
296
299
297
# Restore heap invariant
300
298
if heap_ptr > 1 :
301
- heapify_down(heap, 0 , heap_ptr - 1 )
299
+ self . heapify_down(heap, 0 , heap_ptr - 1 )
302
300
303
301
self .heap_ptr = heap_ptr - 1
304
302
0 commit comments