Move smaller child up until hitting a leaf. Built to mimic code for heapq._siftup only updating position dict too.
(self, pos)
| 237 | self._siftup(pos) |
| 238 | |
| 239 | def _siftup(self, pos): |
| 240 | """Move smaller child up until hitting a leaf. |
| 241 | |
| 242 | Built to mimic code for heapq._siftup |
| 243 | only updating position dict too. |
| 244 | """ |
| 245 | heap, position = self.heap, self.position |
| 246 | end_pos = len(heap) |
| 247 | startpos = pos |
| 248 | newitem = heap[pos] |
| 249 | # Shift up the smaller child until hitting a leaf |
| 250 | child_pos = (pos << 1) + 1 # start with leftmost child position |
| 251 | while child_pos < end_pos: |
| 252 | # Set child_pos to index of smaller child. |
| 253 | child = heap[child_pos] |
| 254 | right_pos = child_pos + 1 |
| 255 | if right_pos < end_pos: |
| 256 | right = heap[right_pos] |
| 257 | if not child < right: |
| 258 | child = right |
| 259 | child_pos = right_pos |
| 260 | # Move the smaller child up. |
| 261 | heap[pos] = child |
| 262 | position[child] = pos |
| 263 | pos = child_pos |
| 264 | child_pos = (pos << 1) + 1 |
| 265 | # pos is a leaf position. Put newitem there, and bubble it up |
| 266 | # to its final resting place (by sifting its parents down). |
| 267 | while pos > 0: |
| 268 | parent_pos = (pos - 1) >> 1 |
| 269 | parent = heap[parent_pos] |
| 270 | if not newitem < parent: |
| 271 | break |
| 272 | heap[pos] = parent |
| 273 | position[parent] = pos |
| 274 | pos = parent_pos |
| 275 | heap[pos] = newitem |
| 276 | position[newitem] = pos |
| 277 | |
| 278 | def _siftdown(self, start_pos, pos): |
| 279 | """Restore invariant. keep swapping with parent until smaller. |
no outgoing calls