1 /* Copyright (C) 1999,2000,2001,2002, 2004 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"
42 #include "libguile/async.h"
43 #include "libguile/dynwind.h"
45 #include "libguile/validate.h"
46 #include "libguile/sort.h"
48 /* We have two quicksort variants: one for contigous vectors and one
49 for vectors with arbitrary increments between elements. Note that
50 increments can be negative.
53 #define NAME quicksort1
54 #define INC_PARAM /* empty */
56 #include "libguile/quicksort.i.c"
58 #define NAME quicksort
59 #define INC_PARAM ssize_t inc,
61 #include "libguile/quicksort.i.c"
63 static scm_t_trampoline_2
64 compare_function (SCM less
, unsigned int arg_nr
, const char* fname
)
66 const scm_t_trampoline_2 cmp
= scm_trampoline_2 (less
);
67 SCM_ASSERT_TYPE (cmp
!= NULL
, less
, arg_nr
, fname
, "less predicate");
72 SCM_DEFINE (scm_restricted_vector_sort_x
, "restricted-vector-sort!", 4, 0, 0,
73 (SCM vec
, SCM less
, SCM startpos
, SCM endpos
),
74 "Sort the vector @var{vec}, using @var{less} for comparing\n"
75 "the vector elements. @var{startpos} (inclusively) and\n"
76 "@var{endpos} (exclusively) delimit\n"
77 "the range of the vector which gets sorted. The return value\n"
79 #define FUNC_NAME s_scm_restricted_vector_sort_x
81 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
82 size_t vlen
, spos
, len
;
84 scm_t_array_handle handle
;
87 velts
= scm_vector_writable_elements (vec
, &handle
, &vlen
, &vinc
);
88 spos
= scm_to_unsigned_integer (startpos
, 0, vlen
);
89 len
= scm_to_unsigned_integer (endpos
, spos
, vlen
) - spos
;
92 quicksort1 (velts
+ spos
*vinc
, len
, cmp
, less
);
94 quicksort (velts
+ spos
*vinc
, len
, vinc
, cmp
, less
);
96 return SCM_UNSPECIFIED
;
101 /* (sorted? sequence less?)
102 * is true when sequence is a list (x0 x1 ... xm) or a vector #(x0 ... xm)
103 * such that for all 1 <= i <= m,
104 * (not (less? (list-ref list i) (list-ref list (- i 1)))). */
105 SCM_DEFINE (scm_sorted_p
, "sorted?", 2, 0, 0,
106 (SCM items
, SCM less
),
107 "Return @code{#t} iff @var{items} is a list or a vector such that\n"
108 "for all 1 <= i <= m, the predicate @var{less} returns true when\n"
109 "applied to all elements i - 1 and i")
110 #define FUNC_NAME s_scm_sorted_p
112 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
113 long len
, j
; /* list/vector length, temp j */
114 SCM item
, rest
; /* rest of items loop variable */
116 if (SCM_NULL_OR_NIL_P (items
))
119 if (scm_is_pair (items
))
121 len
= scm_ilength (items
); /* also checks that it's a pure list */
122 SCM_ASSERT_RANGE (1, items
, len
>= 0);
126 item
= SCM_CAR (items
);
127 rest
= SCM_CDR (items
);
131 if (scm_is_true ((*cmp
) (less
, SCM_CAR (rest
), item
)))
135 item
= SCM_CAR (rest
);
136 rest
= SCM_CDR (rest
);
144 scm_t_array_handle handle
;
148 SCM result
= SCM_BOOL_T
;
150 elts
= scm_vector_elements (items
, &handle
, &len
, &inc
);
152 for (i
= 1; i
< len
; i
++, elts
+= inc
)
154 if (scm_is_true ((*cmp
) (less
, elts
[inc
], elts
[0])))
170 takes two lists a and b such that (sorted? a less?) and (sorted? b less?)
171 and returns a new list in which the elements of a and b have been stably
172 interleaved so that (sorted? (merge a b less?) less?).
173 Note: this does _not_ accept vectors. */
174 SCM_DEFINE (scm_merge
, "merge", 3, 0, 0,
175 (SCM alist
, SCM blist
, SCM less
),
176 "Merge two already sorted lists into one.\n"
177 "Given two lists @var{alist} and @var{blist}, such that\n"
178 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)},\n"
179 "return a new list in which the elements of @var{alist} and\n"
180 "@var{blist} have been stably interleaved so that\n"
181 "@code{(sorted? (merge alist blist less?) less?)}.\n"
182 "Note: this does _not_ accept vectors.")
183 #define FUNC_NAME s_scm_merge
187 if (SCM_NULL_OR_NIL_P (alist
))
189 else if (SCM_NULL_OR_NIL_P (blist
))
193 const scm_t_trampoline_2 cmp
= compare_function (less
, 3, FUNC_NAME
);
194 long alen
, blen
; /* list lengths */
197 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist
, alen
);
198 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist
, blen
);
199 if (scm_is_true ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
201 build
= scm_cons (SCM_CAR (blist
), SCM_EOL
);
202 blist
= SCM_CDR (blist
);
207 build
= scm_cons (SCM_CAR (alist
), SCM_EOL
);
208 alist
= SCM_CDR (alist
);
212 while ((alen
> 0) && (blen
> 0))
215 if (scm_is_true ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
217 SCM_SETCDR (last
, scm_cons (SCM_CAR (blist
), SCM_EOL
));
218 blist
= SCM_CDR (blist
);
223 SCM_SETCDR (last
, scm_cons (SCM_CAR (alist
), SCM_EOL
));
224 alist
= SCM_CDR (alist
);
227 last
= SCM_CDR (last
);
229 if ((alen
> 0) && (blen
== 0))
230 SCM_SETCDR (last
, alist
);
231 else if ((alen
== 0) && (blen
> 0))
232 SCM_SETCDR (last
, blist
);
240 scm_merge_list_x (SCM alist
, SCM blist
,
241 long alen
, long blen
,
242 scm_t_trampoline_2 cmp
, SCM less
)
246 if (SCM_NULL_OR_NIL_P (alist
))
248 else if (SCM_NULL_OR_NIL_P (blist
))
252 if (scm_is_true ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
255 blist
= SCM_CDR (blist
);
261 alist
= SCM_CDR (alist
);
265 while ((alen
> 0) && (blen
> 0))
268 if (scm_is_true ((*cmp
) (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
270 SCM_SETCDR (last
, blist
);
271 blist
= SCM_CDR (blist
);
276 SCM_SETCDR (last
, alist
);
277 alist
= SCM_CDR (alist
);
280 last
= SCM_CDR (last
);
282 if ((alen
> 0) && (blen
== 0))
283 SCM_SETCDR (last
, alist
);
284 else if ((alen
== 0) && (blen
> 0))
285 SCM_SETCDR (last
, blist
);
288 } /* scm_merge_list_x */
291 SCM_DEFINE (scm_merge_x
, "merge!", 3, 0, 0,
292 (SCM alist
, SCM blist
, SCM less
),
293 "Takes two lists @var{alist} and @var{blist} such that\n"
294 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)} and\n"
295 "returns a new list in which the elements of @var{alist} and\n"
296 "@var{blist} have been stably interleaved so that\n"
297 " @code{(sorted? (merge alist blist less?) less?)}.\n"
298 "This is the destructive variant of @code{merge}\n"
299 "Note: this does _not_ accept vectors.")
300 #define FUNC_NAME s_scm_merge_x
302 if (SCM_NULL_OR_NIL_P (alist
))
304 else if (SCM_NULL_OR_NIL_P (blist
))
308 const scm_t_trampoline_2 cmp
= compare_function (less
, 3, FUNC_NAME
);
309 long alen
, blen
; /* list lengths */
310 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist
, alen
);
311 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist
, blen
);
312 return scm_merge_list_x (alist
, blist
, alen
, blen
, cmp
, less
);
318 /* This merge sort algorithm is same as slib's by Richard A. O'Keefe.
319 The algorithm is stable. We also tried to use the algorithm used by
320 scsh's merge-sort but that algorithm showed to not be stable, even
321 though it claimed to be.
324 scm_merge_list_step (SCM
* seq
, scm_t_trampoline_2 cmp
, SCM less
, long n
)
332 a
= scm_merge_list_step (seq
, cmp
, less
, mid
);
333 b
= scm_merge_list_step (seq
, cmp
, less
, n
- mid
);
334 return scm_merge_list_x (a
, b
, mid
, n
- mid
, cmp
, less
);
339 SCM rest
= SCM_CDR (*seq
);
340 SCM x
= SCM_CAR (*seq
);
341 SCM y
= SCM_CAR (SCM_CDR (*seq
));
342 *seq
= SCM_CDR (rest
);
343 SCM_SETCDR (rest
, SCM_EOL
);
344 if (scm_is_true ((*cmp
) (less
, y
, x
)))
347 SCM_SETCAR (rest
, x
);
355 SCM_SETCDR (p
, SCM_EOL
);
360 } /* scm_merge_list_step */
363 SCM_DEFINE (scm_sort_x
, "sort!", 2, 0, 0,
364 (SCM items
, SCM less
),
365 "Sort the sequence @var{items}, which may be a list or a\n"
366 "vector. @var{less} is used for comparing the sequence\n"
367 "elements. The sorting is destructive, that means that the\n"
368 "input sequence is modified to produce the sorted result.\n"
369 "This is not a stable sort.")
370 #define FUNC_NAME s_scm_sort_x
372 long len
; /* list/vector length */
373 if (SCM_NULL_OR_NIL_P (items
))
376 if (scm_is_pair (items
))
378 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
379 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
380 return scm_merge_list_step (&items
, cmp
, less
, len
);
382 else if (scm_is_vector (items
))
384 scm_restricted_vector_sort_x (items
,
387 scm_vector_length (items
));
391 SCM_WRONG_TYPE_ARG (1, items
);
396 SCM_DEFINE (scm_sort
, "sort", 2, 0, 0,
397 (SCM items
, SCM less
),
398 "Sort the sequence @var{items}, which may be a list or a\n"
399 "vector. @var{less} is used for comparing the sequence\n"
400 "elements. This is not a stable sort.")
401 #define FUNC_NAME s_scm_sort
403 if (SCM_NULL_OR_NIL_P (items
))
406 if (scm_is_pair (items
))
407 return scm_sort_x (scm_list_copy (items
), less
);
408 else if (scm_is_vector (items
))
409 return scm_sort_x (scm_vector_copy (items
), less
);
411 SCM_WRONG_TYPE_ARG (1, items
);
417 scm_merge_vector_x (SCM
*vec
,
419 scm_t_trampoline_2 cmp
,
426 size_t it
; /* Index for temp vector */
427 size_t i1
= low
; /* Index for lower vector segment */
428 size_t i2
= mid
+ 1; /* Index for upper vector segment */
430 #define VEC(i) vec[(i)*inc]
432 /* Copy while both segments contain more characters */
433 for (it
= low
; (i1
<= mid
) && (i2
<= high
); ++it
)
435 if (scm_is_true ((*cmp
) (less
, VEC(i2
), VEC(i1
))))
436 temp
[it
] = VEC(i2
++);
438 temp
[it
] = VEC(i1
++);
442 /* Copy while first segment contains more characters */
444 temp
[it
++] = VEC(i1
++);
446 /* Copy while second segment contains more characters */
448 temp
[it
++] = VEC(i2
++);
450 /* Copy back from temp to vp */
451 for (it
= low
; it
<= high
; it
++)
454 } /* scm_merge_vector_x */
458 scm_merge_vector_step (SCM
*vec
,
460 scm_t_trampoline_2 cmp
,
468 size_t mid
= (low
+ high
) / 2;
470 scm_merge_vector_step (vec
, temp
, cmp
, less
, low
, mid
, inc
);
471 scm_merge_vector_step (vec
, temp
, cmp
, less
, mid
+1, high
, inc
);
472 scm_merge_vector_x (vec
, temp
, cmp
, less
, low
, mid
, high
, inc
);
474 } /* scm_merge_vector_step */
477 SCM_DEFINE (scm_stable_sort_x
, "stable-sort!", 2, 0, 0,
478 (SCM items
, SCM less
),
479 "Sort the sequence @var{items}, which may be a list or a\n"
480 "vector. @var{less} is used for comparing the sequence elements.\n"
481 "The sorting is destructive, that means that the input sequence\n"
482 "is modified to produce the sorted result.\n"
483 "This is a stable sort.")
484 #define FUNC_NAME s_scm_stable_sort_x
486 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
487 long len
; /* list/vector length */
489 if (SCM_NULL_OR_NIL_P (items
))
492 if (scm_is_pair (items
))
494 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
495 return scm_merge_list_step (&items
, cmp
, less
, len
);
497 else if (scm_is_vector (items
))
499 scm_t_array_handle temp_handle
, vec_handle
;
500 SCM temp
, *temp_elts
, *vec_elts
;
504 vec_elts
= scm_vector_writable_elements (items
, &vec_handle
,
506 temp
= scm_c_make_vector (len
, SCM_UNDEFINED
);
507 temp_elts
= scm_vector_writable_elements (temp
, &temp_handle
,
510 scm_merge_vector_step (vec_elts
, temp_elts
, cmp
, less
, 0, len
-1, inc
);
515 SCM_WRONG_TYPE_ARG (1, items
);
520 SCM_DEFINE (scm_stable_sort
, "stable-sort", 2, 0, 0,
521 (SCM items
, SCM less
),
522 "Sort the sequence @var{items}, which may be a list or a\n"
523 "vector. @var{less} is used for comparing the sequence elements.\n"
524 "This is a stable sort.")
525 #define FUNC_NAME s_scm_stable_sort
527 if (scm_is_pair (items
))
528 return scm_stable_sort_x (scm_list_copy (items
), less
);
529 else if (scm_is_vector (items
))
530 return scm_stable_sort_x (scm_vector_copy (items
), less
);
532 SCM_WRONG_TYPE_ARG (1, items
);
537 SCM_DEFINE (scm_sort_list_x
, "sort-list!", 2, 0, 0,
538 (SCM items
, SCM less
),
539 "Sort the list @var{items}, using @var{less} for comparing the\n"
540 "list elements. The sorting is destructive, that means that the\n"
541 "input list is modified to produce the sorted result.\n"
542 "This is a stable sort.")
543 #define FUNC_NAME s_scm_sort_list_x
545 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
548 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
549 return scm_merge_list_step (&items
, cmp
, less
, len
);
554 SCM_DEFINE (scm_sort_list
, "sort-list", 2, 0, 0,
555 (SCM items
, SCM less
),
556 "Sort the list @var{items}, using @var{less} for comparing the\n"
557 "list elements. This is a stable sort.")
558 #define FUNC_NAME s_scm_sort_list
560 const scm_t_trampoline_2 cmp
= compare_function (less
, 2, FUNC_NAME
);
563 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
564 items
= scm_list_copy (items
);
565 return scm_merge_list_step (&items
, cmp
, less
, len
);
573 #include "libguile/sort.x"
575 scm_add_feature ("sort");