Commit | Line | Data |
---|---|---|
f2c9fcb0 | 1 | /* Copyright (C) 1999, 2000 Free Software Foundation, Inc. |
54e09076 MD |
2 | * This program is free software; you can redistribute it and/or modify |
3 | * it under the terms of the GNU General Public License as published by | |
4 | * the Free Software Foundation; either version 2, or (at your option) | |
5 | * any later version. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, | |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU General Public License | |
13 | * along with this software; see the file COPYING. If not, write to | |
14 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
15 | * Boston, MA 02111-1307 USA | |
16 | * | |
17 | * As a special exception, the Free Software Foundation gives permission | |
18 | * for additional uses of the text contained in its release of GUILE. | |
19 | * | |
20 | * The exception is that, if you link the GUILE library with other files | |
21 | * to produce an executable, this does not by itself cause the | |
22 | * resulting executable to be covered by the GNU General Public License. | |
23 | * Your use of that executable is in no way restricted on account of | |
24 | * linking the GUILE library code into it. | |
25 | * | |
26 | * This exception does not however invalidate any other reasons why | |
27 | * the executable file might be covered by the GNU General Public License. | |
28 | * | |
29 | * This exception applies only to the code released by the | |
30 | * Free Software Foundation under the name GUILE. If you copy | |
31 | * code from other Free Software Foundation releases into a copy of | |
32 | * GUILE, as the General Public License permits, the exception does | |
33 | * not apply to the code that you add in this way. To avoid misleading | |
34 | * anyone as to the status of such modified files, you must delete | |
35 | * this exception notice from them. | |
36 | * | |
37 | * If you write modifications of your own for GUILE, it is your choice | |
38 | * whether to permit this exception to apply to your modifications. | |
39 | * If you do not wish that, delete this exception notice. */ | |
40 | ||
1bbd0b84 GB |
41 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, |
42 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
43 | ||
44 | ||
54e09076 MD |
45 | /* Written in December 1998 by Roland Orre <orre@nada.kth.se> |
46 | * This implements the same sort interface as slib/sort.scm | |
47 | * for lists and vectors where slib defines: | |
48 | * sorted?, merge, merge!, sort, sort! | |
15d9c4e3 | 49 | * For scsh compatibility sort-list and sort-list! are also defined. |
54e09076 | 50 | * In cases where a stable-sort is required use stable-sort or |
15d9c4e3 | 51 | * stable-sort!. An additional feature is |
54e09076 | 52 | * (restricted-vector-sort! vector less? startpos endpos) |
15d9c4e3 | 53 | * which allows you to sort part of a vector. |
54e09076 MD |
54 | * Thanks to Aubrey Jaffer for the slib/sort.scm library. |
55 | * Thanks to Richard A. O'Keefe (based on Prolog code by D.H.D.Warren) | |
56 | * for the merge sort inspiration. | |
57 | * Thanks to Douglas C. Schmidt (schmidt@ics.uci.edu) for the | |
58 | * quicksort code. | |
59 | */ | |
60 | ||
61 | /* We need this to get the definitions for HAVE_ALLOCA_H, etc. */ | |
a0599745 | 62 | #include "libguile/scmconfig.h" |
54e09076 MD |
63 | |
64 | /* AIX requires this to be the first thing in the file. The #pragma | |
65 | directive is indented so pre-ANSI compilers will ignore it, rather | |
66 | than choke on it. */ | |
67 | #ifndef __GNUC__ | |
68 | # if HAVE_ALLOCA_H | |
69 | # include <alloca.h> | |
70 | # else | |
71 | # ifdef _AIX | |
72 | #pragma alloca | |
73 | # else | |
74 | # ifndef alloca /* predefined by HP cc +Olibcalls */ | |
75 | char *alloca (); | |
76 | # endif | |
77 | # endif | |
78 | # endif | |
79 | #endif | |
80 | ||
a0599745 | 81 | #include "libguile/_scm.h" |
54e09076 | 82 | |
a0599745 MD |
83 | #include "libguile/eval.h" |
84 | #include "libguile/unif.h" | |
85 | #include "libguile/ramap.h" | |
86 | #include "libguile/alist.h" | |
87 | #include "libguile/feature.h" | |
88 | #include "libguile/vectors.h" | |
54e09076 | 89 | |
a0599745 MD |
90 | #include "libguile/validate.h" |
91 | #include "libguile/sort.h" | |
54e09076 MD |
92 | |
93 | /* The routine quicksort was extracted from the GNU C Library qsort.c | |
94 | written by Douglas C. Schmidt (schmidt@ics.uci.edu) | |
95 | and adapted to guile by adding an extra pointer less | |
96 | to quicksort by Roland Orre <orre@nada.kth.se>. | |
97 | ||
98 | The reason to do this instead of using the library function qsort | |
99 | was to avoid dependency of the ANSI-C extensions for local functions | |
100 | and also to avoid obscure pool based solutions. | |
15d9c4e3 MD |
101 | |
102 | This sorting routine is not much more efficient than the stable | |
103 | version but doesn't consume extra memory. | |
54e09076 MD |
104 | */ |
105 | ||
106 | /* Byte-wise swap two items of size SIZE. */ | |
107 | #define SWAP(a, b, size) \ | |
108 | do \ | |
109 | { \ | |
110 | register size_t __size = (size); \ | |
111 | register char *__a = (a), *__b = (b); \ | |
112 | do \ | |
113 | { \ | |
114 | char __tmp = *__a; \ | |
115 | *__a++ = *__b; \ | |
116 | *__b++ = __tmp; \ | |
117 | } while (--__size > 0); \ | |
118 | } while (0) | |
119 | ||
120 | /* Discontinue quicksort algorithm when partition gets below this size. | |
121 | This particular magic number was chosen to work best on a Sun 4/260. */ | |
122 | #define MAX_THRESH 4 | |
123 | ||
124 | /* Stack node declarations used to store unfulfilled partition obligations. */ | |
125 | typedef struct | |
126 | { | |
127 | char *lo; | |
128 | char *hi; | |
129 | } | |
130 | stack_node; | |
131 | ||
132 | /* The next 4 #defines implement a very fast in-line stack abstraction. */ | |
133 | #define STACK_SIZE (8 * sizeof(unsigned long int)) | |
134 | #define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top)) | |
135 | #define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi))) | |
136 | #define STACK_NOT_EMPTY (stack < top) | |
137 | ||
138 | ||
139 | /* Order size using quicksort. This implementation incorporates | |
140 | four optimizations discussed in Sedgewick: | |
141 | ||
142 | 1. Non-recursive, using an explicit stack of pointer that store the | |
143 | next array partition to sort. To save time, this maximum amount | |
144 | of space required to store an array of MAX_INT is allocated on the | |
145 | stack. Assuming a 32-bit integer, this needs only 32 * | |
146 | sizeof(stack_node) == 136 bits. Pretty cheap, actually. | |
147 | ||
148 | 2. Chose the pivot element using a median-of-three decision tree. | |
149 | This reduces the probability of selecting a bad pivot value and | |
150 | eliminates certain extraneous comparisons. | |
151 | ||
152 | 3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving | |
153 | insertion sort to order the MAX_THRESH items within each partition. | |
154 | This is a big win, since insertion sort is faster for small, mostly | |
155 | sorted array segments. | |
156 | ||
157 | 4. The larger of the two sub-partitions is always pushed onto the | |
158 | stack first, with the algorithm then concentrating on the | |
159 | smaller partition. This *guarantees* no more than log (n) | |
160 | stack size is needed (actually O(1) in this case)! */ | |
161 | ||
162 | typedef int (*cmp_fun_t) (SCM less, | |
163 | const void*, | |
164 | const void*); | |
165 | ||
a34af05e MD |
166 | static const char s_buggy_less[] = "buggy less predicate used when sorting"; |
167 | ||
54e09076 MD |
168 | static void |
169 | quicksort (void *const pbase, | |
170 | size_t total_elems, | |
171 | size_t size, | |
172 | cmp_fun_t cmp, | |
173 | SCM less) | |
174 | { | |
175 | register char *base_ptr = (char *) pbase; | |
176 | ||
177 | /* Allocating SIZE bytes for a pivot buffer facilitates a better | |
178 | algorithm below since we can do comparisons directly on the pivot. */ | |
179 | char *pivot_buffer = (char *) alloca (size); | |
180 | const size_t max_thresh = MAX_THRESH * size; | |
181 | ||
182 | if (total_elems == 0) | |
183 | /* Avoid lossage with unsigned arithmetic below. */ | |
184 | return; | |
185 | ||
186 | if (total_elems > MAX_THRESH) | |
187 | { | |
188 | char *lo = base_ptr; | |
189 | char *hi = &lo[size * (total_elems - 1)]; | |
190 | /* Largest size needed for 32-bit int!!! */ | |
191 | stack_node stack[STACK_SIZE]; | |
192 | stack_node *top = stack + 1; | |
193 | ||
194 | while (STACK_NOT_EMPTY) | |
195 | { | |
196 | char *left_ptr; | |
197 | char *right_ptr; | |
198 | ||
199 | char *pivot = pivot_buffer; | |
200 | ||
201 | /* Select median value from among LO, MID, and HI. Rearrange | |
202 | LO and HI so the three values are sorted. This lowers the | |
203 | probability of picking a pathological pivot value and | |
204 | skips a comparison for both the LEFT_PTR and RIGHT_PTR. */ | |
205 | ||
206 | char *mid = lo + size * ((hi - lo) / size >> 1); | |
207 | ||
208 | if ((*cmp) (less, (void *) mid, (void *) lo)) | |
209 | SWAP (mid, lo, size); | |
210 | if ((*cmp) (less, (void *) hi, (void *) mid)) | |
211 | SWAP (mid, hi, size); | |
212 | else | |
213 | goto jump_over; | |
214 | if ((*cmp) (less, (void *) mid, (void *) lo)) | |
215 | SWAP (mid, lo, size); | |
216 | jump_over:; | |
217 | memcpy (pivot, mid, size); | |
218 | pivot = pivot_buffer; | |
219 | ||
220 | left_ptr = lo + size; | |
221 | right_ptr = hi - size; | |
222 | ||
223 | /* Here's the famous ``collapse the walls'' section of quicksort. | |
224 | Gotta like those tight inner loops! They are the main reason | |
225 | that this algorithm runs much faster than others. */ | |
226 | do | |
227 | { | |
228 | while ((*cmp) (less, (void *) left_ptr, (void *) pivot)) | |
a34af05e MD |
229 | { |
230 | left_ptr += size; | |
231 | /* The comparison predicate may be buggy */ | |
232 | if (left_ptr > hi) | |
5d2d2ffc | 233 | scm_misc_error (NULL, s_buggy_less, SCM_EOL); |
a34af05e | 234 | } |
54e09076 MD |
235 | |
236 | while ((*cmp) (less, (void *) pivot, (void *) right_ptr)) | |
a34af05e MD |
237 | { |
238 | right_ptr -= size; | |
239 | /* The comparison predicate may be buggy */ | |
240 | if (right_ptr < lo) | |
5d2d2ffc | 241 | scm_misc_error (NULL, s_buggy_less, SCM_EOL); |
a34af05e | 242 | } |
54e09076 MD |
243 | |
244 | if (left_ptr < right_ptr) | |
245 | { | |
246 | SWAP (left_ptr, right_ptr, size); | |
247 | left_ptr += size; | |
248 | right_ptr -= size; | |
249 | } | |
250 | else if (left_ptr == right_ptr) | |
251 | { | |
252 | left_ptr += size; | |
253 | right_ptr -= size; | |
254 | break; | |
255 | } | |
256 | } | |
257 | while (left_ptr <= right_ptr); | |
258 | ||
259 | /* Set up pointers for next iteration. First determine whether | |
260 | left and right partitions are below the threshold size. If so, | |
261 | ignore one or both. Otherwise, push the larger partition's | |
262 | bounds on the stack and continue sorting the smaller one. */ | |
263 | ||
264 | if ((size_t) (right_ptr - lo) <= max_thresh) | |
265 | { | |
266 | if ((size_t) (hi - left_ptr) <= max_thresh) | |
267 | /* Ignore both small partitions. */ | |
268 | POP (lo, hi); | |
269 | else | |
270 | /* Ignore small left partition. */ | |
271 | lo = left_ptr; | |
272 | } | |
273 | else if ((size_t) (hi - left_ptr) <= max_thresh) | |
274 | /* Ignore small right partition. */ | |
275 | hi = right_ptr; | |
276 | else if ((right_ptr - lo) > (hi - left_ptr)) | |
277 | { | |
278 | /* Push larger left partition indices. */ | |
279 | PUSH (lo, right_ptr); | |
280 | lo = left_ptr; | |
281 | } | |
282 | else | |
283 | { | |
284 | /* Push larger right partition indices. */ | |
285 | PUSH (left_ptr, hi); | |
286 | hi = right_ptr; | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | /* Once the BASE_PTR array is partially sorted by quicksort the rest | |
292 | is completely sorted using insertion sort, since this is efficient | |
293 | for partitions below MAX_THRESH size. BASE_PTR points to the beginning | |
294 | of the array to sort, and END_PTR points at the very last element in | |
295 | the array (*not* one beyond it!). */ | |
296 | ||
297 | { | |
298 | char *const end_ptr = &base_ptr[size * (total_elems - 1)]; | |
299 | char *tmp_ptr = base_ptr; | |
300 | char *thresh = min (end_ptr, base_ptr + max_thresh); | |
301 | register char *run_ptr; | |
302 | ||
303 | /* Find smallest element in first threshold and place it at the | |
304 | array's beginning. This is the smallest array element, | |
305 | and the operation speeds up insertion sort's inner loop. */ | |
306 | ||
307 | for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size) | |
308 | if ((*cmp) (less, (void *) run_ptr, (void *) tmp_ptr)) | |
309 | tmp_ptr = run_ptr; | |
310 | ||
311 | if (tmp_ptr != base_ptr) | |
312 | SWAP (tmp_ptr, base_ptr, size); | |
313 | ||
314 | /* Insertion sort, running from left-hand-side up to right-hand-side. */ | |
315 | ||
316 | run_ptr = base_ptr + size; | |
317 | while ((run_ptr += size) <= end_ptr) | |
318 | { | |
319 | tmp_ptr = run_ptr - size; | |
320 | while ((*cmp) (less, (void *) run_ptr, (void *) tmp_ptr)) | |
a34af05e MD |
321 | { |
322 | tmp_ptr -= size; | |
323 | /* The comparison predicate may be buggy */ | |
324 | if (tmp_ptr < base_ptr) | |
5d2d2ffc | 325 | scm_misc_error (NULL, s_buggy_less, SCM_EOL); |
a34af05e | 326 | } |
54e09076 MD |
327 | |
328 | tmp_ptr += size; | |
329 | if (tmp_ptr != run_ptr) | |
330 | { | |
331 | char *trav; | |
332 | ||
333 | trav = run_ptr + size; | |
334 | while (--trav >= run_ptr) | |
335 | { | |
336 | char c = *trav; | |
337 | char *hi, *lo; | |
338 | ||
339 | for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo) | |
340 | *hi = *lo; | |
341 | *hi = c; | |
342 | } | |
343 | } | |
344 | } | |
345 | } | |
346 | } /* quicksort */ | |
347 | ||
348 | ||
349 | /* comparison routines */ | |
350 | ||
351 | static int | |
352 | subr2less (SCM less, const void *a, const void *b) | |
353 | { | |
354 | return SCM_NFALSEP (SCM_SUBRF (less) (*(SCM *) a, *(SCM *) b)); | |
355 | } /* subr2less */ | |
356 | ||
357 | static int | |
358 | subr2oless (SCM less, const void *a, const void *b) | |
359 | { | |
360 | return SCM_NFALSEP (SCM_SUBRF (less) (*(SCM *) a, | |
361 | *(SCM *) b, | |
362 | SCM_UNDEFINED)); | |
363 | } /* subr2oless */ | |
364 | ||
365 | static int | |
366 | lsubrless (SCM less, const void *a, const void *b) | |
367 | { | |
368 | return SCM_NFALSEP (SCM_SUBRF (less) | |
369 | (scm_cons (*(SCM *) a, | |
370 | scm_cons (*(SCM *) b, SCM_EOL)))); | |
371 | } /* lsubrless */ | |
372 | ||
373 | static int | |
374 | closureless (SCM code, const void *a, const void *b) | |
375 | { | |
4c6fe5fc MD |
376 | SCM env = SCM_EXTEND_ENV (SCM_CAR (SCM_CODE (code)), |
377 | scm_cons (*(SCM *) a, | |
378 | scm_cons (*(SCM *) b, SCM_EOL)), | |
379 | SCM_ENV (code)); | |
54e09076 | 380 | /* Evaluate the closure body */ |
34249025 | 381 | return SCM_NFALSEP (scm_eval_body (SCM_CDR (SCM_CODE (code)), env)); |
54e09076 MD |
382 | } /* closureless */ |
383 | ||
384 | static int | |
385 | applyless (SCM less, const void *a, const void *b) | |
386 | { | |
54778cd3 | 387 | return SCM_NFALSEP (scm_apply (less, |
54e09076 MD |
388 | scm_cons (*(SCM *) a, |
389 | scm_cons (*(SCM *) b, SCM_EOL)), | |
390 | SCM_EOL)); | |
391 | } /* applyless */ | |
392 | ||
393 | static cmp_fun_t | |
394 | scm_cmp_function (SCM p) | |
395 | { | |
396 | switch (SCM_TYP7 (p)) | |
397 | { | |
398 | case scm_tc7_subr_2: | |
399 | case scm_tc7_rpsubr: | |
400 | case scm_tc7_asubr: | |
401 | return subr2less; | |
402 | case scm_tc7_subr_2o: | |
403 | return subr2oless; | |
404 | case scm_tc7_lsubr: | |
405 | return lsubrless; | |
406 | case scm_tcs_closures: | |
407 | return closureless; | |
408 | default: | |
409 | return applyless; | |
410 | } | |
411 | } /* scm_cmp_function */ | |
412 | ||
54e09076 MD |
413 | |
414 | /* Question: Is there any need to make this a more general array sort? | |
415 | It is probably enough to manage the vector type. */ | |
416 | /* endpos equal as for substring, i.e. endpos is not included. */ | |
da4a1dba | 417 | /* More natural with length? */ |
1bbd0b84 | 418 | |
a1ec6916 | 419 | SCM_DEFINE (scm_restricted_vector_sort_x, "restricted-vector-sort!", 4, 0, 0, |
1bbd0b84 GB |
420 | (SCM vec, SCM less, SCM startpos, SCM endpos), |
421 | "") | |
422 | #define FUNC_NAME s_scm_restricted_vector_sort_x | |
54e09076 MD |
423 | { |
424 | size_t vlen, spos, len, size = sizeof (SCM); | |
425 | SCM *vp; | |
426 | ||
b5c2579a | 427 | SCM_VALIDATE_VECTOR (1,vec); |
6b5a304f | 428 | SCM_VALIDATE_NIM (2,less); |
b5c2579a | 429 | |
54e09076 | 430 | vp = SCM_VELTS (vec); /* vector pointer */ |
b5c2579a | 431 | vlen = SCM_VECTOR_LENGTH (vec); |
54e09076 | 432 | |
3b3b36dd | 433 | SCM_VALIDATE_INUM_COPY (3,startpos,spos); |
1bbd0b84 | 434 | SCM_ASSERT_RANGE (3,startpos,(spos >= 0) && (spos <= vlen)); |
3b3b36dd | 435 | SCM_VALIDATE_INUM_RANGE (4,endpos,0,vlen+1); |
54e09076 MD |
436 | len = SCM_INUM (endpos) - spos; |
437 | ||
438 | quicksort (&vp[spos], len, size, scm_cmp_function (less), less); | |
439 | return SCM_UNSPECIFIED; | |
440 | /* return vec; */ | |
1bbd0b84 GB |
441 | } |
442 | #undef FUNC_NAME | |
54e09076 MD |
443 | |
444 | /* (sorted? sequence less?) | |
445 | * is true when sequence is a list (x0 x1 ... xm) or a vector #(x0 ... xm) | |
446 | * such that for all 1 <= i <= m, | |
447 | * (not (less? (list-ref list i) (list-ref list (- i 1)))). */ | |
a1ec6916 | 448 | SCM_DEFINE (scm_sorted_p, "sorted?", 2, 0, 0, |
1bbd0b84 GB |
449 | (SCM items, SCM less), |
450 | "") | |
451 | #define FUNC_NAME s_scm_sorted_p | |
54e09076 MD |
452 | { |
453 | long len, j; /* list/vector length, temp j */ | |
454 | SCM item, rest; /* rest of items loop variable */ | |
455 | SCM *vp; | |
456 | cmp_fun_t cmp = scm_cmp_function (less); | |
457 | ||
458 | if (SCM_NULLP (items)) | |
459 | return SCM_BOOL_T; | |
1bbd0b84 | 460 | |
6b5a304f | 461 | SCM_VALIDATE_NIM (2,less); |
54e09076 MD |
462 | |
463 | if (SCM_CONSP (items)) | |
464 | { | |
465 | len = scm_ilength (items); /* also checks that it's a pure list */ | |
1bbd0b84 | 466 | SCM_ASSERT_RANGE (1,items,len >= 0); |
54e09076 MD |
467 | if (len <= 1) |
468 | return SCM_BOOL_T; | |
469 | ||
470 | item = SCM_CAR (items); | |
471 | rest = SCM_CDR (items); | |
472 | j = len - 1; | |
473 | while (j > 0) | |
474 | { | |
4b479d98 | 475 | if ((*cmp) (less, SCM_CARLOC(rest), &item)) |
54e09076 MD |
476 | return SCM_BOOL_F; |
477 | else | |
478 | { | |
479 | item = SCM_CAR (rest); | |
480 | rest = SCM_CDR (rest); | |
481 | j--; | |
482 | } | |
483 | } | |
484 | return SCM_BOOL_T; | |
485 | } | |
486 | else | |
487 | { | |
b5c2579a DH |
488 | SCM_VALIDATE_VECTOR (1, items); |
489 | ||
490 | vp = SCM_VELTS (items); /* vector pointer */ | |
491 | len = SCM_VECTOR_LENGTH (items); | |
492 | j = len - 1; | |
493 | while (j > 0) | |
54e09076 | 494 | { |
b5c2579a DH |
495 | if ((*cmp) (less, &vp[1], vp)) |
496 | return SCM_BOOL_F; | |
497 | else | |
498 | { | |
499 | vp++; | |
500 | j--; | |
501 | } | |
54e09076 | 502 | } |
b5c2579a | 503 | return SCM_BOOL_T; |
54e09076 | 504 | } |
b5c2579a | 505 | |
54e09076 | 506 | return SCM_BOOL_F; |
1bbd0b84 GB |
507 | } |
508 | #undef FUNC_NAME | |
54e09076 MD |
509 | |
510 | /* (merge a b less?) | |
511 | takes two lists a and b such that (sorted? a less?) and (sorted? b less?) | |
512 | and returns a new list in which the elements of a and b have been stably | |
513 | interleaved so that (sorted? (merge a b less?) less?). | |
514 | Note: this does _not_ accept vectors. */ | |
a1ec6916 | 515 | SCM_DEFINE (scm_merge, "merge", 3, 0, 0, |
1bbd0b84 GB |
516 | (SCM alist, SCM blist, SCM less), |
517 | "") | |
518 | #define FUNC_NAME s_scm_merge | |
54e09076 MD |
519 | { |
520 | long alen, blen; /* list lengths */ | |
521 | SCM build, last; | |
522 | cmp_fun_t cmp = scm_cmp_function (less); | |
6b5a304f | 523 | SCM_VALIDATE_NIM (3,less); |
54e09076 MD |
524 | |
525 | if (SCM_NULLP (alist)) | |
526 | return blist; | |
527 | else if (SCM_NULLP (blist)) | |
528 | return alist; | |
529 | else | |
530 | { | |
3b3b36dd GB |
531 | SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1,alist,alen); |
532 | SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2,blist,blen); | |
4b479d98 | 533 | if ((*cmp) (less, SCM_CARLOC (blist), SCM_CARLOC (alist))) |
54e09076 MD |
534 | { |
535 | build = scm_cons (SCM_CAR (blist), SCM_EOL); | |
536 | blist = SCM_CDR (blist); | |
537 | blen--; | |
538 | } | |
c56cc3c8 MD |
539 | else |
540 | { | |
541 | build = scm_cons (SCM_CAR (alist), SCM_EOL); | |
542 | alist = SCM_CDR (alist); | |
543 | alen--; | |
544 | } | |
54e09076 MD |
545 | last = build; |
546 | while ((alen > 0) && (blen > 0)) | |
547 | { | |
4b479d98 | 548 | if ((*cmp) (less, SCM_CARLOC (blist), SCM_CARLOC (alist))) |
54e09076 MD |
549 | { |
550 | SCM_SETCDR (last, scm_cons (SCM_CAR (blist), SCM_EOL)); | |
551 | blist = SCM_CDR (blist); | |
552 | blen--; | |
553 | } | |
c56cc3c8 MD |
554 | else |
555 | { | |
556 | SCM_SETCDR (last, scm_cons (SCM_CAR (alist), SCM_EOL)); | |
557 | alist = SCM_CDR (alist); | |
558 | alen--; | |
559 | } | |
54e09076 MD |
560 | last = SCM_CDR (last); |
561 | } | |
562 | if ((alen > 0) && (blen == 0)) | |
563 | SCM_SETCDR (last, alist); | |
564 | else if ((alen == 0) && (blen > 0)) | |
565 | SCM_SETCDR (last, blist); | |
566 | } | |
567 | return build; | |
1bbd0b84 GB |
568 | } |
569 | #undef FUNC_NAME | |
570 | ||
54e09076 MD |
571 | |
572 | static SCM | |
573 | scm_merge_list_x (SCM alist, SCM blist, | |
574 | long alen, long blen, | |
575 | cmp_fun_t cmp, SCM less) | |
576 | { | |
577 | SCM build, last; | |
578 | ||
579 | if (SCM_NULLP (alist)) | |
580 | return blist; | |
581 | else if (SCM_NULLP (blist)) | |
582 | return alist; | |
583 | else | |
584 | { | |
4b479d98 | 585 | if ((*cmp) (less, SCM_CARLOC (blist), SCM_CARLOC (alist))) |
54e09076 MD |
586 | { |
587 | build = blist; | |
588 | blist = SCM_CDR (blist); | |
589 | blen--; | |
590 | } | |
c56cc3c8 MD |
591 | else |
592 | { | |
593 | build = alist; | |
594 | alist = SCM_CDR (alist); | |
595 | alen--; | |
596 | } | |
54e09076 MD |
597 | last = build; |
598 | while ((alen > 0) && (blen > 0)) | |
599 | { | |
4b479d98 | 600 | if ((*cmp) (less, SCM_CARLOC (blist), SCM_CARLOC (alist))) |
54e09076 MD |
601 | { |
602 | SCM_SETCDR (last, blist); | |
603 | blist = SCM_CDR (blist); | |
604 | blen--; | |
605 | } | |
c56cc3c8 MD |
606 | else |
607 | { | |
608 | SCM_SETCDR (last, alist); | |
609 | alist = SCM_CDR (alist); | |
610 | alen--; | |
611 | } | |
54e09076 MD |
612 | last = SCM_CDR (last); |
613 | } | |
614 | if ((alen > 0) && (blen == 0)) | |
615 | SCM_SETCDR (last, alist); | |
616 | else if ((alen == 0) && (blen > 0)) | |
617 | SCM_SETCDR (last, blist); | |
618 | } | |
619 | return build; | |
620 | } /* scm_merge_list_x */ | |
621 | ||
a1ec6916 | 622 | SCM_DEFINE (scm_merge_x, "merge!", 3, 0, 0, |
1bbd0b84 GB |
623 | (SCM alist, SCM blist, SCM less), |
624 | "") | |
625 | #define FUNC_NAME s_scm_merge_x | |
54e09076 MD |
626 | { |
627 | long alen, blen; /* list lengths */ | |
628 | ||
6b5a304f | 629 | SCM_VALIDATE_NIM (3,less); |
54e09076 MD |
630 | if (SCM_NULLP (alist)) |
631 | return blist; | |
632 | else if (SCM_NULLP (blist)) | |
633 | return alist; | |
634 | else | |
635 | { | |
3b3b36dd GB |
636 | SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1,alist,alen); |
637 | SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2,blist,blen); | |
54e09076 MD |
638 | return scm_merge_list_x (alist, blist, |
639 | alen, blen, | |
640 | scm_cmp_function (less), | |
641 | less); | |
642 | } | |
1bbd0b84 GB |
643 | } |
644 | #undef FUNC_NAME | |
54e09076 MD |
645 | |
646 | /* This merge sort algorithm is same as slib's by Richard A. O'Keefe. | |
647 | The algorithm is stable. We also tried to use the algorithm used by | |
648 | scsh's merge-sort but that algorithm showed to not be stable, even | |
649 | though it claimed to be. | |
650 | */ | |
651 | static SCM | |
652 | scm_merge_list_step (SCM * seq, | |
653 | cmp_fun_t cmp, | |
654 | SCM less, | |
655 | int n) | |
656 | { | |
c56cc3c8 MD |
657 | SCM a, b; |
658 | ||
54e09076 MD |
659 | if (n > 2) |
660 | { | |
661 | long mid = n / 2; | |
c56cc3c8 MD |
662 | a = scm_merge_list_step (seq, cmp, less, mid); |
663 | b = scm_merge_list_step (seq, cmp, less, n - mid); | |
664 | return scm_merge_list_x (a, b, mid, n - mid, cmp, less); | |
54e09076 MD |
665 | } |
666 | else if (n == 2) | |
667 | { | |
668 | SCM p = *seq; | |
669 | SCM rest = SCM_CDR (*seq); | |
670 | SCM x = SCM_CAR (*seq); | |
671 | SCM y = SCM_CAR (SCM_CDR (*seq)); | |
672 | *seq = SCM_CDR (rest); | |
673 | SCM_SETCDR (rest, SCM_EOL); | |
674 | if ((*cmp) (less, &y, &x)) | |
675 | { | |
4b479d98 DH |
676 | SCM_SETCAR (p, y); |
677 | SCM_SETCAR (rest, x); | |
54e09076 MD |
678 | } |
679 | return p; | |
680 | } | |
681 | else if (n == 1) | |
682 | { | |
683 | SCM p = *seq; | |
684 | *seq = SCM_CDR (p); | |
685 | SCM_SETCDR (p, SCM_EOL); | |
686 | return p; | |
687 | } | |
688 | else | |
689 | return SCM_EOL; | |
690 | } /* scm_merge_list_step */ | |
691 | ||
692 | ||
54e09076 | 693 | /* scm_sort_x manages lists and vectors, not stable sort */ |
a1ec6916 | 694 | SCM_DEFINE (scm_sort_x, "sort!", 2, 0, 0, |
1bbd0b84 GB |
695 | (SCM items, SCM less), |
696 | "") | |
697 | #define FUNC_NAME s_scm_sort_x | |
54e09076 MD |
698 | { |
699 | long len; /* list/vector length */ | |
700 | if (SCM_NULLP(items)) | |
701 | return SCM_EOL; | |
b5c2579a | 702 | |
6b5a304f | 703 | SCM_VALIDATE_NIM (2,less); |
54e09076 MD |
704 | |
705 | if (SCM_CONSP (items)) | |
706 | { | |
3b3b36dd | 707 | SCM_VALIDATE_LIST_COPYLEN (1,items,len); |
54e09076 MD |
708 | return scm_merge_list_step (&items, scm_cmp_function (less), less, len); |
709 | } | |
710 | else if (SCM_VECTORP (items)) | |
711 | { | |
b5c2579a | 712 | len = SCM_VECTOR_LENGTH (items); |
54e09076 MD |
713 | scm_restricted_vector_sort_x (items, |
714 | less, | |
715 | SCM_MAKINUM (0L), | |
716 | SCM_MAKINUM (len)); | |
717 | return items; | |
718 | } | |
719 | else | |
1bbd0b84 GB |
720 | RETURN_SCM_WTA (1,items); |
721 | } | |
0f981281 | 722 | #undef FUNC_NAME |
54e09076 MD |
723 | |
724 | /* scm_sort manages lists and vectors, not stable sort */ | |
1bbd0b84 | 725 | |
a1ec6916 | 726 | SCM_DEFINE (scm_sort, "sort", 2, 0, 0, |
1bbd0b84 GB |
727 | (SCM items, SCM less), |
728 | "") | |
729 | #define FUNC_NAME s_scm_sort | |
54e09076 MD |
730 | { |
731 | SCM sortvec; /* the vector we actually sort */ | |
732 | long len; /* list/vector length */ | |
733 | if (SCM_NULLP(items)) | |
734 | return SCM_EOL; | |
b5c2579a | 735 | |
6b5a304f | 736 | SCM_VALIDATE_NIM (2,less); |
54e09076 MD |
737 | if (SCM_CONSP (items)) |
738 | { | |
3b3b36dd | 739 | SCM_VALIDATE_LIST_COPYLEN (1,items,len); |
54e09076 MD |
740 | items = scm_list_copy (items); |
741 | return scm_merge_list_step (&items, scm_cmp_function (less), less, len); | |
742 | } | |
afe5177e GH |
743 | #ifdef HAVE_ARRAYS |
744 | /* support ordinary vectors even if arrays not available? */ | |
54e09076 MD |
745 | else if (SCM_VECTORP (items)) |
746 | { | |
b5c2579a | 747 | len = SCM_VECTOR_LENGTH (items); |
54e09076 MD |
748 | sortvec = scm_make_uve (len, scm_array_prototype (items)); |
749 | scm_array_copy_x (items, sortvec); | |
750 | scm_restricted_vector_sort_x (sortvec, | |
751 | less, | |
752 | SCM_MAKINUM (0L), | |
753 | SCM_MAKINUM (len)); | |
754 | return sortvec; | |
755 | } | |
afe5177e | 756 | #endif |
54e09076 | 757 | else |
1bbd0b84 GB |
758 | RETURN_SCM_WTA (1,items); |
759 | } | |
0f981281 | 760 | #undef FUNC_NAME |
54e09076 MD |
761 | |
762 | static void | |
763 | scm_merge_vector_x (void *const vecbase, | |
764 | void *const tempbase, | |
765 | cmp_fun_t cmp, | |
766 | SCM less, | |
767 | long low, | |
768 | long mid, | |
769 | long high) | |
770 | { | |
771 | register SCM *vp = (SCM *) vecbase; | |
772 | register SCM *temp = (SCM *) tempbase; | |
773 | long it; /* Index for temp vector */ | |
774 | long i1 = low; /* Index for lower vector segment */ | |
775 | long i2 = mid + 1; /* Index for upper vector segment */ | |
776 | ||
777 | /* Copy while both segments contain more characters */ | |
778 | for (it = low; (i1 <= mid) && (i2 <= high); ++it) | |
779 | if ((*cmp) (less, &vp[i2], &vp[i1])) | |
780 | temp[it] = vp[i2++]; | |
781 | else | |
782 | temp[it] = vp[i1++]; | |
783 | ||
784 | /* Copy while first segment contains more characters */ | |
785 | while (i1 <= mid) | |
786 | temp[it++] = vp[i1++]; | |
787 | ||
788 | /* Copy while second segment contains more characters */ | |
789 | while (i2 <= high) | |
790 | temp[it++] = vp[i2++]; | |
791 | ||
792 | /* Copy back from temp to vp */ | |
793 | for (it = low; it <= high; ++it) | |
794 | vp[it] = temp[it]; | |
795 | } /* scm_merge_vector_x */ | |
796 | ||
797 | static void | |
798 | scm_merge_vector_step (void *const vp, | |
799 | void *const temp, | |
800 | cmp_fun_t cmp, | |
801 | SCM less, | |
802 | long low, | |
803 | long high) | |
804 | { | |
805 | if (high > low) | |
806 | { | |
807 | long mid = (low + high) / 2; | |
808 | scm_merge_vector_step (vp, temp, cmp, less, low, mid); | |
809 | scm_merge_vector_step (vp, temp, cmp, less, mid+1, high); | |
810 | scm_merge_vector_x (vp, temp, cmp, less, low, mid, high); | |
811 | } | |
812 | } /* scm_merge_vector_step */ | |
813 | ||
814 | ||
54e09076 MD |
815 | /* stable-sort! manages lists and vectors */ |
816 | ||
a1ec6916 | 817 | SCM_DEFINE (scm_stable_sort_x, "stable-sort!", 2, 0, 0, |
1bbd0b84 GB |
818 | (SCM items, SCM less), |
819 | "") | |
820 | #define FUNC_NAME s_scm_stable_sort_x | |
54e09076 MD |
821 | { |
822 | long len; /* list/vector length */ | |
823 | ||
824 | if (SCM_NULLP (items)) | |
825 | return SCM_EOL; | |
b5c2579a | 826 | |
6b5a304f | 827 | SCM_VALIDATE_NIM (2,less); |
54e09076 MD |
828 | if (SCM_CONSP (items)) |
829 | { | |
3b3b36dd | 830 | SCM_VALIDATE_LIST_COPYLEN (1,items,len); |
54e09076 MD |
831 | return scm_merge_list_step (&items, scm_cmp_function (less), less, len); |
832 | } | |
833 | else if (SCM_VECTORP (items)) | |
834 | { | |
835 | SCM *temp, *vp; | |
b5c2579a | 836 | len = SCM_VECTOR_LENGTH (items); |
54e09076 MD |
837 | temp = malloc (len * sizeof(SCM)); |
838 | vp = SCM_VELTS (items); | |
839 | scm_merge_vector_step (vp, | |
840 | temp, | |
841 | scm_cmp_function (less), | |
842 | less, | |
843 | 0, | |
844 | len - 1); | |
845 | free(temp); | |
846 | return items; | |
847 | } | |
848 | else | |
1bbd0b84 GB |
849 | RETURN_SCM_WTA (1,items); |
850 | } | |
0f981281 | 851 | #undef FUNC_NAME |
54e09076 MD |
852 | |
853 | /* stable_sort manages lists and vectors */ | |
1bbd0b84 | 854 | |
a1ec6916 | 855 | SCM_DEFINE (scm_stable_sort, "stable-sort", 2, 0, 0, |
1bbd0b84 GB |
856 | (SCM items, SCM less), |
857 | "") | |
858 | #define FUNC_NAME s_scm_stable_sort | |
54e09076 MD |
859 | { |
860 | long len; /* list/vector length */ | |
861 | if (SCM_NULLP (items)) | |
862 | return SCM_EOL; | |
b5c2579a | 863 | |
6b5a304f | 864 | SCM_VALIDATE_NIM (2,less); |
54e09076 MD |
865 | if (SCM_CONSP (items)) |
866 | { | |
3b3b36dd | 867 | SCM_VALIDATE_LIST_COPYLEN (1,items,len); |
54e09076 MD |
868 | items = scm_list_copy (items); |
869 | return scm_merge_list_step (&items, scm_cmp_function (less), less, len); | |
870 | } | |
afe5177e GH |
871 | #ifdef HAVE_ARRAYS |
872 | /* support ordinary vectors even if arrays not available? */ | |
54e09076 MD |
873 | else if (SCM_VECTORP (items)) |
874 | { | |
875 | SCM retvec; | |
876 | SCM *temp, *vp; | |
b5c2579a | 877 | len = SCM_VECTOR_LENGTH (items); |
54e09076 MD |
878 | retvec = scm_make_uve (len, scm_array_prototype (items)); |
879 | scm_array_copy_x (items, retvec); | |
880 | temp = malloc (len * sizeof (SCM)); | |
881 | vp = SCM_VELTS (retvec); | |
882 | scm_merge_vector_step (vp, | |
883 | temp, | |
884 | scm_cmp_function (less), | |
885 | less, | |
886 | 0, | |
887 | len - 1); | |
888 | free (temp); | |
889 | return retvec; | |
890 | } | |
afe5177e | 891 | #endif |
54e09076 | 892 | else |
1bbd0b84 GB |
893 | RETURN_SCM_WTA (1,items); |
894 | } | |
0f981281 | 895 | #undef FUNC_NAME |
54e09076 | 896 | |
1bbd0b84 | 897 | /* stable */ |
a1ec6916 | 898 | SCM_DEFINE (scm_sort_list_x, "sort-list!", 2, 0, 0, |
1bbd0b84 GB |
899 | (SCM items, SCM less), |
900 | "") | |
901 | #define FUNC_NAME s_scm_sort_list_x | |
54e09076 | 902 | { |
1bbd0b84 | 903 | long len; |
3b3b36dd | 904 | SCM_VALIDATE_LIST_COPYLEN (1,items,len); |
6b5a304f | 905 | SCM_VALIDATE_NIM (2,less); |
54e09076 | 906 | return scm_merge_list_step (&items, scm_cmp_function (less), less, len); |
1bbd0b84 | 907 | } |
0f981281 | 908 | #undef FUNC_NAME |
54e09076 | 909 | |
1bbd0b84 | 910 | /* stable */ |
a1ec6916 | 911 | SCM_DEFINE (scm_sort_list, "sort-list", 2, 0, 0, |
1bbd0b84 GB |
912 | (SCM items, SCM less), |
913 | "") | |
914 | #define FUNC_NAME s_scm_sort_list | |
54e09076 | 915 | { |
1bbd0b84 | 916 | long len; |
3b3b36dd | 917 | SCM_VALIDATE_LIST_COPYLEN (1,items,len); |
6b5a304f | 918 | SCM_VALIDATE_NIM (2,less); |
54e09076 MD |
919 | items = scm_list_copy (items); |
920 | return scm_merge_list_step (&items, scm_cmp_function (less), less, len); | |
1bbd0b84 | 921 | } |
0f981281 | 922 | #undef FUNC_NAME |
54e09076 MD |
923 | |
924 | void | |
925 | scm_init_sort () | |
926 | { | |
8dc9439f | 927 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 928 | #include "libguile/sort.x" |
8dc9439f | 929 | #endif |
54e09076 MD |
930 | |
931 | scm_add_feature ("sort"); | |
932 | } | |
89e00824 ML |
933 | |
934 | /* | |
935 | Local Variables: | |
936 | c-file-style: "gnu" | |
937 | End: | |
938 | */ |