forked from factor/factor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquick.factor
56 lines (41 loc) · 1.54 KB
/
quick.factor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
! Copyright (C) 2014 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: arrays combinators kernel locals math math.order
math.private sequences sequences.private strings vectors ;
IN: sorting.quick
<PRIVATE
:: quicksort ( seq from to quot: ( obj1 obj2 -- <=> ) -- )
from to < [
from to fixnum+fast 2/ seq nth-unsafe :> pivot
from to [ 2dup <= ] [
[
over seq nth-unsafe pivot quot call
+lt+ eq?
] [ [ 1 fixnum+fast ] dip ] while
[
dup seq nth-unsafe pivot quot call
+gt+ eq?
] [ 1 fixnum-fast ] while
2dup <= [
[ seq exchange-unsafe ]
[ [ 1 fixnum+fast ] [ 1 fixnum-fast ] bi* ] 2bi
] when
] while
[ seq from ] dip quot quicksort
[ seq ] dip to quot quicksort
] when ; inline recursive
: check-array-capacity ( n -- n )
integer>fixnum-strict dup array-capacity?
[ "too large" throw ] unless ; inline
PRIVATE>
: sort! ( seq quot: ( obj1 obj2 -- <=> ) -- )
[ 0 over length check-array-capacity 1 - ] dip quicksort ; inline
: sort-with! ( seq quot: ( elt -- key ) -- )
[ compare ] curry sort! ; inline
: inv-sort-with! ( seq quot: ( elt -- key ) -- )
[ compare invert-comparison ] curry sort! ; inline
GENERIC: natural-sort! ( seq -- )
M: object natural-sort! [ <=> ] sort! ;
M: array natural-sort! [ <=> ] sort! ;
M: vector natural-sort! [ <=> ] sort! ;
M: string natural-sort! [ <=> ] sort! ;