/* PSPP - a program for statistical analysis.
- Copyright (C) 2004, 2008 Free Software Foundation, Inc.
+ Copyright (C) 2004, 2008, 2009, 2011 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
-#include "histogram.h"
-#include <gl/xalloc.h>
-#include <libpspp/assertion.h>
+#include "math/histogram.h"
#include <gsl/gsl_histogram.h>
-#include "chart-geometry.h"
#include <math.h>
+#include "libpspp/assertion.h"
+#include "libpspp/cast.h"
+#include "math/chart-geometry.h"
+
+#include "gl/xalloc.h"
void
histogram_add (struct histogram *h, double y, double c)
{
- ((struct statistic *)h)->accumulate ((struct statistic *) h, NULL, c, 0, y);
+ struct statistic *stat = &h->parent;
+ stat->accumulate (stat, NULL, c, 0, y);
}
static void
acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc UNUSED, double y)
{
- struct histogram *hist = (struct histogram *) s;
+ struct histogram *hist = UP_CAST (s, struct histogram, parent);
gsl_histogram_accumulate (hist->gsl_hist, y, c);
}
static void
destroy (struct statistic *s)
{
- struct histogram *h = (struct histogram *) s;
+ struct histogram *h = UP_CAST (s, struct histogram, parent);
gsl_histogram_free (h->gsl_hist);
free (s);
}
-struct statistic *
+struct histogram *
histogram_create (int bins, double min, double max)
{
struct histogram *h = xmalloc (sizeof *h);
- struct statistic *stat = (struct statistic *) h;
+ struct statistic *stat = &h->parent;
double upper_limit, lower_limit;
double bin_width = chart_rounded_tick ((max - min) / (double) bins);
int n = ceil (max / (bin_width_2) ) ;
- assert (max > min);
+ assert (max >= min);
if ( ! (n % 2 ) ) n++;
upper_limit = n * bin_width_2;
stat->accumulate = acc;
stat->destroy = destroy;
- return stat;
+ return h;
}