1 ! Copyright (C) 2007, 2008 Ryan Murphy, Doug Coleman,
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors arrays assocs combinators fry kernel
5 kernel.private locals math math.order math.private sequences
6 sequences.private summary vectors ;
9 ! names and optimizations copied from pypy's heapq.py,
10 ! refer to it from in depth explanations of the optimizations.
12 GENERIC: heap-push* ( value key heap -- entry )
13 GENERIC: heap-peek ( heap -- value key )
14 GENERIC: heap-pop* ( heap -- )
15 GENERIC: heap-delete ( entry heap -- )
16 GENERIC: heap-empty? ( heap -- ? )
17 GENERIC: heap-size ( heap -- n )
21 TUPLE: heap { data vector } ;
23 : <heap> ( class -- heap )
24 V{ } clone swap boa ; inline
26 TUPLE: entry value key heap index ;
28 : <entry> ( value key heap -- entry )
33 TUPLE: min-heap < heap ;
35 : <min-heap> ( -- min-heap ) min-heap <heap> ;
37 TUPLE: max-heap < heap ;
39 : <max-heap> ( -- max-heap ) max-heap <heap> ;
41 M: heap heap-empty? ( heap -- ? )
42 data>> empty? ; inline
44 M: heap heap-size ( heap -- n )
45 data>> length ; inline
50 { fixnum } declare 1 fixnum-shift-fast 1 fixnum+fast ; inline
53 { fixnum } declare 1 fixnum-shift-fast 2 fixnum+fast ; inline
56 { fixnum } declare 1 fixnum-fast 2/ ; inline
58 : data-nth ( n data -- entry )
59 nth-unsafe { entry } declare ; inline
61 : data-set-nth ( entry n data -- )
62 [ [ >>index ] keep ] dip set-nth-unsafe ; inline
64 : data-push ( entry data -- n )
65 [ length [ >>index ] keep ]
66 [ [ set-nth ] keepd ] bi ; inline
68 GENERIC: heap-compare ( entry1 entry2 heap -- ? )
70 M: min-heap heap-compare
71 drop { entry entry } declare [ key>> ] bi@ after? ; inline
73 M: max-heap heap-compare
74 drop { entry entry } declare [ key>> ] bi@ before? ; inline
76 : (heapdata-compare) ( m n data heap -- ? )
77 [ '[ _ data-nth ] bi@ ] [ heap-compare ] bi* ; inline
79 : heapdata-compare ( m n heap -- ? )
80 [ data>> ] keep (heapdata-compare) ; inline
84 : >entry< ( entry -- value key )
85 [ value>> ] [ key>> ] bi ; inline
87 M: heap heap-peek ( heap -- value key )
88 data>> first >entry< ;
92 ! called bubble-up in the litterature... but we keep pypy's name.
93 :: sift-down ( heap from to -- )
95 to data data-nth :> tmp
97 to t [ over from > and ] [
100 dup tmp heap heap-compare [
101 rot data data-set-nth t
107 tmp swap data data-set-nth ; inline
112 [ <entry> dup ] [ data>> data-push ] [ 0 rot sift-down ] tri ;
114 : heap-push ( value key heap -- )
117 : heap-push-all ( assoc heap -- )
118 '[ swap _ heap-push ] assoc-each ;
122 ! called bubble-down in the litterature... but we keep pypy's name.
123 ! A quote from pypy's implementation:
124 ! > We *could* break out of the loop as soon as we find a pos where newitem <=
125 ! > both its children, but turns out that's not a good idea [...]
126 ! Indeed the code is 33% slower if we remove this optmization.
127 :: sift-up ( heap n -- )
130 n data data-nth :> tmp
132 n dup left [ dup end < ] [
135 2dup data heap (heapdata-compare)
138 [ data data-nth swap data data-set-nth ]
142 tmp over data data-set-nth
143 heap n rot sift-down ; inline
145 : (heap-pop*) ( heap data -- )
146 [ first f >>index drop ] [ pop ] [ set-first ] tri 0 sift-up ; inline
151 dup data>> dup length 1 > [
157 : heap-pop ( heap -- value key )
158 [ heap-peek ] [ heap-pop* ] bi ;
160 : slurp-heap ( ... heap quot: ( ... value key -- ... ) -- ... )
161 [ drop '[ _ heap-empty? ] ]
162 [ '[ _ heap-pop @ ] until ] 2bi ; inline
164 : heap-pop-all ( heap -- alist )
165 [ heap-size <vector> ] keep
166 [ swap 2array suffix! ] slurp-heap { } like ;
168 ERROR: bad-heap-delete ;
170 M: bad-heap-delete summary
171 drop "Invalid entry passed to heap-delete" ;
175 : entry>index ( entry heap -- n )
176 over heap>> eq? [ bad-heap-delete ] unless
177 index>> dup [ bad-heap-delete ] unless
178 { fixnum } declare ; inline
182 : ((heap-delete)) ( n heap -- )
183 2dup [ dup up ] dip heapdata-compare
184 [ swap sift-up ] [ 0 rot sift-down ] if ;
186 : (heap-delete) ( n heap -- )
188 [ data>> data-set-nth ]
189 [ ((heap-delete)) ] 2tri ;
192 [ entry>index ] [ f rot index<< ] 2bi
194 { [ 2dup heap-size 1 - = ] [ nip data>> pop* ] }
195 { [ over zero? ] [ nip dup data>> (heap-pop*) ] }
199 : >min-heap ( assoc -- min-heap )
200 dup assoc-size <vector> min-heap boa
201 [ heap-push-all ] keep ;
203 : >max-heap ( assoc -- max-heap )
204 dup assoc-size <vector> max-heap boa
205 [ heap-push-all ] keep ;