(quicksort, scm_merge, scm_merge_list_x,
[bpt/guile.git] / libguile / sort.c
1 /* Copyright (C) 1999,2000,2001,2002 Free Software Foundation, Inc.
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library 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 GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17
18
19 /* Written in December 1998 by Roland Orre <orre@nada.kth.se>
20 * This implements the same sort interface as slib/sort.scm
21 * for lists and vectors where slib defines:
22 * sorted?, merge, merge!, sort, sort!
23 * For scsh compatibility sort-list and sort-list! are also defined.
24 * In cases where a stable-sort is required use stable-sort or
25 * stable-sort!. An additional feature is
26 * (restricted-vector-sort! vector less? startpos endpos)
27 * which allows you to sort part of a vector.
28 * Thanks to Aubrey Jaffer for the slib/sort.scm library.
29 * Thanks to Richard A. O'Keefe (based on Prolog code by D.H.D.Warren)
30 * for the merge sort inspiration.
31 * Thanks to Douglas C. Schmidt (schmidt@ics.uci.edu) for the
32 * quicksort code.
33 */
34
35 #include "libguile/_scm.h"
36 #include "libguile/eval.h"
37 #include "libguile/unif.h"
38 #include "libguile/ramap.h"
39 #include "libguile/feature.h"
40 #include "libguile/vectors.h"
41 #include "libguile/lang.h"
42 #include "libguile/async.h"
43
44 #include "libguile/validate.h"
45 #include "libguile/sort.h"
46
47 /* The routine quicksort was extracted from the GNU C Library qsort.c
48 written by Douglas C. Schmidt (schmidt@ics.uci.edu)
49 and adapted to guile by adding an extra pointer less
50 to quicksort by Roland Orre <orre@nada.kth.se>.
51
52 The reason to do this instead of using the library function qsort
53 was to avoid dependency of the ANSI-C extensions for local functions
54 and also to avoid obscure pool based solutions.
55
56 This sorting routine is not much more efficient than the stable
57 version but doesn't consume extra memory.
58 */
59
60 #define SWAP(a, b) do { const SCM _tmp = a; a = b; b = _tmp; } while (0)
61
62
63 /* Order size using quicksort. This implementation incorporates
64 four optimizations discussed in Sedgewick:
65
66 1. Non-recursive, using an explicit stack of pointer that store the next
67 array partition to sort. To save time, this maximum amount of space
68 required to store an array of MAX_SIZE_T is allocated on the stack.
69 Assuming a bit width of 32 bits for size_t, this needs only
70 32 * sizeof (stack_node) == 128 bytes. Pretty cheap, actually.
71
72 2. Chose the pivot element using a median-of-three decision tree. This
73 reduces the probability of selecting a bad pivot value and eliminates
74 certain extraneous comparisons.
75
76 3. Only quicksorts NR_ELEMS / MAX_THRESH partitions, leaving insertion sort
77 to order the MAX_THRESH items within each partition. This is a big win,
78 since insertion sort is faster for small, mostly sorted array segments.
79
80 4. The larger of the two sub-partitions is always pushed onto the
81 stack first, with the algorithm then concentrating on the
82 smaller partition. This *guarantees* no more than log (n)
83 stack size is needed (actually O(1) in this case)! */
84
85
86 /* Discontinue quicksort algorithm when partition gets below this size.
87 * This particular magic number was chosen to work best on a Sun 4/260. */
88 #define MAX_THRESH 4
89
90
91 /* Inline stack abstraction: The stack size for quicksorting at most as many
92 * elements as can be given by a value of type size_t is, as described above,
93 * log (MAX_SIZE_T), which is the number of bits of size_t. More accurately,
94 * we would only need ceil (log (MAX_SIZE_T / MAX_THRESH)), but this is
95 * ignored below. */
96
97 /* Stack node declarations used to store unfulfilled partition obligations. */
98 typedef struct {
99 size_t lo;
100 size_t hi;
101 } stack_node;
102
103 #define STACK_SIZE (8 * sizeof (size_t)) /* assume 8 bit char */
104 #define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
105 #define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi)))
106 #define STACK_NOT_EMPTY (stack < top)
107
108
109 static void
110 quicksort (SCM *const base_ptr, size_t nr_elems, scm_t_trampoline_2 cmp, SCM less)
111 {
112 static const char s_buggy_less[] = "buggy less predicate used when sorting";
113
114 if (nr_elems == 0)
115 /* Avoid lossage with unsigned arithmetic below. */
116 return;
117
118 if (nr_elems > MAX_THRESH)
119 {
120 size_t lo = 0;
121 size_t hi = nr_elems - 1;
122
123 stack_node stack[STACK_SIZE];
124 stack_node *top = stack + 1;
125
126 while (STACK_NOT_EMPTY)
127 {
128 size_t left;
129 size_t right;
130 SCM pivot;
131
132 SCM_TICK;
133
134 /* Select median value from among LO, MID, and HI. Rearrange
135 LO and HI so the three values are sorted. This lowers the
136 probability of picking a pathological pivot value and
137 skips a comparison for both the left and right. */
138
139 size_t mid = lo + (hi - lo) / 2;
140
141 if (scm_is_true ((*cmp) (less, base_ptr[mid], base_ptr[lo])))
142 SWAP (base_ptr[mid], base_ptr[lo]);
143 if (scm_is_true ((*cmp) (less, base_ptr[hi], base_ptr[mid])))
144 SWAP (base_ptr[mid], base_ptr[hi]);
145 else
146 goto jump_over;
147 if (scm_is_true ((*cmp) (less, base_ptr[mid], base_ptr[lo])))
148 SWAP (base_ptr[mid], base_ptr[lo]);
149 jump_over:;
150
151 pivot = base_ptr[mid];
152 left = lo + 1;
153 right = hi - 1;
154
155 /* Here's the famous ``collapse the walls'' section of quicksort.
156 Gotta like those tight inner loops! They are the main reason
157 that this algorithm runs much faster than others. */
158 do
159 {
160 while (scm_is_true ((*cmp) (less, base_ptr[left], pivot)))
161 {
162 left++;
163 /* The comparison predicate may be buggy */
164 if (left > hi)
165 scm_misc_error (NULL, s_buggy_less, SCM_EOL);
166 }
167
168 while (scm_is_true ((*cmp) (less, pivot, base_ptr[right])))
169 {
170 right--;
171 /* The comparison predicate may be buggy */
172 if (right < lo)
173 scm_misc_error (NULL, s_buggy_less, SCM_EOL);
174 }
175
176 if (left < right)
177 {
178 SWAP (base_ptr[left], base_ptr[right]);
179 left++;
180 right--;
181 }
182 else if (left == right)
183 {
184 left++;
185 right--;
186 break;
187 }
188 }
189 while (left <= right);
190
191 /* Set up pointers for next iteration. First determine whether
192 left and right partitions are below the threshold size. If so,
193 ignore one or both. Otherwise, push the larger partition's
194 bounds on the stack and continue sorting the smaller one. */
195
196 if ((size_t) (right - lo) <= MAX_THRESH)
197 {
198 if ((size_t) (hi - left) <= MAX_THRESH)
199 /* Ignore both small partitions. */
200 POP (lo, hi);
201 else
202 /* Ignore small left partition. */
203 lo = left;
204 }
205 else if ((size_t) (hi - left) <= MAX_THRESH)
206 /* Ignore small right partition. */
207 hi = right;
208 else if ((right - lo) > (hi - left))
209 {
210 /* Push larger left partition indices. */
211 PUSH (lo, right);
212 lo = left;
213 }
214 else
215 {
216 /* Push larger right partition indices. */
217 PUSH (left, hi);
218 hi = right;
219 }
220 }
221 }
222
223 /* Once the BASE_PTR array is partially sorted by quicksort the rest is
224 completely sorted using insertion sort, since this is efficient for
225 partitions below MAX_THRESH size. BASE_PTR points to the beginning of the
226 array to sort, and END idexes the very last element in the array (*not*
227 one beyond it!). */
228
229 {
230 size_t tmp = 0;
231 size_t end = nr_elems - 1;
232 size_t thresh = min (end, MAX_THRESH);
233 size_t run;
234
235 /* Find smallest element in first threshold and place it at the
236 array's beginning. This is the smallest array element,
237 and the operation speeds up insertion sort's inner loop. */
238
239 for (run = tmp + 1; run <= thresh; run++)
240 if (scm_is_true ((*cmp) (less, base_ptr[run], base_ptr[tmp])))
241 tmp = run;
242
243 if (tmp != 0)
244 SWAP (base_ptr[tmp], base_ptr[0]);
245
246 /* Insertion sort, running from left-hand-side up to right-hand-side. */
247
248 run = 1;
249 while (++run <= end)
250 {
251 SCM_TICK;
252
253 tmp = run - 1;
254 while (scm_is_true ((*cmp) (less, base_ptr[run], base_ptr[tmp])))
255 {
256 /* The comparison predicate may be buggy */
257 if (tmp == 0)
258 scm_misc_error (NULL, s_buggy_less, SCM_EOL);
259
260 tmp--;
261 }
262
263 tmp++;
264 if (tmp != run)
265 {
266 SCM to_insert = base_ptr[run];
267 size_t hi, lo;
268
269 for (hi = lo = run; --lo >= tmp; hi = lo)
270 base_ptr[hi] = base_ptr[lo];
271 base_ptr[hi] = to_insert;
272 }
273 }
274 }
275 }
276
277
278 static scm_t_trampoline_2
279 compare_function (SCM less, unsigned int arg_nr, const char* fname)
280 {
281 const scm_t_trampoline_2 cmp = scm_trampoline_2 (less);
282 SCM_ASSERT_TYPE (cmp != NULL, less, arg_nr, fname, "less predicate");
283 return cmp;
284 }
285
286
287 /* Question: Is there any need to make this a more general array sort?
288 It is probably enough to manage the vector type. */
289 /* endpos equal as for substring, i.e. endpos is not included. */
290 /* More natural with length? */
291
292 SCM_DEFINE (scm_restricted_vector_sort_x, "restricted-vector-sort!", 4, 0, 0,
293 (SCM vec, SCM less, SCM startpos, SCM endpos),
294 "Sort the vector @var{vec}, using @var{less} for comparing\n"
295 "the vector elements. @var{startpos} (inclusively) and\n"
296 "@var{endpos} (exclusively) delimit\n"
297 "the range of the vector which gets sorted. The return value\n"
298 "is not specified.")
299 #define FUNC_NAME s_scm_restricted_vector_sort_x
300 {
301 const scm_t_trampoline_2 cmp = compare_function (less, 2, FUNC_NAME);
302 size_t vlen, spos, len;
303 SCM *vp;
304
305 SCM_VALIDATE_VECTOR (1, vec);
306 vp = SCM_WRITABLE_VELTS (vec); /* vector pointer */
307 vlen = SCM_VECTOR_LENGTH (vec);
308
309 spos = scm_to_unsigned_integer (startpos, 0, vlen);
310 len = scm_to_unsigned_integer (endpos, spos, vlen) - spos;
311
312 quicksort (&vp[spos], len, cmp, less);
313 scm_remember_upto_here_1 (vec);
314
315 return SCM_UNSPECIFIED;
316 }
317 #undef FUNC_NAME
318
319
320 /* (sorted? sequence less?)
321 * is true when sequence is a list (x0 x1 ... xm) or a vector #(x0 ... xm)
322 * such that for all 1 <= i <= m,
323 * (not (less? (list-ref list i) (list-ref list (- i 1)))). */
324 SCM_DEFINE (scm_sorted_p, "sorted?", 2, 0, 0,
325 (SCM items, SCM less),
326 "Return @code{#t} iff @var{items} is a list or a vector such that\n"
327 "for all 1 <= i <= m, the predicate @var{less} returns true when\n"
328 "applied to all elements i - 1 and i")
329 #define FUNC_NAME s_scm_sorted_p
330 {
331 const scm_t_trampoline_2 cmp = compare_function (less, 2, FUNC_NAME);
332 long len, j; /* list/vector length, temp j */
333 SCM item, rest; /* rest of items loop variable */
334 SCM const *vp;
335
336 if (SCM_NULL_OR_NIL_P (items))
337 return SCM_BOOL_T;
338
339 if (scm_is_pair (items))
340 {
341 len = scm_ilength (items); /* also checks that it's a pure list */
342 SCM_ASSERT_RANGE (1, items, len >= 0);
343 if (len <= 1)
344 return SCM_BOOL_T;
345
346 item = SCM_CAR (items);
347 rest = SCM_CDR (items);
348 j = len - 1;
349 while (j > 0)
350 {
351 if (scm_is_true ((*cmp) (less, SCM_CAR (rest), item)))
352 return SCM_BOOL_F;
353 else
354 {
355 item = SCM_CAR (rest);
356 rest = SCM_CDR (rest);
357 j--;
358 }
359 }
360 return SCM_BOOL_T;
361 }
362 else
363 {
364 SCM_VALIDATE_VECTOR (1, items);
365
366 vp = SCM_VELTS (items); /* vector pointer */
367 len = SCM_VECTOR_LENGTH (items);
368 j = len - 1;
369 while (j > 0)
370 {
371 if (scm_is_true ((*cmp) (less, vp[1], vp[0])))
372 return SCM_BOOL_F;
373 else
374 {
375 vp++;
376 j--;
377 }
378 }
379 return SCM_BOOL_T;
380 }
381
382 return SCM_BOOL_F;
383 }
384 #undef FUNC_NAME
385
386
387 /* (merge a b less?)
388 takes two lists a and b such that (sorted? a less?) and (sorted? b less?)
389 and returns a new list in which the elements of a and b have been stably
390 interleaved so that (sorted? (merge a b less?) less?).
391 Note: this does _not_ accept vectors. */
392 SCM_DEFINE (scm_merge, "merge", 3, 0, 0,
393 (SCM alist, SCM blist, SCM less),
394 "Merge two already sorted lists into one.\n"
395 "Given two lists @var{alist} and @var{blist}, such that\n"
396 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)},\n"
397 "return a new list in which the elements of @var{alist} and\n"
398 "@var{blist} have been stably interleaved so that\n"
399 "@code{(sorted? (merge alist blist less?) less?)}.\n"
400 "Note: this does _not_ accept vectors.")
401 #define FUNC_NAME s_scm_merge
402 {
403 SCM build;
404
405 if (SCM_NULL_OR_NIL_P (alist))
406 return blist;
407 else if (SCM_NULL_OR_NIL_P (blist))
408 return alist;
409 else
410 {
411 const scm_t_trampoline_2 cmp = compare_function (less, 3, FUNC_NAME);
412 long alen, blen; /* list lengths */
413 SCM last;
414
415 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist, alen);
416 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist, blen);
417 if (scm_is_true ((*cmp) (less, SCM_CAR (blist), SCM_CAR (alist))))
418 {
419 build = scm_cons (SCM_CAR (blist), SCM_EOL);
420 blist = SCM_CDR (blist);
421 blen--;
422 }
423 else
424 {
425 build = scm_cons (SCM_CAR (alist), SCM_EOL);
426 alist = SCM_CDR (alist);
427 alen--;
428 }
429 last = build;
430 while ((alen > 0) && (blen > 0))
431 {
432 SCM_TICK;
433 if (scm_is_true ((*cmp) (less, SCM_CAR (blist), SCM_CAR (alist))))
434 {
435 SCM_SETCDR (last, scm_cons (SCM_CAR (blist), SCM_EOL));
436 blist = SCM_CDR (blist);
437 blen--;
438 }
439 else
440 {
441 SCM_SETCDR (last, scm_cons (SCM_CAR (alist), SCM_EOL));
442 alist = SCM_CDR (alist);
443 alen--;
444 }
445 last = SCM_CDR (last);
446 }
447 if ((alen > 0) && (blen == 0))
448 SCM_SETCDR (last, alist);
449 else if ((alen == 0) && (blen > 0))
450 SCM_SETCDR (last, blist);
451 }
452 return build;
453 }
454 #undef FUNC_NAME
455
456
457 static SCM
458 scm_merge_list_x (SCM alist, SCM blist,
459 long alen, long blen,
460 scm_t_trampoline_2 cmp, SCM less)
461 {
462 SCM build, last;
463
464 if (SCM_NULL_OR_NIL_P (alist))
465 return blist;
466 else if (SCM_NULL_OR_NIL_P (blist))
467 return alist;
468 else
469 {
470 if (scm_is_true ((*cmp) (less, SCM_CAR (blist), SCM_CAR (alist))))
471 {
472 build = blist;
473 blist = SCM_CDR (blist);
474 blen--;
475 }
476 else
477 {
478 build = alist;
479 alist = SCM_CDR (alist);
480 alen--;
481 }
482 last = build;
483 while ((alen > 0) && (blen > 0))
484 {
485 SCM_TICK;
486 if (scm_is_true ((*cmp) (less, SCM_CAR (blist), SCM_CAR (alist))))
487 {
488 SCM_SETCDR (last, blist);
489 blist = SCM_CDR (blist);
490 blen--;
491 }
492 else
493 {
494 SCM_SETCDR (last, alist);
495 alist = SCM_CDR (alist);
496 alen--;
497 }
498 last = SCM_CDR (last);
499 }
500 if ((alen > 0) && (blen == 0))
501 SCM_SETCDR (last, alist);
502 else if ((alen == 0) && (blen > 0))
503 SCM_SETCDR (last, blist);
504 }
505 return build;
506 } /* scm_merge_list_x */
507
508
509 SCM_DEFINE (scm_merge_x, "merge!", 3, 0, 0,
510 (SCM alist, SCM blist, SCM less),
511 "Takes two lists @var{alist} and @var{blist} such that\n"
512 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)} and\n"
513 "returns a new list in which the elements of @var{alist} and\n"
514 "@var{blist} have been stably interleaved so that\n"
515 " @code{(sorted? (merge alist blist less?) less?)}.\n"
516 "This is the destructive variant of @code{merge}\n"
517 "Note: this does _not_ accept vectors.")
518 #define FUNC_NAME s_scm_merge_x
519 {
520 if (SCM_NULL_OR_NIL_P (alist))
521 return blist;
522 else if (SCM_NULL_OR_NIL_P (blist))
523 return alist;
524 else
525 {
526 const scm_t_trampoline_2 cmp = compare_function (less, 3, FUNC_NAME);
527 long alen, blen; /* list lengths */
528 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist, alen);
529 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist, blen);
530 return scm_merge_list_x (alist, blist, alen, blen, cmp, less);
531 }
532 }
533 #undef FUNC_NAME
534
535
536 /* This merge sort algorithm is same as slib's by Richard A. O'Keefe.
537 The algorithm is stable. We also tried to use the algorithm used by
538 scsh's merge-sort but that algorithm showed to not be stable, even
539 though it claimed to be.
540 */
541 static SCM
542 scm_merge_list_step (SCM * seq, scm_t_trampoline_2 cmp, SCM less, long n)
543 {
544 SCM a, b;
545
546 if (n > 2)
547 {
548 long mid = n / 2;
549 SCM_TICK;
550 a = scm_merge_list_step (seq, cmp, less, mid);
551 b = scm_merge_list_step (seq, cmp, less, n - mid);
552 return scm_merge_list_x (a, b, mid, n - mid, cmp, less);
553 }
554 else if (n == 2)
555 {
556 SCM p = *seq;
557 SCM rest = SCM_CDR (*seq);
558 SCM x = SCM_CAR (*seq);
559 SCM y = SCM_CAR (SCM_CDR (*seq));
560 *seq = SCM_CDR (rest);
561 SCM_SETCDR (rest, SCM_EOL);
562 if (scm_is_true ((*cmp) (less, y, x)))
563 {
564 SCM_SETCAR (p, y);
565 SCM_SETCAR (rest, x);
566 }
567 return p;
568 }
569 else if (n == 1)
570 {
571 SCM p = *seq;
572 *seq = SCM_CDR (p);
573 SCM_SETCDR (p, SCM_EOL);
574 return p;
575 }
576 else
577 return SCM_EOL;
578 } /* scm_merge_list_step */
579
580
581 SCM_DEFINE (scm_sort_x, "sort!", 2, 0, 0,
582 (SCM items, SCM less),
583 "Sort the sequence @var{items}, which may be a list or a\n"
584 "vector. @var{less} is used for comparing the sequence\n"
585 "elements. The sorting is destructive, that means that the\n"
586 "input sequence is modified to produce the sorted result.\n"
587 "This is not a stable sort.")
588 #define FUNC_NAME s_scm_sort_x
589 {
590 long len; /* list/vector length */
591 if (SCM_NULL_OR_NIL_P (items))
592 return items;
593
594 if (scm_is_pair (items))
595 {
596 const scm_t_trampoline_2 cmp = compare_function (less, 2, FUNC_NAME);
597 SCM_VALIDATE_LIST_COPYLEN (1, items, len);
598 return scm_merge_list_step (&items, cmp, less, len);
599 }
600 else if (SCM_VECTORP (items))
601 {
602 len = SCM_VECTOR_LENGTH (items);
603 scm_restricted_vector_sort_x (items,
604 less,
605 scm_from_int (0),
606 scm_from_long (len));
607 return items;
608 }
609 else
610 SCM_WRONG_TYPE_ARG (1, items);
611 }
612 #undef FUNC_NAME
613
614
615 SCM_DEFINE (scm_sort, "sort", 2, 0, 0,
616 (SCM items, SCM less),
617 "Sort the sequence @var{items}, which may be a list or a\n"
618 "vector. @var{less} is used for comparing the sequence\n"
619 "elements. This is not a stable sort.")
620 #define FUNC_NAME s_scm_sort
621 {
622 if (SCM_NULL_OR_NIL_P (items))
623 return items;
624
625 if (scm_is_pair (items))
626 {
627 const scm_t_trampoline_2 cmp = compare_function (less, 2, FUNC_NAME);
628 long len;
629
630 SCM_VALIDATE_LIST_COPYLEN (1, items, len);
631 items = scm_list_copy (items);
632 return scm_merge_list_step (&items, cmp, less, len);
633 }
634 #if SCM_HAVE_ARRAYS
635 /* support ordinary vectors even if arrays not available? */
636 else if (SCM_VECTORP (items))
637 {
638 long len = SCM_VECTOR_LENGTH (items);
639 SCM sortvec = scm_make_uve (len, scm_array_prototype (items));
640
641 scm_array_copy_x (items, sortvec);
642 scm_restricted_vector_sort_x (sortvec,
643 less,
644 scm_from_int (0),
645 scm_from_long (len));
646 return sortvec;
647 }
648 #endif
649 else
650 SCM_WRONG_TYPE_ARG (1, items);
651 }
652 #undef FUNC_NAME
653
654
655 static void
656 scm_merge_vector_x (SCM vec,
657 SCM * temp,
658 scm_t_trampoline_2 cmp,
659 SCM less,
660 long low,
661 long mid,
662 long high)
663 {
664 long it; /* Index for temp vector */
665 long i1 = low; /* Index for lower vector segment */
666 long i2 = mid + 1; /* Index for upper vector segment */
667
668 /* Copy while both segments contain more characters */
669 for (it = low; (i1 <= mid) && (i2 <= high); ++it)
670 {
671 /*
672 Every call of LESS might invoke GC. For full correctness, we
673 should reset the generation of vecbase and tempbase between
674 every call of less.
675
676 */
677 register SCM *vp = SCM_WRITABLE_VELTS(vec);
678
679 if (scm_is_true ((*cmp) (less, vp[i2], vp[i1])))
680 temp[it] = vp[i2++];
681 else
682 temp[it] = vp[i1++];
683 }
684
685 {
686 register SCM *vp = SCM_WRITABLE_VELTS(vec);
687
688 /* Copy while first segment contains more characters */
689 while (i1 <= mid)
690 temp[it++] = vp[i1++];
691
692 /* Copy while second segment contains more characters */
693 while (i2 <= high)
694 temp[it++] = vp[i2++];
695
696 /* Copy back from temp to vp */
697 for (it = low; it <= high; ++it)
698 vp[it] = temp[it];
699 }
700 } /* scm_merge_vector_x */
701
702
703 static void
704 scm_merge_vector_step (SCM vp,
705 SCM * temp,
706 scm_t_trampoline_2 cmp,
707 SCM less,
708 long low,
709 long high)
710 {
711 if (high > low)
712 {
713 long mid = (low + high) / 2;
714 SCM_TICK;
715 scm_merge_vector_step (vp, temp, cmp, less, low, mid);
716 scm_merge_vector_step (vp, temp, cmp, less, mid+1, high);
717 scm_merge_vector_x (vp, temp, cmp, less, low, mid, high);
718 }
719 } /* scm_merge_vector_step */
720
721
722 SCM_DEFINE (scm_stable_sort_x, "stable-sort!", 2, 0, 0,
723 (SCM items, SCM less),
724 "Sort the sequence @var{items}, which may be a list or a\n"
725 "vector. @var{less} is used for comparing the sequence elements.\n"
726 "The sorting is destructive, that means that the input sequence\n"
727 "is modified to produce the sorted result.\n"
728 "This is a stable sort.")
729 #define FUNC_NAME s_scm_stable_sort_x
730 {
731 const scm_t_trampoline_2 cmp = compare_function (less, 2, FUNC_NAME);
732 long len; /* list/vector length */
733
734 if (SCM_NULL_OR_NIL_P (items))
735 return items;
736
737 if (scm_is_pair (items))
738 {
739 SCM_VALIDATE_LIST_COPYLEN (1, items, len);
740 return scm_merge_list_step (&items, cmp, less, len);
741 }
742 else if (SCM_VECTORP (items))
743 {
744 SCM *temp;
745 len = SCM_VECTOR_LENGTH (items);
746
747 /*
748 the following array does not contain any new references to
749 SCM objects, so we can get away with allocing it on the heap.
750 */
751 temp = scm_malloc (len * sizeof(SCM));
752
753 scm_merge_vector_step (items, temp, cmp, less, 0, len - 1);
754 free(temp);
755 return items;
756 }
757 else
758 SCM_WRONG_TYPE_ARG (1, items);
759 }
760 #undef FUNC_NAME
761
762
763 SCM_DEFINE (scm_stable_sort, "stable-sort", 2, 0, 0,
764 (SCM items, SCM less),
765 "Sort the sequence @var{items}, which may be a list or a\n"
766 "vector. @var{less} is used for comparing the sequence elements.\n"
767 "This is a stable sort.")
768 #define FUNC_NAME s_scm_stable_sort
769 {
770 const scm_t_trampoline_2 cmp = compare_function (less, 2, FUNC_NAME);
771
772 if (SCM_NULL_OR_NIL_P (items))
773 return items;
774
775 if (scm_is_pair (items))
776 {
777 long len; /* list/vector length */
778
779 SCM_VALIDATE_LIST_COPYLEN (1, items, len);
780 items = scm_list_copy (items);
781 return scm_merge_list_step (&items, cmp, less, len);
782 }
783 #if SCM_HAVE_ARRAYS
784 /* support ordinary vectors even if arrays not available? */
785 else if (SCM_VECTORP (items))
786 {
787 long len = SCM_VECTOR_LENGTH (items);
788 SCM *temp = scm_malloc (len * sizeof (SCM));
789 SCM retvec = scm_make_uve (len, scm_array_prototype (items));
790 scm_array_copy_x (items, retvec);
791
792 scm_merge_vector_step (retvec, temp, cmp, less, 0, len - 1);
793 free (temp);
794 return retvec;
795 }
796 #endif
797 else
798 SCM_WRONG_TYPE_ARG (1, items);
799 }
800 #undef FUNC_NAME
801
802
803 SCM_DEFINE (scm_sort_list_x, "sort-list!", 2, 0, 0,
804 (SCM items, SCM less),
805 "Sort the list @var{items}, using @var{less} for comparing the\n"
806 "list elements. The sorting is destructive, that means that the\n"
807 "input list is modified to produce the sorted result.\n"
808 "This is a stable sort.")
809 #define FUNC_NAME s_scm_sort_list_x
810 {
811 const scm_t_trampoline_2 cmp = compare_function (less, 2, FUNC_NAME);
812 long len;
813
814 SCM_VALIDATE_LIST_COPYLEN (1, items, len);
815 return scm_merge_list_step (&items, cmp, less, len);
816 }
817 #undef FUNC_NAME
818
819
820 SCM_DEFINE (scm_sort_list, "sort-list", 2, 0, 0,
821 (SCM items, SCM less),
822 "Sort the list @var{items}, using @var{less} for comparing the\n"
823 "list elements. This is a stable sort.")
824 #define FUNC_NAME s_scm_sort_list
825 {
826 const scm_t_trampoline_2 cmp = compare_function (less, 2, FUNC_NAME);
827 long len;
828
829 SCM_VALIDATE_LIST_COPYLEN (1, items, len);
830 items = scm_list_copy (items);
831 return scm_merge_list_step (&items, cmp, less, len);
832 }
833 #undef FUNC_NAME
834
835
836 void
837 scm_init_sort ()
838 {
839 #include "libguile/sort.x"
840
841 scm_add_feature ("sort");
842 }
843
844 /*
845 Local Variables:
846 c-file-style: "gnu"
847 End:
848 */