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