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