| 1 | ;;; This file extracts four merge procedures from lmsort.scm and vmsort.scm |
| 2 | ;;; files written by Olin Shivers. |
| 3 | |
| 4 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 5 | ;;; |
| 6 | ;;; Start of code extracted from Olin's lmsort.scm file. |
| 7 | ;;; |
| 8 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 9 | |
| 10 | ;;; list merge & list merge-sort -*- Scheme -*- |
| 11 | ;;; Copyright (c) 1998 by Olin Shivers. |
| 12 | ;;; This code is open-source; see the end of the file for porting and |
| 13 | ;;; more copyright information. |
| 14 | ;;; Olin Shivers |
| 15 | |
| 16 | ;;; Exports: |
| 17 | ;;; (list-merge < lis lis) -> list |
| 18 | ;;; (list-merge! < lis lis) -> list |
| 19 | |
| 20 | ;;; Merge |
| 21 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 22 | ;;; These two merge procedures are stable -- ties favor list A. |
| 23 | |
| 24 | (define (list-merge < a b) |
| 25 | (cond ((not (pair? a)) b) |
| 26 | ((not (pair? b)) a) |
| 27 | (else (let recur ((x (car a)) (a a) ; A is a pair; X = (CAR A). |
| 28 | (y (car b)) (b b)) ; B is a pair; Y = (CAR B). |
| 29 | (if (< y x) |
| 30 | |
| 31 | (let ((b (cdr b))) |
| 32 | (if (pair? b) |
| 33 | (cons y (recur x a (car b) b)) |
| 34 | (cons y a))) |
| 35 | |
| 36 | (let ((a (cdr a))) |
| 37 | (if (pair? a) |
| 38 | (cons x (recur (car a) a y b)) |
| 39 | (cons x b)))))))) |
| 40 | |
| 41 | |
| 42 | ;;; This destructive merge does as few SET-CDR!s as it can -- for example, if |
| 43 | ;;; the list is already sorted, it does no SET-CDR!s at all. It is also |
| 44 | ;;; iterative, running in constant stack. |
| 45 | |
| 46 | (define (list-merge! < a b) |
| 47 | ;; The logic of these two loops is completely driven by these invariants: |
| 48 | ;; SCAN-A: (CDR PREV) = A. X = (CAR A). Y = (CAR B). |
| 49 | ;; SCAN-B: (CDR PREV) = B. X = (CAR A). Y = (CAR B). |
| 50 | (letrec ((scan-a (lambda (prev a x b y) ; Zip down A doing |
| 51 | (if (< y x) ; no SET-CDR!s until |
| 52 | (let ((next-b (cdr b))) ; we hit a B elt that |
| 53 | (set-cdr! prev b) ; has to be inserted. |
| 54 | (if (pair? next-b) |
| 55 | (scan-b b a x next-b (car next-b)) |
| 56 | (set-cdr! b a))) |
| 57 | |
| 58 | (let ((next-a (cdr a))) |
| 59 | (if (pair? next-a) |
| 60 | (scan-a a next-a (car next-a) b y) |
| 61 | (set-cdr! a b)))))) |
| 62 | |
| 63 | (scan-b (lambda (prev a x b y) ; Zip down B doing |
| 64 | (if (< y x) ; no SET-CDR!s until |
| 65 | (let ((next-b (cdr b))) ; we hit an A elt that |
| 66 | (if (pair? next-b) ; has to be |
| 67 | (scan-b b a x next-b (car next-b)) ; inserted. |
| 68 | (set-cdr! b a))) |
| 69 | |
| 70 | (let ((next-a (cdr a))) |
| 71 | (set-cdr! prev a) |
| 72 | (if (pair? next-a) |
| 73 | (scan-a a next-a (car next-a) b y) |
| 74 | (set-cdr! a b))))))) |
| 75 | |
| 76 | (cond ((not (pair? a)) b) |
| 77 | ((not (pair? b)) a) |
| 78 | |
| 79 | ;; B starts the answer list. |
| 80 | ((< (car b) (car a)) |
| 81 | (let ((next-b (cdr b))) |
| 82 | (if (null? next-b) |
| 83 | (set-cdr! b a) |
| 84 | (scan-b b a (car a) next-b (car next-b)))) |
| 85 | b) |
| 86 | |
| 87 | ;; A starts the answer list. |
| 88 | (else (let ((next-a (cdr a))) |
| 89 | (if (null? next-a) |
| 90 | (set-cdr! a b) |
| 91 | (scan-a a next-a (car next-a) b (car b)))) |
| 92 | a)))) |
| 93 | |
| 94 | |
| 95 | ;;; Copyright |
| 96 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 97 | ;;; This code is |
| 98 | ;;; Copyright (c) 1998 by Olin Shivers. |
| 99 | ;;; The terms are: You may do as you please with this code, as long as |
| 100 | ;;; you do not delete this notice or hold me responsible for any outcome |
| 101 | ;;; related to its use. |
| 102 | ;;; |
| 103 | ;;; Blah blah blah. |
| 104 | |
| 105 | |
| 106 | ;;; Code tuning & porting |
| 107 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 108 | ;;; This is very portable code. It's R4RS with the following exceptions: |
| 109 | ;;; - The R5RS multiple-value VALUES & CALL-WITH-VALUES procedures for |
| 110 | ;;; handling multiple-value return. |
| 111 | ;;; |
| 112 | ;;; This code is *tightly* bummed as far as I can go in portable Scheme. |
| 113 | ;;; |
| 114 | ;;; - The fixnum arithmetic in LIST-MERGE-SORT! and COUNTED-LIST-MERGE! |
| 115 | ;;; that could be safely switched over to unsafe, fixnum-specific ops, |
| 116 | ;;; if you're sure that 2*maxlen is a fixnum, where maxlen is the length |
| 117 | ;;; of the longest list you could ever have. |
| 118 | ;;; |
| 119 | ;;; - I typically write my code in a style such that every CAR and CDR |
| 120 | ;;; application is protected by an upstream PAIR?. This is the case in this |
| 121 | ;;; code, so all the CAR's and CDR's could safely switched over to unsafe |
| 122 | ;;; versions. But check over the code before you do it, in case the source |
| 123 | ;;; has been altered since I wrote this. |
| 124 | |
| 125 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 126 | ;;; |
| 127 | ;;; End of code extracted from Olin's lmsort.scm file. |
| 128 | ;;; |
| 129 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 130 | |
| 131 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 132 | ;;; |
| 133 | ;;; Start of code extracted from Olin's vmsort.scm file. |
| 134 | ;;; |
| 135 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 136 | |
| 137 | ;;; The sort package -- stable vector merge & merge sort -*- Scheme -*- |
| 138 | ;;; Copyright (c) 1998 by Olin Shivers. |
| 139 | ;;; This code is open-source; see the end of the file for porting and |
| 140 | ;;; more copyright information. |
| 141 | ;;; Olin Shivers 10/98. |
| 142 | |
| 143 | ;;; Exports: |
| 144 | ;;; (vector-merge < v1 v2 [start1 end1 start2 end2]) -> vector |
| 145 | ;;; (vector-merge! < v v1 v2 [start0 start1 end1 start2 end2]) -> unspecific |
| 146 | ;;; |
| 147 | ;;; (vector-merge-sort < v [start end temp]) -> vector |
| 148 | ;;; (vector-merge-sort! < v [start end temp]) -> unspecific |
| 149 | |
| 150 | |
| 151 | ;;; Merge |
| 152 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 153 | ;;; (vector-merge < v1 v2 [start1 end1 start2 end2]) -> vector |
| 154 | ;;; (vector-merge! < v v1 v2 [start start1 end1 start2 end2]) -> unspecific |
| 155 | ;;; |
| 156 | ;;; Stable vector merge -- V1's elements come out ahead of equal V2 elements. |
| 157 | |
| 158 | (define (vector-merge < v1 v2 . maybe-starts+ends) |
| 159 | (call-with-values |
| 160 | (lambda () (vectors-start+end-2 v1 v2 maybe-starts+ends)) |
| 161 | (lambda (start1 end1 start2 end2) |
| 162 | (let ((ans (make-vector (+ (- end1 start1) (- end2 start2))))) |
| 163 | (%vector-merge! < ans v1 v2 0 start1 end1 start2 end2) |
| 164 | ans)))) |
| 165 | |
| 166 | (define (vector-merge! < v v1 v2 . maybe-starts+ends) |
| 167 | (call-with-values |
| 168 | (lambda () |
| 169 | (if (pair? maybe-starts+ends) |
| 170 | (values (car maybe-starts+ends) |
| 171 | (cdr maybe-starts+ends)) |
| 172 | (values 0 |
| 173 | '()))) |
| 174 | (lambda (start rest) |
| 175 | (call-with-values |
| 176 | (lambda () (vectors-start+end-2 v1 v2 rest)) |
| 177 | (lambda (start1 end1 start2 end2) |
| 178 | (%vector-merge! < v v1 v2 start start1 end1 start2 end2)))))) |
| 179 | |
| 180 | |
| 181 | ;;; This routine is not exported. The code is tightly bummed. |
| 182 | ;;; |
| 183 | ;;; If these preconditions hold, the routine can be bummed to run with |
| 184 | ;;; unsafe vector-indexing and fixnum arithmetic ops: |
| 185 | ;;; - V V1 V2 are vectors. |
| 186 | ;;; - START START1 END1 START2 END2 are fixnums. |
| 187 | ;;; - (<= 0 START END0 (vector-length V), |
| 188 | ;;; where end0 = start + (end1 - start1) + (end2 - start2) |
| 189 | ;;; - (<= 0 START1 END1 (vector-length V1)) |
| 190 | ;;; - (<= 0 START2 END2 (vector-length V2)) |
| 191 | ;;; If you put these error checks in the two client procedures above, you can |
| 192 | ;;; safely convert this procedure to use unsafe ops -- which is why it isn't |
| 193 | ;;; exported. This will provide *huge* speedup. |
| 194 | |
| 195 | (define (%vector-merge! elt< v v1 v2 start start1 end1 start2 end2) |
| 196 | (letrec ((vblit (lambda (fromv j i end) ; Blit FROMV[J,END) to V[I,?]. |
| 197 | (let lp ((j j) (i i)) |
| 198 | (vector-set! v i (vector-ref fromv j)) |
| 199 | (let ((j (+ j 1))) |
| 200 | (if (< j end) (lp j (+ i 1)))))))) |
| 201 | |
| 202 | (cond ((<= end1 start1) (if (< start2 end2) (vblit v2 start2 start end2))) |
| 203 | ((<= end2 start2) (vblit v1 start1 start end1)) |
| 204 | |
| 205 | ;; Invariants: I is next index of V to write; X = V1[J]; Y = V2[K]. |
| 206 | (else (let lp ((i start) |
| 207 | (j start1) (x (vector-ref v1 start1)) |
| 208 | (k start2) (y (vector-ref v2 start2))) |
| 209 | (let ((i1 (+ i 1))) ; "i+1" is a complex number in R4RS! |
| 210 | (if (elt< y x) |
| 211 | (let ((k (+ k 1))) |
| 212 | (vector-set! v i y) |
| 213 | (if (< k end2) |
| 214 | (lp i1 j x k (vector-ref v2 k)) |
| 215 | (vblit v1 j i1 end1))) |
| 216 | (let ((j (+ j 1))) |
| 217 | (vector-set! v i x) |
| 218 | (if (< j end1) |
| 219 | (lp i1 j (vector-ref v1 j) k y) |
| 220 | (vblit v2 k i1 end2)))))))))) |
| 221 | |
| 222 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 223 | ;;; |
| 224 | ;;; End of code extracted from Olin's vmsort.scm file. |
| 225 | ;;; |
| 226 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |