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 accum>> ] bi push-unsafe ; inline
47 [ r-elt ] [ [ 1 + ] change-from2 accum>> ] bi push-unsafe ; inline
49 : decide? ( merge quot: ( elt1 elt2 -- <=> ) -- ? )
50 [ [ l-elt ] [ r-elt ] bi ] dip call +gt+ eq? ; inline
52 : (merge) ( merge quot: ( elt1 elt2 -- <=> ) -- )
53 over r-done? [ drop dump-l ] [
54 over l-done? [ drop dump-r ] [
56 [ over r-next ] [ over l-next ] if
59 ] if ; inline recursive
61 : flip-accum ( merge -- )
62 dup [ accum>> ] [ accum1>> ] bi eq? [
63 dup accum1>> underlying>> >>seq
67 dup accum2>> underlying>> >>seq
69 dup accum>> 0 >>length 2drop ; inline
71 : <merge> ( seq -- merge )
74 swap length <vector> >>accum2
75 dup accum1>> underlying>> >>seq
76 dup accum2>> >>accum ; inline
78 : compute-midpoint ( merge -- merge )
79 dup [ from1>> ] [ to2>> ] bi + 2/ >>to1 ; inline
81 : merging ( from to merge -- )
85 dup [ to1>> ] [ seq>> length ] bi min >>to1
86 dup [ to2>> ] [ seq>> length ] bi min >>to2
90 : nth-chunk ( n size -- from to ) [ * dup ] keep + ; inline
92 : chunks ( length size -- n ) [ align ] keep /i ; inline
94 : each-chunk ( length size quot -- )
95 [ [ chunks ] keep ] dip
96 [ nth-chunk ] prepose curry
99 : merge ( from to merge quot -- )
100 [ [ merging ] keep ] dip (merge) ; inline
102 : sort-pass ( merge size quot -- )
105 over [ seq>> length ] 2dip
107 [ merge ] 2curry each-chunk ; inline
109 : sort-loop ( merge quot -- )
110 [ 2 over seq>> length [ over > ] curry ] dip
111 [ [ 1 shift 2dup ] dip sort-pass ] curry
114 : each-pair ( seq quot -- )
115 [ [ length 1 + 2/ ] keep ] dip
116 [ [ 1 shift dup 1 + ] dip ] prepose curry each-integer ; inline
118 : (sort-pairs) ( i1 i2 seq quot accum -- )
119 [ 2dup length = ] 2dip rot [
120 [ drop nip nth-unsafe ] dip push-unsafe
123 [ [ nth-unsafe ] curry bi@ 2dup ] dip call +gt+ eq?
125 ] dip [ push-unsafe ] curry bi@
128 : sort-pairs ( merge quot -- )
129 [ [ seq>> ] [ accum>> ] bi ] dip swap
130 [ (sort-pairs) ] 2curry each-pair ; inline
134 : sort ( seq quot: ( obj1 obj2 -- <=> ) -- sortedseq )
136 [ sort-pairs ] [ sort-loop ] [ drop accum>> underlying>> ] 2tri ; inline
138 : natural-sort ( seq -- sortedseq ) [ <=> ] sort ;
140 : sort-with ( seq quot: ( elt -- key ) -- sortedseq )
141 [ compare ] curry sort ; inline
143 : inv-sort-with ( seq quot: ( elt -- key ) -- sortedseq )
144 [ compare invert-comparison ] curry sort ; inline
148 : check-bounds ( alist n -- alist )
149 [ swap bounds-check 2drop ] curry dupd each ; inline
153 GENERIC: sort-keys ( obj -- sortedseq )
155 M: object sort-keys >alist sort-keys ;
157 M: sequence sort-keys
158 0 check-bounds [ first-unsafe ] sort-with ;
160 M: hashtable sort-keys
161 >alist [ { array } declare first-unsafe ] sort-with ;
163 GENERIC: sort-values ( obj -- sortedseq )
165 M: object sort-values >alist sort-values ;
167 M: sequence sort-values
168 1 check-bounds [ second-unsafe ] sort-with ;
170 M: hashtable sort-values
171 >alist [ { array } declare second-unsafe ] sort-with ;
173 : sort-pair ( a b -- c d ) 2dup after? [ swap ] when ;
175 MACRO: compare-with ( quots -- <=> )
177 [ '[ _ 2keep rot dup +eq+ eq? [ drop @ ] [ 2nip ] if ] ]