src/Pure/General/graph.ML
changeset 46614 165886a4fe64
parent 46613 74331911375d
child 46658 f11400424782
equal deleted inserted replaced
46613:74331911375d 46614:165886a4fe64
    23   val is_empty: 'a T -> bool
    23   val is_empty: 'a T -> bool
    24   val keys: 'a T -> key list
    24   val keys: 'a T -> key list
    25   val dest: 'a T -> (key * key list) list
    25   val dest: 'a T -> (key * key list) list
    26   val get_first: (key * ('a * (Keys.T * Keys.T)) -> 'b option) -> 'a T -> 'b option
    26   val get_first: (key * ('a * (Keys.T * Keys.T)) -> 'b option) -> 'a T -> 'b option
    27   val fold: (key * ('a * (Keys.T * Keys.T)) -> 'b -> 'b) -> 'a T -> 'b -> 'b
    27   val fold: (key * ('a * (Keys.T * Keys.T)) -> 'b -> 'b) -> 'a T -> 'b -> 'b
    28   val subgraph: (key -> bool) -> 'a T -> 'a T
       
    29   val get_entry: 'a T -> key -> key * ('a * (Keys.T * Keys.T))        (*exception UNDEF*)
    28   val get_entry: 'a T -> key -> key * ('a * (Keys.T * Keys.T))        (*exception UNDEF*)
    30   val get_node: 'a T -> key -> 'a                                     (*exception UNDEF*)
    29   val get_node: 'a T -> key -> 'a                                     (*exception UNDEF*)
    31   val map_node: key -> ('a -> 'a) -> 'a T -> 'a T
    30   val map_node: key -> ('a -> 'a) -> 'a T -> 'a T
    32   val map_node_yield: key -> ('a -> 'b * 'a) -> 'a T -> 'b * 'a T
    31   val map_node_yield: key -> ('a -> 'b * 'a) -> 'a T -> 'b * 'a T
    33   val map: (key -> 'a -> 'b) -> 'a T -> 'b T
    32   val map: (key -> 'a -> 'b) -> 'a T -> 'b T
    47   val del_nodes: key list -> 'a T -> 'a T                             (*exception UNDEF*)
    46   val del_nodes: key list -> 'a T -> 'a T                             (*exception UNDEF*)
    48   val del_node: key -> 'a T -> 'a T                                   (*exception UNDEF*)
    47   val del_node: key -> 'a T -> 'a T                                   (*exception UNDEF*)
    49   val is_edge: 'a T -> key * key -> bool
    48   val is_edge: 'a T -> key * key -> bool
    50   val add_edge: key * key -> 'a T -> 'a T                             (*exception UNDEF*)
    49   val add_edge: key * key -> 'a T -> 'a T                             (*exception UNDEF*)
    51   val del_edge: key * key -> 'a T -> 'a T                             (*exception UNDEF*)
    50   val del_edge: key * key -> 'a T -> 'a T                             (*exception UNDEF*)
       
    51   val restrict: (key -> bool) -> 'a T -> 'a T
    52   val merge: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T                 (*exception DUP*)
    52   val merge: ('a * 'a -> bool) -> 'a T * 'a T -> 'a T                 (*exception DUP*)
    53   val join: (key -> 'a * 'a -> 'a) (*exception DUP/SAME*) ->
    53   val join: (key -> 'a * 'a -> 'a) (*exception DUP/SAME*) ->
    54     'a T * 'a T -> 'a T                                               (*exception DUP*)
    54     'a T * 'a T -> 'a T                                               (*exception DUP*)
    55   val irreducible_paths: 'a T -> key * key -> key list list
    55   val irreducible_paths: 'a T -> key * key -> key list list
    56   val all_paths: 'a T -> key * key -> key list list
    56   val all_paths: 'a T -> key * key -> key list list
   113 fun dest (Graph tab) = map (fn (x, (_, (_, succs))) => (x, Keys.dest succs)) (Table.dest tab);
   113 fun dest (Graph tab) = map (fn (x, (_, (_, succs))) => (x, Keys.dest succs)) (Table.dest tab);
   114 
   114 
   115 fun get_first f (Graph tab) = Table.get_first f tab;
   115 fun get_first f (Graph tab) = Table.get_first f tab;
   116 fun fold_graph f (Graph tab) = Table.fold f tab;
   116 fun fold_graph f (Graph tab) = Table.fold f tab;
   117 
   117 
   118 fun subgraph P G =
       
   119   let
       
   120     fun subg (k, (i, (preds, succs))) =
       
   121       if P k then Table.update (k, (i, (Keys.filter P preds, Keys.filter P succs)))
       
   122       else I;
       
   123   in Graph (fold_graph subg G Table.empty) end;
       
   124 
       
   125 fun get_entry (Graph tab) x =
   118 fun get_entry (Graph tab) x =
   126   (case Table.lookup_key tab x of
   119   (case Table.lookup_key tab x of
   127     SOME entry => entry
   120     SOME entry => entry
   128   | NONE => raise UNDEF x);
   121   | NONE => raise UNDEF x);
   129 
   122 
   206     Graph (tab
   199     Graph (tab
   207       |> Table.delete x
   200       |> Table.delete x
   208       |> Keys.fold (del_adjacent apsnd) preds
   201       |> Keys.fold (del_adjacent apsnd) preds
   209       |> Keys.fold (del_adjacent apfst) succs)
   202       |> Keys.fold (del_adjacent apfst) succs)
   210   end;
   203   end;
       
   204 
       
   205 fun restrict pred G =
       
   206   fold_graph (fn (x, _) => not (pred x) ? del_node x) G G;
   211 
   207 
   212 
   208 
   213 (* edges *)
   209 (* edges *)
   214 
   210 
   215 fun is_edge G (x, y) = Keys.member (imm_succs G x) y handle UNDEF _ => false;
   211 fun is_edge G (x, y) = Keys.member (imm_succs G x) y handle UNDEF _ => false;