Debian Codegraph @

%3a132/select.scm

raw symbols SCIP
1 ;;; Linear-time (average case) algorithms for:
2 ;;;
3 ;;; Selecting the kth smallest element from an unsorted vector.
4 ;;; Selecting the kth and (k+1)st smallest elements from an unsorted vector.
5 ;;; Selecting the median from an unsorted vector.
6
7 ;;; These procedures are part of SRFI 132 but are missing from
8 ;;; its reference implementation as of 10 March 2016.
9
10 ;;; SRFI 132 says this procedure runs in O(n) time.
11 ;;; As implemented, however, the worst-case time is O(n^2) because
12 ;;; vector-select is implemented using randomized quickselect.
13 ;;; The average time is O(n), and you'd have to be unlucky
14 ;;; to approach the worst case.
15
16 (define (vector-find-median < v knil . rest)
17 (let* ((mean (if (null? rest)
18 (lambda (a b) (/ (+ a b) 2))
19 (car rest)))
20 (n (vector-length v)))
21 (cond ((zero? n)
22 knil)
23 ((odd? n)
24 (%vector-select < v (quotient n 2) 0 n))
25 (else
26 (call-with-values
27 (lambda () (%vector-select2 < v (- (quotient n 2) 1) 0 n))
28 (lambda (a b)
29 (mean a b)))))))
30
31 ;;; For this procedure, the SRFI 132 specification
32 ;;; demands the vector be sorted (by side effect).
33
34 (define (vector-find-median! < v knil . rest)
35 (let* ((mean (if (null? rest)
36 (lambda (a b) (/ (+ a b) 2))
37 (car rest)))
38 (n (vector-length v)))
39 (vector-sort! < v)
40 (cond ((zero? n)
41 knil)
42 ((odd? n)
43 (vector-ref v (quotient n 2)))
44 (else
45 (mean (vector-ref v (- (quotient n 2) 1))
46 (vector-ref v (quotient n 2)))))))
47
48 ;;; SRFI 132 says this procedure runs in O(n) time.
49 ;;; As implemented, however, the worst-case time is O(n^2).
50 ;;; The average time is O(n), and you'd have to be unlucky
51 ;;; to approach the worst case.
52 ;;;
53 ;;; After rest argument processing, calls the private version defined below.
54
55 (define (vector-select < v k . rest)
56 (let* ((start (if (null? rest)
57 0
58 (car rest)))
59 (end (if (and (pair? rest)
60 (pair? (cdr rest)))
61 (car (cdr rest))
62 (vector-length v))))
63 (%vector-select < v k start end)))
64
65 ;;; The vector-select procedure is needed internally to implement
66 ;;; vector-find-median, but SRFI 132 has been changed (for no good
67 ;;; reason) to export vector-select! instead of vector-select.
68 ;;; Fortunately, vector-select! is not required to have side effects.
69
70 (define vector-select! vector-select)
71
72 ;;; This could be made slightly more efficient, but who cares?
73
74 (define (vector-separate! < v k . rest)
75 (let* ((start (if (null? rest)
76 0
77 (car rest)))
78 (end (if (and (pair? rest)
79 (pair? (cdr rest)))
80 (car (cdr rest))
81 (vector-length v))))
82 (if (and (> k 0)
83 (> end start))
84 (let ((pivot (vector-select < v (- k 1) start end)))
85 (call-with-values
86 (lambda () (count-smaller < pivot v start end 0 0))
87 (lambda (count count2)
88 (let* ((v2 (make-vector count))
89 (v3 (make-vector (- end start count count2))))
90 (copy-smaller! < pivot v2 0 v start end)
91 (copy-bigger! < pivot v3 0 v start end)
92 (r7rs-vector-copy! v start v2)
93 (r7rs-vector-fill! v pivot (+ start count) (+ start count count2))
94 (r7rs-vector-copy! v (+ start count count2) v3))))))))
95
96 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
97
98 ;;; For small ranges, sorting may be the fastest way to find the kth element.
99 ;;; This threshold is not at all critical, and may not even be worthwhile.
100
101 (define just-sort-it-threshold 50)
102
103 ;;; Given
104 ;;; an irreflexive total order <?
105 ;;; a vector v
106 ;;; an index k
107 ;;; an index start
108 ;;; an index end
109 ;;; with
110 ;;; 0 <= k < (- end start)
111 ;;; 0 <= start < end <= (vector-length v)
112 ;;; returns
113 ;;; (vector-ref (vector-sort <? (vector-copy v start end)) (+ start k))
114 ;;; but is usually faster than that.
115
116 (define (%vector-select <? v k start end)
117 (assert (and 'vector-select
118 (procedure? <?)
119 (vector? v)
120 (exact-integer? k)
121 (exact-integer? start)
122 (exact-integer? end)
123 (<= 0 k (- end start 1))
124 (<= 0 start end (vector-length v))))
125 (%%vector-select <? v k start end))
126
127 ;;; Given
128 ;;; an irreflexive total order <?
129 ;;; a vector v
130 ;;; an index k
131 ;;; an index start
132 ;;; an index end
133 ;;; with
134 ;;; 0 <= k < (- end start 1)
135 ;;; 0 <= start < end <= (vector-length v)
136 ;;; returns two values:
137 ;;; (vector-ref (vector-sort <? (vector-copy v start end)) (+ start k))
138 ;;; (vector-ref (vector-sort <? (vector-copy v start end)) (+ start k 1))
139 ;;; but is usually faster than that.
140
141 (define (%vector-select2 <? v k start end)
142 (assert (and 'vector-select
143 (procedure? <?)
144 (vector? v)
145 (exact-integer? k)
146 (exact-integer? start)
147 (exact-integer? end)
148 (<= 0 k (- end start 1 1))
149 (<= 0 start end (vector-length v))))
150 (%%vector-select2 <? v k start end))
151
152 ;;; Like %vector-select, but its preconditions have been checked.
153
154 (define (%%vector-select <? v k start end)
155 (let ((size (- end start)))
156 (cond ((= 1 size)
157 (vector-ref v (+ k start)))
158 ((= 2 size)
159 (cond ((<? (vector-ref v start)
160 (vector-ref v (+ start 1)))
161 (vector-ref v (+ k start)))
162 (else
163 (vector-ref v (+ (- 1 k) start)))))
164 ((< size just-sort-it-threshold)
165 (vector-ref (vector-sort <? (r7rs-vector-copy v start end)) k))
166 (else
167 (let* ((ip (random-integer size))
168 (pivot (vector-ref v (+ start ip))))
169 (call-with-values
170 (lambda () (count-smaller <? pivot v start end 0 0))
171 (lambda (count count2)
172 (cond ((< k count)
173 (let* ((n count)
174 (v2 (make-vector n)))
175 (copy-smaller! <? pivot v2 0 v start end)
176 (%%vector-select <? v2 k 0 n)))
177 ((< k (+ count count2))
178 pivot)
179 (else
180 (let* ((n (- size count count2))
181 (v2 (make-vector n))
182 (k2 (- k count count2)))
183 (copy-bigger! <? pivot v2 0 v start end)
184 (%%vector-select <? v2 k2 0 n)))))))))))
185
186 ;;; Like %%vector-select, but returns two values:
187 ;;;
188 ;;; (vector-ref (vector-sort <? (vector-copy v start end)) (+ start k))
189 ;;; (vector-ref (vector-sort <? (vector-copy v start end)) (+ start k 1))
190 ;;;
191 ;;; Returning two values is useful when finding the median of an even
192 ;;; number of things.
193
194 (define (%%vector-select2 <? v k start end)
195 (let ((size (- end start)))
196 (cond ((= 2 size)
197 (let ((a (vector-ref v start))
198 (b (vector-ref v (+ start 1))))
199 (cond ((<? a b)
200 (values a b))
201 (else
202 (values b a)))))
203 ((< size just-sort-it-threshold)
204 (let ((v2 (vector-sort <? (r7rs-vector-copy v start end))))
205 (values (vector-ref v2 k)
206 (vector-ref v2 (+ k 1)))))
207 (else
208 (let* ((ip (random-integer size))
209 (pivot (vector-ref v (+ start ip))))
210 (call-with-values
211 (lambda () (count-smaller <? pivot v start end 0 0))
212 (lambda (count count2)
213 (cond ((= (+ k 1) count)
214 (values (%%vector-select <? v k start end)
215 pivot))
216 ((< k count)
217 (let* ((n count)
218 (v2 (make-vector n)))
219 (copy-smaller! <? pivot v2 0 v start end)
220 (%%vector-select2 <? v2 k 0 n)))
221 ((< k (+ count count2))
222 (values pivot
223 (if (< (+ k 1) (+ count count2))
224 pivot
225 (%%vector-select <? v (+ k 1) start end))))
226 (else
227 (let* ((n (- size count count2))
228 (v2 (make-vector n))
229 (k2 (- k count count2)))
230 (copy-bigger! <? pivot v2 0 v start end)
231 (%%vector-select2 <? v2 k2 0 n)))))))))))
232
233 ;;; Counts how many elements within the range are less than the pivot
234 ;;; and how many are equal to the pivot, returning both of those counts.
235
236 (define (count-smaller <? pivot v i end count count2)
237 (cond ((= i end)
238 (values count count2))
239 ((<? (vector-ref v i) pivot)
240 (count-smaller <? pivot v (+ i 1) end (+ count 1) count2))
241 ((<? pivot (vector-ref v i))
242 (count-smaller <? pivot v (+ i 1) end count count2))
243 (else
244 (count-smaller <? pivot v (+ i 1) end count (+ count2 1)))))
245
246 ;;; Like vector-copy! but copies an element only if it is less than the pivot.
247 ;;; The destination vector must be large enough.
248
249 (define (copy-smaller! <? pivot dst at src start end)
250 (cond ((= start end) dst)
251 ((<? (vector-ref src start) pivot)
252 (vector-set! dst at (vector-ref src start))
253 (copy-smaller! <? pivot dst (+ at 1) src (+ start 1) end))
254 (else
255 (copy-smaller! <? pivot dst at src (+ start 1) end))))
256
257 ;;; Like copy-smaller! but copies only elements that are greater than the pivot.
258
259 (define (copy-bigger! <? pivot dst at src start end)
260 (cond ((= start end) dst)
261 ((<? pivot (vector-ref src start))
262 (vector-set! dst at (vector-ref src start))
263 (copy-bigger! <? pivot dst (+ at 1) src (+ start 1) end))
264 (else
265 (copy-bigger! <? pivot dst at src (+ start 1) end))))
266