| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
module type S = sig |
| 12 |
|
| 13 |
type node |
| 14 |
module NodeSet : Set.S with type elt = node |
| 15 |
|
| 16 |
type ('edge,'info,'cdata) t |
| 17 |
|
| 18 |
val empty : ('e,'i,'d) t |
| 19 |
|
| 20 |
val add_edge : ('e,'i,'d) t -> node -> 'e -> node -> ('e,'i,'d) t |
| 21 |
val from_node : ('e,'i,'d) t -> node -> (node * 'e) list |
| 22 |
val mem : ('e,'i,'d) t -> node -> bool |
| 23 |
val del_edge : ('e,'i,'d) t -> node -> node -> ('e,'i,'d) t |
| 24 |
val del_nodes : ('e,'i,'d) t -> NodeSet.t -> ('e,'i,'d) t |
| 25 |
val all_nodes : ('e,'i,'d) t -> NodeSet.t |
| 26 |
|
| 27 |
val get_info : ('e,'i,'d) t -> node -> 'i option |
| 28 |
val set_info : ('e,'i,'d) t -> node -> 'i -> ('e,'i,'d) t |
| 29 |
val clear_info : ('e,'i,'d) t -> node -> ('e,'i,'d) t |
| 30 |
|
| 31 |
module Property : |
| 32 |
sig |
| 33 |
type 'd t |
| 34 |
val equal : 'd t -> 'd t -> bool |
| 35 |
val compare : 'd t -> 'd t -> int |
| 36 |
val to_string : 'd t -> string |
| 37 |
val data : 'd t -> 'd |
| 38 |
val having_it : 'd t -> NodeSet.t |
| 39 |
end |
| 40 |
|
| 41 |
val create_property : ('e,'i,'d) t -> node list -> 'd -> ('e,'i,'d) t |
| 42 |
val property_of : ('e,'i,'d) t -> node -> 'd Property.t list |
| 43 |
val del_property : ('e,'i,'d) t -> 'd Property.t -> ('e,'i,'d) t |
| 44 |
|
| 45 |
val iter : ('e,'i,'d) t -> |
| 46 |
(node -> 'd Property.t list -> 'i option -> |
| 47 |
(node * 'e) list -> unit) -> unit |
| 48 |
|
| 49 |
end |
| 50 |
|
| 51 |
module Make(OT : Map.OrderedType) = struct |
| 52 |
|
| 53 |
module NodeSet = Set.Make(OT) |
| 54 |
|
| 55 |
module Property = |
| 56 |
struct |
| 57 |
type 'd t = { data : 'd; uid : int; having_it : NodeSet.t } |
| 58 |
let equal { uid = i1 } { uid = i2 } = Int.equal i1 i2 |
| 59 |
let compare { uid = i1 } { uid = i2 } = Int.compare i1 i2 |
| 60 |
let to_string { uid = i } = string_of_int i |
| 61 |
let data { data = d } = d |
| 62 |
let having_it { having_it } = having_it |
| 63 |
end |
| 64 |
|
| 65 |
type node = OT.t |
| 66 |
|
| 67 |
module NodeMap = CMap.Make(OT) |
| 68 |
|
| 69 |
type ('edge,'info,'data) t = { |
| 70 |
graph : (node * 'edge) list NodeMap.t; |
| 71 |
properties : 'data Property.t list NodeMap.t; |
| 72 |
infos : 'info NodeMap.t; |
| 73 |
} |
| 74 |
|
| 75 |
let empty = { |
| 76 |
graph = NodeMap.empty; |
| 77 |
properties = NodeMap.empty; |
| 78 |
infos = NodeMap.empty; |
| 79 |
} |
| 80 |
|
| 81 |
let mem { graph } id = NodeMap.mem id graph |
| 82 |
|
| 83 |
let add_edge dag from trans dest = { dag with |
| 84 |
graph = |
| 85 |
try NodeMap.modify from (fun _ arcs -> (dest, trans) :: arcs) dag.graph |
| 86 |
with Not_found -> NodeMap.add from [dest, trans] dag.graph } |
| 87 |
|
| 88 |
let from_node { graph } id = NodeMap.find id graph |
| 89 |
|
| 90 |
let del_edge dag id tgt = { dag with |
| 91 |
graph = |
| 92 |
try |
| 93 |
let modify _ arcs = |
| 94 |
let filter (d, _) = OT.compare d tgt <> 0 in |
| 95 |
List.filter filter arcs |
| 96 |
in |
| 97 |
NodeMap.modify id modify dag.graph |
| 98 |
with Not_found -> dag.graph } |
| 99 |
|
| 100 |
let del_nodes dag s = { |
| 101 |
infos = NodeMap.filter (fun n _ -> not(NodeSet.mem n s)) dag.infos; |
| 102 |
properties = NodeMap.filter (fun n _ -> not(NodeSet.mem n s)) dag.properties; |
| 103 |
graph = NodeMap.filter (fun n l -> |
| 104 |
let drop = NodeSet.mem n s in |
| 105 |
if not drop then |
| 106 |
assert(List.for_all (fun (n',_) -> not(NodeSet.mem n' s)) l); |
| 107 |
not drop) |
| 108 |
dag.graph } |
| 109 |
|
| 110 |
let map_add_list k v m = |
| 111 |
try |
| 112 |
let l = NodeMap.find k m in |
| 113 |
NodeMap.add k (v::l) m |
| 114 |
with Not_found -> NodeMap.add k [v] m |
| 115 |
|
| 116 |
let clid = ref 1 |
| 117 |
let create_property dag l data = |
| 118 |
incr clid; |
| 119 |
let having_it = List.fold_right NodeSet.add l NodeSet.empty in |
| 120 |
let property = { Property.data; uid = !clid; having_it } in |
| 121 |
{ dag with properties = |
| 122 |
List.fold_right (fun x ps -> map_add_list x property ps) l dag.properties } |
| 123 |
|
| 124 |
let property_of dag id = |
| 125 |
try NodeMap.find id dag.properties |
| 126 |
with Not_found -> [] |
| 127 |
|
| 128 |
let del_property dag c = |
| 129 |
{ dag with properties = |
| 130 |
NodeMap.filter (fun _ cl -> cl <> []) |
| 131 |
(NodeMap.map (fun cl -> |
| 132 |
List.filter (fun c' -> |
| 133 |
not (Property.equal c' c)) cl) |
| 134 |
dag.properties) } |
| 135 |
|
| 136 |
let get_info dag id = |
| 137 |
try Some (NodeMap.find id dag.infos) |
| 138 |
with Not_found -> None |
| 139 |
|
| 140 |
let set_info dag id info = { dag with infos = NodeMap.add id info dag.infos } |
| 141 |
|
| 142 |
let clear_info dag id = { dag with infos = NodeMap.remove id dag.infos } |
| 143 |
|
| 144 |
let iter dag f = |
| 145 |
NodeMap.iter (fun k v -> f k (property_of dag k) (get_info dag k) v) dag.graph |
| 146 |
|
| 147 |
let all_nodes dag = NodeMap.domain dag.graph |
| 148 |
|
| 149 |
end |
| 150 |
|