7 #define LIST struct Histogram_list 9 static FILE *fopen_histogram_new(
const char *);
10 static int cmp(
const void *,
const void *);
11 static int cmp_count(
const void *,
const void *);
55 G_warning(
_(
"Histogram for [%s in %s] missing (run r.support)"), name,
62 G_fatal_error(
_(
"Can't read histogram for [%s in %s]"), name, mapset);
64 while (fgets(buf,
sizeof buf, fd)) {
65 if (sscanf(buf,
"%ld:%ld", &cat, &count) != 2)
72 if (histogram->
num == 0)
73 G_fatal_error(
_(
"Invalid histogram file for [%s in %s]"), name, mapset);
96 fp = fopen_histogram_new(name);
98 list = histogram->
list;
99 for (n = 0; n < histogram->
num; n++) {
101 fprintf(fp,
"%ld:%ld\n", (
long)list[n].cat, list[n].count);
122 fp = fopen_histogram_new(name);
127 fprintf(fp,
"%ld:%ld\n", (
long)cat, count);
167 return histogram->
num;
180 if (n < 0 || n >= histogram->
num)
197 if (n < 0 || n >= histogram->
num)
213 if (histogram->
num > 0)
234 if ((n = histogram->
num) <= 1)
237 list = histogram->
list;
240 for (a = 1; a < n; a++)
241 if (list[a - 1].cat >= list[a].cat)
247 qsort(list, n,
sizeof(
LIST), &cmp);
250 for (a = 0, b = 1; b < n; b++) {
251 if (list[a].cat != list[b].cat) {
253 list[a].count = list[
b].count;
254 list[a].cat = list[
b].cat;
257 list[a].count += list[
b].count;
260 histogram->
num = a + 1;
266 static int cmp(
const void *aa,
const void *bb)
268 const LIST *a = aa, *
b = bb;
294 if ((n = histogram->
num) <= 1)
297 list = histogram->
list;
300 qsort(list, n,
sizeof(
LIST), &cmp_count);
306 static int cmp_count(
const void *aa,
const void *bb)
308 const LIST *a = aa, *
b = bb;
310 if (a->count < b->count)
313 if (a->count > b->count)
325 static FILE *fopen_histogram_new(
const char *
name)
331 G_fatal_error(
_(
"Unable to create histogram file for <%s>"), name);
364 for (i = 0; i < histogram->
num; i++) {
365 if (histogram->
list[i].
cat == cat) {
390 for (i = 0; i < histogram->
num; i++) {
391 if (histogram->
list[i].
cat == cat) {
415 histogram->
list[histogram->
num - 1].
cat = cat;
430 for (i = 0; i < histogram->
num; i++)
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void Rast_write_histogram_cs(const char *name, struct Cell_stats *statf)
Writes the histogram based on cell statistics to file.
int Rast_next_cell_stat(CELL *, long *, struct Cell_stats *)
Retrieve sorted cell stats.
int Rast_read_histogram(const char *name, const char *mapset, struct Histogram *histogram)
read the histogram information
int Rast_rewind_cell_stats(struct Cell_stats *)
Reset/rewind cell stats.
CELL Rast_get_histogram_cat(int n, const struct Histogram *histogram)
Returns cat for the nth element in the histogram.
void G_free(void *)
Free allocated memory.
int Rast_sort_histogram(struct Histogram *histogram)
Sorts the histogram.
void Rast_remove_histogram(const char *name)
Removes the histogram.
void Rast_make_histogram_cs(struct Cell_stats *statf, struct Histogram *histogram)
Creates histogram based on cell statistics.
int G_remove_misc(const char *, const char *, const char *)
Remove a database misc file.
int Rast_sort_histogram_by_count(struct Histogram *histogram)
Sorts the histogram by counts.
int Rast_add_histogram(CELL cat, long count, struct Histogram *histogram)
adds count to the histogram value for cat
FILE * G_fopen_old_misc(const char *, const char *, const char *, const char *)
open a database file for reading
void Rast_init_histogram(struct Histogram *histogram)
initializes the histogram structure
void Rast_extend_histogram(CELL cat, long count, struct Histogram *histogram)
Extends histogram struct to accommodate a new value.
void Rast_free_histogram(struct Histogram *histogram)
Frees memory allocated for the histogram.
int Rast_set_histogram(CELL cat, long count, struct Histogram *histogram)
sets the histogram value for cat to count
struct Histogram::Histogram_list * list
const char * G_find_file2_misc(const char *, const char *, const char *, const char *)
Searches for a file from the mapset search list or in a specified mapset. (look but don't touch) ...
FILE * G_fopen_new_misc(const char *, const char *, const char *)
open a new database file
void Rast_write_histogram(const char *name, const struct Histogram *histogram)
Writes the histogram information.
void G_warning(const char *,...) __attribute__((format(printf
void Rast_zero_histogram(struct Histogram *histogram)
Zero out histogram struct.
int Rast_get_histogram_num(const struct Histogram *histogram)
Sorts the histogram in ascending order by counts then category.
long Rast_get_histogram_count(int n, const struct Histogram *histogram)
Returns count for the nth element in the histogram.