Pop and return the current smallest value, and add the new item. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this ro
(heap, item)
| 146 | return lastelt |
| 147 | |
| 148 | def heapreplace(heap, item): |
| 149 | """Pop and return the current smallest value, and add the new item. |
| 150 | |
| 151 | This is more efficient than heappop() followed by heappush(), and can be |
| 152 | more appropriate when using a fixed-size heap. Note that the value |
| 153 | returned may be larger than item! That constrains reasonable uses of |
| 154 | this routine unless written as part of a conditional replacement: |
| 155 | |
| 156 | if item > heap[0]: |
| 157 | item = heapreplace(heap, item) |
| 158 | """ |
| 159 | returnitem = heap[0] # raises appropriate IndexError if heap is empty |
| 160 | heap[0] = item |
| 161 | _siftup(heap, 0) |
| 162 | return returnitem |
| 163 | |
| 164 | def heappushpop(heap, item): |
| 165 | """Fast version of a heappush followed by a heappop.""" |