1 ! Copyright (C) 2007, 2008 Ryan Murphy, Doug Coleman,
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: kernel math sequences arrays assocs sequences.private
5 growable accessors math.order summary ;
8 GENERIC: heap-push* ( value key heap -- entry )
9 GENERIC: heap-peek ( heap -- value key )
10 GENERIC: heap-pop* ( heap -- )
11 GENERIC: heap-pop ( heap -- value key )
12 GENERIC: heap-delete ( entry heap -- )
13 GENERIC: heap-empty? ( heap -- ? )
14 GENERIC: heap-size ( heap -- n )
20 : <heap> ( class -- heap )
21 [ V{ } clone ] dip boa ; inline
23 TUPLE: entry value key heap index ;
25 : <entry> ( value key heap -- entry ) f entry boa ;
29 TUPLE: min-heap < heap ;
31 : <min-heap> ( -- min-heap ) min-heap <heap> ;
33 TUPLE: max-heap < heap ;
35 : <max-heap> ( -- max-heap ) max-heap <heap> ;
37 M: heap heap-empty? ( heap -- ? )
40 M: heap heap-size ( heap -- n )
45 : left ( n -- m ) 1 shift 1 + ; inline
47 : right ( n -- m ) 1 shift 2 + ; inline
49 : up ( n -- m ) 1- 2/ ; inline
51 : data-nth ( n heap -- entry )
52 data>> nth-unsafe ; inline
54 : up-value ( n heap -- entry )
55 [ up ] dip data-nth ; inline
57 : left-value ( n heap -- entry )
58 [ left ] dip data-nth ; inline
60 : right-value ( n heap -- entry )
61 [ right ] dip data-nth ; inline
63 : data-set-nth ( entry n heap -- )
64 [ [ >>index drop ] 2keep ] dip
65 data>> set-nth-unsafe ; inline
67 : data-push ( entry heap -- n )
69 swap 2dup data>> ensure 2drop data-set-nth
72 : data-pop ( heap -- entry )
75 : data-pop* ( heap -- )
78 : data-peek ( heap -- entry )
81 : data-first ( heap -- entry )
84 : data-exchange ( m n heap -- )
85 [ tuck data-nth [ data-nth ] dip ] 3keep
86 tuck [ data-set-nth ] 2dip data-set-nth ; inline
88 GENERIC: heap-compare ( pair1 pair2 heap -- ? )
90 : (heap-compare) ( pair1 pair2 heap -- <=> )
91 drop [ key>> ] compare ; inline
93 M: min-heap heap-compare (heap-compare) +gt+ eq? ;
95 M: max-heap heap-compare (heap-compare) +lt+ eq? ;
97 : heap-bounds-check? ( m heap -- ? )
100 : left-bounds-check? ( m heap -- ? )
101 [ left ] dip heap-bounds-check? ; inline
103 : right-bounds-check? ( m heap -- ? )
104 [ right ] dip heap-bounds-check? ; inline
106 : continue? ( m up[m] heap -- ? )
107 [ data-nth swap ] keep [ data-nth ] keep
108 heap-compare ; inline
112 : (up-heap) ( n heap -- )
115 [ data-exchange ] 2keep up-heap
120 : up-heap ( n heap -- )
121 over 0 > [ (up-heap) ] [ 2drop ] if ;
123 : (child) ( m heap -- n )
125 [ 2dup left-value ] dip
127 [ right ] [ left ] if ;
129 : child ( m heap -- n )
130 2dup right-bounds-check?
131 [ drop left ] [ (child) ] if ;
133 : swap-down ( m heap -- )
134 [ child ] 2keep data-exchange ;
138 : (down-heap) ( m heap -- )
139 [ child ] 2keep swapd
143 [ data-exchange ] 2keep down-heap
146 : down-heap ( m heap -- )
147 2dup left-bounds-check? [ 2drop ] [ (down-heap) ] if ;
151 M: heap heap-push* ( value key heap -- entry )
152 [ <entry> dup ] keep [ data-push ] keep up-heap ;
154 : heap-push ( value key heap -- ) heap-push* drop ;
156 : heap-push-all ( assoc heap -- )
157 [ swapd heap-push ] curry assoc-each ;
159 : >entry< ( entry -- key value )
160 [ value>> ] [ key>> ] bi ;
162 M: heap heap-peek ( heap -- value key )
165 ERROR: bad-heap-delete ;
167 M: bad-heap-delete summary
168 drop "Invalid entry passed to heap-delete" ;
170 : entry>index ( entry heap -- n )
171 over heap>> eq? [ bad-heap-delete ] unless
174 M: heap heap-delete ( entry heap -- )
176 2dup heap-size 1- = [
179 [ nip data-pop ] 2keep
180 [ data-set-nth ] 2keep
184 M: heap heap-pop* ( heap -- )
185 dup data-first swap heap-delete ;
187 M: heap heap-pop ( heap -- value key )
188 dup data-first [ swap heap-delete ] keep >entry< ;
190 : heap-pop-all ( heap -- alist )
191 [ dup heap-empty? not ]
192 [ dup heap-pop swap 2array ]
195 : slurp-heap ( heap quot: ( elt -- ) -- )
196 over heap-empty? [ 2drop ] [
197 [ [ heap-pop drop ] dip call ] [ slurp-heap ] 2bi
198 ] if ; inline recursive