1 | ;;; sort.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2005 Peter Graves |
---|
4 | ;;; $Id: sort.lisp 12516 2010-03-03 21:05:41Z astalla $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | |
---|
32 | (in-package #:system) |
---|
33 | |
---|
34 | (require "EXTENSIBLE-SEQUENCES-BASE") |
---|
35 | |
---|
36 | (defun sort (sequence predicate &rest args &key key) |
---|
37 | (sequence::seq-dispatch sequence |
---|
38 | (sort-list sequence predicate key) |
---|
39 | (quick-sort sequence 0 (length sequence) predicate key) |
---|
40 | (apply #'sequence:sort sequence predicate args))) |
---|
41 | |
---|
42 | (defun stable-sort (sequence predicate &rest args &key key) |
---|
43 | (sequence::seq-dispatch sequence |
---|
44 | (sort-list sequence predicate key) |
---|
45 | (quick-sort sequence 0 (length sequence) predicate key) |
---|
46 | (apply #'sequence:stable-sort sequence predicate args))) |
---|
47 | |
---|
48 | ;; Adapted from SBCL. |
---|
49 | (declaim (ftype (function (list) cons) last-cons-of)) |
---|
50 | (defun last-cons-of (list) |
---|
51 | (loop |
---|
52 | (let ((rest (rest list))) |
---|
53 | (if rest |
---|
54 | (setf list rest) |
---|
55 | (return list))))) |
---|
56 | |
---|
57 | ;; Adapted from OpenMCL. |
---|
58 | (defun merge-lists (list1 list2 pred key) |
---|
59 | (declare (optimize (speed 3) (safety 0))) |
---|
60 | (if (null key) |
---|
61 | (merge-lists-no-key list1 list2 pred) |
---|
62 | (cond ((null list1) |
---|
63 | (values list2 (last-cons-of list2))) |
---|
64 | ((null list2) |
---|
65 | (values list1 (last-cons-of list1))) |
---|
66 | (t |
---|
67 | (let* ((result (cons nil nil)) |
---|
68 | (p result) ; p points to last cell of result |
---|
69 | (key1 (funcall key (car list1))) |
---|
70 | (key2 (funcall key (car list2)))) |
---|
71 | (declare (type list p)) |
---|
72 | (loop |
---|
73 | (cond ((funcall pred key2 key1) |
---|
74 | (rplacd p list2) ; append the lesser list to last cell of |
---|
75 | (setf p (cdr p)) ; result. Note: test must bo done for |
---|
76 | (pop list2) ; list2 < list1 so merge will be |
---|
77 | (unless list2 ; stable for list1 |
---|
78 | (rplacd p list1) |
---|
79 | (return (values (cdr result) (last-cons-of p)))) |
---|
80 | (setf key2 (funcall key (car list2)))) |
---|
81 | (t |
---|
82 | (rplacd p list1) |
---|
83 | (setf p (cdr p)) |
---|
84 | (pop list1) |
---|
85 | (unless list1 |
---|
86 | (rplacd p list2) |
---|
87 | (return (values (cdr result) (last-cons-of p)))) |
---|
88 | (setf key1 (funcall key (car list1))))))))))) |
---|
89 | |
---|
90 | (defun merge-lists-no-key (list1 list2 pred) |
---|
91 | (declare (optimize (speed 3) (safety 0))) |
---|
92 | (cond ((null list1) |
---|
93 | (values list2 (last-cons-of list2))) |
---|
94 | ((null list2) |
---|
95 | (values list1 (last-cons-of list1))) |
---|
96 | (t |
---|
97 | (let* ((result (cons nil nil)) |
---|
98 | (p result) ; p points to last cell of result |
---|
99 | (key1 (car list1)) |
---|
100 | (key2 (car list2))) |
---|
101 | (declare (type list p)) |
---|
102 | (loop |
---|
103 | (cond ((funcall pred key2 key1) |
---|
104 | (rplacd p list2) ; append the lesser list to last cell of |
---|
105 | (setf p (cdr p)) ; result. Note: test must bo done for |
---|
106 | (pop list2) ; list2 < list1 so merge will be |
---|
107 | (unless list2 ; stable for list1 |
---|
108 | (rplacd p list1) |
---|
109 | (return (values (cdr result) (last-cons-of p)))) |
---|
110 | (setf key2 (car list2))) |
---|
111 | (t |
---|
112 | (rplacd p list1) |
---|
113 | (setf p (cdr p)) |
---|
114 | (pop list1) |
---|
115 | (unless list1 |
---|
116 | (rplacd p list2) |
---|
117 | (return (values (cdr result) (last-cons-of p)))) |
---|
118 | (setf key1 (car list1))))))))) |
---|
119 | |
---|
120 | ;;; SORT-LIST uses a bottom up merge sort. First a pass is made over |
---|
121 | ;;; the list grabbing one element at a time and merging it with the next one |
---|
122 | ;;; form pairs of sorted elements. Then n is doubled, and elements are taken |
---|
123 | ;;; in runs of two, merging one run with the next to form quadruples of sorted |
---|
124 | ;;; elements. This continues until n is large enough that the inner loop only |
---|
125 | ;;; runs for one iteration; that is, there are only two runs that can be merged, |
---|
126 | ;;; the first run starting at the beginning of the list, and the second being |
---|
127 | ;;; the remaining elements. |
---|
128 | |
---|
129 | (defun sort-list (list pred key) |
---|
130 | (when (or (eq key #'identity) (eq key 'identity)) |
---|
131 | (setf key nil)) |
---|
132 | (let ((head (cons nil list)) ; head holds on to everything |
---|
133 | (n 1) ; bottom-up size of lists to be merged |
---|
134 | unsorted ; unsorted is the remaining list to be |
---|
135 | ; broken into n size lists and merged |
---|
136 | list-1 ; list-1 is one length n list to be merged |
---|
137 | last ; last points to the last visited cell |
---|
138 | ) |
---|
139 | (declare (type fixnum n)) |
---|
140 | (loop |
---|
141 | ;; start collecting runs of n at the first element |
---|
142 | (setf unsorted (cdr head)) |
---|
143 | ;; tack on the first merge of two n-runs to the head holder |
---|
144 | (setf last head) |
---|
145 | (let ((n-1 (1- n))) |
---|
146 | (declare (type fixnum n-1)) |
---|
147 | (loop |
---|
148 | (setf list-1 unsorted) |
---|
149 | (let ((temp (nthcdr n-1 list-1)) |
---|
150 | list-2) |
---|
151 | (cond (temp |
---|
152 | ;; there are enough elements for a second run |
---|
153 | (setf list-2 (cdr temp)) |
---|
154 | (setf (cdr temp) nil) |
---|
155 | (setf temp (nthcdr n-1 list-2)) |
---|
156 | (cond (temp |
---|
157 | (setf unsorted (cdr temp)) |
---|
158 | (setf (cdr temp) nil)) |
---|
159 | ;; the second run goes off the end of the list |
---|
160 | (t (setf unsorted nil))) |
---|
161 | (multiple-value-bind (merged-head merged-last) |
---|
162 | (merge-lists list-1 list-2 pred key) |
---|
163 | (setf (cdr last) merged-head) |
---|
164 | (setf last merged-last)) |
---|
165 | (if (null unsorted) (return))) |
---|
166 | ;; if there is only one run, then tack it on to the end |
---|
167 | (t (setf (cdr last) list-1) |
---|
168 | (return))))) |
---|
169 | (setf n (+ n n)) |
---|
170 | ;; If the inner loop only executed once, then there were only enough |
---|
171 | ;; elements for two runs given n, so all the elements have been merged |
---|
172 | ;; into one list. This may waste one outer iteration to realize. |
---|
173 | (if (eq list-1 (cdr head)) |
---|
174 | (return list-1)))))) |
---|
175 | |
---|
176 | ;;; From ECL. |
---|
177 | (defun quick-sort (seq start end pred key) |
---|
178 | (unless key (setq key #'identity)) |
---|
179 | (if (<= end (1+ start)) |
---|
180 | seq |
---|
181 | (let* ((j start) (k end) (d (elt seq start)) (kd (funcall key d))) |
---|
182 | (block outer-loop |
---|
183 | (loop (loop (decf k) |
---|
184 | (unless (< j k) (return-from outer-loop)) |
---|
185 | (when (funcall pred (funcall key (elt seq k)) kd) |
---|
186 | (return))) |
---|
187 | (loop (incf j) |
---|
188 | (unless (< j k) (return-from outer-loop)) |
---|
189 | (unless (funcall pred (funcall key (elt seq j)) kd) |
---|
190 | (return))) |
---|
191 | (let ((temp (elt seq j))) |
---|
192 | (setf (elt seq j) (elt seq k) |
---|
193 | (elt seq k) temp)))) |
---|
194 | (setf (elt seq start) (elt seq j) |
---|
195 | (elt seq j) d) |
---|
196 | (quick-sort seq start j pred key) |
---|
197 | (quick-sort seq (1+ j) end pred key)))) |
---|
198 | |
---|
199 | ;;; From ECL. Should already be user-extensible as it does no type dispatch |
---|
200 | ;;; and uses only user-extensible functions. |
---|
201 | (defun merge (result-type sequence1 sequence2 predicate |
---|
202 | &key key |
---|
203 | &aux (l1 (length sequence1)) (l2 (length sequence2))) |
---|
204 | (unless key (setq key #'identity)) |
---|
205 | (do ((newseq (make-sequence result-type (+ l1 l2))) |
---|
206 | (j 0 (1+ j)) |
---|
207 | (i1 0) |
---|
208 | (i2 0)) |
---|
209 | ((and (= i1 l1) (= i2 l2)) newseq) |
---|
210 | (cond ((and (< i1 l1) (< i2 l2)) |
---|
211 | (cond ((funcall predicate |
---|
212 | (funcall key (elt sequence1 i1)) |
---|
213 | (funcall key (elt sequence2 i2))) |
---|
214 | (setf (elt newseq j) (elt sequence1 i1)) |
---|
215 | (incf i1)) |
---|
216 | ((funcall predicate |
---|
217 | (funcall key (elt sequence2 i2)) |
---|
218 | (funcall key (elt sequence1 i1))) |
---|
219 | (setf (elt newseq j) (elt sequence2 i2)) |
---|
220 | (incf i2)) |
---|
221 | (t |
---|
222 | (setf (elt newseq j) (elt sequence1 i1)) |
---|
223 | (incf i1)))) |
---|
224 | ((< i1 l1) |
---|
225 | (setf (elt newseq j) (elt sequence1 i1)) |
---|
226 | (incf i1)) |
---|
227 | (t |
---|
228 | (setf (elt newseq j) (elt sequence2 i2)) |
---|
229 | (incf i2))))) |
---|