]> gitweb.factorcode.org Git - factor.git/blob - basis/heaps/heaps.factor
core: Add the shuffler words but without primitives.
[factor.git] / basis / heaps / heaps.factor
1 ! Copyright (C) 2007, 2008 Ryan Murphy, Doug Coleman,
2 ! Slava Pestov.
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
6 vectors ;
7 IN: heaps
8
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 )
15
16 <PRIVATE
17
18 TUPLE: heap { data vector } ;
19
20 : <heap> ( class -- heap )
21     V{ } clone swap boa ; inline
22
23 TUPLE: entry value key heap index ;
24
25 : <entry> ( value key heap -- entry )
26     f entry boa ; inline
27
28 PRIVATE>
29
30 TUPLE: min-heap < heap ;
31
32 : <min-heap> ( -- min-heap ) min-heap <heap> ;
33
34 TUPLE: max-heap < heap ;
35
36 : <max-heap> ( -- max-heap ) max-heap <heap> ;
37
38 M: heap heap-empty? ( heap -- ? )
39     data>> empty? ; inline
40
41 M: heap heap-size ( heap -- n )
42     data>> length ; inline
43
44 <PRIVATE
45
46 : left ( n -- m )
47     { fixnum } declare 1 fixnum-shift-fast 1 fixnum+fast ; inline
48
49 : right ( n -- m )
50     { fixnum } declare 1 fixnum-shift-fast 2 fixnum+fast ; inline
51
52 : up ( n -- m )
53     { fixnum } declare 1 fixnum-fast 2/ ; inline
54
55 : data-nth ( n data -- entry )
56     nth-unsafe { entry } declare ; inline
57
58 : data-set-nth ( entry n data -- )
59     [ [ >>index ] keep ] dip set-nth-unsafe ; inline
60
61 : data-push ( entry data -- n )
62     [ length [ >>index ] keep ]
63     [ [ set-nth ] keepd ] bi ; inline
64
65 GENERIC: heap-compare ( entry1 entry2 heap -- ? )
66
67 M: min-heap heap-compare
68     drop { entry entry } declare [ key>> ] bi@ after? ; inline
69
70 M: max-heap heap-compare
71     drop { entry entry } declare [ key>> ] bi@ before? ; inline
72
73 PRIVATE>
74
75 : >entry< ( entry -- value key )
76     [ value>> ] [ key>> ] bi ; inline
77
78 M: heap heap-peek ( heap -- value key )
79     data>> first >entry< ;
80
81 <PRIVATE
82
83 :: sift-down ( heap from to -- )
84     heap data>>      :> data
85     to data data-nth :> tmp
86
87     to t [ over from > and ] [
88         dup up
89         dup data data-nth
90         dup tmp heap heap-compare [
91             rot data data-set-nth t
92         ] [
93             2drop f
94         ] if
95     ] while
96
97     tmp swap data data-set-nth ; inline
98
99 PRIVATE>
100
101 M: heap heap-push*
102     [ <entry> dup ] [ data>> data-push ] [ 0 rot sift-down ] tri ;
103
104 : heap-push ( value key heap -- )
105     heap-push* drop ;
106
107 : heap-push-all ( assoc heap -- )
108     '[ swap _ heap-push ] assoc-each ;
109
110 <PRIVATE
111
112 :: sift-up ( heap n -- )
113     heap data>>     :> data
114     data length     :> end
115     n data data-nth :> tmp
116
117     n dup left [ dup end < ] [
118         dup 1 fixnum+fast
119         dup end < [
120             2dup [ data data-nth ] bi@ heap heap-compare
121         ] [ f ] if
122         [ nip ] [ drop ] if
123         [ data data-nth swap data data-set-nth ]
124         [ dup left ] bi
125     ] while drop
126
127     tmp over data data-set-nth
128     heap n rot sift-down ; inline
129
130 PRIVATE>
131
132 M: heap heap-pop*
133     dup data>> dup length 1 > [
134         [ pop ] [ set-first ] bi 0 sift-up
135     ] [
136         pop* drop
137     ] if ; inline
138
139 : heap-pop ( heap -- value key )
140     [ heap-peek ] [ heap-pop* ] bi ;
141
142 : slurp-heap ( ... heap quot: ( ... value key -- ... ) -- ... )
143     [ drop '[ _ heap-empty? ] ]
144     [ '[ _ heap-pop @ ] until ] 2bi ; inline
145
146 : heap-pop-all ( heap -- alist )
147     [ heap-size <vector> ] keep
148     [ swap 2array suffix! ] slurp-heap { } like ;
149
150 ERROR: bad-heap-delete ;
151
152 M: bad-heap-delete summary
153     drop "Invalid entry passed to heap-delete" ;
154
155 <PRIVATE
156
157 : entry>index ( entry heap -- n )
158     over heap>> eq? [ bad-heap-delete ] unless
159     index>> { fixnum } declare ; inline
160
161 PRIVATE>
162
163 M: heap heap-delete
164     [ entry>index ] keep
165     2dup heap-size 1 - = [
166         nip data>> pop*
167     ] [
168         [ nip data>> pop ]
169         [ data>> data-set-nth ]
170         [ swap sift-up ] 2tri
171     ] if ;
172
173 : >min-heap ( assoc -- min-heap )
174     dup assoc-size <vector> min-heap boa
175     [ heap-push-all ] keep ;
176
177 : >max-heap ( assoc -- max-heap )
178     dup assoc-size <vector> max-heap boa
179     [ heap-push-all ] keep ;