| 1 | ;;; The sort package -- delete neighboring duplicate elts |
| 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 11/98. |
| 6 | |
| 7 | ;;; Problem: |
| 8 | ;;; vector-delete-neighbor-dups pushes N stack frames, where N is the number |
| 9 | ;;; of elements in the answer vector. This is arguably a very efficient thing |
| 10 | ;;; to do, but it might blow out on a system with a limited stack but a big |
| 11 | ;;; heap. We could rewrite this to "chunk" up answers in temp vectors if we |
| 12 | ;;; push more than a certain number of frames, then allocate a final answer, |
| 13 | ;;; copying all the chunks into the answer. But it's much more complex code. |
| 14 | |
| 15 | ;;; Exports: |
| 16 | ;;; (list-delete-neighbor-dups = lis) -> list |
| 17 | ;;; (list-delete-neighbor-dups! = lis) -> list |
| 18 | ;;; (vector-delete-neighbor-dups = v [start end]) -> vector |
| 19 | ;;; (vector-delete-neighbor-dups! = v [start end]) -> end' |
| 20 | |
| 21 | ;;; These procedures delete adjacent duplicate elements from a list or |
| 22 | ;;; a vector, using a given element equality procedure. The first or leftmost |
| 23 | ;;; element of a run of equal elements is the one that survives. The list |
| 24 | ;;; or vector is not otherwise disordered. |
| 25 | ;;; |
| 26 | ;;; These procedures are linear time -- much faster than the O(n^2) general |
| 27 | ;;; duplicate-elt deletors that do not assume any "bunching" of elements. |
| 28 | ;;; If you want to delete duplicate elements from a large list or vector, |
| 29 | ;;; sort the elements to bring equal items together, then use one of these |
| 30 | ;;; procedures -- for a total time of O(n lg n). |
| 31 | |
| 32 | ;;; LIST-DELETE-NEIGHBOR-DUPS |
| 33 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 34 | ;;; Below are multiple versions of the LIST-DELETE-NEIGHBOR-DUPS procedure, |
| 35 | ;;; from simple to complex. RECUR's contract: Strip off any leading X's from |
| 36 | ;;; LIS, and return that list neighbor-dup-deleted. |
| 37 | ;;; |
| 38 | ;;; The final version |
| 39 | ;;; - shares a common subtail between the input & output list, up to 1024 |
| 40 | ;;; elements; |
| 41 | ;;; - Needs no more than 1024 stack frames. |
| 42 | |
| 43 | #; |
| 44 | ;;; Simplest version. |
| 45 | ;;; - Always allocates a fresh list / never shares storage. |
| 46 | ;;; - Needs N stack frames, if answer is length N. |
| 47 | (define (list-delete-neighbor-dups = lis) |
| 48 | (if (pair? lis) |
| 49 | (let ((x0 (car lis))) |
| 50 | (cons x0 (let recur ((x0 x0) (xs (cdr lis))) |
| 51 | (if (pair? xs) |
| 52 | (let ((x1 (car xs)) |
| 53 | (x2+ (cdr xs))) |
| 54 | (if (= x0 x1) |
| 55 | (recur x0 x2+) ; Loop, actually. |
| 56 | (cons x1 (recur x1 x2+)))) |
| 57 | xs)))) |
| 58 | lis)) |
| 59 | |
| 60 | ;;; This version tries to use cons cells from input by sharing longest |
| 61 | ;;; common tail between input & output. Still needs N stack frames, for ans |
| 62 | ;;; of length N. |
| 63 | (define (list-delete-neighbor-dups = lis) |
| 64 | (if (pair? lis) |
| 65 | (let* ((x0 (car lis)) |
| 66 | (xs (cdr lis)) |
| 67 | (ans (let recur ((x0 x0) (xs xs)) |
| 68 | (if (pair? xs) |
| 69 | (let ((x1 (car xs)) |
| 70 | (x2+ (cdr xs))) |
| 71 | (if (= x0 x1) |
| 72 | (recur x0 x2+) |
| 73 | (let ((ans-tail (recur x1 x2+))) |
| 74 | (if (eq? ans-tail x2+) xs |
| 75 | (cons x1 ans-tail))))) |
| 76 | xs)))) |
| 77 | (if (eq? ans xs) lis (cons x0 ans))) |
| 78 | |
| 79 | lis)) |
| 80 | |
| 81 | ;;; LIST-DELETE-NEIGHBOR-DUPS! |
| 82 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 83 | ;;; This code runs in constant list space, constant stack, and also |
| 84 | ;;; does only the minimum SET-CDR!'s necessary. |
| 85 | |
| 86 | (define (list-delete-neighbor-dups! = lis) |
| 87 | (if (pair? lis) |
| 88 | (let lp1 ((prev lis) (prev-elt (car lis)) (lis (cdr lis))) |
| 89 | (if (pair? lis) |
| 90 | (let ((lis-elt (car lis)) |
| 91 | (next (cdr lis))) |
| 92 | (if (= prev-elt lis-elt) |
| 93 | |
| 94 | ;; We found the first elts of a run of dups, so we know |
| 95 | ;; we're going to have to do a SET-CDR!. Scan to the end of |
| 96 | ;; the run, do the SET-CDR!, and loop on LP1. |
| 97 | (let lp2 ((lis next)) |
| 98 | (if (pair? lis) |
| 99 | (let ((lis-elt (car lis)) |
| 100 | (next (cdr lis))) |
| 101 | (if (= prev-elt lis-elt) |
| 102 | (lp2 next) |
| 103 | (begin (set-cdr! prev lis) |
| 104 | (lp1 lis lis-elt next)))) |
| 105 | (set-cdr! prev lis))) ; Ran off end => quit. |
| 106 | |
| 107 | (lp1 lis lis-elt next)))))) |
| 108 | lis) |
| 109 | |
| 110 | |
| 111 | (define (vector-delete-neighbor-dups elt= v . maybe-start+end) |
| 112 | (call-with-values |
| 113 | (lambda () (vector-start+end v maybe-start+end)) |
| 114 | (lambda (start end) |
| 115 | (if (< start end) |
| 116 | (let* ((x (vector-ref v start)) |
| 117 | (ans (let recur ((x x) (i start) (j 1)) |
| 118 | (if (< i end) |
| 119 | (let ((y (vector-ref v i)) |
| 120 | (nexti (+ i 1))) |
| 121 | (if (elt= x y) |
| 122 | (recur x nexti j) |
| 123 | (let ((ansvec (recur y nexti (+ j 1)))) |
| 124 | (vector-set! ansvec j y) |
| 125 | ansvec))) |
| 126 | (make-vector j))))) |
| 127 | (vector-set! ans 0 x) |
| 128 | ans) |
| 129 | '#())))) |
| 130 | |
| 131 | |
| 132 | ;;; Packs the surviving elements to the left, in range [start,end'), |
| 133 | ;;; and returns END'. |
| 134 | (define (vector-delete-neighbor-dups! elt= v . maybe-start+end) |
| 135 | (call-with-values |
| 136 | (lambda () (vector-start+end v maybe-start+end)) |
| 137 | (lambda (start end) |
| 138 | |
| 139 | (if (>= start end) |
| 140 | end |
| 141 | ;; To eliminate unnecessary copying (read elt i then write the value |
| 142 | ;; back at index i), we scan until we find the first dup. |
| 143 | (let skip ((j start) (vj (vector-ref v start))) |
| 144 | (let ((j+1 (+ j 1))) |
| 145 | (if (>= j+1 end) |
| 146 | end |
| 147 | (let ((vj+1 (vector-ref v j+1))) |
| 148 | (if (not (elt= vj vj+1)) |
| 149 | (skip j+1 vj+1) |
| 150 | |
| 151 | ;; OK -- j & j+1 are dups, so we're committed to moving |
| 152 | ;; data around. In lp2, v[start,j] is what we've done; |
| 153 | ;; v[k,end) is what we have yet to handle. |
| 154 | (let lp2 ((j j) (vj vj) (k (+ j 2))) |
| 155 | (let lp3 ((k k)) |
| 156 | (if (>= k end) |
| 157 | (+ j 1) ; Done. |
| 158 | (let ((vk (vector-ref v k)) |
| 159 | (k+1 (+ k 1))) |
| 160 | (if (elt= vj vk) |
| 161 | (lp3 k+1) |
| 162 | (let ((j+1 (+ j 1))) |
| 163 | (vector-set! v j+1 vk) |
| 164 | (lp2 j+1 vk k+1)))))))))))))))) |
| 165 | |
| 166 | ;;; Copyright |
| 167 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 168 | ;;; This code is |
| 169 | ;;; Copyright (c) 1998 by Olin Shivers. |
| 170 | ;;; The terms are: You may do as you please with this code, as long as |
| 171 | ;;; you do not delete this notice or hold me responsible for any outcome |
| 172 | ;;; related to its use. |
| 173 | ;;; |
| 174 | ;;; Blah blah blah. Don't you think source files should contain more lines |
| 175 | ;;; of code than copyright notice? |
| 176 | ;;; |
| 177 | ;;; Code porting |
| 178 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 179 | ;;; |
| 180 | ;;; If your Scheme has a faster mechanism for handling optional arguments |
| 181 | ;;; (e.g., Chez), you should definitely port over to it. Note that argument |
| 182 | ;;; defaulting and error-checking are interleaved -- you don't have to |
| 183 | ;;; error-check defaulted START/END args to see if they are fixnums that are |
| 184 | ;;; legal vector indices for the corresponding vector, etc. |
| 185 | |
| 186 |