-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathsort.ss
58 lines (48 loc) · 1.47 KB
/
sort.ss
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
57
58
;;; -*- Gerbil -*-
;;; (C) vyzo at hackzen.org
;;; Sorting -- using Olin Shivers' withdrawn srfi-32 impl
(import :std/error)
(export sort sort! stable-sort stable-sort!)
(declare
(not safe)
(fixnum))
;; front-end procs
(defrules defproc ()
((_ proc lsort vsort)
(def (proc seq elt<)
(unless (procedure? elt<)
(error "Expected procedure" elt<))
(cond
((null? seq) seq)
((pair? seq) (lsort seq elt<))
((vector? seq) (vsort seq elt<))
(else
(error "Expected list or vector" seq))))))
(defproc sort list-merge-sort heap-sort)
(defproc sort! list-merge-sort! vector-heap-sort!)
(defproc stable-sort list-merge-sort vector-merge-sort)
(defproc stable-sort! list-merge-sort! vector-merge-sort!)
;;; Implementation
;; frobbing for sort-support
(extern (real-vector-copy vector-copy))
(defrules vector-copy ()
((_ v) (real-vector-copy v))
((_ v start end) (subvector v start end)))
(def (vector-copy! target src start end)
(subvector-move! src start end target 0))
;; frobbing for vector-merge-sort
(def (vector-merge-sort! v elt<)
(%vector-merge-sort! v elt< 0 (vector-length v) (vector-copy v))
v)
(def (vector-merge-sort v elt<)
(vector-merge-sort! (vector-copy v) elt<))
;; frobbing for heap sort
(def (vector-heap-sort! v elt<)
(heap-sort! v elt<)
v)
;; Olin's implementation
(include "sort/sort-support.scm")
(include "sort/lmsort.scm")
(include "sort/vhsort.scm")
(include "sort/vmsort.scm")
(include "sort/sortp.scm")