X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fcharts%2Fbarchart.c;h=1b946fddffb780a0cd636eb8b3ff15742f4153f3;hb=08bfd1f8740fbf71e7d9671bb77ee9b234dbc9b8;hp=da72fc89f87b84e3b7e3a4b33da38304792ad69f;hpb=c1279d1145ad588c0b4831df5b8ee4d802892b73;p=pspp diff --git a/src/output/charts/barchart.c b/src/output/charts/barchart.c index da72fc89f8..1b946fddff 100644 --- a/src/output/charts/barchart.c +++ b/src/output/charts/barchart.c @@ -24,7 +24,7 @@ #include "libpspp/cast.h" #include "libpspp/str.h" #include "libpspp/array.h" -#include "output/chart-item-provider.h" +#include "output/chart-provider.h" #include "gl/xalloc.h" #include "data/variable.h" @@ -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) { @@ -146,12 +170,11 @@ barchart_dump (const struct barchart *bc, FILE *fp) CATS are the counts of the values of those variables. N_CATS is the number of distinct values. */ -struct chart_item * +struct chart * barchart_create (const struct variable **var, int n_vars, const char *ylabel, bool percent, struct freq *const *cats, int n_cats) { - struct barchart *bar; int i; const int pidx = 0; @@ -160,14 +183,14 @@ 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); + struct barchart *bar = XZALLOC (struct barchart); bar->percent = percent; bar->var = var; bar->n_vars = n_vars; bar->n_nzcats = n_cats; - chart_item_init (&bar->chart_item, &barchart_class, var_to_string (var[pidx])); + chart_init (&bar->chart, &barchart_class, var_to_string (var[pidx])); bar->largest = -1; bar->ylabel = strdup (ylabel); @@ -198,7 +221,7 @@ barchart_create (const struct variable **var, int n_vars, if (!flag) { - struct category *s = xzalloc (sizeof *s); + struct category *s = XZALLOC (struct category); s->idx = idx++; s->width = var_get_width (var[pidx]); value_init (&s->val, s->width); @@ -236,7 +259,7 @@ barchart_create (const struct variable **var, int n_vars, if (!flag) { - struct category *s = xzalloc (sizeof *s); + struct category *s = XZALLOC (struct category); s->idx = idx++; s->width = var_get_width (var[sidx]); value_init (&s->val, s->width); @@ -317,7 +340,7 @@ barchart_create (const struct variable **var, int n_vars, if (settings_get_testing_mode ()) barchart_dump (bar, stdout); - return &bar->chart_item; + return &bar->chart; } static void @@ -337,9 +360,9 @@ destroy_cat_map (struct hmap *m) } static void -barchart_destroy (struct chart_item *chart_item) +barchart_destroy (struct chart *chart) { - struct barchart *bar = to_barchart (chart_item); + struct barchart *bar = to_barchart (chart); int i; @@ -360,7 +383,7 @@ barchart_destroy (struct chart_item *chart_item) free (bar); } -const struct chart_item_class barchart_class = +const struct chart_class barchart_class = { barchart_destroy };