View source with raw comments or as raw
    1/*  Part of CHR (Constraint Handling Rules)
    2
    3    Author:        Tom Schrijvers
    4    E-mail:        Tom.Schrijvers@cs.kuleuven.be
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2005-2011, K.U. Leuven
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(listmap,
   36	[
   37		listmap_empty/1,
   38		listmap_lookup/3,
   39		listmap_insert/4,
   40		listmap_remove/3,
   41		listmap_merge/5
   42	]).   43
   44listmap_empty([]).
   45
   46listmap_lookup([K-V|R],Key,Q) :-
   47	( Key == K ->
   48		Q = V
   49	;
   50		Key @> K,
   51		listmap_lookup(R,Key,Q)
   52	).
   53
   54listmap_insert([],Key,Value,[Key-Value]).
   55listmap_insert([P|R],Key,Value,ML) :-
   56	P = K-_,
   57	compare(C,Key,K),
   58	( C == (=) ->
   59		ML = [K-Value|R]
   60	; C == (<) ->
   61		ML = [Key-Value,P|R]
   62	;
   63		ML = [P|Tail],
   64		listmap_insert(R,Key,Value,Tail)
   65	).
   66
   67listmap_merge(ML1,ML2,F,G,ML) :-
   68	( ML1 == [] ->
   69		ML = ML2
   70	; ML2 == [] ->
   71		ML = ML1
   72	;
   73		ML1 = [P1|R1], P1 = K1-V1,
   74		ML2 = [P2|R2], P2 = K2-V2,
   75		compare(C,K1,K2),
   76		( C == (=) ->
   77			Call =.. [F,V1,V2,NV],
   78			call(Call),
   79			ML = [K1-NV|Tail],
   80			listmap_merge(R1,R2,F,G,Tail)
   81		; C == (<) ->
   82			Call =.. [G,V1,NV],
   83			call(Call),
   84			ML = [K1-NV|Tail],
   85			listmap_merge(R1,ML2,F,G,Tail)
   86		;
   87			Call =.. [G,V2,NV],
   88			call(Call),
   89			ML = [K2-NV|Tail],
   90			listmap_merge(ML1,R2,F,G,Tail)
   91		)
   92	).
   93		
   94	
   95listmap_remove([],_,[]).
   96listmap_remove([P|R],Key,NLM) :-
   97	P = K-_,
   98	compare(C,Key,K),
   99	( C == (=) ->
  100		NLM = R
  101	; C == (<) ->
  102		NLM = [P|R]
  103	;
  104		NLM = [P|Tail],
  105		listmap_remove(R,Key,Tail)
  106	)