1.1 --- a/src/Pure/General/alist.ML Wed Oct 05 14:01:32 2005 +0200
1.2 +++ b/src/Pure/General/alist.ML Wed Oct 05 18:38:28 2005 +0200
1.3 @@ -9,6 +9,7 @@
1.4
1.5 signature ALIST =
1.6 sig
1.7 + exception DUP
1.8 val lookup: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> 'c option
1.9 val defined: ('a * 'b -> bool) -> ('b * 'c) list -> 'a -> bool
1.10 val update: ('a * 'a -> bool) -> ('a * 'b)
1.11 @@ -20,12 +21,23 @@
1.12 val map_entry: ('a * 'b -> bool) -> 'a -> ('c -> 'c)
1.13 -> ('b * 'c) list -> ('b * 'c) list
1.14 val make: ('a -> 'b) -> 'a list -> ('a * 'b) list
1.15 + val merge: ('a * 'a -> bool) -> ('b * 'b -> bool)
1.16 + -> ('a * 'b) list * ('a * 'b) list -> ('a * 'b) list (*exception DUP*)
1.17 val find: ('a * 'b -> bool) -> ('c * 'b) list -> 'a -> 'c list
1.18 end;
1.19
1.20 structure AList: ALIST =
1.21 struct
1.22
1.23 +fun find_index eq xs key =
1.24 + let
1.25 + fun find [] _ = 0
1.26 + | find ((key', value)::xs) i =
1.27 + if eq (key, key')
1.28 + then i
1.29 + else find xs (i+1);
1.30 + in find xs 1 end;
1.31 +
1.32 fun lookup _ [] _ = NONE
1.33 | lookup eq ((key, value)::xs) key' =
1.34 if eq (key', key) then SOME value
1.35 @@ -35,30 +47,44 @@
1.36 | defined eq ((key, value)::xs) key' =
1.37 eq (key', key) orelse defined eq xs key';
1.38
1.39 -fun update eq (key, value) xs =
1.40 +fun update eq (x as (key, value)) xs =
1.41 let
1.42 - fun upd ((x as (key', _))::xs) =
1.43 - if eq (key, key')
1.44 - then (key, value)::xs
1.45 - else x :: upd xs
1.46 - in if defined eq xs key then upd xs else (key, value)::xs end;
1.47 + val i = find_index eq xs key;
1.48 + fun upd 1 (_::xs) = (x::xs)
1.49 + | upd i (x::xs) = x :: upd (i-1) xs;
1.50 + in if i = 0 then x::xs else upd i xs end;
1.51
1.52 fun default eq (key, value) xs =
1.53 if defined eq xs key then xs else (key, value)::xs;
1.54
1.55 fun delete eq key xs =
1.56 let
1.57 - fun del ((x as (key', _))::xs) =
1.58 - if eq (key, key') then xs
1.59 - else x :: del xs;
1.60 - in if defined eq xs key then del xs else xs end;
1.61 + val i = find_index eq xs key;
1.62 + fun del 1 (_::xs) = xs
1.63 + | del i (x::xs) = x :: del (i-1) xs;
1.64 + in if i = 0 then xs else del i xs end;
1.65
1.66 -fun map_entry eq key' f xs =
1.67 +fun map_entry eq key f xs =
1.68 let
1.69 - fun mapp ((x as (key, value))::xs) =
1.70 - if eq (key', key) then (key, f value)::xs
1.71 - else x :: mapp xs
1.72 - in if defined eq xs key' then mapp xs else xs end;
1.73 + val i = find_index eq xs key;
1.74 + fun mapp 1 ((x as (key, value))::xs) = (key, f value) :: xs
1.75 + | mapp i (x::xs) = x :: mapp (i-1) xs;
1.76 + in if i = 0 then xs else mapp i xs end;
1.77 +
1.78 +exception DUP;
1.79 +
1.80 +fun merge eq_key eq_val (xs, ys) =
1.81 + let
1.82 + fun add (x as (key, value)) ys =
1.83 + case lookup eq_key ys key
1.84 + of NONE => update eq_key x ys
1.85 + | SOME value' =>
1.86 + if eq_val (value, value')
1.87 + then ys
1.88 + else raise DUP;
1.89 + in
1.90 + fold_rev add xs ys
1.91 + end;
1.92
1.93 fun make keyfun =
1.94 let fun keypair x = (x, keyfun x)
1.95 @@ -67,7 +93,7 @@
1.96 fun find eq [] _ = []
1.97 | find eq ((key, value) :: xs) value' =
1.98 let
1.99 - val values = find eq xs value'
1.100 + val values = find eq xs value';
1.101 in if eq (value', value) then key :: values else values end;
1.102
1.103 end;