2 * libsortnetwork - src/histrogram.c
3 * Copyright (C) 2011 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <ff at octo.it>
22 #ifndef _ISOC99_SOURCE
23 # define _ISOC99_SOURCE
25 #ifndef _POSIX_C_SOURCE
26 # define _POSIX_C_SOURCE 200809L
29 # define _XOPEN_SOURCE 700
40 #include "histogram.h"
51 static int hist_resize (histogram_t *h, int rating) /* {{{ */
59 if ((h->index_min == 0) && (h->index_max == 0))
66 min_new = ((h->index_min < rating) ? h->index_min : rating);
67 max_new = ((h->index_max > rating) ? h->index_max : rating);
70 nelem_new = (size_t) ((max_new - min_new) + 1);
71 nelem_old = (size_t) ((h->index_max - h->index_min) + 1);
73 assert (nelem_new >= nelem_old);
75 tmp = realloc (h->data, nelem_new * sizeof (*h->data));
80 if ((h->index_min == 0) && (h->index_max == 0))
82 h->index_min = min_new;
83 h->index_max = max_new;
88 if (min_new < h->index_min)
90 size_t diff = (size_t) (h->index_min - min_new);
92 memmove (h->data + diff, h->data, nelem_old * sizeof (*h->data));
93 memset (h->data, 0, diff * sizeof (*h->data));
95 h->index_min = min_new;
97 else if (max_new > h->index_max)
99 size_t diff = (size_t) (max_new - h->index_max);
100 memset (h->data + nelem_old, 0, diff * sizeof (*h->data));
102 h->index_max = max_new;
106 } /* }}} int hist_resize */
108 histogram_t *hist_create (void) /* {{{ */
110 histogram_t *h = malloc (sizeof (*h));
113 memset (h, 0, sizeof (*h));
117 } /* }}} hist_create */
119 void hist_destroy (histogram_t *h) /* {{{ */
124 } /* }}} hist_destroy */
126 int hist_account (histogram_t *h, int rating) /* {{{ */
128 if ((h == NULL) || (rating < 0))
131 if ((rating < h->index_min) || (rating > h->index_max))
132 hist_resize (h, rating);
134 h->data[rating - h->index_min]++;
137 } /* }}} int hist_account */
139 int hist_print (histogram_t *h) /* {{{ */
141 char bar[BAR_WIDTH + 1];
149 for (i = h->index_min; i <= h->index_max; i++)
150 if (max < h->data[i - h->index_min])
151 max = h->data[i - h->index_min];
153 for (i = h->index_min; i <= h->index_max; i++)
155 uint64_t num = h->data[i - h->index_min];
156 uint64_t points = (BAR_WIDTH * num) / max;
159 for (j = 0; j < (BAR_WIDTH + 1); j++)
160 bar[j] = (j < points) ? '#' : 0;
162 printf ("%4i: %8"PRIu64" %s\n", i, num, bar);
166 } /* }}} int hist_print */
168 /* vim: set shiftwidth=2 softtabstop=2 fdm=marker : */