From e2ce7c065ef4b5326901a2374ca9fa54ade83846 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Sun, 20 Jun 2021 11:47:58 -0700 Subject: [PATCH] return errors from node_distinct_coloring via the return value, rather than flag This makes things simpler for readers, callers, as well as the compiler. --- cmd/gvmap/make_map.c | 3 +-- lib/edgepaint/edge_distinct_coloring.c | 2 +- lib/edgepaint/node_distinct_coloring.c | 15 ++++++--------- lib/edgepaint/node_distinct_coloring.h | 2 +- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/cmd/gvmap/make_map.c b/cmd/gvmap/make_map.c index f7f5b98bf..3a8626fec 100644 --- a/cmd/gvmap/make_map.c +++ b/cmd/gvmap/make_map.c @@ -53,7 +53,6 @@ void map_palette_optimal_coloring(char *color_scheme, char *lightness, SparseMat color_diff_sum: the sum of min color dfference across all nodes */ real *colors = NULL, color_diff, color_diff_sum; - int flag; int n = A0->m, i, cdim; SparseMatrix A; @@ -70,7 +69,7 @@ void map_palette_optimal_coloring(char *color_scheme, char *lightness, SparseMat SparseMatrix_export(stdout, A); } - node_distinct_coloring(color_scheme, lightness, weightedQ, A, accuracy, iter_max, seed, &cdim, &colors, &color_diff, &color_diff_sum, &flag); + node_distinct_coloring(color_scheme, lightness, weightedQ, A, accuracy, iter_max, seed, &cdim, &colors, &color_diff, &color_diff_sum); if (A != A0){ SparseMatrix_delete(A); diff --git a/lib/edgepaint/edge_distinct_coloring.c b/lib/edgepaint/edge_distinct_coloring.c index 28062c748..bc9ff453a 100644 --- a/lib/edgepaint/edge_distinct_coloring.c +++ b/lib/edgepaint/edge_distinct_coloring.c @@ -239,7 +239,7 @@ Agraph_t* edge_distinct_coloring(char *color_scheme, char *lightness, Agraph_t* #endif int weightedQ = FALSE; int iter_max = 100; - node_distinct_coloring(color_scheme, lightness, weightedQ, C, accuracy, iter_max, seed, &cdim, &colors, &color_diff, &color_diff_sum, &flag); + flag = node_distinct_coloring(color_scheme, lightness, weightedQ, C, accuracy, iter_max, seed, &cdim, &colors, &color_diff, &color_diff_sum); if (flag) goto RETURN; #ifdef TIME fprintf(stderr, "cpu for color assignmment =%10.3f\n", ((real) (clock() - start))/CLOCKS_PER_SEC); diff --git a/lib/edgepaint/node_distinct_coloring.c b/lib/edgepaint/node_distinct_coloring.c index 61e210d10..14a8d896e 100644 --- a/lib/edgepaint/node_distinct_coloring.c +++ b/lib/edgepaint/node_distinct_coloring.c @@ -206,8 +206,8 @@ static void node_distinct_coloring_internal(int scheme, QuadTree qt, int weighte } -void node_distinct_coloring(char *color_scheme, char *lightness, int weightedQ, SparseMatrix A0, real accuracy, int iter_max, int seed, int *cdim0, real **colors, real *color_diff0, - real *color_diff_sum0, int *flag){ +int node_distinct_coloring(char *color_scheme, char *lightness, int weightedQ, SparseMatrix A0, real accuracy, int iter_max, int seed, int *cdim0, real **colors, real *color_diff0, + real *color_diff_sum0){ /* for a graph A, get a distinctive color of its nodes so that the color distance among all neighboring nodes are maximized. Here color distance on a node is defined as the minimum of color differences between a node and its neighbors (or the minimum of weighted color differences if weightedQ = true, @@ -250,8 +250,7 @@ void node_distinct_coloring(char *color_scheme, char *lightness, int weightedQ, qt = lab_gamut_quadtree(lightness, max_qtree_level); if (!qt){ fprintf(stderr, "out of memory\n"); - *flag = -1; - return; + return -1; } } else if (strcmp(color_scheme, "rgb") == 0){ if (Verbose) fprintf(stderr,"rgb\n"); @@ -267,19 +266,16 @@ void node_distinct_coloring(char *color_scheme, char *lightness, int weightedQ, qt = QuadTree_new_from_point_list(cdim, maxcolors, max_qtree_level, colors, NULL); assert(qt); } else { - *flag = ERROR_BAD_COLOR_SCHEME; - return; + return ERROR_BAD_COLOR_SCHEME; } *color_diff0 = *color_diff_sum0 = -1; if (accuracy <= 0) accuracy = 0.0001; - *flag = 0; n = A->m; if (n != A->n) { - *flag = -1; - return; + return -1; } if (!(*colors)) { @@ -316,4 +312,5 @@ void node_distinct_coloring(char *color_scheme, char *lightness, int weightedQ, *color_diff_sum0 /= nnodes; if (A != A0) SparseMatrix_delete(A); + return 0; } diff --git a/lib/edgepaint/node_distinct_coloring.h b/lib/edgepaint/node_distinct_coloring.h index 7a5dc6913..a371584b2 100644 --- a/lib/edgepaint/node_distinct_coloring.h +++ b/lib/edgepaint/node_distinct_coloring.h @@ -13,6 +13,6 @@ enum {COLOR_RGB, COLOR_GRAY, COLOR_LAB}; enum {ERROR_BAD_COLOR_SCHEME = -9}; -void node_distinct_coloring(char *color_scheme, char *lightness, int weightedQ, SparseMatrix A, real accuracy, int iter_max, int seed, int *cdim, real **colors, real *color_diff, real *color_diff_sum, int *flag); +int node_distinct_coloring(char *color_scheme, char *lightness, int weightedQ, SparseMatrix A, real accuracy, int iter_max, int seed, int *cdim, real **colors, real *color_diff, real *color_diff_sum); #endif -- 2.40.0