| 1 |
(* |
| 2 |
* Single-entry-multiple exit view. Add a new exit node to graph view. |
| 3 |
* |
| 4 |
* All exit edges are now directed into the exit node. |
| 5 |
* The unique node with entry edges becomes the new entry node. |
| 6 |
* |
| 7 |
* -- Allen |
| 8 |
*) |
| 9 |
|
| 10 |
signature SINGLE_ENTRY_MULTIPLE_EXIT_VIEW = |
| 11 |
sig |
| 12 |
|
| 13 |
exception NoEntry |
| 14 |
exception MultipleEntries of Graph.node_id list |
| 15 |
|
| 16 |
val SEME : { exit : 'n Graph.node } -> |
| 17 |
('n,'e,'g) Graph.graph -> ('n,'e,'g) Graph.graph |
| 18 |
end |
| 19 |
|
| 20 |
structure SingleEntryMultipleExit : SINGLE_ENTRY_MULTIPLE_EXIT_VIEW = |
| 21 |
struct |
| 22 |
|
| 23 |
structure G = Graph |
| 24 |
|
| 25 |
exception NoEntry |
| 26 |
exception MultipleEntries of Graph.node_id list |
| 27 |
|
| 28 |
fun SEME {exit=exit as (EXIT,ex)} (G.GRAPH G) = |
| 29 |
let fun readonly _ = raise G.Readonly |
| 30 |
fun get_nodes() = exit :: #nodes G () |
| 31 |
fun order() = #order G () + 1 |
| 32 |
fun capacity() = Int.max(EXIT+1,#capacity G ()) |
| 33 |
fun findEntry() = |
| 34 |
case #entries G () of |
| 35 |
[ENTRY] => ENTRY |
| 36 |
| [] => raise NoEntry |
| 37 |
| nodes => raise MultipleEntries nodes |
| 38 |
val ENTRY = findEntry() |
| 39 |
fun exitEdges n = map (fn (i,j,e) => (i,EXIT,e)) (#exit_edges G n) |
| 40 |
fun out_edges n = exitEdges n @ #out_edges G n |
| 41 |
fun in_edges n = if n = EXIT then exitEdges n |
| 42 |
else #in_edges G n |
| 43 |
fun get_edges() = List.concat(map (fn (n,_) => out_edges n) |
| 44 |
(get_nodes ())) |
| 45 |
fun get_succ n = map #2 (out_edges n) |
| 46 |
fun get_pred n = map #1 (in_edges n) |
| 47 |
fun has_edge(i,j) = List.exists (fn (_,k,_) => j = k) (out_edges i) |
| 48 |
fun has_node n = n = EXIT orelse #has_node G n |
| 49 |
fun node_info n = if n = EXIT then ex else #node_info G n |
| 50 |
fun forall_nodes f = (#forall_nodes G f; f exit) |
| 51 |
fun forall_edges f = app f (get_edges()) |
| 52 |
fun entries() = [ENTRY] |
| 53 |
fun exits() = [EXIT] |
| 54 |
in |
| 55 |
G.GRAPH |
| 56 |
{ name = #name G, |
| 57 |
graph_info = #graph_info G, |
| 58 |
new_id = #new_id G, |
| 59 |
add_node = readonly, |
| 60 |
add_edge = readonly, |
| 61 |
remove_node = readonly, |
| 62 |
set_in_edges = readonly, |
| 63 |
set_out_edges = readonly, |
| 64 |
set_entries = readonly, |
| 65 |
set_exits = readonly, |
| 66 |
garbage_collect = #garbage_collect G, |
| 67 |
nodes = get_nodes, |
| 68 |
edges = get_edges, |
| 69 |
order = order, |
| 70 |
size = #size G, |
| 71 |
capacity = capacity, |
| 72 |
out_edges = out_edges, |
| 73 |
in_edges = in_edges, |
| 74 |
succ = get_succ, |
| 75 |
pred = get_pred, |
| 76 |
has_edge = has_edge, |
| 77 |
has_node = has_node, |
| 78 |
node_info = node_info, |
| 79 |
entries = entries, |
| 80 |
exits = exits, |
| 81 |
entry_edges = #entry_edges G, |
| 82 |
exit_edges = #exit_edges G, |
| 83 |
forall_nodes = forall_nodes, |
| 84 |
forall_edges = forall_edges |
| 85 |
} |
| 86 |
end |
| 87 |
|
| 88 |
end |
| 89 |
|