(__TEXT@@__text__TEXT__stubs__TEXT!l!__stub_helper__TEXTX"X"__cstring__TEXT""__const__TEXT##__unwind_info__TEXTt?t?__DATA_CONST@@@@__got__DATA_CONST@(@ __DATA@@__la_symbol_ptr__DATAH__data__DATAHHH__LTEXT@@H__LDATA@@H__LINKEDIT@X
@X
"0@ @h@pAA-DPPxD'86&O2 *8d/usr/lib/libSystem.B.dylib&A )A0H(R O{C ep 7R{AO¨_h@h`TO{C_Tt@@B9h6a@5
@5 ՠJX@{AO¨_``0 BRCR4 hHX@a0 նWO{_Tt@@B9H6s@
@@@@R@T7@5h@h{COBWA_ֈ
@@@i@)i 5XP BRCR5 H?X@!ZP q >X@AX0 @T{w@! {C_T @ {ARP BRCR\@@5{A_ Ո A ՀO{C_T`@ {AO¨`O BRCR85{AO¨_og_WO{C@MTA_T@i9Ry) ?Tyiyy@@R @
T@
TV 7
@!TR9@ yy
y{
y9 y;?TT?T@yy)7Ayw@@R@T@T 7
@T@4@ ywyyy7 y9TR{EODWC_BgAoƨ_ &X@AP H&X@A0 էO{C@B96@@@)@))@?`5{AO¨_ X@a:0 z hX@A:P h@hTg_WO{C@m
TA?mT@I5RU) ?Tyuyi@@RV@T@T7
@TR@ yu
yz
y5 y:+T{EODWC_BgA<@7 HX@/0 {EODWC_BgA_ HX@!-P g_WO{C@T_T@yt9Ayy@@R@@T@`T`7
@AT@4@ yyyty9 y4?TR{EODWC_BgA_ h
X@%0 @ X@"P WO{@B96@'JTAT`?`4 `X@{BOAWè_ X@0 Փ{BOAWèg_WO{@A T
iAJ
T8!ʚѿ
+TB`?56A`?`4ѿJT-T`? 56A`?`4
ѿJT X@{DOCWB_AgŨ_
WO{@B96@@yah@h25 T@@Ҁ?֠5{BOAWè_ hX@
0 X@
P @T h@hT ՐX pX PX 0X X X X հX ՐXPPP,PCP[PuPPP1 G pX_heapq__about__heappushheappushpopheappopheapreplaceheapify_heappop_max_heapify_max_heapreplace_maxheap argument must be a listindex out of rangelist changed size during iterationHeap queue algorithm (a.k.a. priority queue).
Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for
all k, counting elements from 0. For the sake of comparison,
non-existing elements are considered to be infinite. The interesting
property of a heap is that a[0] is always its smallest element.
Usage:
heap = [] # creates an empty heap
heappush(heap, item) # pushes a new item on the heap
item = heappop(heap) # pops the smallest item from the heap
item = heap[0] # smallest item on the heap without popping it
heapify(x) # transforms list into a heap, in-place, in linear time
item = heapreplace(heap, item) # pops and returns smallest item, and adds
# new item; the heap size is unchanged
Our API differs from textbook heap algorithms as follows:
- We use 0-based indexing. This makes the relationship between the
index for a node and the indexes for its children slightly less
obvious, but is more suitable since Python uses 0-based indexing.
- Our heappop() method returns the smallest item, not the largest.
These two make it possible to view the heap as a regular Python list
without surprises: heap[0] is the smallest item, and heap.sort()
maintains the heap invariant!
Heap queues
[explanation by François Pinard]
Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for
all k, counting elements from 0. For the sake of comparison,
non-existing elements are considered to be infinite. The interesting
property of a heap is that a[0] is always its smallest element.
The strange invariant above is meant to be an efficient memory
representation for a tournament. The numbers below are `k', not a[k]:
0
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
In the tree above, each cell `k' is topping `2*k+1' and `2*k+2'. In
a usual binary tournament we see in sports, each cell is the winner
over the two cells it tops, and we can trace the winner down the tree
to see all opponents s/he had. However, in many computer applications
of such tournaments, we do not need to trace the history of a winner.
To be more memory efficient, when a winner is promoted, we try to
replace it by something else at a lower level, and the rule becomes
that a cell and the two cells it tops contain three different items,
but the top cell "wins" over the two topped cells.
If this heap invariant is protected at all time, index 0 is clearly
the overall winner. The simplest algorithmic way to remove it and
find the "next" winner is to move some loser (let's say cell 30 in the
diagram above) into the 0 position, and then percolate this new 0 down
the tree, exchanging values, until the invariant is re-established.
This is clearly logarithmic on the total number of items in the tree.
By iterating over all items, you get an O(n ln n) sort.
A nice feature of this sort is that you can efficiently insert new
items while the sort is going on, provided that the inserted items are
not "better" than the last 0'th element you extracted. This is
especially useful in simulation contexts, where the tree holds all
incoming events, and the "win" condition means the smallest scheduled
time. When an event schedule other events for execution, they are
scheduled into the future, so they can easily go into the heap. So, a
heap is a good structure for implementing schedulers (this is what I
used for my MIDI sequencer :-).
Various structures for implementing schedulers have been extensively
studied, and heaps are good for this, as they are reasonably speedy,
the speed is almost constant, and the worst case is not much different
than the average case. However, there are other representations which
are more efficient overall, yet the worst cases might be terrible.
Heaps are also very useful in big disk sorts. You most probably all
know that a big sort implies producing "runs" (which are pre-sorted
sequences, which size is usually related to the amount of CPU memory),
followed by a merging passes for these runs, which merging is often
very cleverly organised[1]. It is very important that the initial
sort produces the longest runs possible. Tournaments are a good way
to that. If, using all the memory available to hold a tournament, you
replace and percolate items that happen to fit the current run, you'll
produce runs which are twice the size of the memory for random input,
and much better for input fuzzily ordered.
Moreover, if you output the 0'th item on disk and get an input which
may not fit in the current tournament (because the value "wins" over
the last output value), it cannot fit in the heap, so the size of the
heap decreases. The freed memory could be cleverly reused immediately
for progressively building a second heap, which grows at exactly the
same rate the first heap is melting. When the first heap completely
vanishes, you switch heaps and start a new run. Clever and quite
effective!
In a word, heaps are useful memory structures to know. I use them in
a few applications, and I think it is good to keep a `heap' module
around. :-)
--------------------
[1] The disk balancing algorithms which are current, nowadays, are
more annoying than clever, and this is a consequence of the seeking
capabilities of the disks. On devices which cannot seek, like big
tape drives, the story was quite different, and one had to be very
clever to ensure (far in advance) that each tape movement will be the
most effective possible (that is, will best participate at
"progressing" the merge). Some tapes were even able to read
backwards, and this was also used to avoid the rewinding time.
Believe me, real good tape sorts were quite spectacular to watch!
From all times, sorting has always been a Great Art! :-)
heappush($module, heap, item, /)
--
Push item onto heap, maintaining the heap invariant.heappushpop($module, heap, item, /)
--
Push item on the heap, then pop and return the smallest item from the heap.
The combined action runs more efficiently than heappush() followed by
a separate call to heappop().heappop($module, heap, /)
--
Pop the smallest item off the heap, maintaining the heap invariant.heapreplace($module, heap, item, /)
--
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 routine unless written as part of a conditional replacement:
if item > heap[0]:
item = heapreplace(heap, item)heapify($module, heap, /)
--
Transform list into a heap, in-place, in O(len(heap)) time._heappop_max($module, heap, /)
--
Maxheap variant of heappop._heapify_max($module, heap, /)
--
Maxheap variant of heapify._heapreplace_max($module, heap, item, /)
--
Maxheap variant of heapreplace.,,DD!D
@($L
X"d"p"|"""""""#"p:" V;#|/<
#<#O>#>+#>8#'?pH__PAGEZERO__TEXT@@__text__TEXT$8$8__cstring__TEXT?9?__DATA@@@@__data__DATA@@H__LINKEDIT@ Hn\S)l @ <
@;P @`{BOAog_WO{7R#R[9 @9)"4RRR
U" 3҈4
H}I 4_9*QJ_%q T)?qT[9@95@9 %Q?qTq@Tq q @9*Q+_iqj1JQJ_%qHT
+R
)@9+Q-iq1kQmJ)qT
4
*
.R_8Qjq/2q/) ӌaTH}5
H})5*JQ_MqhTzk`@K!U@)?qT6RW9@WR@r"RcT99K!@~@[9CRR 9RR_9(qT(R@95z@_R@r"RcT9R9@95j@_R@r"RT.@ !@W9@WR@r"R#T9@R@ra" BRcT99_@9@K!B@A}@)?aq[9RG ՛9R[9@95- @K!U@` ji8)J5>-T+9T@[R@r"RT9@R@rcT99u {GOFWE_DgCoB_0p@R@rT)R 9_C_WO{?9*
RR?6MȚ)o1ЁQqi)8)_
bTMȚ)o1i)8)_
T7 KqT?"R`?qT?7"R`?T{HOGWF_EC_LIEF says hello!
Time to jump on the real function: %p
0x"YFRARCRASASASASASASASAp(Q>@_PyExc_IndexErrorQq@_PyExc_RuntimeError@_PyExc_TypeError@__Py_NoneStruct@dyld_stub_binderr>@_PyErr_SetStringr>@_PyList_Appendr>@_PyList_SetSlicer>@_PyModuleDef_Initr >@_PyModule_AddObjectr(>@_PyObject_RichCompareBoolr0>@_PyUnicode_FromStringr8>@__PyArg_CheckPositionalr@>@__Py_Dealloc_PyInit__heapq+l`\p/ |^N3x+D! #(:CV;/<r<O>c>2>'? H P I$8Xgx?#$%&'()* !"+,#$%&'()*_PyErr_SetString_PyExc_IndexError_PyExc_RuntimeError_PyExc_TypeError_PyInit__heapq_PyList_Append_PyList_SetSlice_PyModuleDef_Init_PyModule_AddObject_PyObject_RichCompareBool_PyUnicode_FromString__PyArg_CheckPositional__Py_Dealloc__Py_NoneStruct___about____dyld_private__heapq__heapify_max__heapq__heapify_max__doc____heapq__heappop_max__heapq__heappop_max__doc____heapq__heapreplace_max__heapq__heapreplace_max__doc____heapq_heapify__heapq_heapify__doc____heapq_heappop__heapq_heappop__doc____heapq_heappush__heapq_heappush__doc____heapq_heappushpop__heapq_heappushpop__doc____heapq_heapreplace__heapq_heapreplace__doc____heapqmodule_cache_friendly_heapify_heapify_internal_heappop_internal_heapq_exec_heapq_methods_heapq_slots_heapreplace_internal_module_doc_siftdown_siftup_siftup_maxdyld_stub_binder(tX
0 @_heapq.cpython-39-darwin.so&*Ua$jjL=Z
aCQy&]Ew*a(cDd5ENShǞ$w8?cb3rafC7<ψbEy]Yk3cϲ
Q.
B܃>?ƉYXofkOX||zڽH,XofkOX||zڽH,XofkOX||zڽH,XofkOX||zڽH,:s@ŖV&
Og?p_ޤ̭XofkOX||zڽH,XofkOX||zڽH,XofkOX||zڽH,ˬF&C,(0-#
nV),0n