| 1 |
(* |
| 2 |
* Start-stop adaptor. Add a new start/stop node to a graph view. |
| 3 |
* |
| 4 |
* -- Allen |
| 5 |
*) |
| 6 |
|
| 7 |
signature START_STOP_VIEW = |
| 8 |
sig |
| 9 |
|
| 10 |
val start_stop_view |
| 11 |
: {start : 'n Graph.node, |
| 12 |
stop : 'n Graph.node, |
| 13 |
edges : 'e Graph.edge list |
| 14 |
} -> ('n,'e,'g) Graph.graph -> |
| 15 |
('n,'e,'g) Graph.graph |
| 16 |
end |
| 17 |
|
| 18 |
structure StartStopView : START_STOP_VIEW = |
| 19 |
struct |
| 20 |
|
| 21 |
structure G = Graph |
| 22 |
|
| 23 |
fun start_stop_view {start=start as (START,x), |
| 24 |
stop=stop as (STOP,y), edges} (G.GRAPH G) = |
| 25 |
let fun readonly _ = raise G.Readonly |
| 26 |
fun get_nodes() = start :: stop :: #nodes G () |
| 27 |
fun order() = #order G () + 2 |
| 28 |
fun size() = #size G () + 1 |
| 29 |
fun capacity() = Int.max(START+1,Int.max(STOP+1,#capacity G ())) |
| 30 |
fun exit_to_stop n = map (fn (i,_,e) => (i,STOP,e)) (#exit_edges G n) |
| 31 |
fun entry_to_start n = map (fn (_,j,e) => (START,j,e)) (#entry_edges G n) |
| 32 |
fun out_edges n = (if n = START then edges else []) |
| 33 |
@ (exit_to_stop n) @ #out_edges G n |
| 34 |
fun in_edges n = (if n = STOP then edges else []) |
| 35 |
@ (entry_to_start n) @ #in_edges G n |
| 36 |
fun get_edges() = List.concat(map (fn (n,_) => out_edges n) |
| 37 |
(get_nodes ())) |
| 38 |
fun get_succ n = map #2 (out_edges n) |
| 39 |
fun get_pred n = map #1 (in_edges n) |
| 40 |
fun has_edge(i,j) = List.exists (fn (_,k,_) => j = k) (out_edges i) |
| 41 |
fun has_node n = n = START orelse n = STOP orelse #has_node G n |
| 42 |
fun node_info n = if n = START then x |
| 43 |
else if n = STOP then y |
| 44 |
else #node_info G n |
| 45 |
fun entries() = [START] |
| 46 |
fun exits() = [STOP] |
| 47 |
fun entry_edges n = [] |
| 48 |
fun exit_edges n = [] |
| 49 |
fun forall_nodes f = app f (get_nodes()) |
| 50 |
fun forall_edges f = app f (get_edges()) |
| 51 |
in |
| 52 |
G.GRAPH |
| 53 |
{ name = #name G, |
| 54 |
graph_info = #graph_info G, |
| 55 |
new_id = readonly, |
| 56 |
add_node = readonly, |
| 57 |
add_edge = readonly, |
| 58 |
remove_node = readonly, |
| 59 |
set_in_edges = readonly, |
| 60 |
set_out_edges = readonly, |
| 61 |
set_entries = readonly, |
| 62 |
set_exits = readonly, |
| 63 |
garbage_collect = #garbage_collect G, |
| 64 |
nodes = get_nodes, |
| 65 |
edges = get_edges, |
| 66 |
order = order, |
| 67 |
size = size, |
| 68 |
capacity = capacity, |
| 69 |
out_edges = out_edges, |
| 70 |
in_edges = in_edges, |
| 71 |
succ = get_succ, |
| 72 |
pred = get_pred, |
| 73 |
has_edge = has_edge, |
| 74 |
has_node = has_node, |
| 75 |
node_info = node_info, |
| 76 |
entries = entries, |
| 77 |
exits = exits, |
| 78 |
entry_edges = entry_edges, |
| 79 |
exit_edges = exit_edges, |
| 80 |
forall_nodes = forall_nodes, |
| 81 |
forall_edges = forall_edges |
| 82 |
} |
| 83 |
end |
| 84 |
|
| 85 |
|
| 86 |
end |
| 87 |
|