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"
69 SCM_DEFINE (scm_restricted_vector_sort_x
, "restricted-vector-sort!", 4, 0, 0,
70 (SCM vec
, SCM less
, SCM startpos
, SCM endpos
),
71 "Sort the vector @var{vec}, using @var{less} for comparing\n"
72 "the vector elements. @var{startpos} (inclusively) and\n"
73 "@var{endpos} (exclusively) delimit\n"
74 "the range of the vector which gets sorted. The return value\n"
76 #define FUNC_NAME s_scm_restricted_vector_sort_x
78 size_t vlen
, spos
, len
;
80 scm_t_array_handle handle
;
83 velts
= scm_vector_writable_elements (vec
, &handle
, &vlen
, &vinc
);
84 spos
= scm_to_unsigned_integer (startpos
, 0, vlen
);
85 len
= scm_to_unsigned_integer (endpos
, spos
, vlen
) - spos
;
88 quicksort1 (velts
+ spos
*vinc
, len
, less
);
90 quicksort (velts
+ spos
*vinc
, len
, vinc
, less
);
92 scm_array_handle_release (&handle
);
94 return SCM_UNSPECIFIED
;
99 /* (sorted? sequence less?)
100 * is true when sequence is a list (x0 x1 ... xm) or a vector #(x0 ... xm)
101 * such that for all 1 <= i <= m,
102 * (not (less? (list-ref list i) (list-ref list (- i 1)))). */
103 SCM_DEFINE (scm_sorted_p
, "sorted?", 2, 0, 0,
104 (SCM items
, SCM less
),
105 "Return @code{#t} iff @var{items} is a list or a vector such that\n"
106 "for all 1 <= i <= m, the predicate @var{less} returns true when\n"
107 "applied to all elements i - 1 and i")
108 #define FUNC_NAME s_scm_sorted_p
110 long len
, j
; /* list/vector length, temp j */
111 SCM item
, rest
; /* rest of items loop variable */
113 if (SCM_NULL_OR_NIL_P (items
))
116 if (scm_is_pair (items
))
118 len
= scm_ilength (items
); /* also checks that it's a pure list */
119 SCM_ASSERT_RANGE (1, items
, len
>= 0);
123 item
= SCM_CAR (items
);
124 rest
= SCM_CDR (items
);
128 if (scm_is_true (scm_call_2 (less
, SCM_CAR (rest
), item
)))
132 item
= SCM_CAR (rest
);
133 rest
= SCM_CDR (rest
);
141 scm_t_array_handle handle
;
145 SCM result
= SCM_BOOL_T
;
147 elts
= scm_vector_elements (items
, &handle
, &len
, &inc
);
149 for (i
= 1; i
< len
; i
++, elts
+= inc
)
151 if (scm_is_true (scm_call_2 (less
, elts
[inc
], elts
[0])))
158 scm_array_handle_release (&handle
);
169 takes two lists a and b such that (sorted? a less?) and (sorted? b less?)
170 and returns a new list in which the elements of a and b have been stably
171 interleaved so that (sorted? (merge a b less?) less?).
172 Note: this does _not_ accept vectors. */
173 SCM_DEFINE (scm_merge
, "merge", 3, 0, 0,
174 (SCM alist
, SCM blist
, SCM less
),
175 "Merge two already sorted lists into one.\n"
176 "Given two lists @var{alist} and @var{blist}, such that\n"
177 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)},\n"
178 "return a new list in which the elements of @var{alist} and\n"
179 "@var{blist} have been stably interleaved so that\n"
180 "@code{(sorted? (merge alist blist less?) less?)}.\n"
181 "Note: this does _not_ accept vectors.")
182 #define FUNC_NAME s_scm_merge
186 if (SCM_NULL_OR_NIL_P (alist
))
188 else if (SCM_NULL_OR_NIL_P (blist
))
192 long alen
, blen
; /* list lengths */
195 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist
, alen
);
196 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist
, blen
);
197 if (scm_is_true (scm_call_2 (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
199 build
= scm_cons (SCM_CAR (blist
), SCM_EOL
);
200 blist
= SCM_CDR (blist
);
205 build
= scm_cons (SCM_CAR (alist
), SCM_EOL
);
206 alist
= SCM_CDR (alist
);
210 while ((alen
> 0) && (blen
> 0))
213 if (scm_is_true (scm_call_2 (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
215 SCM_SETCDR (last
, scm_cons (SCM_CAR (blist
), SCM_EOL
));
216 blist
= SCM_CDR (blist
);
221 SCM_SETCDR (last
, scm_cons (SCM_CAR (alist
), SCM_EOL
));
222 alist
= SCM_CDR (alist
);
225 last
= SCM_CDR (last
);
227 if ((alen
> 0) && (blen
== 0))
228 SCM_SETCDR (last
, alist
);
229 else if ((alen
== 0) && (blen
> 0))
230 SCM_SETCDR (last
, blist
);
238 scm_merge_list_x (SCM alist
, SCM blist
,
239 long alen
, long blen
,
244 if (SCM_NULL_OR_NIL_P (alist
))
246 else if (SCM_NULL_OR_NIL_P (blist
))
250 if (scm_is_true (scm_call_2 (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
253 blist
= SCM_CDR (blist
);
259 alist
= SCM_CDR (alist
);
263 while ((alen
> 0) && (blen
> 0))
266 if (scm_is_true (scm_call_2 (less
, SCM_CAR (blist
), SCM_CAR (alist
))))
268 SCM_SETCDR (last
, blist
);
269 blist
= SCM_CDR (blist
);
274 SCM_SETCDR (last
, alist
);
275 alist
= SCM_CDR (alist
);
278 last
= SCM_CDR (last
);
280 if ((alen
> 0) && (blen
== 0))
281 SCM_SETCDR (last
, alist
);
282 else if ((alen
== 0) && (blen
> 0))
283 SCM_SETCDR (last
, blist
);
286 } /* scm_merge_list_x */
289 SCM_DEFINE (scm_merge_x
, "merge!", 3, 0, 0,
290 (SCM alist
, SCM blist
, SCM less
),
291 "Takes two lists @var{alist} and @var{blist} such that\n"
292 "@code{(sorted? alist less?)} and @code{(sorted? blist less?)} and\n"
293 "returns a new list in which the elements of @var{alist} and\n"
294 "@var{blist} have been stably interleaved so that\n"
295 " @code{(sorted? (merge alist blist less?) less?)}.\n"
296 "This is the destructive variant of @code{merge}\n"
297 "Note: this does _not_ accept vectors.")
298 #define FUNC_NAME s_scm_merge_x
300 if (SCM_NULL_OR_NIL_P (alist
))
302 else if (SCM_NULL_OR_NIL_P (blist
))
306 long alen
, blen
; /* list lengths */
307 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (1, alist
, alen
);
308 SCM_VALIDATE_NONEMPTYLIST_COPYLEN (2, blist
, blen
);
309 return scm_merge_list_x (alist
, blist
, alen
, blen
, less
);
315 /* This merge sort algorithm is same as slib's by Richard A. O'Keefe.
316 The algorithm is stable. We also tried to use the algorithm used by
317 scsh's merge-sort but that algorithm showed to not be stable, even
318 though it claimed to be.
321 scm_merge_list_step (SCM
* seq
, SCM less
, long n
)
329 a
= scm_merge_list_step (seq
, less
, mid
);
330 b
= scm_merge_list_step (seq
, less
, n
- mid
);
331 return scm_merge_list_x (a
, b
, mid
, n
- mid
, less
);
336 SCM rest
= SCM_CDR (*seq
);
337 SCM x
= SCM_CAR (*seq
);
338 SCM y
= SCM_CAR (SCM_CDR (*seq
));
339 *seq
= SCM_CDR (rest
);
340 SCM_SETCDR (rest
, SCM_EOL
);
341 if (scm_is_true (scm_call_2 (less
, y
, x
)))
344 SCM_SETCAR (rest
, x
);
352 SCM_SETCDR (p
, SCM_EOL
);
357 } /* scm_merge_list_step */
360 SCM_DEFINE (scm_sort_x
, "sort!", 2, 0, 0,
361 (SCM items
, SCM less
),
362 "Sort the sequence @var{items}, which may be a list or a\n"
363 "vector. @var{less} is used for comparing the sequence\n"
364 "elements. The sorting is destructive, that means that the\n"
365 "input sequence is modified to produce the sorted result.\n"
366 "This is not a stable sort.")
367 #define FUNC_NAME s_scm_sort_x
369 long len
; /* list/vector length */
370 if (SCM_NULL_OR_NIL_P (items
))
373 if (scm_is_pair (items
))
375 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
376 return scm_merge_list_step (&items
, less
, len
);
378 else if (scm_is_vector (items
))
380 scm_restricted_vector_sort_x (items
,
383 scm_vector_length (items
));
387 SCM_WRONG_TYPE_ARG (1, items
);
392 SCM_DEFINE (scm_sort
, "sort", 2, 0, 0,
393 (SCM items
, SCM less
),
394 "Sort the sequence @var{items}, which may be a list or a\n"
395 "vector. @var{less} is used for comparing the sequence\n"
396 "elements. This is not a stable sort.")
397 #define FUNC_NAME s_scm_sort
399 if (SCM_NULL_OR_NIL_P (items
))
402 if (scm_is_pair (items
))
403 return scm_sort_x (scm_list_copy (items
), less
);
404 else if (scm_is_vector (items
))
405 return scm_sort_x (scm_vector_copy (items
), less
);
407 SCM_WRONG_TYPE_ARG (1, items
);
413 scm_merge_vector_x (SCM
*vec
,
421 size_t it
; /* Index for temp vector */
422 size_t i1
= low
; /* Index for lower vector segment */
423 size_t i2
= mid
+ 1; /* Index for upper vector segment */
425 #define VEC(i) vec[(i)*inc]
427 /* Copy while both segments contain more characters */
428 for (it
= low
; (i1
<= mid
) && (i2
<= high
); ++it
)
430 if (scm_is_true (scm_call_2 (less
, VEC(i2
), VEC(i1
))))
431 temp
[it
] = VEC(i2
++);
433 temp
[it
] = VEC(i1
++);
437 /* Copy while first segment contains more characters */
439 temp
[it
++] = VEC(i1
++);
441 /* Copy while second segment contains more characters */
443 temp
[it
++] = VEC(i2
++);
445 /* Copy back from temp to vp */
446 for (it
= low
; it
<= high
; it
++)
449 } /* scm_merge_vector_x */
453 scm_merge_vector_step (SCM
*vec
,
462 size_t mid
= (low
+ high
) / 2;
464 scm_merge_vector_step (vec
, temp
, less
, low
, mid
, inc
);
465 scm_merge_vector_step (vec
, temp
, less
, mid
+1, high
, inc
);
466 scm_merge_vector_x (vec
, temp
, less
, low
, mid
, high
, inc
);
468 } /* scm_merge_vector_step */
471 SCM_DEFINE (scm_stable_sort_x
, "stable-sort!", 2, 0, 0,
472 (SCM items
, SCM less
),
473 "Sort the sequence @var{items}, which may be a list or a\n"
474 "vector. @var{less} is used for comparing the sequence elements.\n"
475 "The sorting is destructive, that means that the input sequence\n"
476 "is modified to produce the sorted result.\n"
477 "This is a stable sort.")
478 #define FUNC_NAME s_scm_stable_sort_x
480 long len
; /* list/vector length */
482 if (SCM_NULL_OR_NIL_P (items
))
485 if (scm_is_pair (items
))
487 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
488 return scm_merge_list_step (&items
, less
, len
);
490 else if (scm_is_vector (items
))
492 scm_t_array_handle temp_handle
, vec_handle
;
493 SCM temp
, *temp_elts
, *vec_elts
;
497 vec_elts
= scm_vector_writable_elements (items
, &vec_handle
,
499 temp
= scm_c_make_vector (len
, SCM_UNDEFINED
);
500 temp_elts
= scm_vector_writable_elements (temp
, &temp_handle
,
503 scm_merge_vector_step (vec_elts
, temp_elts
, less
, 0, len
-1, inc
);
505 scm_array_handle_release (&temp_handle
);
506 scm_array_handle_release (&vec_handle
);
511 SCM_WRONG_TYPE_ARG (1, items
);
516 SCM_DEFINE (scm_stable_sort
, "stable-sort", 2, 0, 0,
517 (SCM items
, SCM less
),
518 "Sort the sequence @var{items}, which may be a list or a\n"
519 "vector. @var{less} is used for comparing the sequence elements.\n"
520 "This is a stable sort.")
521 #define FUNC_NAME s_scm_stable_sort
523 if (SCM_NULL_OR_NIL_P (items
))
526 if (scm_is_pair (items
))
527 return scm_stable_sort_x (scm_list_copy (items
), less
);
528 else if (scm_is_vector (items
))
529 return scm_stable_sort_x (scm_vector_copy (items
), less
);
531 SCM_WRONG_TYPE_ARG (1, items
);
536 SCM_DEFINE (scm_sort_list_x
, "sort-list!", 2, 0, 0,
537 (SCM items
, SCM less
),
538 "Sort the list @var{items}, using @var{less} for comparing the\n"
539 "list elements. The sorting is destructive, that means that the\n"
540 "input list is modified to produce the sorted result.\n"
541 "This is a stable sort.")
542 #define FUNC_NAME s_scm_sort_list_x
546 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
547 return scm_merge_list_step (&items
, less
, len
);
552 SCM_DEFINE (scm_sort_list
, "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. This is a stable sort.")
556 #define FUNC_NAME s_scm_sort_list
560 SCM_VALIDATE_LIST_COPYLEN (1, items
, len
);
561 items
= scm_list_copy (items
);
562 return scm_merge_list_step (&items
, less
, len
);
570 #include "libguile/sort.x"
572 scm_add_feature ("sort");