2 #include <grass/glocale.h>
6 #define LIST struct Histogram_list
8 static FILE *fopen_histogram_new(
const char *);
9 static int cmp(
const void *,
const void *);
10 static int cmp_count(
const void *,
const void *);
25 histogram->list =
NULL;
47 struct Histogram *histogram)
57 G_warning(_(
"Histogram for [%s in %s] missing (run r.support)"), name,
65 G_warning(_(
"Can't read histogram for [%s in %s]"), name, mapset);
70 while (fgets(buf,
sizeof buf, fd)) {
71 if (sscanf(buf,
"%ld:%ld", &cat, &count) != 2) {
74 G_warning(_(
"Invalid histogram file for [%s in %s]"), name,
83 if (histogram->num == 0) {
84 G_warning(_(
"Invalid histogram file for [%s in %s]"), name, mapset);
111 fd = fopen_histogram_new(name);
115 list = histogram->list;
116 for (n = 0; n < histogram->num; n++) {
118 fprintf(fd,
"%ld:%ld\n", (
long)list[n].cat, list[n].count);
141 fd = fopen_histogram_new(name);
148 fprintf(fd,
"%ld:%ld\n", (
long)cat, count);
190 return histogram->num;
203 if (n < 0 || n >= histogram->num)
206 return histogram->list[n].cat;
220 if (n < 0 || n >= histogram->num)
223 return histogram->list[n].count;
236 if (histogram->num > 0)
239 histogram->list =
NULL;
259 if ((n = histogram->num) <= 1)
262 list = histogram->list;
265 for (a = 1; a < n; a++)
266 if (list[a - 1].cat >= list[a].cat)
272 qsort(list, n,
sizeof(
LIST), &cmp);
275 for (a = 0, b = 1; b < n; b++) {
276 if (list[a].cat != list[b].cat) {
278 list[a].count = list[
b].count;
279 list[a].cat = list[
b].cat;
282 list[a].count += list[
b].count;
285 histogram->num = a + 1;
291 static int cmp(
const void *aa,
const void *bb)
293 const LIST *a = aa, *
b = bb;
319 if ((n = histogram->num) <= 1)
322 list = histogram->list;
325 qsort(list, n,
sizeof(
LIST), &cmp_count);
331 static int cmp_count(
const void *aa,
const void *bb)
333 const LIST *a = aa, *b = bb;
335 if (a->count < b->count)
338 if (a->count > b->count)
350 static FILE *fopen_histogram_new(
const char *
name)
356 G_warning(_(
"can't create histogram for [%s in %s]"), name,
393 for (i = 0; i < histogram->num; i++) {
394 if (histogram->list[i].cat == cat) {
395 histogram->list[i].count += count;
419 for (i = 0; i < histogram->num; i++) {
420 if (histogram->list[i].cat == cat) {
421 histogram->list[i].count = count;
443 (
LIST *) G_realloc((
char *)histogram->list,
444 histogram->num *
sizeof(
LIST));
445 histogram->list[histogram->num - 1].cat = cat;
446 histogram->list[histogram->num - 1].count = count;
462 for (i = 0; i < histogram->num; i++)
463 histogram->list[i].count = 0;