1 ! Copyright (C) 2007, 2008 Ryan Murphy, Doug Coleman,
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays assocs fry kernel kernel.private locals
5 math math.order math.private sequences sequences.private summary
9 GENERIC: heap-push* ( value key heap -- entry )
10 GENERIC: heap-peek ( heap -- value key )
11 GENERIC: heap-pop* ( heap -- )
12 GENERIC: heap-delete ( entry heap -- )
13 GENERIC: heap-empty? ( heap -- ? )
14 GENERIC: heap-size ( heap -- n )
18 TUPLE: heap { data vector } ;
20 : <heap> ( class -- heap )
21 V{ } clone swap boa ; inline
23 TUPLE: entry value key heap index ;
25 : <entry> ( value key heap -- entry )
30 TUPLE: min-heap < heap ;
32 : <min-heap> ( -- min-heap ) min-heap <heap> ;
34 TUPLE: max-heap < heap ;
36 : <max-heap> ( -- max-heap ) max-heap <heap> ;
38 M: heap heap-empty? data>> empty? ; inline
40 M: heap heap-size data>> length ; inline
45 { fixnum } declare 1 fixnum-shift-fast 1 fixnum+fast ; inline
48 { fixnum } declare 1 fixnum-shift-fast 2 fixnum+fast ; inline
51 { fixnum } declare 1 fixnum-fast 2/ ; inline
53 : data-nth ( n data -- entry )
54 nth-unsafe { entry } declare ; inline
56 : data-set-nth ( entry n data -- )
57 [ [ >>index ] keep ] dip set-nth-unsafe ; inline
59 : data-push ( entry data -- n )
60 [ length [ >>index ] keep ]
61 [ [ set-nth ] keepd ] bi ; inline
63 GENERIC: heap-compare ( entry1 entry2 heap -- ? )
65 M: min-heap heap-compare
66 drop { entry entry } declare [ key>> ] bi@ after? ; inline
68 M: max-heap heap-compare
69 drop { entry entry } declare [ key>> ] bi@ before? ; inline
73 : >entry< ( entry -- value key )
74 [ value>> ] [ key>> ] bi ; inline
77 data>> first >entry< ;
81 ! names and optimizations inspired by cpython/Lib/heapq.py,
82 ! refer to it from in depth explanations of the optimizations.
84 ! called bubble-up in the literature... but we keep cpython's name.
85 :: sift-down ( heap from to -- )
87 to data data-nth :> tmp
89 to t [ over from > and ] [
92 dup tmp heap heap-compare [
93 rot data data-set-nth t
99 tmp swap data data-set-nth ; inline
104 [ <entry> dup ] [ data>> data-push ] [ 0 rot sift-down ] tri ;
106 : heap-push ( value key heap -- )
109 : heap-push-all ( assoc heap -- )
110 '[ swap _ heap-push ] assoc-each ;
114 ! called bubble-down in the literature... but we keep cpython's name.
115 ! A quote from cpython's implementation:
116 ! > We *could* break out of the loop as soon as we find a pos where newitem <=
117 ! > both its children, but turns out that's not a good idea [...]
118 ! Indeed the code is 33% slower if we remove this optimization.
119 :: sift-up ( heap n -- )
122 n data data-nth :> tmp
124 n dup left [ dup end < ] [
127 2dup [ data data-nth ] bi@ heap heap-compare
130 [ data data-nth swap data data-set-nth ]
134 tmp over data data-set-nth
135 heap n rot sift-down ; inline
140 dup data>> f over first index<< [ pop ] keep
141 [ 2drop ] [ set-first 0 sift-up ] if-empty ;
143 : heap-pop ( heap -- value key )
144 [ heap-peek ] [ heap-pop* ] bi ;
146 : slurp-heap ( ... heap quot: ( ... value key -- ... ) -- ... )
147 [ drop '[ _ heap-empty? ] ]
148 [ '[ _ heap-pop @ ] until ] 2bi ; inline
150 : heap-pop-all ( heap -- alist )
151 [ heap-size <vector> ] keep
152 [ swap 2array suffix! ] slurp-heap { } like ;
154 ERROR: bad-heap-delete ;
156 M: bad-heap-delete summary
157 drop "Invalid entry passed to heap-delete" ;
161 : entry>index ( entry heap -- n )
162 over heap>> eq? [ bad-heap-delete ] unless
163 index>> dup [ bad-heap-delete ] unless
164 { fixnum } declare ; inline
168 M:: heap heap-delete ( entry heap -- )
169 entry heap entry>index :> n
171 data pop :> nth-entry
174 nth-entry n data data-set-nth
175 n 0 = [ t ] [ nth-entry n up data data-nth heap heap-compare ] if
176 [ heap n sift-up ] [ heap 0 n sift-down ] if
179 : >min-heap ( assoc -- min-heap )
180 dup assoc-size <vector> min-heap boa
181 [ heap-push-all ] keep ;
183 : >max-heap ( assoc -- max-heap )
184 dup assoc-size <vector> max-heap boa
185 [ heap-push-all ] keep ;