View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2006-2023, University of Amsterdam
    7                              VU University Amsterdam
    8                              SWI-Prolog Solutions b.v.
    9    All rights reserved.
   10
   11    Redistribution and use in source and binary forms, with or without
   12    modification, are permitted provided that the following conditions
   13    are met:
   14
   15    1. Redistributions of source code must retain the above copyright
   16       notice, this list of conditions and the following disclaimer.
   17
   18    2. Redistributions in binary form must reproduce the above copyright
   19       notice, this list of conditions and the following disclaimer in
   20       the documentation and/or other materials provided with the
   21       distribution.
   22
   23    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   24    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   25    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   26    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   27    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   28    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   29    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   30    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   31    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   32    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   33    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   34    POSSIBILITY OF SUCH DAMAGE.
   35*/
   36
   37:- module(sort,
   38          [ predsort/3,                 % :Compare, +List, -Sorted
   39            locale_sort/2               % +ListOfAtoms, -Sorted
   40          ]).   41
   42:- set_prolog_flag(generate_debug_info, false).   43
   44:- meta_predicate
   45    predsort(3, +, -).              % 3: Delta, Left, Right
   46
   47%!  predsort(:Compare, +List, -Sorted) is det.
   48%
   49%   Sorts similar to sort/2, but determines   the order of two terms
   50%   by calling Compare(-Delta, +E1, +E2). This call must unify Delta
   51%   with one of <, > or =.  If built-in predicate compare/3 is used,
   52%   the result is the same as sort/2 (but sort/2 is built using more
   53%   low-level primitives and is considerably faster).
   54%
   55%   @see keysort/2 provides a more portable way to sort on
   56%   arbitrary keys that is usually faster.  The predicate sort/4
   57%   can sort on specific arguments of compound terms or key values
   58%   of dicts.
   59
   60predsort(P, L, R) :-
   61    '$skip_list'(N, L, Tail),
   62    (   Tail == []
   63    ->  predsort(P, N, L, _, R1),
   64        R = R1
   65    ;   must_be(L, list)
   66    ).
   67
   68predsort(P, 2, [X1, X2|L], L, R) :-
   69    !,
   70    call(P, Delta, X1, X2),
   71    sort2(Delta, X1, X2, R).
   72predsort(_, 1, [X|L], L, [X]) :- !.
   73predsort(_, 0, L, L, []) :- !.
   74predsort(P, N, L1, L3, R) :-
   75    N1 is N // 2,
   76    plus(N1, N2, N),
   77    predsort(P, N1, L1, L2, R1),
   78    predsort(P, N2, L2, L3, R2),
   79    predmerge(P, R1, R2, R).
   80
   81sort2(<, X1, X2, [X1, X2]).
   82sort2(=, X1, _,  [X1]).
   83sort2(>, X1, X2, [X2, X1]).
   84
   85predmerge(_, [], R, R) :- !.
   86predmerge(_, R, [], R) :- !.
   87predmerge(P, [H1|T1], [H2|T2], Result) :-
   88    call(P, Delta, H1, H2),
   89    !,
   90    predmerge(Delta, P, H1, H2, T1, T2, Result).
   91
   92predmerge(>, P, H1, H2, T1, T2, [H2|R]) :-
   93    predmerge(P, [H1|T1], T2, R).
   94predmerge(=, P, H1, _, T1, T2, [H1|R]) :-
   95    predmerge(P, T1, T2, R).
   96predmerge(<, P, H1, H2, T1, T2, [H1|R]) :-
   97    predmerge(P, T1, [H2|T2], R).
   98
   99%!  locale_sort(+List, -Sorted) is det.
  100%
  101%   Sort a list of atoms using the current locale.
  102%
  103%   @param List     List of atoms
  104%   @param Sorted   Sorted atoms.
  105
  106locale_sort(List, Sorted) :-
  107    collation_keys(List, Keyed),
  108    keysort(Keyed, KeySorted),
  109    unkey(KeySorted, Sorted).
  110
  111collation_keys([], []).
  112collation_keys([H|T0], [K-H|T]) :-
  113    collation_key(H, K),
  114    collation_keys(T0, T).
  115
  116
  117unkey([], []).
  118unkey([_-H|T0], [H|T]) :-
  119    unkey(T0, T)