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