Debian Codegraph @

packages/seacas/libraries/chaco/connect/connected.c

raw
1 /*
2 * Copyright(C) 1999-2020, 2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "smalloc.h" // for sfree, smalloc
10 #include "structs.h" // for edgeslist, connect_data, etc
11 #include <stdio.h> // for NULL, printf
12
13 void make_connected(
14 /* Add edges to make graph connected. */
15 struct vtx_data **graph, /* graph data structure */
16 int nvtxs, /* number of vertices in graph */
17 int *nedges, /* number of edges in graph */
18 int *mark, /* space for nvtxs+1 ints */
19 int *vtxlist, /* space for nvtxs ints */
20 struct connect_data **cdata, /* space for connectivity data */
21 int using_ewgts /* are edges of graph weighted? */
22 )
23 {
24 struct edgeslist *new_edges; /* list of edges connecting graph */
25 struct edgeslist *prev_edge; /* pointer for manipulating edge list */
26 struct edgeslist *curr_edge; /* pointer for manipulating edge list */
27 struct edgeslist *next_edge; /* pointer for manipulating edge list */
28 int nadded; /* number of edges being added */
29
30 /* First find edges needed to make graph connected. */
31 nadded = find_edges(graph, nvtxs, mark, vtxlist, &new_edges);
32
33 /* Now add these needed edges to graph data structure if needed. */
34 if (nadded == 0) {
35 *cdata = NULL;
36 }
37 else {
38 *cdata = smalloc(sizeof(struct connect_data));
39 (*cdata)->old_edges = NULL;
40 (*cdata)->old_ewgts = NULL;
41 add_edges(graph, new_edges, &(*cdata)->old_edges, &(*cdata)->old_ewgts, using_ewgts);
42 *nedges += nadded;
43
44 /* Now, reverse the order of the new_edges list for consistency with */
45 /* the removal order. */
46 curr_edge = new_edges->next;
47 new_edges->next = NULL;
48 prev_edge = new_edges;
49 while (curr_edge != NULL) {
50 next_edge = curr_edge->next;
51 curr_edge->next = prev_edge;
52 prev_edge = curr_edge;
53 curr_edge = next_edge;
54 }
55 (*cdata)->new_edges = prev_edge;
56 }
57 }
58
59 void make_unconnected(
60 /* Restore graph to its pristine state and free space for connectivity. */
61 struct vtx_data **graph, /* graph data structure */
62 int *nedges, /* number of edges in graph */
63 struct connect_data **cdata, /* space for connectivity data */
64 int using_ewgts /* are edges of graph weighted? */
65 )
66 {
67 struct ilists *old_edges = NULL; /* edges overwritten for connecting */
68 struct flists *old_ewgts = NULL; /* weights of edges overwritten */
69 struct edgeslist *new_edges; /* list of edges connecting graph */
70 struct ilists *tempi; /* used for freeing space */
71 struct flists *tempf; /* used for freeing space */
72 struct edgeslist *tempe; /* used for freeing edgelist space */
73 struct edgeslist *edges; /* loops through new edges */
74 int vtx; /* vertex in an added edge */
75 int j; /* loop counters */
76
77 if (*cdata == NULL) {
78 return;
79 }
80
81 old_edges = (*cdata)->old_edges;
82 old_ewgts = (*cdata)->old_ewgts;
83 new_edges = (*cdata)->new_edges;
84 sfree(*cdata);
85 *cdata = NULL;
86
87 edges = new_edges;
88 while (edges != NULL) {
89 /* Restore edges and weights to original status. */
90 (*nedges)--;
91 for (j = 0; j < 2; j++) {
92 if (j == 0) {
93 vtx = edges->vtx2;
94 }
95 else {
96 vtx = edges->vtx1;
97 }
98
99 sfree(graph[vtx]->edges);
100 graph[vtx]->edges = old_edges->list;
101 graph[vtx]->nedges--;
102 tempi = old_edges;
103 old_edges = old_edges->next;
104 sfree(tempi);
105
106 if (using_ewgts) {
107 sfree(graph[vtx]->ewgts);
108 graph[vtx]->ewgts = old_ewgts->list;
109 tempf = old_ewgts;
110 old_ewgts = old_ewgts->next;
111 sfree(tempf);
112 }
113 }
114 tempe = edges;
115 edges = edges->next;
116 sfree(tempe);
117 }
118 }
119
120 /* Print out the added edges. */
121 void print_connected(struct connect_data *cdata /* space for connectivity data */
122 )
123 {
124 struct edgeslist *edges; /* loops through new edges */
125
126 if (cdata == NULL) {
127 printf("No phantom edges\n");
128 }
129 else {
130 printf("Phantom edges: ");
131 edges = cdata->new_edges;
132 while (edges != NULL) {
133 printf("(%d,%d) ", edges->vtx1, edges->vtx2);
134 edges = edges->next;
135 }
136 printf("\n");
137 }
138 }
139
140 /* Free the edge list created by find_edges. */
141 void free_edgeslist(struct edgeslist *edge_list /* list to be freed */
142 )
143 {
144 struct edgeslist *next_list; /* next guy in list */
145
146 while (edge_list != NULL) {
147 next_list = edge_list->next;
148 sfree(edge_list);
149 edge_list = next_list;
150 }
151 }