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.
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.
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
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
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"
43 #include "libguile/validate.h"
44 #include "libguile/sort.h"
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>.
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.
56 This sorting routine is not much more efficient than the stable
57 version but doesn't consume extra memory.
60 #define SWAP(a, b) do { const SCM _tmp = a; a = b; b = _tmp; } while (0)
63 /* Order size using quicksort. This implementation incorporates
64 four optimizations discussed in Sedgewick:
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.
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.
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.
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)! */
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. */
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
97 /* Stack node declarations used to store unfulfilled partition obligations. */
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)
110 quicksort (SCM
*const base_ptr
, size_t nr_elems
, scm_t_trampoline_2 cmp
, SCM less
)
112 static const char s_buggy_less
[] = "buggy less predicate used when sorting";
115 /* Avoid lossage with unsigned arithmetic below. */
118 if (nr_elems
> MAX_THRESH
)
121 size_t hi
= nr_elems
- 1;
123 stack_node stack
[STACK_SIZE
];
124 stack_node
*top
= stack
+ 1;
126 while (STACK_NOT_EMPTY
)
131 /* Select median value from among LO, MID, and HI. Rearrange
132 LO and HI so the three values are sorted. This lowers the
133 probability of picking a pathological pivot value and
134 skips a comparison for both the left and right. */
136 size_t mid
= lo
+ (hi
- lo
) / 2;
138 if (!SCM_FALSEP ((*cmp
) (less
, base_ptr
[mid
], base_ptr
[lo
])))
139 SWAP (base_ptr
[mid
], base_ptr
[lo
]);
140 if (!SCM_FALSEP ((*cmp
) (less
, base_ptr
[hi
], base_ptr
[mid
])))
141 SWAP (base_ptr
[mid
], base_ptr
[hi
]);
144 if (!SCM_FALSEP ((*cmp
) (less
, base_ptr
[mid
], base_ptr
[lo
])))
145 SWAP (base_ptr
[mid
], base_ptr
[lo
]);
151 /* Here's the famous ``collapse the walls'' section of quicksort.
152 Gotta like those tight inner loops! They are the main reason
153 that this algorithm runs much faster than others. */
156 while (!SCM_FALSEP ((*cmp
) (less
, base_ptr
[left
], base_ptr
[mid
])))
159 /* The comparison predicate may be buggy */
161 scm_misc_error (NULL
, s_buggy_less
, SCM_EOL
);
164 while (!SCM_FALSEP ((*cmp
) (less
, base_ptr
[mid
], base_ptr
[right
])))
167 /* The comparison predicate may be buggy */
169 scm_misc_error (NULL
, s_buggy_less
, SCM_EOL
);
174 SWAP (base_ptr
[left
], base_ptr
[right
]);
178 else if (left
== right
)
185 while (left
<= right
);
187 /* Set up pointers for next iteration. First determine whether
188 left and right partitions are below the threshold size. If so,
189 ignore one or both. Otherwise, push the larger partition's
190 bounds on the stack and continue sorting the smaller one. */
192 if ((size_t) (right
- lo
) <= MAX_THRESH
)
194 if ((size_t) (hi
- left
) <= MAX_THRESH
)
195 /* Ignore both small partitions. */
198 /* Ignore small left partition. */
201 else if ((size_t) (hi
- left
) <= MAX_THRESH
)
202 /* Ignore small right partition. */
204 else if ((right
- lo
) > (hi
- left
))
206 /* Push larger left partition indices. */
212 /* Push larger right partition indices. */
219 /* Once the BASE_PTR array is partially sorted by quicksort the rest is
220 completely sorted using insertion sort, since this is efficient for
221 partitions below MAX_THRESH size. BASE_PTR points to the beginning of the
222 array to sort, and END idexes the very last element in the array (*not*
227 size_t end
= nr_elems
- 1;
228 size_t thresh
= min (end
, MAX_THRESH
);
231 /* Find smallest element in first threshold and place it at the
232 array's beginning. This is the smallest array element,
233 and the operation speeds up insertion sort's inner loop. */
235 for (run
= tmp
+ 1; run
<= thresh
; run
++)
236 if (!SCM_FALSEP ((*cmp
) (less
, base_ptr
[run
], base_ptr
[tmp
])))
240 SWAP (base_ptr
[tmp
], base_ptr
[0]);
242 /* Insertion sort, running from left-hand-side up to right-hand-side. */
248 while (!SCM_FALSEP ((*cmp
) (less
, base_ptr
[run
], base_ptr
[tmp
])))
250 /* The comparison predicate may be buggy */
252 scm_misc_error (NULL
, s_buggy_less
, SCM_EOL
);
260 SCM to_insert
= base_ptr
[run
];
263 for (hi
= lo
= run
; --lo
>= tmp
; hi
= lo
)
264 base_ptr
[hi
] = base_ptr
[lo
];
265 base_ptr
[hi
] = to_insert
;
272 static scm_t_trampoline_2
273 compare_function (SCM less
, unsigned int arg_nr
, const char* fname
)
275 const scm_t_trampoline_2 cmp
= scm_trampoline_2 (less
);
276 SCM_ASSERT_TYPE (cmp
!= NULL
, less
, arg_nr
, fname
, "less predicate");
281 /* Question: Is there any need to make this a more general array sort?
282 It is probably enough to manage the vector type. */
283 /* endpos equal as for substring, i.e. endpos is not included. */
284 /* More natural with length? */
286 SCM_DEFINE (scm_restricted_vector_sort_x
, "restricted-vector-sort!", 4, 0, 0,
287 (SCM vec
, SCM less
, SCM startpos
, SCM endpos
),
288 "Sort the vector @var{vec}, using @var{less} for comparing\n"
289 "the vector elements. @var{startpos} and @var{endpos} delimit\n"
290 "the range of the vector which gets sorted. The return value\n"
292 #define FUNC_NAME s_scm_restricted_vector_sort_x
294 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
295 size_t vlen
, spos
, len
;
298 SCM_VALIDATE_VECTOR (1, vec
);
299 vp
= SCM_WRITABLE_VELTS (vec
); /* vector pointer */
300 vlen
= SCM_VECTOR_LENGTH (vec
);
302 SCM_VALIDATE_INUM_MIN_COPY (3, startpos
, 0, spos
);
303 SCM_ASSERT_RANGE (3, startpos
, spos
<= vlen
);
304 SCM_VALIDATE_INUM_RANGE (4, endpos
,0, vlen
+1);
305 len
= SCM_INUM (endpos
) - spos
;
307 quicksort (&vp
[spos
], len
, cmp
, less
);
308 scm_remember_upto_here_1 (vec
);
310 return SCM_UNSPECIFIED
;
315 /* (sorted? sequence less?)
316 * is true when sequence is a list (x0 x1 ... xm) or a vector #(x0 ... xm)
317 * such that for all 1 <= i <= m,
318 * (not (less? (list-ref list i) (list-ref list (- i 1)))). */
319 SCM_DEFINE (scm_sorted_p
, "sorted?", 2, 0, 0,
320 (SCM items
, SCM less
),
321 "Return @code{#t} iff @var{items} is a list or a vector such that\n"
322 "for all 1 <= i <= m, the predicate @var{less} returns true when\n"
323 "applied to all elements i - 1 and i")
324 #define FUNC_NAME s_scm_sorted_p
326 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
327 long len
, j
; /* list/vector length, temp j */
328 SCM item
, rest
; /* rest of items loop variable */
331 if (SCM_NULL_OR_NIL_P (items
))
334 if (SCM_CONSP (items
))
336 len
= scm_ilength (items
); /* also checks that it's a pure list */
337 SCM_ASSERT_RANGE (1, items
, len
>= 0);
341 item
= SCM_CAR (items
);
342 rest
= SCM_CDR (items
);
346 if (!SCM_FALSEP ((*cmp
) (less
, SCM_CAR (rest
), item
)))
350 item
= SCM_CAR (rest
);
351 rest
= SCM_CDR (rest
);
359 SCM_VALIDATE_VECTOR (1, items
);
361 vp
= SCM_VELTS (items
); /* vector pointer */
362 len
= SCM_VECTOR_LENGTH (items
);
366 if (!SCM_FALSEP ((*cmp
) (less
, vp
[1], vp
[0])))
383 takes two lists a and b such that (sorted? a less?) and (sorted? b less?)
384 and returns a new list in which the elements of a and b have been stably
385 interleaved so that (sorted? (merge a b less?) less?).
386 Note: this does _not_ accept vectors. */
387 SCM_DEFINE (scm_merge
, "merge", 3, 0, 0,
388 (SCM alist
, SCM blist
, SCM less
),
389 "Merge two already sorted lists into one.\n"
390 "Given two lists @var{alist} and @var{blist}, such that\n"
391 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)},\n"
392 "return a new list in which the elements of @var{alist} and\n"
393 "@var{blist} have been stably interleaved so that\n"
394 "@code{(sorted? (merge alist blist less?) less?)}.\n"
395 "Note: this does _not_ accept vectors.")
396 #define FUNC_NAME s_scm_merge
400 if (SCM_NULL_OR_NIL_P (alist
))
402 else if (SCM_NULL_OR_NIL_P (blist
))
406 const scm_t_trampoline_2 cmp
= compare_function (less
, 3, FUNC_NAME
);
407 long alen
, blen
; /* list lengths */
410 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist
, alen
);
411 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist
, blen
);
412 if (!SCM_FALSEP ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
414 build
= scm_cons (SCM_CAR (blist
), SCM_EOL
);
415 blist
= SCM_CDR (blist
);
420 build
= scm_cons (SCM_CAR (alist
), SCM_EOL
);
421 alist
= SCM_CDR (alist
);
425 while ((alen
> 0) && (blen
> 0))
427 if (!SCM_FALSEP ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
429 SCM_SETCDR (last
, scm_cons (SCM_CAR (blist
), SCM_EOL
));
430 blist
= SCM_CDR (blist
);
435 SCM_SETCDR (last
, scm_cons (SCM_CAR (alist
), SCM_EOL
));
436 alist
= SCM_CDR (alist
);
439 last
= SCM_CDR (last
);
441 if ((alen
> 0) && (blen
== 0))
442 SCM_SETCDR (last
, alist
);
443 else if ((alen
== 0) && (blen
> 0))
444 SCM_SETCDR (last
, blist
);
452 scm_merge_list_x (SCM alist
, SCM blist
,
453 long alen
, long blen
,
454 scm_t_trampoline_2 cmp
, SCM less
)
458 if (SCM_NULL_OR_NIL_P (alist
))
460 else if (SCM_NULL_OR_NIL_P (blist
))
464 if (!SCM_FALSEP ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
467 blist
= SCM_CDR (blist
);
473 alist
= SCM_CDR (alist
);
477 while ((alen
> 0) && (blen
> 0))
479 if (!SCM_FALSEP ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
481 SCM_SETCDR (last
, blist
);
482 blist
= SCM_CDR (blist
);
487 SCM_SETCDR (last
, alist
);
488 alist
= SCM_CDR (alist
);
491 last
= SCM_CDR (last
);
493 if ((alen
> 0) && (blen
== 0))
494 SCM_SETCDR (last
, alist
);
495 else if ((alen
== 0) && (blen
> 0))
496 SCM_SETCDR (last
, blist
);
499 } /* scm_merge_list_x */
502 SCM_DEFINE (scm_merge_x
, "merge!", 3, 0, 0,
503 (SCM alist
, SCM blist
, SCM less
),
504 "Takes two lists @var{alist} and @var{blist} such that\n"
505 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)} and\n"
506 "returns a new list in which the elements of @var{alist} and\n"
507 "@var{blist} have been stably interleaved so that\n"
508 " @code{(sorted? (merge alist blist less?) less?)}.\n"
509 "This is the destructive variant of @code{merge}\n"
510 "Note: this does _not_ accept vectors.")
511 #define FUNC_NAME s_scm_merge_x
513 if (SCM_NULL_OR_NIL_P (alist
))
515 else if (SCM_NULL_OR_NIL_P (blist
))
519 const scm_t_trampoline_2 cmp
= compare_function (less
, 3, FUNC_NAME
);
520 long alen
, blen
; /* list lengths */
521 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist
, alen
);
522 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist
, blen
);
523 return scm_merge_list_x (alist
, blist
, alen
, blen
, cmp
, less
);
529 /* This merge sort algorithm is same as slib's by Richard A. O'Keefe.
530 The algorithm is stable. We also tried to use the algorithm used by
531 scsh's merge-sort but that algorithm showed to not be stable, even
532 though it claimed to be.
535 scm_merge_list_step (SCM
* seq
, scm_t_trampoline_2 cmp
, SCM less
, long n
)
542 a
= scm_merge_list_step (seq
, cmp
, less
, mid
);
543 b
= scm_merge_list_step (seq
, cmp
, less
, n
- mid
);
544 return scm_merge_list_x (a
, b
, mid
, n
- mid
, cmp
, less
);
549 SCM rest
= SCM_CDR (*seq
);
550 SCM x
= SCM_CAR (*seq
);
551 SCM y
= SCM_CAR (SCM_CDR (*seq
));
552 *seq
= SCM_CDR (rest
);
553 SCM_SETCDR (rest
, SCM_EOL
);
554 if (!SCM_FALSEP ((*cmp
) (less
, y
, x
)))
557 SCM_SETCAR (rest
, x
);
565 SCM_SETCDR (p
, SCM_EOL
);
570 } /* scm_merge_list_step */
573 SCM_DEFINE (scm_sort_x
, "sort!", 2, 0, 0,
574 (SCM items
, SCM less
),
575 "Sort the sequence @var{items}, which may be a list or a\n"
576 "vector. @var{less} is used for comparing the sequence\n"
577 "elements. The sorting is destructive, that means that the\n"
578 "input sequence is modified to produce the sorted result.\n"
579 "This is not a stable sort.")
580 #define FUNC_NAME s_scm_sort_x
582 long len
; /* list/vector length */
583 if (SCM_NULL_OR_NIL_P (items
))
586 if (SCM_CONSP (items
))
588 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
589 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
590 return scm_merge_list_step (&items
, cmp
, less
, len
);
592 else if (SCM_VECTORP (items
))
594 len
= SCM_VECTOR_LENGTH (items
);
595 scm_restricted_vector_sort_x (items
,
602 SCM_WRONG_TYPE_ARG (1, items
);
607 SCM_DEFINE (scm_sort
, "sort", 2, 0, 0,
608 (SCM items
, SCM less
),
609 "Sort the sequence @var{items}, which may be a list or a\n"
610 "vector. @var{less} is used for comparing the sequence\n"
611 "elements. This is not a stable sort.")
612 #define FUNC_NAME s_scm_sort
614 if (SCM_NULL_OR_NIL_P (items
))
617 if (SCM_CONSP (items
))
619 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
622 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
623 items
= scm_list_copy (items
);
624 return scm_merge_list_step (&items
, cmp
, less
, len
);
627 /* support ordinary vectors even if arrays not available? */
628 else if (SCM_VECTORP (items
))
630 long len
= SCM_VECTOR_LENGTH (items
);
631 SCM sortvec
= scm_make_uve (len
, scm_array_prototype (items
));
633 scm_array_copy_x (items
, sortvec
);
634 scm_restricted_vector_sort_x (sortvec
,
642 SCM_WRONG_TYPE_ARG (1, items
);
648 scm_merge_vector_x (SCM vec
,
650 scm_t_trampoline_2 cmp
,
656 long it
; /* Index for temp vector */
657 long i1
= low
; /* Index for lower vector segment */
658 long i2
= mid
+ 1; /* Index for upper vector segment */
660 /* Copy while both segments contain more characters */
661 for (it
= low
; (i1
<= mid
) && (i2
<= high
); ++it
)
664 Every call of LESS might invoke GC. For full correctness, we
665 should reset the generation of vecbase and tempbase between
669 register SCM
*vp
= SCM_WRITABLE_VELTS(vec
);
671 if (!SCM_FALSEP ((*cmp
) (less
, vp
[i2
], vp
[i1
])))
678 register SCM
*vp
= SCM_WRITABLE_VELTS(vec
);
680 /* Copy while first segment contains more characters */
682 temp
[it
++] = vp
[i1
++];
684 /* Copy while second segment contains more characters */
686 temp
[it
++] = vp
[i2
++];
688 /* Copy back from temp to vp */
689 for (it
= low
; it
<= high
; ++it
)
692 } /* scm_merge_vector_x */
696 scm_merge_vector_step (SCM vp
,
698 scm_t_trampoline_2 cmp
,
705 long mid
= (low
+ high
) / 2;
706 scm_merge_vector_step (vp
, temp
, cmp
, less
, low
, mid
);
707 scm_merge_vector_step (vp
, temp
, cmp
, less
, mid
+1, high
);
708 scm_merge_vector_x (vp
, temp
, cmp
, less
, low
, mid
, high
);
710 } /* scm_merge_vector_step */
713 SCM_DEFINE (scm_stable_sort_x
, "stable-sort!", 2, 0, 0,
714 (SCM items
, SCM less
),
715 "Sort the sequence @var{items}, which may be a list or a\n"
716 "vector. @var{less} is used for comparing the sequence elements.\n"
717 "The sorting is destructive, that means that the input sequence\n"
718 "is modified to produce the sorted result.\n"
719 "This is a stable sort.")
720 #define FUNC_NAME s_scm_stable_sort_x
722 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
723 long len
; /* list/vector length */
725 if (SCM_NULL_OR_NIL_P (items
))
728 if (SCM_CONSP (items
))
730 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
731 return scm_merge_list_step (&items
, cmp
, less
, len
);
733 else if (SCM_VECTORP (items
))
736 len
= SCM_VECTOR_LENGTH (items
);
739 the following array does not contain any new references to
740 SCM objects, so we can get away with allocing it on the heap.
742 temp
= scm_malloc (len
* sizeof(SCM
));
744 scm_merge_vector_step (items
, temp
, cmp
, less
, 0, len
- 1);
749 SCM_WRONG_TYPE_ARG (1, items
);
754 SCM_DEFINE (scm_stable_sort
, "stable-sort", 2, 0, 0,
755 (SCM items
, SCM less
),
756 "Sort the sequence @var{items}, which may be a list or a\n"
757 "vector. @var{less} is used for comparing the sequence elements.\n"
758 "This is a stable sort.")
759 #define FUNC_NAME s_scm_stable_sort
761 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
763 if (SCM_NULL_OR_NIL_P (items
))
766 if (SCM_CONSP (items
))
768 long len
; /* list/vector length */
770 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
771 items
= scm_list_copy (items
);
772 return scm_merge_list_step (&items
, cmp
, less
, len
);
775 /* support ordinary vectors even if arrays not available? */
776 else if (SCM_VECTORP (items
))
778 long len
= SCM_VECTOR_LENGTH (items
);
779 SCM
*temp
= scm_malloc (len
* sizeof (SCM
));
780 SCM retvec
= scm_make_uve (len
, scm_array_prototype (items
));
781 scm_array_copy_x (items
, retvec
);
783 scm_merge_vector_step (retvec
, temp
, cmp
, less
, 0, len
- 1);
789 SCM_WRONG_TYPE_ARG (1, items
);
794 SCM_DEFINE (scm_sort_list_x
, "sort-list!", 2, 0, 0,
795 (SCM items
, SCM less
),
796 "Sort the list @var{items}, using @var{less} for comparing the\n"
797 "list elements. The sorting is destructive, that means that the\n"
798 "input list is modified to produce the sorted result.\n"
799 "This is a stable sort.")
800 #define FUNC_NAME s_scm_sort_list_x
802 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
805 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
806 return scm_merge_list_step (&items
, cmp
, less
, len
);
811 SCM_DEFINE (scm_sort_list
, "sort-list", 2, 0, 0,
812 (SCM items
, SCM less
),
813 "Sort the list @var{items}, using @var{less} for comparing the\n"
814 "list elements. This is a stable sort.")
815 #define FUNC_NAME s_scm_sort_list
817 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
820 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
821 items
= scm_list_copy (items
);
822 return scm_merge_list_step (&items
, cmp
, less
, len
);
830 #include "libguile/sort.x"
832 scm_add_feature ("sort");