]> gitweb.factorcode.org Git - factor.git/blob - core/sorting/sorting.factor
52c86560082f757b94fe3004c3f3331ac6485595
[factor.git] / core / sorting / sorting.factor
1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs growable.private hashtables
4 kernel kernel.private math math.order sequences
5 sequences.private vectors ;
6 IN: sorting
7
8 ! Optimized merge-sort:
9 !
10 ! 1) only allocates 2 temporary arrays
11
12 ! 2) first phase (interchanging pairs x[i], x[i+1] where
13 ! x[i] > x[i+1]) is handled specially
14
15 <PRIVATE
16
17 TUPLE: merge-state
18 { seq    array }
19 { accum  vector }
20 { accum1 vector }
21 { accum2 vector }
22 { from1  array-capacity }
23 { to1    array-capacity }
24 { from2  array-capacity }
25 { to2    array-capacity } ;
26
27 : l-elt ( merge -- elt ) [ from1>> ] [ seq>> ] bi nth-unsafe ; inline
28
29 : r-elt ( merge -- elt ) [ from2>> ] [ seq>> ] bi nth-unsafe ; inline
30
31 : l-done? ( merge -- ? ) [ from1>> ] [ to1>> ] bi eq? ; inline
32
33 : r-done? ( merge -- ? ) [ from2>> ] [ to2>> ] bi eq? ; inline
34
35 : dump-l ( merge -- )
36     [ [ from1>> ] [ to1>> ] [ seq>> ] tri ] [ accum>> ] bi
37     push-all-unsafe ; inline
38
39 : dump-r ( merge -- )
40     [ [ from2>> ] [ to2>> ] [ seq>> ] tri ] [ accum>> ] bi
41     push-all-unsafe ; inline
42
43 : l-next ( merge -- )
44     [ [ l-elt ] [ [ 1 + ] change-from1 drop ] bi ] [ accum>> ] bi
45     push-unsafe ; inline
46
47 : r-next ( merge -- )
48     [ [ r-elt ] [ [ 1 + ] change-from2 drop ] bi ] [ accum>> ] bi
49     push-unsafe ; inline
50
51 : decide ( merge quot: ( elt1 elt2 -- <=> ) -- ? )
52     [ [ l-elt ] [ r-elt ] bi ] dip call +gt+ eq? ; inline
53
54 : (merge) ( merge quot: ( elt1 elt2 -- <=> ) -- )
55     over r-done? [ drop dump-l ] [
56         over l-done? [ drop dump-r ] [
57             2dup decide
58             [ over r-next ] [ over l-next ] if
59             (merge)
60         ] if
61     ] if ; inline recursive
62
63 : flip-accum ( merge -- )
64     dup [ accum>> ] [ accum1>> ] bi eq? [
65         dup accum1>> underlying>> >>seq
66         dup accum2>> >>accum
67     ] [
68         dup accum1>> >>accum
69         dup accum2>> underlying>> >>seq
70     ] if
71     dup accum>> 0 >>length 2drop ; inline
72
73 : <merge> ( seq -- merge )
74     \ merge-state new
75         over >vector >>accum1
76         swap length <vector> >>accum2
77         dup accum1>> underlying>> >>seq
78         dup accum2>> >>accum ; inline
79
80 : compute-midpoint ( merge -- merge )
81     dup [ from1>> ] [ to2>> ] bi + 2/ >>to1 ; inline
82
83 : merging ( from to merge -- )
84     swap >>to2
85     swap >>from1
86     compute-midpoint
87     dup [ to1>> ] [ seq>> length ] bi min >>to1
88     dup [ to2>> ] [ seq>> length ] bi min >>to2
89     dup to1>> >>from2
90     drop ; inline
91
92 : nth-chunk ( n size -- from to ) [ * dup ] keep + ; inline
93
94 : chunks ( length size -- n ) [ align ] keep /i ; inline
95
96 : each-chunk ( length size quot -- )
97     [ [ chunks ] keep ] dip
98     [ nth-chunk ] prepose curry
99     each-integer ; inline
100
101 : merge ( from to merge quot -- )
102     [ [ merging ] keep ] dip (merge) ; inline
103
104 : sort-pass ( merge size quot -- )
105     [
106         over flip-accum
107         over [ seq>> length ] 2dip
108     ] dip
109     [ merge ] 2curry each-chunk ; inline
110
111 : sort-loop ( merge quot -- )
112     [ 2 over seq>> length [ over > ] curry ] dip
113     [ [ 1 shift 2dup ] dip sort-pass ] curry
114     while 2drop ; inline
115
116 : each-pair ( seq quot -- )
117     [ [ length 1 + 2/ ] keep ] dip
118     [ [ 1 shift dup 1 + ] dip ] prepose curry each-integer ; inline
119
120 : (sort-pairs) ( i1 i2 seq quot accum -- )
121     [ 2dup length = ] 2dip rot [
122         [ drop nip nth-unsafe ] dip push-unsafe
123     ] [
124         [
125             [ [ nth-unsafe ] curry bi@ 2dup ] dip call +gt+ eq?
126             [ swap ] when
127         ] dip [ push-unsafe ] curry bi@
128     ] if ; inline
129
130 : sort-pairs ( merge quot -- )
131     [ [ seq>> ] [ accum>> ] bi ] dip swap
132     [ (sort-pairs) ] 2curry each-pair ; inline
133
134 PRIVATE>
135
136 : sort ( seq quot: ( obj1 obj2 -- <=> ) -- sortedseq )
137     [ <merge> ] dip
138     [ sort-pairs ] [ sort-loop ] [ drop accum>> underlying>> ] 2tri ;
139     inline
140
141 : natural-sort ( seq -- sortedseq ) [ <=> ] sort ;
142
143 : sort-with ( seq quot: ( elt -- key ) -- sortedseq )
144     [ compare ] curry sort ; inline
145
146 : inv-sort-with ( seq quot: ( elt -- key ) -- sortedseq )
147     [ compare invert-comparison ] curry sort ; inline
148
149 <PRIVATE
150
151 : check-bounds ( alist n -- alist )
152     [ swap bounds-check 2drop ] curry dupd each ; inline
153
154 PRIVATE>
155
156 GENERIC: sort-keys ( obj -- sortedseq )
157
158 M: object sort-keys >alist sort-keys ;
159
160 M: sequence sort-keys
161     0 check-bounds [ first-unsafe ] sort-with ;
162
163 M: hashtable sort-keys
164     >alist [ { array } declare first-unsafe ] sort-with ;
165
166 GENERIC: sort-values ( obj -- sortedseq )
167
168 M: object sort-values >alist sort-values ;
169
170 M: sequence sort-values
171     1 check-bounds [ second-unsafe ] sort-with ;
172
173 M: hashtable sort-values
174     >alist [ { array } declare second-unsafe ] sort-with ;
175
176 : sort-pair ( a b -- c d ) 2dup after? [ swap ] when ;