Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
constructors and destructors for graph structures
[simgrid.git] / src / xbt / graph.c
1 #include "xbt/sysdep.h"
2 #include "xbt/log.h"
3 #include "xbt/graph.h"
4 #include "graph_private.h"
5
6 /* XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graph,xbt,"GRAPH"); */
7
8 /** Constructor
9  * \return a new graph
10  */
11 xbt_graph_t xbt_graph_new_graph(const char *name, unsigned short int directed,
12                                void *data)
13 {
14   xbt_graph_t graph=NULL;
15   graph=xbt_new0(struct xbt_graph,1);
16   graph->directed=directed;
17   graph->data=data;
18   graph->nodes= xbt_dynar_new(sizeof(xbt_node_t), free);
19   graph->edges= xbt_dynar_new(sizeof(xbt_edge_t), free);
20
21   return graph;
22 }
23
24 xbt_node_t xbt_graph_new_node(xbt_graph_t g,const char *name, void *data)
25 {
26   xbt_node_t node=NULL;
27   node=xbt_new0(struct xbt_node,1);
28   node->data=data;
29   node->in=xbt_dynar_new(sizeof(xbt_node_t), free);
30   node->out=xbt_dynar_new(sizeof(xbt_node_t), free);
31   xbt_dynar_push(g->nodes,node);  
32
33   return node;
34 }
35
36
37 xbt_edge_t xbt_graph_new_edge(xbt_graph_t g,const char *name,
38                              xbt_node_t src, xbt_node_t dst, void *data)
39 {
40   xbt_edge_t edge=NULL;
41
42   edge=xbt_new0(struct xbt_edge,1);
43   xbt_dynar_push(src->out,edge); 
44   xbt_dynar_push(dst->in,edge); 
45   edge->data=data;
46   edge->src=src;
47   edge->dst=dst;
48   if(!g->directed) 
49    {
50      xbt_dynar_push(src->in,edge); 
51      xbt_dynar_push(dst->out,edge); 
52    }
53  xbt_dynar_push(g->edges,edge);
54
55  return edge; 
56 }
57
58
59 /** Destructor
60  * \param l poor victim
61  *
62  * Free the graph structure. 
63  */
64 void xbt_graph_free_graph(xbt_graph_t g,
65                           void node_free_function(void * ptr),
66                           void edge_free_function(void * ptr),
67                           void graph_free_function(void * ptr))
68 {
69   int cursor; 
70   xbt_node_t node=NULL;
71   xbt_edge_t edge=NULL; 
72
73   xbt_dynar_foreach(g->nodes,cursor,node)
74     xbt_graph_remove_node(g,node,node_free_function);
75
76   /* if xbt_dynar_size(g->edges)>0 SCREAM! */
77
78   xbt_dynar_foreach(g->edges,cursor,edge)
79       xbt_graph_remove_edge(g,edge,edge_free_function); 
80
81   xbt_dynar_free(&(g->nodes));
82   xbt_dynar_free(&(g->edges));
83       
84 /*  void xbt_dynar_free(g->edges); */
85
86
87   return;
88 }
89
90 void xbt_graph_remove_node(xbt_graph_t g, xbt_node_t n, void free_function(void * ptr))
91 {
92   int cursor; 
93   xbt_node_t node=NULL;
94
95   if ((free_function)&&(n->data))
96    free_function(n->data);
97   xbt_dynar_free_container(&(n->in));
98   xbt_dynar_free_container(&(n->out));
99   xbt_dynar_foreach(g->nodes,cursor,node)
100     {
101       if (node==n)
102         xbt_dynar_cursor_rm(g->nodes,&cursor);   
103       
104     }
105   return;
106    
107 }
108 void xbt_graph_remove_edge(xbt_graph_t g, xbt_edge_t e, void free_function(void * ptr))
109 {
110    int cursor=0; 
111    xbt_edge_t edge=NULL;
112    xbt_node_t node=NULL;
113    xbt_node_t temp=NULL;
114    
115    if ((free_function)&&(e->data))
116      free_function(e->data);
117    xbt_dynar_foreach(g->nodes,cursor,node)
118      {
119        if (node==e->src)
120          xbt_dynar_pop(node->out,temp);
121        if (g->directed)
122          xbt_dynar_pop(node->in,temp);
123         
124      }
125    node=NULL;
126    cursor=0;
127    xbt_dynar_foreach(g->nodes,cursor,node)
128      {
129         if (node==e->dst)
130           xbt_dynar_pop(node->in,temp);
131         if (g->directed)
132           xbt_dynar_pop(node->out,temp);
133         
134      }
135    cursor=0;
136    xbt_dynar_foreach(g->edges,cursor,edge)
137      if (edge==e) 
138        {
139          xbt_dynar_cursor_rm(g->edges,&cursor);
140          break;
141        }
142    
143 }