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"
16 /* XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graph,xbt,"Graph"); */
21 xbt_graph_t xbt_graph_new_graph(const char *name, unsigned short int directed,
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,const char *name, 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,const char *name,
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);