Debian Codegraph @

packages/seacas/libraries/chaco/assign/assign_out.c

raw
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"
10 #include <stdio.h>
11
12 static void assign_out_normal(int nvtxs, /* number of vertices to output */
13 int *sets, /* values to be printed */
14 char *outname /* name of output file */
15 )
16 {
17 FILE *fout; /* output file */
18 int i; /* loop counter */
19
20 /* Print assignment in simple format. */
21
22 if (outname != NULL) {
23 fout = fopen(outname, "w");
24 }
25 else {
26 fout = stdout;
27 }
28
29 for (i = 1; i <= nvtxs; i++) {
30 fprintf(fout, "%d\n", sets[i]);
31 }
32
33 if (outname != NULL) {
34 fclose(fout);
35 }
36 }
37
38 static void assign_out_inv(int nvtxs, /* number of vertices to output */
39 int *sets, /* values to be printed */
40 int nsets, /* number of sets */
41 char *outname /* name of output file */
42 )
43 {
44 FILE *fout; /* output file */
45 int *size; /* # vtxs in sets / index into inorder */
46 int *inorder; /* list of vtxs in each set */
47 int i, j; /* loop counter */
48
49 /* Print assignment in inverted format. */
50 /* For each set, print # vertices, followed by list. */
51
52 if (outname != NULL) {
53 fout = fopen(outname, "w");
54 }
55 else {
56 fout = stdout;
57 }
58
59 size = smalloc((nsets + 1) * sizeof(int));
60 inorder = smalloc(nvtxs * sizeof(int));
61
62 for (j = 0; j < nsets; j++) {
63 size[j] = 0;
64 }
65 for (i = 1; i <= nvtxs; i++) {
66 ++size[sets[i]];
67 }
68
69 /* Modify size to become index into vertex list. */
70 for (j = 1; j < nsets; j++) {
71 size[j] += size[j - 1];
72 }
73 for (j = nsets - 1; j > 0; j--) {
74 size[j] = size[j - 1];
75 }
76 size[0] = 0;
77
78 for (i = 1; i <= nvtxs; i++) {
79 j = sets[i];
80 inorder[size[j]] = i;
81 ++size[j];
82 }
83
84 /* The inorder array now clumps all the vertices in each set. */
85 /* Now reconstruct size array to index into inorder. */
86 for (j = nsets - 1; j > 0; j--) {
87 size[j] = size[j - 1];
88 }
89 size[0] = 0;
90 size[nsets] = nvtxs;
91
92 /* Now print out the sets. */
93 for (j = 0; j < nsets; j++) {
94 fprintf(fout, " %d\n", size[j + 1] - size[j]);
95 for (i = size[j]; i < size[j + 1]; i++) {
96 fprintf(fout, "%d\n", inorder[i]);
97 }
98 }
99
100 if (outname != NULL) {
101 fclose(fout);
102 }
103 sfree(size);
104 sfree(inorder);
105 }
106
107 void assign_out(int nvtxs, /* number of vertices to output */
108 int *sets, /* values to be printed */
109 int nsets, /* number of sets */
110 char *outname /* name of output file */
111 )
112 {
113 extern int OUT_ASSIGN_INV; /* print assignment in inverted form? */
114
115 if (OUT_ASSIGN_INV) {
116 assign_out_inv(nvtxs, sets, nsets, outname);
117 }
118 else {
119 assign_out_normal(nvtxs, sets, outname);
120 }
121 }