*** empty log message ***
[bpt/guile.git] / libguile / sort.c
CommitLineData
726d810a 1/* Copyright (C) 1999,2000,2001 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. */
a0599745 60#include "libguile/scmconfig.h"
54e09076
MD
61
62/* AIX requires this to be the first thing in the file. The #pragma
63 directive is indented so pre-ANSI compilers will ignore it, rather
64 than choke on it. */
65#ifndef __GNUC__
66# if HAVE_ALLOCA_H
67# include <alloca.h>
68# else
69# ifdef _AIX
70 #pragma alloca
71# else
72# ifndef alloca /* predefined by HP cc +Olibcalls */
73char *alloca ();
74# endif
75# endif
76# endif
77#endif
78
783e7774 79#include <string.h>
a0599745 80#include "libguile/_scm.h"
54e09076 81
a0599745
MD
82#include "libguile/eval.h"
83#include "libguile/unif.h"
84#include "libguile/ramap.h"
85#include "libguile/alist.h"
86#include "libguile/feature.h"
fdc28395 87#include "libguile/root.h"
a0599745 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. */
125typedef struct
126 {
127 char *lo;
128 char *hi;
129 }
130stack_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
162typedef int (*cmp_fun_t) (SCM less,
163 const void*,
164 const void*);
165
a34af05e
MD
166static const char s_buggy_less[] = "buggy less predicate used when sorting";
167
54e09076
MD
168static void
169quicksort (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
351static int
352subr2less (SCM less, const void *a, const void *b)
353{
354 return SCM_NFALSEP (SCM_SUBRF (less) (*(SCM *) a, *(SCM *) b));
355} /* subr2less */
356
357static int
358subr2oless (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
365static int
366lsubrless (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
373static int
374closureless (SCM code, const void *a, const void *b)
375{
726d810a 376 SCM env = SCM_EXTEND_ENV (SCM_CLOSURE_FORMALS (code),
4c6fe5fc
MD
377 scm_cons (*(SCM *) a,
378 scm_cons (*(SCM *) b, SCM_EOL)),
379 SCM_ENV (code));
54e09076 380 /* Evaluate the closure body */
f9450cdb 381 return !SCM_FALSEP (scm_eval_body (SCM_CLOSURE_BODY (code), env));
54e09076
MD
382} /* closureless */
383
384static int
385applyless (SCM less, const void *a, const void *b)
386{
fdc28395 387 return SCM_NFALSEP (scm_call_2 (less, *(SCM *) a, *(SCM *) b));
54e09076
MD
388} /* applyless */
389
390static cmp_fun_t
391scm_cmp_function (SCM p)
392{
393 switch (SCM_TYP7 (p))
394 {
395 case scm_tc7_subr_2:
396 case scm_tc7_rpsubr:
397 case scm_tc7_asubr:
398 return subr2less;
399 case scm_tc7_subr_2o:
400 return subr2oless;
401 case scm_tc7_lsubr:
402 return lsubrless;
403 case scm_tcs_closures:
404 return closureless;
405 default:
406 return applyless;
407 }
408} /* scm_cmp_function */
409
54e09076
MD
410
411/* Question: Is there any need to make this a more general array sort?
412 It is probably enough to manage the vector type. */
413/* endpos equal as for substring, i.e. endpos is not included. */
da4a1dba 414/* More natural with length? */
1bbd0b84 415
a1ec6916 416SCM_DEFINE (scm_restricted_vector_sort_x, "restricted-vector-sort!", 4, 0, 0,
1bbd0b84 417 (SCM vec, SCM less, SCM startpos, SCM endpos),
e3239868
DH
418 "Sort the vector @var{vec}, using @var{less} for comparing\n"
419 "the vector elements. @var{startpos} and @var{endpos} delimit\n"
420 "the range of the vector which gets sorted. The return value\n"
421 "is not specified.")
1bbd0b84 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
729dbac3
DH
433 SCM_VALIDATE_INUM_MIN_COPY (3, startpos, 0, spos);
434 SCM_ASSERT_RANGE (3,startpos, 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 448SCM_DEFINE (scm_sorted_p, "sorted?", 2, 0, 0,
1bbd0b84 449 (SCM items, SCM less),
e3239868
DH
450 "Return @code{#t} iff @var{items} is a list or a vector such that\n"
451 "for all 1 <= i <= m, the predicate @var{less} returns true when\n"
452 "applied to all elements i - 1 and i")
1bbd0b84 453#define FUNC_NAME s_scm_sorted_p
54e09076 454{
c014a02e 455 long len, j; /* list/vector length, temp j */
54e09076
MD
456 SCM item, rest; /* rest of items loop variable */
457 SCM *vp;
458 cmp_fun_t cmp = scm_cmp_function (less);
459
460 if (SCM_NULLP (items))
461 return SCM_BOOL_T;
1bbd0b84 462
6b5a304f 463 SCM_VALIDATE_NIM (2,less);
54e09076
MD
464
465 if (SCM_CONSP (items))
466 {
467 len = scm_ilength (items); /* also checks that it's a pure list */
1bbd0b84 468 SCM_ASSERT_RANGE (1,items,len >= 0);
54e09076
MD
469 if (len <= 1)
470 return SCM_BOOL_T;
471
472 item = SCM_CAR (items);
473 rest = SCM_CDR (items);
474 j = len - 1;
475 while (j > 0)
476 {
4b479d98 477 if ((*cmp) (less, SCM_CARLOC(rest), &item))
54e09076
MD
478 return SCM_BOOL_F;
479 else
480 {
481 item = SCM_CAR (rest);
482 rest = SCM_CDR (rest);
483 j--;
484 }
485 }
486 return SCM_BOOL_T;
487 }
488 else
489 {
b5c2579a
DH
490 SCM_VALIDATE_VECTOR (1, items);
491
492 vp = SCM_VELTS (items); /* vector pointer */
493 len = SCM_VECTOR_LENGTH (items);
494 j = len - 1;
495 while (j > 0)
54e09076 496 {
b5c2579a
DH
497 if ((*cmp) (less, &vp[1], vp))
498 return SCM_BOOL_F;
499 else
500 {
501 vp++;
502 j--;
503 }
54e09076 504 }
b5c2579a 505 return SCM_BOOL_T;
54e09076 506 }
b5c2579a 507
54e09076 508 return SCM_BOOL_F;
1bbd0b84
GB
509}
510#undef FUNC_NAME
54e09076
MD
511
512/* (merge a b less?)
513 takes two lists a and b such that (sorted? a less?) and (sorted? b less?)
514 and returns a new list in which the elements of a and b have been stably
515 interleaved so that (sorted? (merge a b less?) less?).
516 Note: this does _not_ accept vectors. */
a1ec6916 517SCM_DEFINE (scm_merge, "merge", 3, 0, 0,
1bbd0b84 518 (SCM alist, SCM blist, SCM less),
8f85c0c6
NJ
519 "Merge two already sorted lists into one.\n"
520 "Given two lists @var{alist} and @var{blist}, such that\n"
521 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)},\n"
522 "return a new list in which the elements of @var{alist} and\n"
e3239868
DH
523 "@var{blist} have been stably interleaved so that\n"
524 "@code{(sorted? (merge alist blist less?) less?)}.\n"
525 "Note: this does _not_ accept vectors.")
1bbd0b84 526#define FUNC_NAME s_scm_merge
54e09076 527{
c014a02e 528 long alen, blen; /* list lengths */
54e09076
MD
529 SCM build, last;
530 cmp_fun_t cmp = scm_cmp_function (less);
6b5a304f 531 SCM_VALIDATE_NIM (3,less);
54e09076
MD
532
533 if (SCM_NULLP (alist))
534 return blist;
535 else if (SCM_NULLP (blist))
536 return alist;
537 else
538 {
3b3b36dd
GB
539 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1,alist,alen);
540 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2,blist,blen);
4b479d98 541 if ((*cmp) (less, SCM_CARLOC (blist), SCM_CARLOC (alist)))
54e09076
MD
542 {
543 build = scm_cons (SCM_CAR (blist), SCM_EOL);
544 blist = SCM_CDR (blist);
545 blen--;
546 }
c56cc3c8
MD
547 else
548 {
549 build = scm_cons (SCM_CAR (alist), SCM_EOL);
550 alist = SCM_CDR (alist);
551 alen--;
552 }
54e09076
MD
553 last = build;
554 while ((alen > 0) && (blen > 0))
555 {
4b479d98 556 if ((*cmp) (less, SCM_CARLOC (blist), SCM_CARLOC (alist)))
54e09076
MD
557 {
558 SCM_SETCDR (last, scm_cons (SCM_CAR (blist), SCM_EOL));
559 blist = SCM_CDR (blist);
560 blen--;
561 }
c56cc3c8
MD
562 else
563 {
564 SCM_SETCDR (last, scm_cons (SCM_CAR (alist), SCM_EOL));
565 alist = SCM_CDR (alist);
566 alen--;
567 }
54e09076
MD
568 last = SCM_CDR (last);
569 }
570 if ((alen > 0) && (blen == 0))
571 SCM_SETCDR (last, alist);
572 else if ((alen == 0) && (blen > 0))
573 SCM_SETCDR (last, blist);
574 }
575 return build;
1bbd0b84
GB
576}
577#undef FUNC_NAME
578
54e09076
MD
579
580static SCM
581scm_merge_list_x (SCM alist, SCM blist,
582 long alen, long blen,
583 cmp_fun_t cmp, SCM less)
584{
585 SCM build, last;
586
587 if (SCM_NULLP (alist))
588 return blist;
589 else if (SCM_NULLP (blist))
590 return alist;
591 else
592 {
4b479d98 593 if ((*cmp) (less, SCM_CARLOC (blist), SCM_CARLOC (alist)))
54e09076
MD
594 {
595 build = blist;
596 blist = SCM_CDR (blist);
597 blen--;
598 }
c56cc3c8
MD
599 else
600 {
601 build = alist;
602 alist = SCM_CDR (alist);
603 alen--;
604 }
54e09076
MD
605 last = build;
606 while ((alen > 0) && (blen > 0))
607 {
4b479d98 608 if ((*cmp) (less, SCM_CARLOC (blist), SCM_CARLOC (alist)))
54e09076
MD
609 {
610 SCM_SETCDR (last, blist);
611 blist = SCM_CDR (blist);
612 blen--;
613 }
c56cc3c8
MD
614 else
615 {
616 SCM_SETCDR (last, alist);
617 alist = SCM_CDR (alist);
618 alen--;
619 }
54e09076
MD
620 last = SCM_CDR (last);
621 }
622 if ((alen > 0) && (blen == 0))
623 SCM_SETCDR (last, alist);
624 else if ((alen == 0) && (blen > 0))
625 SCM_SETCDR (last, blist);
626 }
627 return build;
628} /* scm_merge_list_x */
629
a1ec6916 630SCM_DEFINE (scm_merge_x, "merge!", 3, 0, 0,
1bbd0b84 631 (SCM alist, SCM blist, SCM less),
e3239868
DH
632 "Takes two lists @var{alist} and @var{blist} such that\n"
633 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)} and\n"
634 "returns a new list in which the elements of @var{alist} and\n"
635 "@var{blist} have been stably interleaved so that\n"
636 " @code{(sorted? (merge alist blist less?) less?)}.\n"
637 "This is the destructive variant of @code{merge}\n"
638 "Note: this does _not_ accept vectors.")
1bbd0b84 639#define FUNC_NAME s_scm_merge_x
54e09076 640{
c014a02e 641 long alen, blen; /* list lengths */
54e09076 642
6b5a304f 643 SCM_VALIDATE_NIM (3,less);
54e09076
MD
644 if (SCM_NULLP (alist))
645 return blist;
646 else if (SCM_NULLP (blist))
647 return alist;
648 else
649 {
3b3b36dd
GB
650 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1,alist,alen);
651 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2,blist,blen);
54e09076
MD
652 return scm_merge_list_x (alist, blist,
653 alen, blen,
654 scm_cmp_function (less),
655 less);
656 }
1bbd0b84
GB
657}
658#undef FUNC_NAME
54e09076
MD
659
660/* This merge sort algorithm is same as slib's by Richard A. O'Keefe.
661 The algorithm is stable. We also tried to use the algorithm used by
662 scsh's merge-sort but that algorithm showed to not be stable, even
663 though it claimed to be.
664*/
665static SCM
666scm_merge_list_step (SCM * seq,
667 cmp_fun_t cmp,
668 SCM less,
c014a02e 669 long n)
54e09076 670{
c56cc3c8
MD
671 SCM a, b;
672
54e09076
MD
673 if (n > 2)
674 {
c014a02e 675 long mid = n / 2;
c56cc3c8
MD
676 a = scm_merge_list_step (seq, cmp, less, mid);
677 b = scm_merge_list_step (seq, cmp, less, n - mid);
678 return scm_merge_list_x (a, b, mid, n - mid, cmp, less);
54e09076
MD
679 }
680 else if (n == 2)
681 {
682 SCM p = *seq;
683 SCM rest = SCM_CDR (*seq);
684 SCM x = SCM_CAR (*seq);
685 SCM y = SCM_CAR (SCM_CDR (*seq));
686 *seq = SCM_CDR (rest);
687 SCM_SETCDR (rest, SCM_EOL);
688 if ((*cmp) (less, &y, &x))
689 {
4b479d98
DH
690 SCM_SETCAR (p, y);
691 SCM_SETCAR (rest, x);
54e09076
MD
692 }
693 return p;
694 }
695 else if (n == 1)
696 {
697 SCM p = *seq;
698 *seq = SCM_CDR (p);
699 SCM_SETCDR (p, SCM_EOL);
700 return p;
701 }
702 else
703 return SCM_EOL;
704} /* scm_merge_list_step */
705
706
54e09076 707/* scm_sort_x manages lists and vectors, not stable sort */
a1ec6916 708SCM_DEFINE (scm_sort_x, "sort!", 2, 0, 0,
1bbd0b84 709 (SCM items, SCM less),
e3239868
DH
710 "Sort the sequence @var{items}, which may be a list or a\n"
711 "vector. @var{less} is used for comparing the sequence\n"
712 "elements. The sorting is destructive, that means that the\n"
713 "input sequence is modified to produce the sorted result.\n"
714 "This is not a stable sort.")
1bbd0b84 715#define FUNC_NAME s_scm_sort_x
54e09076 716{
c014a02e 717 long len; /* list/vector length */
54e09076
MD
718 if (SCM_NULLP(items))
719 return SCM_EOL;
b5c2579a 720
6b5a304f 721 SCM_VALIDATE_NIM (2,less);
54e09076
MD
722
723 if (SCM_CONSP (items))
724 {
3b3b36dd 725 SCM_VALIDATE_LIST_COPYLEN (1,items,len);
54e09076
MD
726 return scm_merge_list_step (&items, scm_cmp_function (less), less, len);
727 }
728 else if (SCM_VECTORP (items))
729 {
b5c2579a 730 len = SCM_VECTOR_LENGTH (items);
54e09076
MD
731 scm_restricted_vector_sort_x (items,
732 less,
733 SCM_MAKINUM (0L),
734 SCM_MAKINUM (len));
735 return items;
736 }
737 else
276dd677 738 SCM_WRONG_TYPE_ARG (1, items);
1bbd0b84 739}
0f981281 740#undef FUNC_NAME
54e09076
MD
741
742/* scm_sort manages lists and vectors, not stable sort */
1bbd0b84 743
a1ec6916 744SCM_DEFINE (scm_sort, "sort", 2, 0, 0,
1bbd0b84 745 (SCM items, SCM less),
e3239868
DH
746 "Sort the sequence @var{items}, which may be a list or a\n"
747 "vector. @var{less} is used for comparing the sequence\n"
748 "elements. This is not a stable sort.")
1bbd0b84 749#define FUNC_NAME s_scm_sort
54e09076 750{
54e09076
MD
751 if (SCM_NULLP(items))
752 return SCM_EOL;
b5c2579a 753
6b5a304f 754 SCM_VALIDATE_NIM (2,less);
54e09076
MD
755 if (SCM_CONSP (items))
756 {
c014a02e 757 long len;
e9e225e5 758
3b3b36dd 759 SCM_VALIDATE_LIST_COPYLEN (1,items,len);
54e09076
MD
760 items = scm_list_copy (items);
761 return scm_merge_list_step (&items, scm_cmp_function (less), less, len);
762 }
afe5177e
GH
763#ifdef HAVE_ARRAYS
764 /* support ordinary vectors even if arrays not available? */
54e09076
MD
765 else if (SCM_VECTORP (items))
766 {
c014a02e 767 long len = SCM_VECTOR_LENGTH (items);
e9e225e5
GH
768 SCM sortvec = scm_make_uve (len, scm_array_prototype (items));
769
54e09076
MD
770 scm_array_copy_x (items, sortvec);
771 scm_restricted_vector_sort_x (sortvec,
772 less,
773 SCM_MAKINUM (0L),
774 SCM_MAKINUM (len));
775 return sortvec;
776 }
afe5177e 777#endif
54e09076 778 else
276dd677 779 SCM_WRONG_TYPE_ARG (1, items);
1bbd0b84 780}
0f981281 781#undef FUNC_NAME
54e09076
MD
782
783static void
784scm_merge_vector_x (void *const vecbase,
785 void *const tempbase,
786 cmp_fun_t cmp,
787 SCM less,
c014a02e
ML
788 long low,
789 long mid,
790 long high)
54e09076
MD
791{
792 register SCM *vp = (SCM *) vecbase;
793 register SCM *temp = (SCM *) tempbase;
c014a02e
ML
794 long it; /* Index for temp vector */
795 long i1 = low; /* Index for lower vector segment */
796 long i2 = mid + 1; /* Index for upper vector segment */
54e09076
MD
797
798 /* Copy while both segments contain more characters */
799 for (it = low; (i1 <= mid) && (i2 <= high); ++it)
800 if ((*cmp) (less, &vp[i2], &vp[i1]))
801 temp[it] = vp[i2++];
802 else
803 temp[it] = vp[i1++];
804
805 /* Copy while first segment contains more characters */
806 while (i1 <= mid)
807 temp[it++] = vp[i1++];
808
809 /* Copy while second segment contains more characters */
810 while (i2 <= high)
811 temp[it++] = vp[i2++];
812
813 /* Copy back from temp to vp */
814 for (it = low; it <= high; ++it)
815 vp[it] = temp[it];
816} /* scm_merge_vector_x */
817
818static void
819scm_merge_vector_step (void *const vp,
820 void *const temp,
821 cmp_fun_t cmp,
822 SCM less,
c014a02e
ML
823 long low,
824 long high)
54e09076
MD
825{
826 if (high > low)
827 {
c014a02e 828 long mid = (low + high) / 2;
54e09076
MD
829 scm_merge_vector_step (vp, temp, cmp, less, low, mid);
830 scm_merge_vector_step (vp, temp, cmp, less, mid+1, high);
831 scm_merge_vector_x (vp, temp, cmp, less, low, mid, high);
832 }
833} /* scm_merge_vector_step */
834
835
54e09076
MD
836/* stable-sort! manages lists and vectors */
837
a1ec6916 838SCM_DEFINE (scm_stable_sort_x, "stable-sort!", 2, 0, 0,
1bbd0b84 839 (SCM items, SCM less),
e3239868
DH
840 "Sort the sequence @var{items}, which may be a list or a\n"
841 "vector. @var{less} is used for comparing the sequence elements.\n"
842 "The sorting is destructive, that means that the input sequence\n"
843 "is modified to produce the sorted result.\n"
844 "This is a stable sort.")
1bbd0b84 845#define FUNC_NAME s_scm_stable_sort_x
54e09076 846{
c014a02e 847 long len; /* list/vector length */
54e09076
MD
848
849 if (SCM_NULLP (items))
850 return SCM_EOL;
b5c2579a 851
6b5a304f 852 SCM_VALIDATE_NIM (2,less);
54e09076
MD
853 if (SCM_CONSP (items))
854 {
3b3b36dd 855 SCM_VALIDATE_LIST_COPYLEN (1,items,len);
54e09076
MD
856 return scm_merge_list_step (&items, scm_cmp_function (less), less, len);
857 }
858 else if (SCM_VECTORP (items))
859 {
860 SCM *temp, *vp;
b5c2579a 861 len = SCM_VECTOR_LENGTH (items);
54e09076
MD
862 temp = malloc (len * sizeof(SCM));
863 vp = SCM_VELTS (items);
864 scm_merge_vector_step (vp,
865 temp,
866 scm_cmp_function (less),
867 less,
868 0,
869 len - 1);
870 free(temp);
871 return items;
872 }
873 else
276dd677 874 SCM_WRONG_TYPE_ARG (1, items);
1bbd0b84 875}
0f981281 876#undef FUNC_NAME
54e09076
MD
877
878/* stable_sort manages lists and vectors */
1bbd0b84 879
a1ec6916 880SCM_DEFINE (scm_stable_sort, "stable-sort", 2, 0, 0,
1bbd0b84 881 (SCM items, SCM less),
e3239868
DH
882 "Sort the sequence @var{items}, which may be a list or a\n"
883 "vector. @var{less} is used for comparing the sequence elements.\n"
884 "This is a stable sort.")
1bbd0b84 885#define FUNC_NAME s_scm_stable_sort
54e09076 886{
c014a02e 887 long len; /* list/vector length */
54e09076
MD
888 if (SCM_NULLP (items))
889 return SCM_EOL;
b5c2579a 890
6b5a304f 891 SCM_VALIDATE_NIM (2,less);
54e09076
MD
892 if (SCM_CONSP (items))
893 {
3b3b36dd 894 SCM_VALIDATE_LIST_COPYLEN (1,items,len);
54e09076
MD
895 items = scm_list_copy (items);
896 return scm_merge_list_step (&items, scm_cmp_function (less), less, len);
897 }
afe5177e
GH
898#ifdef HAVE_ARRAYS
899 /* support ordinary vectors even if arrays not available? */
54e09076
MD
900 else if (SCM_VECTORP (items))
901 {
902 SCM retvec;
903 SCM *temp, *vp;
b5c2579a 904 len = SCM_VECTOR_LENGTH (items);
54e09076
MD
905 retvec = scm_make_uve (len, scm_array_prototype (items));
906 scm_array_copy_x (items, retvec);
907 temp = malloc (len * sizeof (SCM));
908 vp = SCM_VELTS (retvec);
909 scm_merge_vector_step (vp,
910 temp,
911 scm_cmp_function (less),
912 less,
913 0,
914 len - 1);
915 free (temp);
916 return retvec;
917 }
afe5177e 918#endif
54e09076 919 else
276dd677 920 SCM_WRONG_TYPE_ARG (1, items);
1bbd0b84 921}
0f981281 922#undef FUNC_NAME
54e09076 923
1bbd0b84 924/* stable */
a1ec6916 925SCM_DEFINE (scm_sort_list_x, "sort-list!", 2, 0, 0,
1bbd0b84 926 (SCM items, SCM less),
e3239868
DH
927 "Sort the list @var{items}, using @var{less} for comparing the\n"
928 "list elements. The sorting is destructive, that means that the\n"
929 "input list is modified to produce the sorted result.\n"
930 "This is a stable sort.")
1bbd0b84 931#define FUNC_NAME s_scm_sort_list_x
54e09076 932{
c014a02e 933 long len;
3b3b36dd 934 SCM_VALIDATE_LIST_COPYLEN (1,items,len);
6b5a304f 935 SCM_VALIDATE_NIM (2,less);
54e09076 936 return scm_merge_list_step (&items, scm_cmp_function (less), less, len);
1bbd0b84 937}
0f981281 938#undef FUNC_NAME
54e09076 939
1bbd0b84 940/* stable */
a1ec6916 941SCM_DEFINE (scm_sort_list, "sort-list", 2, 0, 0,
e3239868
DH
942 (SCM items, SCM less),
943 "Sort the list @var{items}, using @var{less} for comparing the\n"
944 "list elements. This is a stable sort.")
1bbd0b84 945#define FUNC_NAME s_scm_sort_list
54e09076 946{
c014a02e 947 long len;
3b3b36dd 948 SCM_VALIDATE_LIST_COPYLEN (1,items,len);
6b5a304f 949 SCM_VALIDATE_NIM (2,less);
54e09076
MD
950 items = scm_list_copy (items);
951 return scm_merge_list_step (&items, scm_cmp_function (less), less, len);
1bbd0b84 952}
0f981281 953#undef FUNC_NAME
54e09076
MD
954
955void
956scm_init_sort ()
957{
8dc9439f 958#ifndef SCM_MAGIC_SNARFER
a0599745 959#include "libguile/sort.x"
8dc9439f 960#endif
54e09076
MD
961
962 scm_add_feature ("sort");
963}
89e00824
ML
964
965/*
966 Local Variables:
967 c-file-style: "gnu"
968 End:
969*/