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 ;
8 ! Optimized merge-sort:
10 ! 1) only allocates 2 temporary arrays
12 ! 2) first phase (interchanging pairs x[i], x[i+1] where
13 ! x[i] > x[i+1]) is handled specially
22 { from1 array-capacity }
23 { to1 array-capacity }
24 { from2 array-capacity }
25 { to2 array-capacity } ;
27 : l-elt ( merge -- elt ) [ from1>> ] [ seq>> ] bi nth-unsafe ; inline
29 : r-elt ( merge -- elt ) [ from2>> ] [ seq>> ] bi nth-unsafe ; inline
31 : l-done? ( merge -- ? ) [ from1>> ] [ to1>> ] bi eq? ; inline
33 : r-done? ( merge -- ? ) [ from2>> ] [ to2>> ] bi eq? ; inline
36 [ [ from1>> ] [ to1>> ] [ seq>> ] tri ] [ accum>> ] bi
37 push-all-unsafe ; inline
40 [ [ from2>> ] [ to2>> ] [ seq>> ] tri ] [ accum>> ] bi
41 push-all-unsafe ; inline
44 [ [ l-elt ] [ [ 1 + ] change-from1 drop ] bi ] [ accum>> ] bi
48 [ [ r-elt ] [ [ 1 + ] change-from2 drop ] bi ] [ accum>> ] bi
51 : decide ( merge quot: ( elt1 elt2 -- <=> ) -- ? )
52 [ [ l-elt ] [ r-elt ] bi ] dip call +gt+ eq? ; inline
54 : (merge) ( merge quot: ( elt1 elt2 -- <=> ) -- )
55 over r-done? [ drop dump-l ] [
56 over l-done? [ drop dump-r ] [
58 [ over r-next ] [ over l-next ] if
61 ] if ; inline recursive
63 : flip-accum ( merge -- )
64 dup [ accum>> ] [ accum1>> ] bi eq? [
65 dup accum1>> underlying>> >>seq
69 dup accum2>> underlying>> >>seq
71 dup accum>> 0 >>length 2drop ; inline
73 : <merge> ( seq -- merge )
76 swap length <vector> >>accum2
77 dup accum1>> underlying>> >>seq
78 dup accum2>> >>accum ; inline
80 : compute-midpoint ( merge -- merge )
81 dup [ from1>> ] [ to2>> ] bi + 2/ >>to1 ; inline
83 : merging ( from to merge -- )
87 dup [ to1>> ] [ seq>> length ] bi min >>to1
88 dup [ to2>> ] [ seq>> length ] bi min >>to2
92 : nth-chunk ( n size -- from to ) [ * dup ] keep + ; inline
94 : chunks ( length size -- n ) [ align ] keep /i ; inline
96 : each-chunk ( length size quot -- )
97 [ [ chunks ] keep ] dip
98 [ nth-chunk ] prepose curry
101 : merge ( from to merge quot -- )
102 [ [ merging ] keep ] dip (merge) ; inline
104 : sort-pass ( merge size quot -- )
107 over [ seq>> length ] 2dip
109 [ merge ] 2curry each-chunk ; inline
111 : sort-loop ( merge quot -- )
112 [ 2 over seq>> length [ over > ] curry ] dip
113 [ [ 1 shift 2dup ] dip sort-pass ] curry
116 : each-pair ( seq quot -- )
117 [ [ length 1 + 2/ ] keep ] dip
118 [ [ 1 shift dup 1 + ] dip ] prepose curry each-integer ; inline
120 : (sort-pairs) ( i1 i2 seq quot accum -- )
121 [ 2dup length = ] 2dip rot [
122 [ drop nip nth-unsafe ] dip push-unsafe
125 [ [ nth-unsafe ] curry bi@ 2dup ] dip call +gt+ eq?
127 ] dip [ push-unsafe ] curry bi@
130 : sort-pairs ( merge quot -- )
131 [ [ seq>> ] [ accum>> ] bi ] dip swap
132 [ (sort-pairs) ] 2curry each-pair ; inline
136 : sort ( seq quot: ( obj1 obj2 -- <=> ) -- sortedseq )
138 [ sort-pairs ] [ sort-loop ] [ drop accum>> underlying>> ] 2tri ;
141 : natural-sort ( seq -- sortedseq ) [ <=> ] sort ;
143 : sort-with ( seq quot: ( elt -- key ) -- sortedseq )
144 [ compare ] curry sort ; inline
146 : inv-sort-with ( seq quot: ( elt -- key ) -- sortedseq )
147 [ compare invert-comparison ] curry sort ; inline
151 : check-bounds ( alist n -- alist )
152 [ swap bounds-check 2drop ] curry dupd each ; inline
156 GENERIC: sort-keys ( obj -- sortedseq )
158 M: object sort-keys >alist sort-keys ;
160 M: sequence sort-keys
161 0 check-bounds [ first-unsafe ] sort-with ;
163 M: hashtable sort-keys
164 >alist [ { array } declare first-unsafe ] sort-with ;
166 GENERIC: sort-values ( obj -- sortedseq )
168 M: object sort-values >alist sort-values ;
170 M: sequence sort-values
171 1 check-bounds [ second-unsafe ] sort-with ;
173 M: hashtable sort-values
174 >alist [ { array } declare second-unsafe ] sort-with ;
176 : sort-pair ( a b -- c d ) 2dup after? [ swap ] when ;