Debian Codegraph @

%3a132/vmsort.scm

raw symbols SCIP
1 ;;; The sort package -- stable vector merge & merge sort -*- Scheme -*-
2 ;;; Copyright (c) 1998 by Olin Shivers.
3 ;;; This code is open-source; see the end of the file for porting and
4 ;;; more copyright information.
5 ;;; Olin Shivers 10/98.
6
7 ;;; Exports:
8 ;;; (vector-merge < v1 v2 [start1 end1 start2 end2]) -> vector
9 ;;; (vector-merge! < v v1 v2 [start0 start1 end1 start2 end2]) -> unspecific
10 ;;;
11 ;;; (vector-merge-sort < v [start end temp]) -> vector
12 ;;; (vector-merge-sort! < v [start end temp]) -> unspecific
13
14
15 ;;; Merge
16 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17 ;;; (vector-merge < v1 v2 [start1 end1 start2 end2]) -> vector
18 ;;; (vector-merge! < v v1 v2 [start start1 end1 start2 end2]) -> unspecific
19 ;;;
20 ;;; Stable vector merge -- V1's elements come out ahead of equal V2 elements.
21
22 (define (vector-merge < v1 v2 . maybe-starts+ends)
23 (call-with-values
24 (lambda () (vectors-start+end-2 v1 v2 maybe-starts+ends))
25 (lambda (start1 end1 start2 end2)
26 (let ((ans (make-vector (+ (- end1 start1) (- end2 start2)))))
27 (%vector-merge! < ans v1 v2 0 start1 end1 start2 end2)
28 ans))))
29
30 (define (vector-merge! < v v1 v2 . maybe-starts+ends)
31 (call-with-values
32 (lambda ()
33 (if (pair? maybe-starts+ends)
34 (values (car maybe-starts+ends)
35 (cdr maybe-starts+ends))
36 (values 0
37 '())))
38 (lambda (start rest)
39 (call-with-values
40 (lambda () (vectors-start+end-2 v1 v2 rest))
41 (lambda (start1 end1 start2 end2)
42 (%vector-merge! < v v1 v2 start start1 end1 start2 end2))))))
43
44
45 ;;; This routine is not exported. The code is tightly bummed.
46 ;;;
47 ;;; If these preconditions hold, the routine can be bummed to run with
48 ;;; unsafe vector-indexing and fixnum arithmetic ops:
49 ;;; - V V1 V2 are vectors.
50 ;;; - START START1 END1 START2 END2 are fixnums.
51 ;;; - (<= 0 START END0 (vector-length V),
52 ;;; where end0 = start + (end1 - start1) + (end2 - start2)
53 ;;; - (<= 0 START1 END1 (vector-length V1))
54 ;;; - (<= 0 START2 END2 (vector-length V2))
55 ;;; If you put these error checks in the two client procedures above, you can
56 ;;; safely convert this procedure to use unsafe ops -- which is why it isn't
57 ;;; exported. This will provide *huge* speedup.
58
59 (define (%vector-merge! elt< v v1 v2 start start1 end1 start2 end2)
60 (letrec ((vblit (lambda (fromv j i end) ; Blit FROMV[J,END) to V[I,?].
61 (let lp ((j j) (i i))
62 (vector-set! v i (vector-ref fromv j))
63 (let ((j (+ j 1)))
64 (if (< j end) (lp j (+ i 1))))))))
65
66 (cond ((<= end1 start1) (if (< start2 end2) (vblit v2 start2 start end2)))
67 ((<= end2 start2) (vblit v1 start1 start end1))
68
69 ;; Invariants: I is next index of V to write; X = V1[J]; Y = V2[K].
70 (else (let lp ((i start)
71 (j start1) (x (vector-ref v1 start1))
72 (k start2) (y (vector-ref v2 start2)))
73 (let ((i1 (+ i 1))) ; "i+1" is a complex number in R4RS!
74 (if (elt< y x)
75 (let ((k (+ k 1)))
76 (vector-set! v i y)
77 (if (< k end2)
78 (lp i1 j x k (vector-ref v2 k))
79 (vblit v1 j i1 end1)))
80 (let ((j (+ j 1)))
81 (vector-set! v i x)
82 (if (< j end1)
83 (lp i1 j (vector-ref v1 j) k y)
84 (vblit v2 k i1 end2))))))))))
85
86
87 ;;; (vector-merge-sort < v [start end temp]) -> vector
88 ;;; (vector-merge-sort! < v [start end temp]) -> unspecific
89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
90 ;;; Stable natural vector merge sort
91
92 (define (vector-merge-sort! < v . maybe-args)
93 (call-with-values
94 (lambda () (vector-start+end v maybe-args))
95 (lambda (start end)
96 (let ((temp (if (and (pair? maybe-args) ; kludge
97 (pair? (cdr maybe-args))
98 (pair? (cddr maybe-args)))
99 (caddr maybe-args)
100 (vector-copy v))))
101 (%vector-merge-sort! < v start end temp)))))
102
103 (define (vector-merge-sort < v . maybe-args)
104 (call-with-values
105 (lambda () (vector-start+end v maybe-args))
106 (lambda (start end)
107 (let ((ans (r7rs-vector-copy v start end)))
108 (vector-merge-sort! < ans)
109 ans))))
110
111
112 ;;; %VECTOR-MERGE-SORT! is not exported.
113 ;;; Preconditions:
114 ;;; V TEMP vectors
115 ;;; START END fixnums
116 ;;; START END legal indices for V and TEMP
117 ;;; If these preconditions are ensured by the cover functions, you
118 ;;; can safely change this code to use unsafe fixnum arithmetic and vector
119 ;;; indexing ops, for *huge* speedup.
120
121 ;;; This merge sort is "opportunistic" -- the leaves of the merge tree are
122 ;;; contiguous runs of already sorted elements in the vector. In the best
123 ;;; case -- an already sorted vector -- it runs in linear time. Worst case
124 ;;; is still O(n lg n) time.
125
126 (define (%vector-merge-sort! elt< v0 l r temp0)
127 (define (xor a b) (not (eq? a b)))
128
129 ;; Merge v1[l,l+len1) and v2[l+len1,l+len1+len2) into target[l,l+len1+len2)
130 ;; Merge left-to-right, so that TEMP may be either V1 or V2
131 ;; (that this is OK takes a little bit of thought).
132 ;; V2=TARGET? is true if V2 and TARGET are the same, which allows
133 ;; merge to punt the final blit half of the time.
134
135 (define (merge target v1 v2 l len1 len2 v2=target?)
136 (letrec ((vblit (lambda (fromv j i end) ; Blit FROMV[J,END) to TARGET[I,?]
137 (let lp ((j j) (i i)) ; J < END. The final copy.
138 (vector-set! target i (vector-ref fromv j))
139 (let ((j (+ j 1)))
140 (if (< j end) (lp j (+ i 1))))))))
141
142 (let* ((r1 (+ l len1))
143 (r2 (+ r1 len2)))
144 ; Invariants:
145 (let lp ((n l) ; N is next index of
146 (j l) (x (vector-ref v1 l)) ; TARGET to write.
147 (k r1) (y (vector-ref v2 r1))) ; X = V1[J]
148 (let ((n+1 (+ n 1))) ; Y = V2[K]
149 (if (elt< y x)
150 (let ((k (+ k 1)))
151 (vector-set! target n y)
152 (if (< k r2)
153 (lp n+1 j x k (vector-ref v2 k))
154 (vblit v1 j n+1 r1)))
155 (let ((j (+ j 1)))
156 (vector-set! target n x)
157 (if (< j r1)
158 (lp n+1 j (vector-ref v1 j) k y)
159 (if (not v2=target?) (vblit v2 k n+1 r2))))))))))
160
161
162 ;; Might hack GETRUN so that if the run is short it pads it out to length
163 ;; 10 with insert sort...
164
165 ;; Precondition: l < r.
166 (define (getrun v l r)
167 (let lp ((i (+ l 1)) (x (vector-ref v l)))
168 (if (>= i r)
169 (- i l)
170 (let ((y (vector-ref v i)))
171 (if (elt< y x)
172 (- i l)
173 (lp (+ i 1) y))))))
174
175 ;; RECUR: Sort V0[L,L+LEN) for some LEN where 0 < WANT <= LEN <= (R-L).
176 ;; That is, sort *at least* WANT elements in V0 starting at index L.
177 ;; May put the result into either V0[L,L+LEN) or TEMP0[L,L+LEN).
178 ;; Must not alter either vector outside this range.
179 ;; Return:
180 ;; - LEN -- the number of values we sorted
181 ;; - ANSVEC -- the vector holding the value
182 ;; - ANS=V0? -- tells if ANSVEC is V0 or TEMP
183 ;;
184 ;; LP: V[L,L+PFXLEN) holds a sorted prefix of V0.
185 ;; TEMP = if V = V0 then TEMP0 else V0. (I.e., TEMP is the other vec.)
186 ;; PFXLEN2 is a power of 2 <= PFXLEN.
187 ;; Solve RECUR's problem.
188 (if (< l r) ; Don't try to sort an empty range.
189 (call-with-values
190 (lambda ()
191 (let recur ((l l) (want (- r l)))
192 (let ((len (- r l)))
193 (let lp ((pfxlen (getrun v0 l r)) (pfxlen2 1)
194 (v v0) (temp temp0)
195 (v=v0? #t))
196 (if (or (>= pfxlen want) (= pfxlen len))
197 (values pfxlen v v=v0?)
198 (let ((pfxlen2 (let lp ((j pfxlen2))
199 (let ((j*2 (+ j j)))
200 (if (<= j pfxlen) (lp j*2) j))))
201 (tail-len (- len pfxlen)))
202 ;; PFXLEN2 is now the largest power of 2 <= PFXLEN.
203 ;; (Just think of it as being roughly PFXLEN.)
204 (call-with-values
205 (lambda ()
206 (recur (+ pfxlen l) pfxlen2))
207 (lambda (nr-len nr-vec nrvec=v0?)
208 (merge temp v nr-vec l pfxlen nr-len
209 (xor nrvec=v0? v=v0?))
210 (lp (+ pfxlen nr-len) (+ pfxlen2 pfxlen2)
211 temp v (not v=v0?))))))))))
212 (lambda (ignored-len ignored-ansvec ansvec=v0?)
213 (if (not ansvec=v0?)
214 (r7rs-vector-copy! v0 l temp0 l r))))))
215
216
217 ;;; Copyright
218 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 ;;; This code is
220 ;;; Copyright (c) 1998 by Olin Shivers.
221 ;;; The terms are: You may do as you please with this code, as long as
222 ;;; you do not delete this notice or hold me responsible for any outcome
223 ;;; related to its use.
224 ;;;
225 ;;; Blah blah blah. Don't you think source files should contain more lines
226 ;;; of code than copyright notice?
227
228
229 ;;; Code tuning & porting
230 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231 ;;; This code is *tightly* bummed as far as I can go in portable Scheme.
232 ;;;
233 ;;; The two internal primitives that do the real work can be converted to
234 ;;; use unsafe vector-indexing and fixnum-specific arithmetic ops *if* you
235 ;;; alter the four small cover functions to enforce the invariants. This should
236 ;;; provide *big* speedups. In fact, all the code bumming I've done pretty
237 ;;; much disappears in the noise unless you have a good compiler and also
238 ;;; can dump the vector-index checks and generic arithmetic -- so I've really
239 ;;; just set things up for you to exploit.
240 ;;;
241 ;;; The optional-arg parsing, defaulting, and error checking is done with a
242 ;;; portable R4RS macro. But if your Scheme has a faster mechanism (e.g.,
243 ;;; Chez), you should definitely port over to it. Note that argument defaulting
244 ;;; and error-checking are interleaved -- you don't have to error-check
245 ;;; defaulted START/END args to see if they are fixnums that are legal vector
246 ;;; indices for the corresponding vector, etc.