]> gitweb.factorcode.org Git - factor.git/blob - core/sorting/sorting.factor
io.files.windows: make sure f absolute-path is f on windows
[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 accum>> ] bi push-unsafe ; inline
45
46 : r-next ( merge -- )
47     [ r-elt ] [ [ 1 + ] change-from2 accum>> ] bi push-unsafe ; inline
48
49 : decide? ( merge quot: ( elt1 elt2 -- <=> ) -- ? )
50     [ [ l-elt ] [ r-elt ] bi ] dip call +gt+ eq? ; inline
51
52 : (merge) ( merge quot: ( elt1 elt2 -- <=> ) -- )
53     over r-done? [ drop dump-l ] [
54         over l-done? [ drop dump-r ] [
55             2dup decide?
56             [ over r-next ] [ over l-next ] if
57             (merge)
58         ] if
59     ] if ; inline recursive
60
61 : flip-accum ( merge -- )
62     dup [ accum>> ] [ accum1>> ] bi eq? [
63         dup accum1>> underlying>> >>seq
64         dup accum2>> >>accum
65     ] [
66         dup accum1>> >>accum
67         dup accum2>> underlying>> >>seq
68     ] if
69     dup accum>> 0 >>length 2drop ; inline
70
71 : <merge> ( seq -- merge )
72     \ merge-state new
73         over >vector >>accum1
74         swap length <vector> >>accum2
75         dup accum1>> underlying>> >>seq
76         dup accum2>> >>accum ; inline
77
78 : compute-midpoint ( merge -- merge )
79     dup [ from1>> ] [ to2>> ] bi + 2/ >>to1 ; inline
80
81 : merging ( from to merge -- )
82     swap >>to2
83     swap >>from1
84     compute-midpoint
85     dup [ to1>> ] [ seq>> length ] bi min >>to1
86     dup [ to2>> ] [ seq>> length ] bi min >>to2
87     dup to1>> >>from2
88     drop ; inline
89
90 : nth-chunk ( n size -- from to ) [ * dup ] keep + ; inline
91
92 : chunks ( length size -- n ) [ align ] keep /i ; inline
93
94 : each-chunk ( length size quot -- )
95     [ [ chunks ] keep ] dip
96     [ nth-chunk ] prepose curry
97     each-integer ; inline
98
99 : merge ( from to merge quot -- )
100     [ [ merging ] keep ] dip (merge) ; inline
101
102 : sort-pass ( merge size quot -- )
103     [
104         over flip-accum
105         over [ seq>> length ] 2dip
106     ] dip
107     [ merge ] 2curry each-chunk ; inline
108
109 : sort-loop ( merge quot -- )
110     [ 2 over seq>> length [ over > ] curry ] dip
111     [ [ 1 shift 2dup ] dip sort-pass ] curry
112     while 2drop ; inline
113
114 : each-pair ( seq quot -- )
115     [ [ length 1 + 2/ ] keep ] dip
116     [ [ 1 shift dup 1 + ] dip ] prepose curry each-integer ; inline
117
118 : (sort-pairs) ( i1 i2 seq quot accum -- )
119     [ 2dup length = ] 2dip rot [
120         [ drop nip nth-unsafe ] dip push-unsafe
121     ] [
122         [
123             [ [ nth-unsafe ] curry bi@ 2dup ] dip call +gt+ eq?
124             [ swap ] when
125         ] dip [ push-unsafe ] curry bi@
126     ] if ; inline
127
128 : sort-pairs ( merge quot -- )
129     [ [ seq>> ] [ accum>> ] bi ] dip swap
130     [ (sort-pairs) ] 2curry each-pair ; inline
131
132 PRIVATE>
133
134 : sort ( seq quot: ( obj1 obj2 -- <=> ) -- sortedseq )
135     [ <merge> ] dip
136     [ sort-pairs ] [ sort-loop ] [ drop accum>> underlying>> ] 2tri ; inline
137
138 : natural-sort ( seq -- sortedseq ) [ <=> ] sort ;
139
140 : sort-with ( seq quot: ( elt -- key ) -- sortedseq )
141     [ compare ] curry sort ; inline
142
143 : inv-sort-with ( seq quot: ( elt -- key ) -- sortedseq )
144     [ compare invert-comparison ] curry sort ; inline
145
146 <PRIVATE
147
148 : check-bounds ( alist n -- alist )
149     [ swap bounds-check 2drop ] curry dupd each ; inline
150
151 PRIVATE>
152
153 GENERIC: sort-keys ( obj -- sortedseq )
154
155 M: object sort-keys >alist sort-keys ;
156
157 M: sequence sort-keys
158     0 check-bounds [ first-unsafe ] sort-with ;
159
160 M: hashtable sort-keys
161     >alist [ { array } declare first-unsafe ] sort-with ;
162
163 GENERIC: sort-values ( obj -- sortedseq )
164
165 M: object sort-values >alist sort-values ;
166
167 M: sequence sort-values
168     1 check-bounds [ second-unsafe ] sort-with ;
169
170 M: hashtable sort-values
171     >alist [ { array } declare second-unsafe ] sort-with ;
172
173 : sort-pair ( a b -- c d ) 2dup after? [ swap ] when ;
174
175 MACRO: compare-with ( quots -- <=> )
176     [ '[ _ bi@ <=> ] ]
177     [ '[ _ 2keep rot dup +eq+ eq? [ drop @ ] [ 2nip ] if ] ]
178     map-reduce ;