1 /* Copyright (C) 1999,2000,2001,2002, 2004, 2006, 2007, 2008, 2009 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 License
4 * as published by the Free Software Foundation; either version 3 of
5 * the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful, but
8 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
20 /* Written in December 1998 by Roland Orre <orre@nada.kth.se>
21 * This implements the same sort interface as slib/sort.scm
22 * for lists and vectors where slib defines:
23 * sorted?, merge, merge!, sort, sort!
24 * For scsh compatibility sort-list and sort-list! are also defined.
25 * In cases where a stable-sort is required use stable-sort or
26 * stable-sort!. An additional feature is
27 * (restricted-vector-sort! vector less? startpos endpos)
28 * which allows you to sort part of a vector.
29 * Thanks to Aubrey Jaffer for the slib/sort.scm library.
30 * Thanks to Richard A. O'Keefe (based on Prolog code by D.H.D.Warren)
31 * for the merge sort inspiration.
32 * Thanks to Douglas C. Schmidt (schmidt@ics.uci.edu) for the
40 #include "libguile/_scm.h"
41 #include "libguile/eval.h"
42 #include "libguile/arrays.h"
43 #include "libguile/array-map.h"
44 #include "libguile/feature.h"
45 #include "libguile/vectors.h"
46 #include "libguile/lang.h"
47 #include "libguile/async.h"
48 #include "libguile/dynwind.h"
50 #include "libguile/validate.h"
51 #include "libguile/sort.h"
53 /* We have two quicksort variants: one for contigous vectors and one
54 for vectors with arbitrary increments between elements. Note that
55 increments can be negative.
58 #define NAME quicksort1
59 #define INC_PARAM /* empty */
61 #include "libguile/quicksort.i.c"
63 #define NAME quicksort
64 #define INC_PARAM ssize_t inc,
66 #include "libguile/quicksort.i.c"
68 static scm_t_trampoline_2
69 compare_function (SCM less
, unsigned int arg_nr
, const char* fname
)
71 const scm_t_trampoline_2 cmp
= scm_trampoline_2 (less
);
72 SCM_ASSERT_TYPE (cmp
!= NULL
, less
, arg_nr
, fname
, "less predicate");
77 SCM_DEFINE (scm_restricted_vector_sort_x
, "restricted-vector-sort!", 4, 0, 0,
78 (SCM vec
, SCM less
, SCM startpos
, SCM endpos
),
79 "Sort the vector @var{vec}, using @var{less} for comparing\n"
80 "the vector elements. @var{startpos} (inclusively) and\n"
81 "@var{endpos} (exclusively) delimit\n"
82 "the range of the vector which gets sorted. The return value\n"
84 #define FUNC_NAME s_scm_restricted_vector_sort_x
86 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
87 size_t vlen
, spos
, len
;
89 scm_t_array_handle handle
;
92 velts
= scm_vector_writable_elements (vec
, &handle
, &vlen
, &vinc
);
93 spos
= scm_to_unsigned_integer (startpos
, 0, vlen
);
94 len
= scm_to_unsigned_integer (endpos
, spos
, vlen
) - spos
;
97 quicksort1 (velts
+ spos
*vinc
, len
, cmp
, less
);
99 quicksort (velts
+ spos
*vinc
, len
, vinc
, cmp
, less
);
101 scm_array_handle_release (&handle
);
103 return SCM_UNSPECIFIED
;
108 /* (sorted? sequence less?)
109 * is true when sequence is a list (x0 x1 ... xm) or a vector #(x0 ... xm)
110 * such that for all 1 <= i <= m,
111 * (not (less? (list-ref list i) (list-ref list (- i 1)))). */
112 SCM_DEFINE (scm_sorted_p
, "sorted?", 2, 0, 0,
113 (SCM items
, SCM less
),
114 "Return @code{#t} iff @var{items} is a list or a vector such that\n"
115 "for all 1 <= i <= m, the predicate @var{less} returns true when\n"
116 "applied to all elements i - 1 and i")
117 #define FUNC_NAME s_scm_sorted_p
119 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
120 long len
, j
; /* list/vector length, temp j */
121 SCM item
, rest
; /* rest of items loop variable */
123 if (SCM_NULL_OR_NIL_P (items
))
126 if (scm_is_pair (items
))
128 len
= scm_ilength (items
); /* also checks that it's a pure list */
129 SCM_ASSERT_RANGE (1, items
, len
>= 0);
133 item
= SCM_CAR (items
);
134 rest
= SCM_CDR (items
);
138 if (scm_is_true ((*cmp
) (less
, SCM_CAR (rest
), item
)))
142 item
= SCM_CAR (rest
);
143 rest
= SCM_CDR (rest
);
151 scm_t_array_handle handle
;
155 SCM result
= SCM_BOOL_T
;
157 elts
= scm_vector_elements (items
, &handle
, &len
, &inc
);
159 for (i
= 1; i
< len
; i
++, elts
+= inc
)
161 if (scm_is_true ((*cmp
) (less
, elts
[inc
], elts
[0])))
168 scm_array_handle_release (&handle
);
179 takes two lists a and b such that (sorted? a less?) and (sorted? b less?)
180 and returns a new list in which the elements of a and b have been stably
181 interleaved so that (sorted? (merge a b less?) less?).
182 Note: this does _not_ accept vectors. */
183 SCM_DEFINE (scm_merge
, "merge", 3, 0, 0,
184 (SCM alist
, SCM blist
, SCM less
),
185 "Merge two already sorted lists into one.\n"
186 "Given two lists @var{alist} and @var{blist}, such that\n"
187 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)},\n"
188 "return a new list in which the elements of @var{alist} and\n"
189 "@var{blist} have been stably interleaved so that\n"
190 "@code{(sorted? (merge alist blist less?) less?)}.\n"
191 "Note: this does _not_ accept vectors.")
192 #define FUNC_NAME s_scm_merge
196 if (SCM_NULL_OR_NIL_P (alist
))
198 else if (SCM_NULL_OR_NIL_P (blist
))
202 const scm_t_trampoline_2 cmp
= compare_function (less
, 3, FUNC_NAME
);
203 long alen
, blen
; /* list lengths */
206 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist
, alen
);
207 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist
, blen
);
208 if (scm_is_true ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
210 build
= scm_cons (SCM_CAR (blist
), SCM_EOL
);
211 blist
= SCM_CDR (blist
);
216 build
= scm_cons (SCM_CAR (alist
), SCM_EOL
);
217 alist
= SCM_CDR (alist
);
221 while ((alen
> 0) && (blen
> 0))
224 if (scm_is_true ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
226 SCM_SETCDR (last
, scm_cons (SCM_CAR (blist
), SCM_EOL
));
227 blist
= SCM_CDR (blist
);
232 SCM_SETCDR (last
, scm_cons (SCM_CAR (alist
), SCM_EOL
));
233 alist
= SCM_CDR (alist
);
236 last
= SCM_CDR (last
);
238 if ((alen
> 0) && (blen
== 0))
239 SCM_SETCDR (last
, alist
);
240 else if ((alen
== 0) && (blen
> 0))
241 SCM_SETCDR (last
, blist
);
249 scm_merge_list_x (SCM alist
, SCM blist
,
250 long alen
, long blen
,
251 scm_t_trampoline_2 cmp
, SCM less
)
255 if (SCM_NULL_OR_NIL_P (alist
))
257 else if (SCM_NULL_OR_NIL_P (blist
))
261 if (scm_is_true ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
264 blist
= SCM_CDR (blist
);
270 alist
= SCM_CDR (alist
);
274 while ((alen
> 0) && (blen
> 0))
277 if (scm_is_true ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
279 SCM_SETCDR (last
, blist
);
280 blist
= SCM_CDR (blist
);
285 SCM_SETCDR (last
, alist
);
286 alist
= SCM_CDR (alist
);
289 last
= SCM_CDR (last
);
291 if ((alen
> 0) && (blen
== 0))
292 SCM_SETCDR (last
, alist
);
293 else if ((alen
== 0) && (blen
> 0))
294 SCM_SETCDR (last
, blist
);
297 } /* scm_merge_list_x */
300 SCM_DEFINE (scm_merge_x
, "merge!", 3, 0, 0,
301 (SCM alist
, SCM blist
, SCM less
),
302 "Takes two lists @var{alist} and @var{blist} such that\n"
303 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)} and\n"
304 "returns a new list in which the elements of @var{alist} and\n"
305 "@var{blist} have been stably interleaved so that\n"
306 " @code{(sorted? (merge alist blist less?) less?)}.\n"
307 "This is the destructive variant of @code{merge}\n"
308 "Note: this does _not_ accept vectors.")
309 #define FUNC_NAME s_scm_merge_x
311 if (SCM_NULL_OR_NIL_P (alist
))
313 else if (SCM_NULL_OR_NIL_P (blist
))
317 const scm_t_trampoline_2 cmp
= compare_function (less
, 3, FUNC_NAME
);
318 long alen
, blen
; /* list lengths */
319 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist
, alen
);
320 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist
, blen
);
321 return scm_merge_list_x (alist
, blist
, alen
, blen
, cmp
, less
);
327 /* This merge sort algorithm is same as slib's by Richard A. O'Keefe.
328 The algorithm is stable. We also tried to use the algorithm used by
329 scsh's merge-sort but that algorithm showed to not be stable, even
330 though it claimed to be.
333 scm_merge_list_step (SCM
* seq
, scm_t_trampoline_2 cmp
, SCM less
, long n
)
341 a
= scm_merge_list_step (seq
, cmp
, less
, mid
);
342 b
= scm_merge_list_step (seq
, cmp
, less
, n
- mid
);
343 return scm_merge_list_x (a
, b
, mid
, n
- mid
, cmp
, less
);
348 SCM rest
= SCM_CDR (*seq
);
349 SCM x
= SCM_CAR (*seq
);
350 SCM y
= SCM_CAR (SCM_CDR (*seq
));
351 *seq
= SCM_CDR (rest
);
352 SCM_SETCDR (rest
, SCM_EOL
);
353 if (scm_is_true ((*cmp
) (less
, y
, x
)))
356 SCM_SETCAR (rest
, x
);
364 SCM_SETCDR (p
, SCM_EOL
);
369 } /* scm_merge_list_step */
372 SCM_DEFINE (scm_sort_x
, "sort!", 2, 0, 0,
373 (SCM items
, SCM less
),
374 "Sort the sequence @var{items}, which may be a list or a\n"
375 "vector. @var{less} is used for comparing the sequence\n"
376 "elements. The sorting is destructive, that means that the\n"
377 "input sequence is modified to produce the sorted result.\n"
378 "This is not a stable sort.")
379 #define FUNC_NAME s_scm_sort_x
381 long len
; /* list/vector length */
382 if (SCM_NULL_OR_NIL_P (items
))
385 if (scm_is_pair (items
))
387 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
388 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
389 return scm_merge_list_step (&items
, cmp
, less
, len
);
391 else if (scm_is_vector (items
))
393 scm_restricted_vector_sort_x (items
,
396 scm_vector_length (items
));
400 SCM_WRONG_TYPE_ARG (1, items
);
405 SCM_DEFINE (scm_sort
, "sort", 2, 0, 0,
406 (SCM items
, SCM less
),
407 "Sort the sequence @var{items}, which may be a list or a\n"
408 "vector. @var{less} is used for comparing the sequence\n"
409 "elements. This is not a stable sort.")
410 #define FUNC_NAME s_scm_sort
412 if (SCM_NULL_OR_NIL_P (items
))
415 if (scm_is_pair (items
))
416 return scm_sort_x (scm_list_copy (items
), less
);
417 else if (scm_is_vector (items
))
418 return scm_sort_x (scm_vector_copy (items
), less
);
420 SCM_WRONG_TYPE_ARG (1, items
);
426 scm_merge_vector_x (SCM
*vec
,
428 scm_t_trampoline_2 cmp
,
435 size_t it
; /* Index for temp vector */
436 size_t i1
= low
; /* Index for lower vector segment */
437 size_t i2
= mid
+ 1; /* Index for upper vector segment */
439 #define VEC(i) vec[(i)*inc]
441 /* Copy while both segments contain more characters */
442 for (it
= low
; (i1
<= mid
) && (i2
<= high
); ++it
)
444 if (scm_is_true ((*cmp
) (less
, VEC(i2
), VEC(i1
))))
445 temp
[it
] = VEC(i2
++);
447 temp
[it
] = VEC(i1
++);
451 /* Copy while first segment contains more characters */
453 temp
[it
++] = VEC(i1
++);
455 /* Copy while second segment contains more characters */
457 temp
[it
++] = VEC(i2
++);
459 /* Copy back from temp to vp */
460 for (it
= low
; it
<= high
; it
++)
463 } /* scm_merge_vector_x */
467 scm_merge_vector_step (SCM
*vec
,
469 scm_t_trampoline_2 cmp
,
477 size_t mid
= (low
+ high
) / 2;
479 scm_merge_vector_step (vec
, temp
, cmp
, less
, low
, mid
, inc
);
480 scm_merge_vector_step (vec
, temp
, cmp
, less
, mid
+1, high
, inc
);
481 scm_merge_vector_x (vec
, temp
, cmp
, less
, low
, mid
, high
, inc
);
483 } /* scm_merge_vector_step */
486 SCM_DEFINE (scm_stable_sort_x
, "stable-sort!", 2, 0, 0,
487 (SCM items
, SCM less
),
488 "Sort the sequence @var{items}, which may be a list or a\n"
489 "vector. @var{less} is used for comparing the sequence elements.\n"
490 "The sorting is destructive, that means that the input sequence\n"
491 "is modified to produce the sorted result.\n"
492 "This is a stable sort.")
493 #define FUNC_NAME s_scm_stable_sort_x
495 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
496 long len
; /* list/vector length */
498 if (SCM_NULL_OR_NIL_P (items
))
501 if (scm_is_pair (items
))
503 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
504 return scm_merge_list_step (&items
, cmp
, less
, len
);
506 else if (scm_is_vector (items
))
508 scm_t_array_handle temp_handle
, vec_handle
;
509 SCM temp
, *temp_elts
, *vec_elts
;
513 vec_elts
= scm_vector_writable_elements (items
, &vec_handle
,
515 temp
= scm_c_make_vector (len
, SCM_UNDEFINED
);
516 temp_elts
= scm_vector_writable_elements (temp
, &temp_handle
,
519 scm_merge_vector_step (vec_elts
, temp_elts
, cmp
, less
, 0, len
-1, inc
);
521 scm_array_handle_release (&temp_handle
);
522 scm_array_handle_release (&vec_handle
);
527 SCM_WRONG_TYPE_ARG (1, items
);
532 SCM_DEFINE (scm_stable_sort
, "stable-sort", 2, 0, 0,
533 (SCM items
, SCM less
),
534 "Sort the sequence @var{items}, which may be a list or a\n"
535 "vector. @var{less} is used for comparing the sequence elements.\n"
536 "This is a stable sort.")
537 #define FUNC_NAME s_scm_stable_sort
539 if (SCM_NULL_OR_NIL_P (items
))
542 if (scm_is_pair (items
))
543 return scm_stable_sort_x (scm_list_copy (items
), less
);
544 else if (scm_is_vector (items
))
545 return scm_stable_sort_x (scm_vector_copy (items
), less
);
547 SCM_WRONG_TYPE_ARG (1, items
);
552 SCM_DEFINE (scm_sort_list_x
, "sort-list!", 2, 0, 0,
553 (SCM items
, SCM less
),
554 "Sort the list @var{items}, using @var{less} for comparing the\n"
555 "list elements. The sorting is destructive, that means that the\n"
556 "input list is modified to produce the sorted result.\n"
557 "This is a stable sort.")
558 #define FUNC_NAME s_scm_sort_list_x
560 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
563 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
564 return scm_merge_list_step (&items
, cmp
, less
, len
);
569 SCM_DEFINE (scm_sort_list
, "sort-list", 2, 0, 0,
570 (SCM items
, SCM less
),
571 "Sort the list @var{items}, using @var{less} for comparing the\n"
572 "list elements. This is a stable sort.")
573 #define FUNC_NAME s_scm_sort_list
575 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
578 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
579 items
= scm_list_copy (items
);
580 return scm_merge_list_step (&items
, cmp
, less
, len
);
588 #include "libguile/sort.x"
590 scm_add_feature ("sort");