X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fcharts%2Fbarchart.c;h=eb252d4811d2b434991fe087d41d21f8933d3c06;hb=08959981d167390d9c9ba0ac5a8a2558de9175af;hp=da72fc89f87b84e3b7e3a4b33da38304792ad69f;hpb=c1279d1145ad588c0b4831df5b8ee4d802892b73;p=pspp diff --git a/src/output/charts/barchart.c b/src/output/charts/barchart.c index da72fc89f8..eb252d4811 100644 --- a/src/output/charts/barchart.c +++ b/src/output/charts/barchart.c @@ -43,6 +43,19 @@ compare_category_3way (const void *a_, const void *b_, const void *bc_) } +static int +compare_category_by_index_3way (const void *a_, const void *b_, + const void *unused UNUSED) +{ + const struct category *const*a = a_; + const struct category *const*b = b_; + + if ( (*a)->idx < (*b)->idx) + return -1; + + return ((*a)->idx > (*b)->idx); +} + static unsigned int hash_freq_2level_ptr (const void *a_, const void *bc_) { @@ -97,10 +110,21 @@ barchart_dump (const struct barchart *bc, FILE *fp) fprintf (fp, "Categories:\n"); struct category *cat; + struct category **cats = XCALLOC (hmap_count (&bc->primaries), struct category *); + int i = 0; HMAP_FOR_EACH (cat, struct category, node, &bc->primaries) { - fprintf (fp, " %d \"%s\"\n", cat->idx, ds_cstr(&cat->label)); + cats[i++] = cat; + } + /* HMAP_FOR_EACH is not guaranteed to iterate in any particular order. So + we must sort here before we output the results. */ + sort (cats, i, sizeof (struct category *), compare_category_by_index_3way, bc); + for (i = 0; i < hmap_count (&bc->primaries); ++i) + { + const struct category *c = cats[i]; + fprintf (fp, " %d \"%s\"\n", c->idx, ds_cstr (&c->label)); } + free (cats); if (bc->ss) { @@ -160,7 +184,7 @@ barchart_create (const struct variable **var, int n_vars, int width = var_get_width (var[pidx]); - assert (n_vars >= 1); + assert (n_vars >= 1 && n_vars <= 2); bar = xzalloc (sizeof *bar); bar->percent = percent;