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-2015, 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(pairs,
   37          [ pairs_keys_values/3,
   38            pairs_values/2,
   39            pairs_keys/2,
   40            group_pairs_by_key/2,
   41            transpose_pairs/2,
   42            map_list_to_pairs/3
   43          ]).   44
   45/** <module> Operations on key-value lists
   46
   47This module implements common operations on  Key-Value lists, also known
   48as  _Pairs_.  Pairs  have  great  practical  value,  especially  due  to
   49keysort/2 and the library assoc.pl.
   50
   51This library is based  on  discussion  in  the  SWI-Prolog  mailinglist,
   52including specifications from Quintus and a  library proposal by Richard
   53O'Keefe.
   54
   55@see    keysort/2, library(assoc)
   56@author Jan Wielemaker
   57*/
   58
   59%!  pairs_keys_values(?Pairs, ?Keys, ?Values) is det.
   60%
   61%   True if Keys holds the keys of Pairs and Values the values.
   62%
   63%   Deterministic if any argument is instantiated   to a finite list
   64%   and the others are either free or  finite lists. All three lists
   65%   are in the same order.
   66%
   67%   @see pairs_values/2 and pairs_keys/2.
   68
   69pairs_keys_values(Pairs, Keys, Values) :-
   70    (   nonvar(Pairs) ->
   71        pairs_keys_values_(Pairs, Keys, Values)
   72    ;   nonvar(Keys) ->
   73        keys_values_pairs(Keys, Values, Pairs)
   74    ;   values_keys_pairs(Values, Keys, Pairs)
   75    ).
   76
   77pairs_keys_values_([], [], []).
   78pairs_keys_values_([K-V|Pairs], [K|Keys], [V|Values]) :-
   79    pairs_keys_values_(Pairs, Keys, Values).
   80
   81keys_values_pairs([], [], []).
   82keys_values_pairs([K|Ks], [V|Vs], [K-V|Pairs]) :-
   83    keys_values_pairs(Ks, Vs, Pairs).
   84
   85values_keys_pairs([], [], []).
   86values_keys_pairs([V|Vs], [K|Ks], [K-V|Pairs]) :-
   87    values_keys_pairs(Vs, Ks, Pairs).
   88
   89%!  pairs_values(+Pairs, -Values) is det.
   90%
   91%   Remove the keys  from  a  list   of  Key-Value  pairs.  Same  as
   92%   pairs_keys_values(Pairs, _, Values)
   93
   94pairs_values([], []).
   95pairs_values([_-V|T0], [V|T]) :-
   96    pairs_values(T0, T).
   97
   98
   99%!  pairs_keys(+Pairs, -Keys) is det.
  100%
  101%   Remove the values  from  a  list   of  Key-Value  pairs.  Same  as
  102%   pairs_keys_values(Pairs, Keys, _)
  103
  104pairs_keys([], []).
  105pairs_keys([K-_|T0], [K|T]) :-
  106    pairs_keys(T0, T).
  107
  108
  109%!  group_pairs_by_key(+Pairs, -Joined:list(Key-Values)) is det.
  110%
  111%   Group  values  with  equivalent  (==/2)  consecutive  keys.  For
  112%   example:
  113%
  114%     ==
  115%     ?- group_pairs_by_key([a-2, a-1, b-4, a-3], X).
  116%
  117%     X = [a-[2,1], b-[4], a-[3]]
  118%     ==
  119%
  120%   Sorting the list of pairs before grouping   can be used to group
  121%   _all_ values associated with a  key.   For  example, finding all
  122%   values associated with the largest key:
  123%
  124%     ==
  125%     ?- sort(1, @>=, [a-1, b-2, c-3, a-4, a-5, c-6], Ps),
  126%        group_pairs_by_key(Ps, [K-Vs|_]).
  127%     K = c,
  128%     Vs = [3, 6].
  129%     ==
  130%
  131%   In this example, sorting by key   only (first argument of sort/4
  132%   is 1) ensures that the order of  the values in the original list
  133%   of pairs is maintained.
  134%
  135%   @param  Pairs   Key-Value list
  136%   @param  Joined  List of Key-Group, where Group is the
  137%                   list of Values associated with equivalent
  138%                   consecutive Keys in the same order as they
  139%                   appear in Pairs.
  140
  141group_pairs_by_key([], []).
  142group_pairs_by_key([M-N|T0], [M-[N|TN]|T]) :-
  143    same_key(M, T0, TN, T1),
  144    group_pairs_by_key(T1, T).
  145
  146same_key(M0, [M-N|T0], [N|TN], T) :-
  147    M0 == M,
  148    !,
  149    same_key(M, T0, TN, T).
  150same_key(_, L, [], L).
  151
  152
  153%!  transpose_pairs(+Pairs, -Transposed) is det.
  154%
  155%   Swap Key-Value to Value-Key. The resulting  list is sorted using
  156%   keysort/2 on the new key.
  157
  158transpose_pairs(Pairs, Transposed) :-
  159    flip_pairs(Pairs, Flipped),
  160    keysort(Flipped, Transposed).
  161
  162flip_pairs([], []).
  163flip_pairs([Key-Val|Pairs], [Val-Key|Flipped]) :-
  164    flip_pairs(Pairs, Flipped).
  165
  166
  167%!  map_list_to_pairs(:Function, +List, -Keyed)
  168%
  169%   Create a Key-Value list by mapping each element of List.
  170%   For example, if we have a list of lists we can create a
  171%   list of Length-List using
  172%
  173%   ==
  174%           map_list_to_pairs(length, ListOfLists, Pairs),
  175%   ==
  176
  177:- meta_predicate
  178    map_list_to_pairs(2, +, -).  179
  180map_list_to_pairs(Function, List, Pairs) :-
  181    map_list_to_pairs2(List, Function, Pairs).
  182
  183map_list_to_pairs2([], _, []).
  184map_list_to_pairs2([H|T0], Pred, [K-H|T]) :-
  185    call(Pred, H, K),
  186    map_list_to_pairs2(T0, Pred, T)