-
Notifications
You must be signed in to change notification settings - Fork 12
/
liii_sort.tmu
439 lines (238 loc) · 10.8 KB
/
liii_sort.tmu
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<TMU|<tuple|1.0.5|1.2.9.7>>
<style|<tuple|generic|chinese|goldfish|literate|reduced-margins>>
<\body>
<\hide-preamble>
<assign|r7rs|<flag|R7RS|dark cyan>>
<assign|srfi|<flag|SRFI|dark red>>
<assign|font|math=Latin Modern Math,cjk=Noto CJK SC,CMU>
</hide-preamble>
<chapter|(liii sort)>
<section|许可证>
<\scm-chunk|goldfish/liii/sort.scm|false|true>
;
; Copyright (C) 2024 The Goldfish Scheme Authors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
; License for the specific language governing permissions and limitations
; under the License.
;
\;
</scm-chunk>
<\scm-chunk|goldfish/srfi/srfi-132.scm|false|true>
;
; Copyright (C) 2024 The Goldfish Scheme Authors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
; License for the specific language governing permissions and limitations
; under the License.
;
\;
</scm-chunk>
<\scm-chunk|tests/goldfish/liii/sort-test.scm|false|true>
;
; Copyright (C) 2024 The Goldfish Scheme Authors
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
; License for the specific language governing permissions and limitations
; under the License.
;
\;
</scm-chunk>
<section|接口>
<\scm-chunk|goldfish/liii/sort.scm|true|false>
(define-library (liii sort)
(export list-sorted? vector-sorted?
\ \ \ \ \ \ \ \ list-merge \ list-sort \ list-stable-sort \ vector-merge \ vector-sort \ vector-stable-sort
\ \ \ \ \ \ \ \ list-merge! list-sort! list-stable-sort! vector-merge! vector-sort! vector-stable-sort!)
(import (srfi srfi-132)))
\;
</scm-chunk>
<\scm-chunk|goldfish/srfi/srfi-132.scm|true|true>
(define-library (srfi srfi-132)
(export list-sorted? vector-sorted?
\ \ \ \ \ \ \ \ list-merge \ list-sort \ list-stable-sort \ vector-merge \ vector-sort \ vector-stable-sort
\ \ \ \ \ \ \ \ list-merge! list-sort! list-stable-sort! vector-merge! vector-sort! vector-stable-sort!)
(import (liii list)
\ \ \ \ \ \ \ \ (liii error)
\ \ \ \ \ \ \ \ (scheme case-lambda))
(begin
\;
</scm-chunk>
<\section>
实现
</section>
<subsection|list-sorted?, vector-sorted?>
判断 list 或 vector 是否有序。<verbatim|vector-sorted?> 尚未实现可选参数 start 和 end。
<\scm-chunk|goldfish/srfi/srfi-132.scm|true|true>
\ \ (define (list-sorted? less-p lis)
\ \ \ \ (if (null? lis)
\ \ \ \ \ \ #t
\ \ \ \ \ \ (do ((first lis (cdr first))
\ \ \ \ \ \ \ \ \ \ \ (second (cdr lis) (cdr second))
\ \ \ \ \ \ \ \ \ \ \ (res #t (not (less-p (car second) (car first)))))
\ \ \ \ \ \ \ \ ((or (null? second) (not res)) res))))
\;
\ \ ; TODO optional parameters
\ \ (define (vector-sorted? less-p v)
\ \ \ \ (let ((start 0)
\ \ \ \ \ \ \ \ \ \ (end (length v)))
\ \ \ \ \ \ (do ((first start (+ 1 first))
\ \ \ \ \ \ \ \ \ \ \ (second (+ 1 start) (+ 1 second))
\ \ \ \ \ \ \ \ \ \ \ (res #t (not (less-p (vector-ref v second) (vector-ref v first)))))
\ \ \ \ \ \ \ \ ((or (\<gtr\>= second end) (not res)) res))))
\;
</scm-chunk>
<subsection|list-merge, list-sort, list-stable-sort>
归并排序。需要注意 <verbatim|list-sort!> 不必修改原 list,但 <verbatim|vector-sort!> 要保证修改原 vector。
<\scm-chunk|goldfish/srfi/srfi-132.scm|true|true>
\ \ (define (list-merge less-p lis1 lis2)
\ \ \ \ (let loop
\ \ \ \ \ \ ((res '())
\ \ \ \ \ \ \ (lis1 lis1)
\ \ \ \ \ \ \ (lis2 lis2))
\ \ \ \ \ \ (cond
\ \ \ \ \ \ \ \ ((and (null? lis1) (null? lis2)) (reverse res))
\ \ \ \ \ \ \ \ ((null? lis1) (loop (cons (car lis2) res) lis1 (cdr lis2)))
\ \ \ \ \ \ \ \ ((null? lis2) (loop (cons (car lis1) res) lis2 (cdr lis1)))
\ \ \ \ \ \ \ \ ((less-p (car lis2) (car lis1)) (loop (cons (car lis2) res) lis1 (cdr lis2)))
\ \ \ \ \ \ \ \ (else (loop (cons (car lis1) res) (cdr lis1) lis2)))))
\;
\ \ ; this list-merge! violates SRFI 132, since it does not satisfy the constant running space constraint specified in SRFI 132, and does not work "in place"
\ \ (define list-merge! list-merge)
\;
\ \ (define (list-stable-sort less-p lis)
\ \ \ \ (define (sort l r)
\ \ \ \ \ \ (cond
\ \ \ \ \ \ \ \ ((= l r) '())
\ \ \ \ \ \ \ \ ((= (+ l 1) r) (list (list-ref lis l)))
\ \ \ \ \ \ \ \ (else
\ \ \ \ \ \ \ \ \ \ (let* ((mid (quotient (+ l r) 2))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (l-sorted (sort l mid))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (r-sorted (sort mid r)))
\ \ \ \ \ \ \ \ \ \ \ \ (list-merge less-p l-sorted r-sorted)))))
\ \ \ \ (sort 0 (length lis)))
\;
\ \ (define list-sort list-stable-sort)
\ \ (define list-sort! list-stable-sort)
\ \ (define list-stable-sort! list-stable-sort)
\;
</scm-chunk>
<subsection|vector-merge, vector-sort, vector-stable-sort>
无副作用时调用 list 相关函数实现。原地排序尚未实现。
<\scm-chunk|goldfish/srfi/srfi-132.scm|true|true>
\ \ (define vector-stable-sort
\ \ \ \ (case-lambda
\ \ \ \ \ \ ((less-p v)
\ \ \ \ \ \ \ (list-\<gtr\>vector (list-stable-sort less-p (vector-\<gtr\>list v))))
\ \ \ \ \ \ ((less-p v start)
\ \ \ \ \ \ \ (list-\<gtr\>vector (list-stable-sort less-p (subvector-\<gtr\>list v start (vector-length v)))))
\ \ \ \ \ \ ((less-p v start end)
\ \ \ \ \ \ \ (list-\<gtr\>vector (list-stable-sort less-p (subvector-\<gtr\>list v start end))))))
\;
\ \ (define vector-sort vector-stable-sort)
\;
\ \ (define (vector-sort! . r) (???))
\ \ (define (vector-stable-sort! . r) (???))
\;
\ \ (define (subvector-\<gtr\>list v start end)
\ \ \ \ (do ((r '() (cons (vector-ref v p) r))
\ \ \ \ \ \ \ \ \ (p start (+ 1 p)))
\ \ \ \ \ \ ((\<gtr\>= p end) (reverse r))))
\;
\ \ (define vector-merge
\ \ \ \ (case-lambda
\ \ \ \ \ \ ((less-p v1 v2)
\ \ \ \ \ \ \ (list-\<gtr\>vector (list-merge less-p (vector-\<gtr\>list v1) (vector-\<gtr\>list v2))))
\ \ \ \ \ \ ((less-p v1 v2 start1)
\ \ \ \ \ \ \ (list-\<gtr\>vector (list-merge less-p (subvector-\<gtr\>list v1 start1 (vector-length v1)) (vector-\<gtr\>list v2))))
\ \ \ \ \ \ ((less-p v1 v2 start1 end1)
\ \ \ \ \ \ \ (list-\<gtr\>vector (list-merge less-p (subvector-\<gtr\>list v1 start1 end1) (vector-\<gtr\>list v2))))
\ \ \ \ \ \ ((less-p v1 v2 start1 end1 start2)
\ \ \ \ \ \ \ (list-\<gtr\>vector (list-merge less-p (subvector-\<gtr\>list v1 start1 end1) (subvector-\<gtr\>list v2 start2 (vector-length v2)))))
\ \ \ \ \ \ ((less-p v1 v2 start1 end1 start2 end2)
\ \ \ \ \ \ \ (list-\<gtr\>vector (list-merge less-p (subvector-\<gtr\>list v1 start1 end1) (subvector-\<gtr\>list v2 start2 end2))))))
\;
\ \ (define (vector-merge! . r) (???))
\;
</scm-chunk>
<section|测试>
<\scm-chunk|tests/goldfish/liii/sort-test.scm|true|false>
(import (liii check)
\ \ \ \ \ \ \ \ (liii sort))
\;
(check-set-mode! 'report-failed)
\;
(define (pair-\<less\> x y)
\ \ (\<less\> (car x) (car y)))
\;
(define (pair-full-\<less\> x y)
\ \ (cond
\ \ \ \ ((not (= (car x) (car y))) (\<less\> (car x) (car y)))
\ \ \ \ (else (\<less\> (cdr y) (cdr x)))))
\;
(check-false (list-sorted? \<less\> '(1 5 1 0 -1 9 2 4 3)))
(check-false (vector-sorted? \<less\> #(1 5 1 0 -1 9 2 4 3)))
\;
(check-true (list-sorted? \<less\> (list-sort \<less\> '(1 5 1 0 -1 9 2 4 3))))
(check-true (list-sorted? \<less\> (list-stable-sort \<less\> '(1 5 1 0 -1 9 2 4 3))))
(check-true (list-sorted? pair-\<less\> (list-merge pair-\<less\> '((1 . 1) (1 . 2) (3 . 1)) '((1 . 3) (2 . 1) (3 . 2) (4 . 1)))))
(check (list-merge pair-\<less\> '((1 . 1) (1 . 2) (3 . 1)) '((1 . 3) (2 . 1) (3 . 2) (4 . 1)))
\ \ \ \ \ \ \ =\<gtr\> '((1 . 1) (1 . 2) (1 . 3) (2 . 1) (3 . 1) (3 . 2) (4 . 1)))
(check-true (list-sorted? pair-full-\<less\> (list-merge pair-full-\<less\> '((1 . 2) (1 . 1) (3 . 1)) '((1 . 3) (2 . 1) (3 . 2) (4 . 1)))))
(check (list-merge pair-full-\<less\> '((1 . 2) (1 . 1) (3 . 1)) '((1 . 3) (2 . 1) (3 . 2) (4 . 1)))
\ \ \ \ \ \ \ =\<gtr\> '((1 . 3) (1 . 2) (1 . 1) (2 . 1) (3 . 2) (3 . 1) (4 . 1)))
\;
(check-true (vector-sorted? \<less\> (vector-sort \<less\> #(1 5 1 0 -1 9 2 4 3))))
(check-true (vector-sorted? \<less\> (vector-stable-sort \<less\> #(1 5 1 0 -1 9 2 4 3))))
(check-true (vector-sorted? pair-\<less\> (vector-merge pair-\<less\> #((1 . 1) (1 . 2) (3 . 1)) #((1 . 3) (2 . 1) (3 . 2) (4 . 1)))))
(check (vector-merge pair-\<less\> #((1 . 1) (1 . 2) (3 . 1)) #((1 . 3) (2 . 1) (3 . 2) (4 . 1)))
\ \ \ \ \ \ \ =\<gtr\> #((1 . 1) (1 . 2) (1 . 3) (2 . 1) (3 . 1) (3 . 2) (4 . 1)))
(check-true (vector-sorted? pair-full-\<less\> (vector-merge pair-full-\<less\> #((1 . 2) (1 . 1) (3 . 1)) #((1 . 3) (2 . 1) (3 . 2) (4 . 1)))))
(check (vector-merge pair-full-\<less\> #((1 . 2) (1 . 1) (3 . 1)) #((1 . 3) (2 . 1) (3 . 2) (4 . 1)))
\ \ \ \ \ \ \ =\<gtr\> #((1 . 3) (1 . 2) (1 . 1) (2 . 1) (3 . 2) (3 . 1) (4 . 1)))
\;
(check-report)
\;
</scm-chunk>
<section|结尾>
<\scm-chunk|goldfish/srfi/srfi-132.scm|true|false>
) ; end of begin
) ; end of library
\;
</scm-chunk>
</body>
<\initial>
<\collection>
<associate|font-base-size|8>
<associate|page-height|auto>
<associate|page-screen-margin|false>
<associate|page-type|a5>
<associate|page-width|auto>
<associate|preamble|false>
<associate|save-aux|false>
</collection>
</initial>