src/Pure/General/table.ML
changeset 18946 ce65d2d2e0c2
parent 18007 2c9005459d15
child 19031 0059b5b195a2
equal deleted inserted replaced
18945:0b15863018a8 18946:ce65d2d2e0c2
    46   val delete: key -> 'a table -> 'a table                              (*exception UNDEF*)
    46   val delete: key -> 'a table -> 'a table                              (*exception UNDEF*)
    47   val delete_safe: key -> 'a table -> 'a table
    47   val delete_safe: key -> 'a table -> 'a table
    48   val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool
    48   val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool
    49   val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table    (*exception DUP*)
    49   val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table    (*exception DUP*)
    50   val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table
    50   val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table
    51   val lookup_multi: 'a list table -> key -> 'a list
    51   val lookup_list: 'a list table -> key -> 'a list
    52   val update_multi: (key * 'a) -> 'a list table -> 'a list table
    52   val update_list: (key * 'a) -> 'a list table -> 'a list table
    53   val remove_multi: ('b * 'a -> bool) -> key * 'b -> 'a list table -> 'a list table
    53   val remove_list: ('b * 'a -> bool) -> key * 'b -> 'a list table -> 'a list table
    54   val make_multi: (key * 'a) list -> 'a list table
    54   val make_list: (key * 'a) list -> 'a list table
    55   val dest_multi: 'a list table -> (key * 'a) list
    55   val dest_list: 'a list table -> (key * 'a) list
    56   val merge_multi: ('a * 'a -> bool) ->
    56   val merge_list: ('a * 'a -> bool) ->
    57     'a list table * 'a list table -> 'a list table                     (*exception DUPS*)
       
    58   val merge_multi': ('a * 'a -> bool) ->
       
    59     'a list table * 'a list table -> 'a list table                     (*exception DUPS*)
    57     'a list table * 'a list table -> 'a list table                     (*exception DUPS*)
    60 end;
    58 end;
    61 
    59 
    62 functor TableFun(Key: KEY): TABLE =
    60 functor TableFun(Key: KEY): TABLE =
    63 struct
    61 struct
   363 fun merge eq tabs = join (fn _ => fn (y, x) => if eq (y, x) then SOME y else NONE) tabs;
   361 fun merge eq tabs = join (fn _ => fn (y, x) => if eq (y, x) then SOME y else NONE) tabs;
   364 
   362 
   365 
   363 
   366 (* tables with multiple entries per key *)
   364 (* tables with multiple entries per key *)
   367 
   365 
   368 fun lookup_multi tab key = if_none (lookup tab key) [];
   366 fun lookup_list tab key = these (lookup tab key);
   369 fun update_multi (key, x) tab = modify key (fn NONE => [x] | SOME xs => x :: xs) tab;
   367 fun update_list (key, x) tab = modify key (fn NONE => [x] | SOME xs => x :: xs) tab;
   370 
   368 
   371 fun remove_multi eq (key, x) tab =
   369 fun remove_list eq (key, x) tab =
   372   map_entry key (fn xs => (case Library.remove eq x xs of [] => raise UNDEF key | ys => ys)) tab
   370   map_entry key (fn xs => (case Library.remove eq x xs of [] => raise UNDEF key | ys => ys)) tab
   373   handle UNDEF _ => delete key tab;
   371   handle UNDEF _ => delete key tab;
   374 
   372 
   375 fun make_multi args = fold_rev update_multi args empty;
   373 fun make_list args = fold_rev update_list args empty;
   376 fun dest_multi tab = List.concat (map (fn (key, xs) => map (pair key) xs) (dest tab));
   374 fun dest_list tab = List.concat (map (fn (key, xs) => map (pair key) xs) (dest tab));
   377 fun merge_multi eq = join (fn _ => fn (xs, xs') => SOME (gen_merge_lists eq xs xs'));
   375 fun merge_list eq = join (fn _ => SOME o Library.merge eq);
   378 fun merge_multi' eq = join (fn _ => fn (xs, xs') => SOME (gen_merge_lists' eq xs xs'));
       
   379 
   376 
   380 
   377 
   381 (*final declarations of this structure!*)
   378 (*final declarations of this structure!*)
   382 fun map f = map_table (K f);
   379 fun map f = map_table (K f);
   383 val map' = map_table;
   380 val map' = map_table;