[13] | 1 | /* |
---|
| 2 | * $Id: graph_s.c,v 1.4 2002/08/25 20:01:57 fabio Exp $ |
---|
| 3 | */ |
---|
| 4 | |
---|
| 5 | #include "graph_int.h" |
---|
| 6 | #include "graph_static_int.h" |
---|
| 7 | |
---|
| 8 | #define g_field(graph) ((g_field_t *) (graph)->user_data) |
---|
| 9 | |
---|
| 10 | graph_t * |
---|
| 11 | g_alloc_static(int ng, int nv, int ne) |
---|
| 12 | { |
---|
| 13 | graph_t *g; |
---|
| 14 | g_field_t *gf;; |
---|
| 15 | |
---|
| 16 | g = g_alloc(); |
---|
| 17 | gf = ALLOC(g_field_t,1); |
---|
| 18 | gf->num_g_slots = ng; |
---|
| 19 | gf->num_v_slots = nv; |
---|
| 20 | gf->num_e_slots = ne; |
---|
| 21 | gf->user_data = (gGeneric) ALLOC(gGeneric, ng); |
---|
| 22 | |
---|
| 23 | g->user_data = (gGeneric) gf; |
---|
| 24 | return(g); |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | void |
---|
| 28 | g_free_static( |
---|
| 29 | graph_t *g, |
---|
| 30 | void (*f_free_g)(gGeneric), |
---|
| 31 | void (*f_free_v)(gGeneric), |
---|
| 32 | void (*f_free_e)(gGeneric)) |
---|
| 33 | { |
---|
| 34 | vertex_t *v; |
---|
| 35 | edge_t *e; |
---|
| 36 | lsGen gen; |
---|
| 37 | lsGeneric junk; |
---|
| 38 | |
---|
| 39 | if (g == NIL(graph_t)) { |
---|
| 40 | return; |
---|
| 41 | } |
---|
| 42 | if (f_free_g != (void (*)(gGeneric)) NULL) { |
---|
| 43 | (*f_free_g)(g_field(g)->user_data); |
---|
| 44 | } |
---|
| 45 | FREE(g_field(g)->user_data); |
---|
| 46 | FREE(g->user_data); |
---|
| 47 | |
---|
| 48 | foreach_vertex(g,gen,v) { |
---|
| 49 | if (f_free_v != (void (*)(gGeneric)) NULL) { |
---|
| 50 | (*f_free_v)(v->user_data); |
---|
| 51 | } |
---|
| 52 | FREE(v->user_data); |
---|
| 53 | (void) lsDestroy(g_get_in_edges(v),(void (*)(lsGeneric)) NULL); |
---|
| 54 | (void) lsDestroy(g_get_out_edges(v),(void (*)(lsGeneric)) NULL); |
---|
| 55 | (void) lsRemoveItem(((vertex_t_int *) v)->handle,&junk); |
---|
| 56 | FREE(v); |
---|
| 57 | } |
---|
| 58 | foreach_edge(g,gen,e) { |
---|
| 59 | if (f_free_e != (void (*)(gGeneric)) NULL) { |
---|
| 60 | (*f_free_e)(e->user_data); |
---|
| 61 | } |
---|
| 62 | FREE(e->user_data); |
---|
| 63 | (void) lsRemoveItem(((edge_t_int *) e)->handle,&junk); |
---|
| 64 | FREE(e); |
---|
| 65 | } |
---|
| 66 | g_free(g,(void (*)(gGeneric)) NULL,(void (*)(gGeneric)) NULL, |
---|
| 67 | (void (*)(gGeneric)) NULL); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | static graph_t *theGraph; |
---|
| 71 | |
---|
| 72 | static gGeneric |
---|
| 73 | copy_v_slots(gGeneric user_data) |
---|
| 74 | { |
---|
| 75 | int i; |
---|
| 76 | int num_v_slots = g_field(theGraph)->num_v_slots; |
---|
| 77 | gGeneric *news = ALLOC(gGeneric,num_v_slots); |
---|
| 78 | |
---|
| 79 | for (i = 0; i < num_v_slots; i++) { |
---|
| 80 | news[i] = ((gGeneric *) user_data)[i]; |
---|
| 81 | } |
---|
| 82 | return((gGeneric) news); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | static gGeneric |
---|
| 86 | copy_e_slots(gGeneric user_data) |
---|
| 87 | { |
---|
| 88 | int i; |
---|
| 89 | int num_e_slots = g_field(theGraph)->num_e_slots; |
---|
| 90 | gGeneric *news = ALLOC(gGeneric,num_e_slots); |
---|
| 91 | |
---|
| 92 | for (i = 0; i < num_e_slots; i++) { |
---|
| 93 | news[i] = ((gGeneric *) user_data)[i]; |
---|
| 94 | } |
---|
| 95 | return((gGeneric) news); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | graph_t * |
---|
| 99 | g_dup_static( |
---|
| 100 | graph_t *g, |
---|
| 101 | gGeneric (*f_copy_g)(gGeneric), |
---|
| 102 | gGeneric (*f_copy_v)(gGeneric), |
---|
| 103 | gGeneric (*f_copy_e)(gGeneric)) |
---|
| 104 | { |
---|
| 105 | g_field_t *gf, *gf2; |
---|
| 106 | graph_t *g2; |
---|
| 107 | gGeneric *news; |
---|
| 108 | int i; |
---|
| 109 | |
---|
| 110 | if (f_copy_v == (gGeneric (*)(gGeneric)) NULL) { |
---|
| 111 | theGraph = g; |
---|
| 112 | f_copy_v = copy_v_slots; |
---|
| 113 | } |
---|
| 114 | if (f_copy_e == (gGeneric (*)(gGeneric)) NULL) { |
---|
| 115 | theGraph = g; |
---|
| 116 | f_copy_e = copy_e_slots; |
---|
| 117 | } |
---|
| 118 | g2 = g_dup(g,(gGeneric (*)(gGeneric)) NULL,f_copy_v,f_copy_e); |
---|
| 119 | if (g == NIL(graph_t)) { |
---|
| 120 | return(g2); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | gf = g_field(g); |
---|
| 124 | gf2 = ALLOC(g_field_t,1); |
---|
| 125 | gf2->num_g_slots = gf->num_g_slots; |
---|
| 126 | gf2->num_v_slots = gf->num_v_slots; |
---|
| 127 | gf2->num_e_slots = gf->num_e_slots; |
---|
| 128 | if (f_copy_g == (gGeneric (*)(gGeneric)) NULL) { |
---|
| 129 | news = ALLOC(gGeneric,gf->num_g_slots); |
---|
| 130 | for (i = gf->num_g_slots - 1; i >= 0; i--) { |
---|
| 131 | news[i] = ((gGeneric *) gf->user_data)[i]; |
---|
| 132 | } |
---|
| 133 | gf2->user_data = (gGeneric) news; |
---|
| 134 | } |
---|
| 135 | else { |
---|
| 136 | gf2->user_data = (*f_copy_g)(gf->user_data); |
---|
| 137 | } |
---|
| 138 | g2->user_data = (gGeneric) gf2; |
---|
| 139 | return(g2); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | void |
---|
| 144 | g_set_g_slot_static(graph_t *g, int i, gGeneric val) |
---|
| 145 | { |
---|
| 146 | if (g == NIL(graph_t)) { |
---|
| 147 | fail("g_set_g_slot_static: Null graph"); |
---|
| 148 | } |
---|
| 149 | ((gGeneric *) g_field(g)->user_data)[i] = val; |
---|
| 150 | return; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | gGeneric |
---|
| 155 | g_get_g_slot_static(graph_t *g, int i) |
---|
| 156 | { |
---|
| 157 | if (g == NIL(graph_t)) { |
---|
| 158 | fail("g_get_g_slot_static: Null graph"); |
---|
| 159 | } |
---|
| 160 | return ((gGeneric *) g_field(g)->user_data)[i]; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void |
---|
| 164 | g_copy_g_slots_static(graph_t *g1, graph_t *g2, gGeneric (*f_copy_g)(gGeneric)) |
---|
| 165 | { |
---|
| 166 | g_field_t *gf1,*gf2; |
---|
| 167 | gGeneric slots1,*slots2; |
---|
| 168 | int n; |
---|
| 169 | |
---|
| 170 | if (g1 == NIL(graph_t) || g2 == NIL(graph_t)) { |
---|
| 171 | fail("g_copy_g_slots_static: Null graph"); |
---|
| 172 | } |
---|
| 173 | gf1 = g_field(g1); |
---|
| 174 | gf2 = g_field(g2); |
---|
| 175 | n = gf1->num_g_slots; |
---|
| 176 | |
---|
| 177 | if (n != gf2->num_g_slots) { |
---|
| 178 | fail("g_copy_g_slots_static: Graphs have different numbers of slots"); |
---|
| 179 | } |
---|
| 180 | slots1 = gf1->user_data; |
---|
| 181 | slots2 = (gGeneric *) gf2->user_data; |
---|
| 182 | if (f_copy_g == (gGeneric (*)(gGeneric)) NULL) { |
---|
| 183 | for (n-- ; n >= 0; n--) { |
---|
| 184 | slots2[n] = ((gGeneric *) slots1)[n]; |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | else { |
---|
| 188 | FREE(slots2); |
---|
| 189 | gf2->user_data = (*f_copy_g)(slots1); |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | edge_t * |
---|
| 195 | g_add_edge_static(vertex_t *v1, vertex_t *v2) |
---|
| 196 | { |
---|
| 197 | edge_t *e; |
---|
| 198 | g_field_t *gf; |
---|
| 199 | |
---|
| 200 | if (v1 == NIL(vertex_t) || v2 == NIL(vertex_t)) { |
---|
| 201 | fail("g_add_edge_static: Null vertex"); |
---|
| 202 | } |
---|
| 203 | e = g_add_edge(v1, v2); |
---|
| 204 | gf = g_field(g_edge_graph(e)); |
---|
| 205 | e->user_data = (gGeneric) ALLOC(gGeneric, gf->num_e_slots); |
---|
| 206 | return(e); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | |
---|
| 210 | void |
---|
| 211 | g_delete_edge_static(edge_t *e, void (*f_free_e)(gGeneric)) |
---|
| 212 | { |
---|
| 213 | if (e == NIL(edge_t)) { |
---|
| 214 | fail("g_delete_edge_static: Null edge"); |
---|
| 215 | } |
---|
| 216 | if (f_free_e != (void (*)(gGeneric)) NULL) { |
---|
| 217 | (*f_free_e)(e->user_data); |
---|
| 218 | } |
---|
| 219 | FREE(e->user_data); |
---|
| 220 | g_delete_edge(e,(void (*)(gGeneric)) NULL); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | void |
---|
| 225 | g_set_e_slot_static(edge_t *e, int i, gGeneric val) |
---|
| 226 | { |
---|
| 227 | if (e == NIL(edge_t)) { |
---|
| 228 | fail("g_set_e_slot_static: Null edge"); |
---|
| 229 | } |
---|
| 230 | ((gGeneric *) e->user_data)[i] = val; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | |
---|
| 234 | gGeneric |
---|
| 235 | g_get_e_slot_static(edge_t *e, int i) |
---|
| 236 | { |
---|
| 237 | if (e == NIL(edge_t)) { |
---|
| 238 | fail("g_get_e_slot_static: Null edge"); |
---|
| 239 | } |
---|
| 240 | return((gGeneric *) e->user_data)[i]; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | void |
---|
| 244 | g_copy_e_slots_static(edge_t *e1, edge_t *e2, gGeneric (*f_copy_e)(gGeneric)) |
---|
| 245 | { |
---|
| 246 | int n; |
---|
| 247 | gGeneric slots1,*slots2; |
---|
| 248 | |
---|
| 249 | if (e1 == NIL(edge_t) || e2 == NIL(edge_t)) { |
---|
| 250 | fail("g_copy_e_slots_static: Null edge"); |
---|
| 251 | } |
---|
| 252 | n = g_field(g_edge_graph(e1))->num_e_slots; |
---|
| 253 | |
---|
| 254 | if (n != g_field(g_edge_graph(e2))->num_e_slots) { |
---|
| 255 | fail("g_copy_e_slots_static: Edges have differing numbers of slots"); |
---|
| 256 | } |
---|
| 257 | slots1 = e1->user_data; |
---|
| 258 | slots2 = (gGeneric *) e2->user_data; |
---|
| 259 | if (f_copy_e == (gGeneric (*)(gGeneric)) NULL) { |
---|
| 260 | for (n--; n >= 0; n--) { |
---|
| 261 | slots2[n] = ((gGeneric *) slots1)[n]; |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | else { |
---|
| 265 | FREE(slots2); |
---|
| 266 | e2->user_data = (*f_copy_e)(slots1); |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | |
---|
| 271 | vertex_t * |
---|
| 272 | g_add_vertex_static(graph_t *g) |
---|
| 273 | { |
---|
| 274 | g_field_t *gf; |
---|
| 275 | vertex_t *v; |
---|
| 276 | |
---|
| 277 | if (g == NIL(graph_t)) { |
---|
| 278 | fail("g_add_vertex_static: Null graph"); |
---|
| 279 | } |
---|
| 280 | gf = g_field(g); |
---|
| 281 | v = g_add_vertex(g); |
---|
| 282 | v->user_data = (gGeneric) ALLOC(gGeneric, gf->num_v_slots); |
---|
| 283 | return(v); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | void |
---|
| 288 | g_delete_vertex_static( |
---|
| 289 | vertex_t *v, |
---|
| 290 | void (*f_free_v)(gGeneric), |
---|
| 291 | void (*f_free_e)(gGeneric)) |
---|
| 292 | { |
---|
| 293 | edge_t *e; |
---|
| 294 | lsGen gen; |
---|
| 295 | |
---|
| 296 | if (v == NIL(vertex_t)) { |
---|
| 297 | fail("g_delete_vertex_static: Null vertex"); |
---|
| 298 | } |
---|
| 299 | foreach_in_edge(v, gen, e) { |
---|
| 300 | if (f_free_e != (void (*)(gGeneric)) NULL) { |
---|
| 301 | (*f_free_e)(e->user_data); |
---|
| 302 | } |
---|
| 303 | FREE(e->user_data); |
---|
| 304 | } |
---|
| 305 | foreach_out_edge(v, gen, e) { |
---|
| 306 | if (f_free_e != (void (*)(gGeneric)) NULL) { |
---|
| 307 | (*f_free_e)(e->user_data); |
---|
| 308 | } |
---|
| 309 | FREE(e->user_data); |
---|
| 310 | } |
---|
| 311 | if (f_free_v != (void (*)(gGeneric)) NULL) { |
---|
| 312 | (*f_free_v)(v->user_data); |
---|
| 313 | } |
---|
| 314 | FREE(v->user_data); |
---|
| 315 | g_delete_vertex(v, (void (*)(gGeneric)) NULL, (void (*)(gGeneric)) NULL); |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | |
---|
| 319 | void |
---|
| 320 | g_set_v_slot_static(vertex_t *v, int i, gGeneric val) |
---|
| 321 | { |
---|
| 322 | if (v == NIL(vertex_t)) { |
---|
| 323 | fail("g_set_v_slot_static: Null vertex"); |
---|
| 324 | } |
---|
| 325 | ((gGeneric *) v->user_data)[i] = val; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | gGeneric |
---|
| 330 | g_get_v_slot_static(vertex_t *v, int i) |
---|
| 331 | { |
---|
| 332 | if (v == NIL(vertex_t)) { |
---|
| 333 | fail("g_get_v_slot_static: Null vertex"); |
---|
| 334 | } |
---|
| 335 | return ((gGeneric *) v->user_data)[i]; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | void |
---|
| 339 | g_copy_v_slots_static( |
---|
| 340 | vertex_t *v1, |
---|
| 341 | vertex_t *v2, |
---|
| 342 | gGeneric (*f_copy_v)(gGeneric)) |
---|
| 343 | { |
---|
| 344 | int n; |
---|
| 345 | gGeneric slots1,*slots2; |
---|
| 346 | |
---|
| 347 | if (v1 == NIL(vertex_t) || v2 == NIL(vertex_t)) { |
---|
| 348 | fail("g_copy_v_slots_static: Null vertex"); |
---|
| 349 | } |
---|
| 350 | n = g_field(g_vertex_graph(v1))->num_v_slots; |
---|
| 351 | |
---|
| 352 | if (n != g_field(g_vertex_graph(v2))->num_v_slots) { |
---|
| 353 | fail("g_copy_v_slots_static: Vertices have differing numbers of slots"); |
---|
| 354 | } |
---|
| 355 | slots1 = v1->user_data; |
---|
| 356 | slots2 = (gGeneric *) v2->user_data; |
---|
| 357 | if (f_copy_v == (gGeneric (*)(gGeneric)) NULL) { |
---|
| 358 | for (n--; n >= 0; n--) { |
---|
| 359 | slots2[n] = ((gGeneric *) slots1)[n]; |
---|
| 360 | } |
---|
| 361 | } |
---|
| 362 | else { |
---|
| 363 | FREE(slots2); |
---|
| 364 | v2->user_data = (*f_copy_v)(slots1); |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | |
---|