# HG changeset patch # User haftmann # Date 1128530308 -7200 # Node ID 10319da54a47af48f27c21c012d50a6667e371d4 # Parent e3cd31bc2e409b820ddbd2390169c5abc7708978 added merge, tuned diff -r e3cd31bc2e40 -r 10319da54a47 src/Pure/General/alist.ML --- a/src/Pure/General/alist.ML Wed Oct 05 14:01:32 2005 +0200 +++ b/src/Pure/General/alist.ML Wed Oct 05 18:38:28 2005 +0200 @@ -9,6 +9,7 @@ signature ALIST = sig + exception DUP val lookup: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option val defined: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool val update: ('a * 'a -> bool) -> ('a * 'b) @@ -20,12 +21,23 @@ val map_entry: ('a * 'b -> bool) -> 'a -> ('c -> 'c) -> ('b * 'c) list -> ('b * 'c) list val make: ('a -> 'b) -> 'a list -> ('a * 'b) list + val merge: ('a * 'a -> bool) -> ('b * 'b -> bool) + -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception DUP*) val find: ('a * 'b -> bool) -> ('c * 'b) list -> 'a -> 'c list end; structure AList: ALIST = struct +fun find_index eq xs key = + let + fun find [] _ = 0 + | find ((key', value)::xs) i = + if eq (key, key') + then i + else find xs (i+1); + in find xs 1 end; + fun lookup _ [] _ = NONE | lookup eq ((key, value)::xs) key' = if eq (key', key) then SOME value @@ -35,30 +47,44 @@ | defined eq ((key, value)::xs) key' = eq (key', key) orelse defined eq xs key'; -fun update eq (key, value) xs = +fun update eq (x as (key, value)) xs = let - fun upd ((x as (key', _))::xs) = - if eq (key, key') - then (key, value)::xs - else x :: upd xs - in if defined eq xs key then upd xs else (key, value)::xs end; + val i = find_index eq xs key; + fun upd 1 (_::xs) = (x::xs) + | upd i (x::xs) = x :: upd (i-1) xs; + in if i = 0 then x::xs else upd i xs end; fun default eq (key, value) xs = if defined eq xs key then xs else (key, value)::xs; fun delete eq key xs = let - fun del ((x as (key', _))::xs) = - if eq (key, key') then xs - else x :: del xs; - in if defined eq xs key then del xs else xs end; + val i = find_index eq xs key; + fun del 1 (_::xs) = xs + | del i (x::xs) = x :: del (i-1) xs; + in if i = 0 then xs else del i xs end; + +fun map_entry eq key f xs = + let + val i = find_index eq xs key; + fun mapp 1 ((x as (key, value))::xs) = (key, f value) :: xs + | mapp i (x::xs) = x :: mapp (i-1) xs; + in if i = 0 then xs else mapp i xs end; -fun map_entry eq key' f xs = +exception DUP; + +fun merge eq_key eq_val (xs, ys) = let - fun mapp ((x as (key, value))::xs) = - if eq (key', key) then (key, f value)::xs - else x :: mapp xs - in if defined eq xs key' then mapp xs else xs end; + fun add (x as (key, value)) ys = + case lookup eq_key ys key + of NONE => update eq_key x ys + | SOME value' => + if eq_val (value, value') + then ys + else raise DUP; + in + fold_rev add xs ys + end; fun make keyfun = let fun keypair x = (x, keyfun x) @@ -67,7 +93,7 @@ fun find eq [] _ = [] | find eq ((key, value) :: xs) value' = let - val values = find eq xs value' + val values = find eq xs value'; in if eq (value', value) then key :: values else values end; end;