95.upstream.scm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ;;; "sort.scm" Defines: sorted?, merge, merge!, sort, sort!
  2. ;;; Author : Richard A. O'Keefe (based on Prolog code by D.H.D.Warren)
  3. ;;; Copyright (C) Aubrey Jaffer 2006. All Rights Reserved.
  4. ;;; Permission is hereby granted, free of charge, to any person obtaining a copy
  5. ;;; of this software and associated documentation files (the "Software"), to
  6. ;;; deal in the Software without restriction, including without limitation the
  7. ;;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. ;;; sell copies of the Software, and to permit persons to whom the Software is
  9. ;;; furnished to do so, subject to the following conditions:
  10. ;;; The above copyright notice and this permission notice shall be included in
  11. ;;; all copies or substantial portions of the Software.
  12. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. ;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18. ;;; IN THE SOFTWARE.
  19. ;;; Updated: 11 June 1991
  20. ;;; Modified for scheme library: Aubrey Jaffer 19 Sept. 1991
  21. ;;; Updated: 19 June 1995
  22. ;;; (sort, sort!, sorted?): Generalized to strings by jaffer: 2003-09-09
  23. ;;; (sort, sort!, sorted?): Generalized to arrays by jaffer: 2003-10-04
  24. ;;; jaffer: 2006-10-08:
  25. ;;; (sort, sort!, sorted?, merge, merge!): Added optional KEY argument.
  26. ;;; jaffer: 2006-11-05:
  27. ;;; (sorted?, merge, merge!, sort, sort!): Call KEY arg at most once
  28. ;;; per element.
  29. (require 'array)
  30. ;;; (sorted? sequence less?)
  31. ;;; is true when sequence is a list (x0 x1 ... xm) or a vector #(x0 ... xm)
  32. ;;; such that for all 1 <= i <= m,
  33. ;;; (not (less? (list-ref list i) (list-ref list (- i 1)))).
  34. ;@
  35. (define (sorted? seq less? . opt-key)
  36. (define key (if (null? opt-key) identity (car opt-key)))
  37. (cond ((null? seq) #t)
  38. ((array? seq)
  39. (let ((dimax (+ -1 (car (array-dimensions seq)))))
  40. (or (<= dimax 1)
  41. (let loop ((idx (+ -1 dimax))
  42. (last (key (array-ref seq dimax))))
  43. (or (negative? idx)
  44. (let ((nxt (key (array-ref seq idx))))
  45. (and (less? nxt last)
  46. (loop (+ -1 idx) nxt))))))))
  47. ((null? (cdr seq)) #t)
  48. (else
  49. (let loop ((last (key (car seq)))
  50. (next (cdr seq)))
  51. (or (null? next)
  52. (let ((nxt (key (car next))))
  53. (and (not (less? nxt last))
  54. (loop nxt (cdr next)))))))))
  55. ;;; (merge a b less?)
  56. ;;; takes two lists a and b such that (sorted? a less?) and (sorted? b less?)
  57. ;;; and returns a new list in which the elements of a and b have been stably
  58. ;;; interleaved so that (sorted? (merge a b less?) less?).
  59. ;;; Note: this does _not_ accept arrays. See below.
  60. ;@
  61. (define (merge a b less? . opt-key)
  62. (define key (if (null? opt-key) identity (car opt-key)))
  63. (cond ((null? a) b)
  64. ((null? b) a)
  65. (else
  66. (let loop ((x (car a)) (kx (key (car a))) (a (cdr a))
  67. (y (car b)) (ky (key (car b))) (b (cdr b)))
  68. ;; The loop handles the merging of non-empty lists. It has
  69. ;; been written this way to save testing and car/cdring.
  70. (if (less? ky kx)
  71. (if (null? b)
  72. (cons y (cons x a))
  73. (cons y (loop x kx a (car b) (key (car b)) (cdr b))))
  74. ;; x <= y
  75. (if (null? a)
  76. (cons x (cons y b))
  77. (cons x (loop (car a) (key (car a)) (cdr a) y ky b))))))))
  78. (define (sort:merge! a b less? key)
  79. (define (loop r a kcara b kcarb)
  80. (cond ((less? kcarb kcara)
  81. (set-cdr! r b)
  82. (if (null? (cdr b))
  83. (set-cdr! b a)
  84. (loop b a kcara (cdr b) (key (cadr b)))))
  85. (else ; (car a) <= (car b)
  86. (set-cdr! r a)
  87. (if (null? (cdr a))
  88. (set-cdr! a b)
  89. (loop a (cdr a) (key (cadr a)) b kcarb)))))
  90. (cond ((null? a) b)
  91. ((null? b) a)
  92. (else
  93. (let ((kcara (key (car a)))
  94. (kcarb (key (car b))))
  95. (cond
  96. ((less? kcarb kcara)
  97. (if (null? (cdr b))
  98. (set-cdr! b a)
  99. (loop b a kcara (cdr b) (key (cadr b))))
  100. b)
  101. (else ; (car a) <= (car b)
  102. (if (null? (cdr a))
  103. (set-cdr! a b)
  104. (loop a (cdr a) (key (cadr a)) b kcarb))
  105. a))))))
  106. ;;; takes two sorted lists a and b and smashes their cdr fields to form a
  107. ;;; single sorted list including the elements of both.
  108. ;;; Note: this does _not_ accept arrays.
  109. ;@
  110. (define (merge! a b less? . opt-key)
  111. (sort:merge! a b less? (if (null? opt-key) identity (car opt-key))))
  112. (define (sort:sort-list! seq less? key)
  113. (define keyer (if key car identity))
  114. (define (step n)
  115. (cond ((> n 2) (let* ((j (quotient n 2))
  116. (a (step j))
  117. (k (- n j))
  118. (b (step k)))
  119. (sort:merge! a b less? keyer)))
  120. ((= n 2) (let ((x (car seq))
  121. (y (cadr seq))
  122. (p seq))
  123. (set! seq (cddr seq))
  124. (cond ((less? (keyer y) (keyer x))
  125. (set-car! p y)
  126. (set-car! (cdr p) x)))
  127. (set-cdr! (cdr p) '())
  128. p))
  129. ((= n 1) (let ((p seq))
  130. (set! seq (cdr seq))
  131. (set-cdr! p '())
  132. p))
  133. (else '())))
  134. (define (key-wrap! lst)
  135. (cond ((null? lst))
  136. (else (set-car! lst (cons (key (car lst)) (car lst)))
  137. (key-wrap! (cdr lst)))))
  138. (define (key-unwrap! lst)
  139. (cond ((null? lst))
  140. (else (set-car! lst (cdar lst))
  141. (key-unwrap! (cdr lst)))))
  142. (cond (key
  143. (key-wrap! seq)
  144. (set! seq (step (length seq)))
  145. (key-unwrap! seq)
  146. seq)
  147. (else
  148. (step (length seq)))))
  149. (define (rank-1-array->list array)
  150. (define dimensions (array-dimensions array))
  151. (do ((idx (+ -1 (car dimensions)) (+ -1 idx))
  152. (lst '() (cons (array-ref array idx) lst)))
  153. ((< idx 0) lst)))
  154. ;;; (sort! sequence less?)
  155. ;;; sorts the list, array, or string sequence destructively. It uses
  156. ;;; a version of merge-sort invented, to the best of my knowledge, by
  157. ;;; David H. D. Warren, and first used in the DEC-10 Prolog system.
  158. ;;; R. A. O'Keefe adapted it to work destructively in Scheme.
  159. ;;; A. Jaffer modified to always return the original list.
  160. ;@
  161. (define (sort! seq less? . opt-key)
  162. (define key (if (null? opt-key) #f (car opt-key)))
  163. (cond ((array? seq)
  164. (let ((dims (array-dimensions seq)))
  165. (do ((sorted (sort:sort-list! (rank-1-array->list seq) less? key)
  166. (cdr sorted))
  167. (i 0 (+ i 1)))
  168. ((null? sorted) seq)
  169. (array-set! seq (car sorted) i))))
  170. (else ; otherwise, assume it is a list
  171. (let ((ret (sort:sort-list! seq less? key)))
  172. (if (not (eq? ret seq))
  173. (do ((crt ret (cdr crt)))
  174. ((eq? (cdr crt) seq)
  175. (set-cdr! crt ret)
  176. (let ((scar (car seq)) (scdr (cdr seq)))
  177. (set-car! seq (car ret)) (set-cdr! seq (cdr ret))
  178. (set-car! ret scar) (set-cdr! ret scdr)))))
  179. seq))))
  180. ;;; (sort sequence less?)
  181. ;;; sorts a array, string, or list non-destructively. It does this
  182. ;;; by sorting a copy of the sequence. My understanding is that the
  183. ;;; Standard says that the result of append is always "newly
  184. ;;; allocated" except for sharing structure with "the last argument",
  185. ;;; so (append x '()) ought to be a standard way of copying a list x.
  186. ;@
  187. (define (sort seq less? . opt-key)
  188. (define key (if (null? opt-key) #f (car opt-key)))
  189. (cond ((array? seq)
  190. (let ((dims (array-dimensions seq)))
  191. (define newra (apply make-array seq dims))
  192. (do ((sorted (sort:sort-list! (rank-1-array->list seq) less? key)
  193. (cdr sorted))
  194. (i 0 (+ i 1)))
  195. ((null? sorted) newra)
  196. (array-set! newra (car sorted) i))))
  197. (else (sort:sort-list! (append seq '()) less? key))))