| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020 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 smalloc |
| 10 |
#include "structs.h" // for vtx_data, edgeslist, flists, etc |
| 11 |
#include <stdio.h> // for NULL |
| 12 |
|
| 13 |
void add_edges(struct vtx_data **graph, /* graph data structure */ |
| 14 |
struct edgeslist *new_edges, /* list of edges connecting graph */ |
| 15 |
struct ilists **old_edges, /* edges data overwritten for connecting */ |
| 16 |
struct flists **old_ewgts, /* weights of edges overwritten */ |
| 17 |
int using_ewgts /* are edge weights being used? */ |
| 18 |
) |
| 19 |
{ |
| 20 |
struct ilists *save_list; /* space to save old edge list */ |
| 21 |
struct flists *save_ewgts; /* space to save old edge weights */ |
| 22 |
struct edgeslist *edges; /* loops through new edges */ |
| 23 |
float *new_ewgts; /* new edge weights */ |
| 24 |
int *new_list; /* new edge list */ |
| 25 |
int nedges; /* number of edges a vertex has */ |
| 26 |
int vtx, vtx2; /* two vertices in edge to be added */ |
| 27 |
int i, j; /* loop counter */ |
| 28 |
|
| 29 |
*old_edges = NULL; |
| 30 |
*old_ewgts = NULL; |
| 31 |
edges = new_edges; |
| 32 |
while (edges != NULL) { |
| 33 |
for (j = 0; j < 2; j++) { |
| 34 |
if (j == 0) { |
| 35 |
vtx = edges->vtx1; |
| 36 |
vtx2 = edges->vtx2; |
| 37 |
} |
| 38 |
else { |
| 39 |
vtx = edges->vtx2; |
| 40 |
vtx2 = edges->vtx1; |
| 41 |
} |
| 42 |
|
| 43 |
/* Copy old edge list to new edge list. */ |
| 44 |
nedges = graph[vtx]->nedges; |
| 45 |
new_list = smalloc((nedges + 1) * sizeof(int)); |
| 46 |
for (i = 0; i < nedges; i++) { |
| 47 |
new_list[i] = graph[vtx]->edges[i]; |
| 48 |
} |
| 49 |
new_list[nedges] = vtx2; |
| 50 |
|
| 51 |
/* Save old edges. */ |
| 52 |
save_list = smalloc(sizeof(struct ilists)); |
| 53 |
save_list->list = graph[vtx]->edges; |
| 54 |
|
| 55 |
/* Add new list at FRONT of linked list to facilitate uncoarsening. */ |
| 56 |
save_list->next = *old_edges; |
| 57 |
*old_edges = save_list; |
| 58 |
|
| 59 |
/* Now modify graph to have new edges list. */ |
| 60 |
graph[vtx]->nedges++; |
| 61 |
graph[vtx]->edges = new_list; |
| 62 |
|
| 63 |
/* If using edge weights, I have to modify those too. */ |
| 64 |
if (using_ewgts) { |
| 65 |
new_ewgts = smalloc((nedges + 1) * sizeof(float)); |
| 66 |
for (i = 1; i < nedges; i++) { |
| 67 |
new_ewgts[i] = graph[vtx]->ewgts[i]; |
| 68 |
} |
| 69 |
new_ewgts[nedges] = 1; |
| 70 |
new_ewgts[0] = graph[vtx]->ewgts[0] - new_ewgts[nedges]; |
| 71 |
|
| 72 |
/* Save old edge weights. */ |
| 73 |
save_ewgts = smalloc(sizeof(struct flists)); |
| 74 |
save_ewgts->list = graph[vtx]->ewgts; |
| 75 |
|
| 76 |
save_ewgts->next = *old_ewgts; |
| 77 |
*old_ewgts = save_ewgts; |
| 78 |
|
| 79 |
/* Finally, modify graph to have new edge weights. */ |
| 80 |
graph[vtx]->ewgts = new_ewgts; |
| 81 |
} |
| 82 |
} |
| 83 |
edges = edges->next; |
| 84 |
} |
| 85 |
} |