--- /dev/null
+#ifndef HISTOGRAM_H
+#define HISTOGRAM_H
+
+struct histogram {
+ unsigned *data;
+ unsigned allocated;
+ unsigned resolution;
+};
+
+int histogram_init(struct histogram *h,
+ unsigned max_value,
+ unsigned resolution)
+{
+ size_t mem;
+ h->allocated = max_value/resolution + 1;
+ h->resolution = resolution;
+ mem = h->allocated*sizeof(*h->data);
+ h->data = malloc(mem);
+ if (h->data) {
+ memset(h->data, 0, mem);
+ return 0;
+ } else
+ return -1;
+}
+
+void histogram_add(struct histogram *h, unsigned value)
+{
+ unsigned index = value / h->resolution;
+ if (index >= h->allocated)
+ index = h->allocated - 1;
+ h->data[index]++;
+}
+
+void histogram_fprint(struct histogram *h, FILE *f)
+{
+ unsigned long long sum = 0, cum;
+ unsigned i;
+ for (i = 0; i < h->allocated; i++)
+ sum += h->data[i];
+ cum = sum;
+ for (i = 0; i < h->allocated; i++) {
+ if (h->data[i] != 0) {
+ if (!getenv("CANPING_MS"))
+ fprintf(f, "%d %lld\n", i*h->resolution, cum);
+ else
+ fprintf(f, "%g %lld\n", 1e-3*(i*h->resolution), cum);
+ }
+ cum -= h->data[i];
+ }
+}
+
+
+
+#endif