Debian Codegraph @

%3a132/visort.scm

raw symbols SCIP
1 ;;; The sort package -- stable vector insertion 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-insert-sort < v [start end] -> vector
9 ;;; vector-insert-sort! < v [start end] -> unspecific
10 ;;;
11 ;;; %vector-insert-sort! is also called from vqsort.scm's quick-sort function.
12
13 (define (vector-insert-sort elt< v . maybe-start+end)
14 (call-with-values
15 (lambda () (vector-start+end v maybe-start+end))
16 (lambda (start end)
17 (let ((ans (vector-portion-copy v start end)))
18 (%vector-insert-sort! elt< ans 0 (- end start))
19 ans))))
20
21 (define (vector-insert-sort! < v . maybe-start+end)
22 (call-with-values
23 (lambda () (vector-start+end v maybe-start+end))
24 (lambda (start end)
25 (%vector-insert-sort! < v start end))))
26
27 (define (%vector-insert-sort! elt< v start end)
28 (do ((i (+ 1 start) (+ i 1))) ; Invariant: [start,i) is sorted.
29 ((>= i end))
30 (let ((val (vector-ref v i)))
31 (vector-set! v (let lp ((j i)) ; J is the location of the
32 (if (<= j start)
33 start ; "hole" as it bubbles down.
34 (let* ((j-1 (- j 1))
35 (vj-1 (vector-ref v j-1)))
36 (cond ((elt< val vj-1)
37 (vector-set! v j vj-1)
38 (lp j-1))
39 (else j)))))
40 val))))
41
42 ;;; Copyright
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;;; This code is
45 ;;; Copyright (c) 1998 by Olin Shivers.
46 ;;; The terms are: You may do as you please with this code, as long as
47 ;;; you do not delete this notice or hold me responsible for any outcome
48 ;;; related to its use.
49 ;;;
50 ;;; Blah blah blah. Don't you think source files should contain more lines
51 ;;; of code than copyright notice?
52
53
54 ;;; Code tuning & porting
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 ;;;
57 ;;; This code is tightly bummed as far as I can go in portable Scheme.
58 ;;;
59 ;;; The code can be converted to use unsafe vector-indexing and
60 ;;; fixnum-specific arithmetic ops -- the safety checks done on entry
61 ;;; to VECTOR-INSERT-SORT and VECTOR-INSERT-SORT! are sufficient to
62 ;;; guarantee nothing bad will happen. However, note that if you alter
63 ;;; %VECTOR-INSERT-SORT! to use dangerous primitives, you must ensure
64 ;;; it is only called from clients that guarantee to observe its
65 ;;; preconditions. In the implementation, %VECTOR-INSERT-SORT! is only
66 ;;; called from VECTOR-INSERT-SORT! and the quick-sort code in
67 ;;; vqsort.scm, and the preconditions are guaranteed for these two
68 ;;; clients. This should provide *big* speedups. In fact, all the
69 ;;; code bumming I've done pretty much disappears in the noise unless
70 ;;; you have a good compiler and also can dump the vector-index checks
71 ;;; and generic arithmetic -- so I've really just set things up for
72 ;;; you to exploit.
73 ;;;
74 ;;; If your Scheme has a faster mechanism for handling optional arguments
75 ;;; (e.g., Chez), you should definitely port over to it. Note that argument
76 ;;; defaulting and error-checking are interleaved -- you don't have to
77 ;;; error-check defaulted START/END args to see if they are fixnums that are
78 ;;; legal vector indices for the corresponding vector, etc.