Sfoglia il codice sorgente

20171209 src/mesh.sml (signature MESH als interface fuer die Verwendung von structure Mesh durch graph2prom.sml)

Altlast 7 anni fa
parent
commit
e1f444cb0f
3 ha cambiato i file con 46 aggiunte e 26 eliminazioni
  1. 1 1
      src/Makefile.in
  2. 6 9
      src/graph2prom.sml
  3. 39 16
      src/mesh.sml

+ 1 - 1
src/Makefile.in

@@ -19,7 +19,7 @@ nodes2prom.%%HEAP_SUFFIX%%: promconfig.sml nodes2prom.cm nodes2prom.sml json_lib
 graph2prom: graph2prom.%%HEAP_SUFFIX%%
 	heap2exec graph2prom.%%HEAP_SUFFIX%% graph2prom || { ../conf/substitute.sh runheap.in $@ && chmod +x $@ ; }
 
-graph2prom.%%HEAP_SUFFIX%%: promconfig.sml graph2prom.cm graph2prom.sml json_lib
+graph2prom.%%HEAP_SUFFIX%%: promconfig.sml graph2prom.cm mesh.sml graph2prom.sml json_lib
 	ml-build graph2prom.cm Main.main graph2prom
 
 json-pp: json-pp.%%HEAP_SUFFIX%%

+ 6 - 9
src/graph2prom.sml

@@ -7,14 +7,12 @@ struct
 	structure JP = JSONParser
 	structure JU = JSONUtil
 
+	structure M : MESH = Mesh
 	structure PC = PromConfig
+
 	val link_prefix = PC.link_prefix
 	val mesh_prefix = PC.mesh_prefix
 
-	structure M = Mesh
-
-	(* val timestamp = LargeInt.toString (Time.toMilliseconds (Time.now ())) *)
-
 	val newline = String.str #"\n"
 	val link_header =
 		"# HELP " ^ link_prefix ^ " link quality between two nodes" ^ newline ^
@@ -128,11 +126,10 @@ struct
 				      handle exn => (json_handler "get_links" exn ; raise Fail "get_links")
 		     val links_extract = map extract_link links_json
 		     val links_prom = map (link2prom nodes_id_vector) links_extract
-		     val graph = foldl (fn (l, g) => M.add_link g (#source l, #target l))
-					M.empty
-					links_extract
-		     val meshes = M.Map.listItems (#meshes graph)
-		     val meshes_prom = map (mesh2prom nodes_id_vector) meshes
+		     val graph = M.links2graph (map (fn l => (#source l, #target l))
+						    links_extract)
+		     val meshes_prom = map (mesh2prom nodes_id_vector)
+					   (M.Map.listItems (#meshes graph))
 		 in (print link_header ;
 		     print mesh_header ;
 		     app print links_prom ;

+ 39 - 16
src/mesh.sml

@@ -1,10 +1,24 @@
 (* Mesh = Zusammenhangskomponente eines einfachen Graphen *)
 
-(* XXX Unterschiedliche key Typen (statt beide = int)
-       für die Maps node2meshindex und meshes
-       um Verwechslungen durch den type checker zu erkennen *)
+signature MESH =
+sig
+	structure Key	  : ORD_KEY
+	structure Set	  : ORD_SET
+	structure PairSet : ORD_SET
+	structure Map	  : ORD_MAP
 
-structure Mesh =
+	type mesh	= { nodes : Set.set,
+		 	    edges : PairSet.set }
+	type meshes	= mesh Map.map
+	type graph	= { meshes : meshes,
+			    node2meshindex : Key.ord_key Map.map }
+
+	val empty_graph	: graph
+	val add_link	: graph -> int * int -> graph
+	val links2graph	: (int * int) list -> graph
+end
+
+structure Mesh : MESH =
 struct
 
 	functor PairKeyFn ( Key : ORD_KEY ) : ORD_KEY =
@@ -21,27 +35,32 @@ struct
 			end
 	end
 
+	(* XXX Unterschiedliche key Typen (statt beide = int)
+	       fuer die Maps node2meshindex und meshes
+	       damit der type checker Verwechslungen erkennen kann *)
+
 	structure Key =
 	struct
-		type ord_key = Int.int
-		val compare = Int.compare
-		val minkey = 0
-		fun nextkey k = k + 1
+		type ord_key	= Int.int
+		val compare	= Int.compare
+		val minkey	= 0
+		fun nextkey k	= k + 1
 	end
 
 	structure PairKey = PairKeyFn (Key)
 
-	structure Set = SplaySetFn (Key)
+	structure Set	  = SplaySetFn (Key)
 	structure PairSet = SplaySetFn (PairKey)
-	structure Map = SplayMapFn (Key)
+	structure Map	  = SplayMapFn (Key)
 
-	type mesh = { nodes : Set.set,
-		      edges : PairSet.set }
-	type meshes = mesh Map.map
-	type graph = { meshes : meshes,
-		       node2meshindex : Key.ord_key Map.map }
+	type mesh	= { nodes : Set.set,
+		 	    edges : PairSet.set }
+	type meshes	= mesh Map.map
+	type graph	= { meshes : meshes,
+			    node2meshindex : Key.ord_key Map.map }
 
-	val empty = { meshes = Map.empty, node2meshindex = Map.empty }
+	val empty_graph	= { meshes = Map.empty,
+			    node2meshindex = Map.empty }
 
 	fun n2im (g : graph) n = 
 		case Map.find (#node2meshindex g, n)
@@ -114,4 +133,8 @@ struct
 				   then (n2, n1) else e
 		in add_link' g (na, nb)
 		end
+
+	fun links2graph links = foldl (fn (e, g) => add_link g e)
+				      empty_graph
+				      links
 end