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