3 /* a generic graph library. */
5 /* Copyright (c) 2006 Darina Dimitrova, Arnaud Legrand.
6 All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/sysdep.h"
13 #include "xbt/graph.h"
14 #include "graph_private.h"
15 #include "xbt/graphxml_parse.h"
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graph,xbt,"Graph");
22 xbt_graph_t xbt_graph_new_graph(unsigned short int directed, void *data)
24 xbt_graph_t graph=NULL;
25 graph=xbt_new0(struct xbt_graph,1);
26 graph->directed=directed;
28 graph->nodes= xbt_dynar_new(sizeof(xbt_node_t), free);
29 graph->edges= xbt_dynar_new(sizeof(xbt_edge_t), free);
34 xbt_node_t xbt_graph_new_node(xbt_graph_t g, void *data)
37 node=xbt_new0(struct xbt_node,1);
39 node->in=xbt_dynar_new(sizeof(xbt_node_t), free);
40 node->out=xbt_dynar_new(sizeof(xbt_node_t), free);
41 xbt_dynar_push(g->nodes,node);
47 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,
48 xbt_node_t src, xbt_node_t dst, void *data)
52 edge=xbt_new0(struct xbt_edge,1);
53 xbt_dynar_push(src->out,edge);
54 xbt_dynar_push(dst->in,edge);
60 xbt_dynar_push(src->in,edge);
61 xbt_dynar_push(dst->out,edge);
63 xbt_dynar_push(g->edges,edge);
70 * \param l poor victim
72 * Free the graph structure.
74 void xbt_graph_free_graph(xbt_graph_t g,
75 void node_free_function(void * ptr),
76 void edge_free_function(void * ptr),
77 void graph_free_function(void * ptr))
83 xbt_dynar_foreach(g->nodes,cursor,node)
84 xbt_graph_remove_node(g,node,node_free_function);
86 xbt_assert0(!xbt_dynar_length(g->edges),
87 "Damnit, there are some remaining edges!");
89 xbt_dynar_foreach(g->edges,cursor,edge)
90 xbt_graph_remove_edge(g,edge,edge_free_function);
92 xbt_dynar_free(&(g->nodes));
93 xbt_dynar_free(&(g->edges));
95 /* void xbt_dynar_free(g->edges); */
101 void xbt_graph_remove_node(xbt_graph_t g, xbt_node_t n, void free_function(void * ptr))
104 xbt_node_t node=NULL;
106 if ((free_function)&&(n->data))
107 free_function(n->data);
108 xbt_dynar_free_container(&(n->in));
109 xbt_dynar_free_container(&(n->out));
110 xbt_dynar_foreach(g->nodes,cursor,node)
113 xbt_dynar_cursor_rm(g->nodes,&cursor);
119 void xbt_graph_remove_edge(xbt_graph_t g, xbt_edge_t e, void free_function(void * ptr))
122 xbt_edge_t edge=NULL;
123 xbt_node_t node=NULL;
124 xbt_node_t temp=NULL;
126 if ((free_function)&&(e->data))
127 free_function(e->data);
128 xbt_dynar_foreach(g->nodes,cursor,node)
131 xbt_dynar_pop(node->out,temp);
133 xbt_dynar_pop(node->in,temp);
138 xbt_dynar_foreach(g->nodes,cursor,node)
141 xbt_dynar_pop(node->in,temp);
143 xbt_dynar_pop(node->out,temp);
147 xbt_dynar_foreach(g->edges,cursor,edge)
150 xbt_dynar_cursor_rm(g->edges,&cursor);
156 static xbt_graph_t parsed_graph=NULL;
158 static void __parse_graph_begin(void) {
161 static void __parse_graph_end(void) {
164 static void __parse_node(void) {
165 DEBUG1("<node label=\"%s\"/>",A_node_name);
167 static void __parse_edge(void) {
168 DEBUG2("<edge source=\"%s\" target=\"%s\"/>",A_edge_source,A_edge_target);
171 xbt_graph_t xbt_graph_read(const char *filename)
173 xbt_graph_t graph = xbt_graph_new_graph(1,NULL);
175 parsed_graph = graph;
177 xbt_graph_parse_reset_parser();
179 STag_graph_fun = __parse_graph_begin;
180 ETag_graph_fun = __parse_graph_end;
181 ETag_node_fun = __parse_node;
182 ETag_edge_fun = __parse_edge;
184 xbt_graph_parse_open(filename);
185 xbt_assert1((!xbt_graph_parse()),"Parse error in %s",filename);
186 xbt_graph_parse_close();