19 #include <grass/gis.h>
20 #include <grass/Vect.h>
36 (
struct Cat_index *)G_malloc(Plus->a_cidx *
sizeof(
struct Cat_index));
39 Plus->cidx_up_to_date = 0;
50 for (i = 0; i < Plus->n_cidx; i++) {
51 ci = &(Plus->cidx[0]);
54 ci->field = ci->n_cats = ci->a_cats = ci->n_types = 0;
57 Plus->cidx_up_to_date = 0;
74 G_debug(3,
"dig_cidx_add_cat(): field = %d cat = %d line = %d type = %d",
75 field, cat, line, type);
79 for (i = 0; i < Plus->n_cidx; i++) {
80 if (Plus->cidx[i].field == field) {
85 if (Plus->n_cidx == Plus->a_cidx) {
88 (
struct Cat_index *)G_realloc(Plus->cidx,
90 sizeof(
struct Cat_index));
95 ci = &(Plus->cidx[si]);
97 ci->n_cats = ci->a_cats = 0;
105 ci = &(Plus->cidx[si]);
106 if (ci->n_cats == ci->a_cats) {
108 ci->cat = G_realloc(ci->cat, ci->a_cats * 3 *
sizeof(
int));
111 ci->cat[ci->n_cats][0] =
cat;
112 ci->cat[ci->n_cats][1] =
type;
113 ci->cat[ci->n_cats][2] = line;
118 for (i = 0; i < ci->n_types; i++) {
119 if (ci->type[i][0] == type) {
125 ci->type[ci->n_types][0] =
type;
126 ci->type[ci->n_types][1] = 1;
134 static int cmp_cat(
const void *pa,
const void *pb)
147 static int cmp_field(
const void *pa,
const void *pb)
149 struct Cat_index *p1 = (
struct Cat_index *)pa;
150 struct Cat_index *p2 = (
struct Cat_index *)pb;
152 if (p1->field < p2->field)
154 if (p1->field > p2->field)
170 int i, si, found, position;
171 struct Cat_index *ci;
174 "dig_cidx_add_cat_sorted(): field = %d cat = %d line = %d type = %d",
175 field, cat, line, type);
179 for (i = 0; i < Plus->n_cidx; i++) {
180 if (Plus->cidx[i].field == field) {
185 if (Plus->n_cidx == Plus->a_cidx) {
188 (
struct Cat_index *)G_realloc(Plus->cidx,
190 sizeof(
struct Cat_index));
195 ci = &(Plus->cidx[si]);
197 ci->n_cats = ci->a_cats = 0;
205 ci = &(Plus->cidx[si]);
206 if (ci->n_cats == ci->a_cats) {
208 ci->cat = G_realloc(ci->cat, ci->a_cats * 3 *
sizeof(
int));
212 for (position = 0; position < ci->n_cats; position++) {
213 if (ci->cat[position][0] >= cat) {
218 G_debug(4,
"position = %d", position);
221 for (i = ci->n_cats; i > position; i--) {
222 ci->cat[i][0] = ci->cat[i - 1][0];
223 ci->cat[i][1] = ci->cat[i - 1][1];
224 ci->cat[i][2] = ci->cat[i - 1][2];
227 ci->cat[position][0] =
cat;
228 ci->cat[position][1] =
type;
229 ci->cat[position][2] = line;
234 for (i = 0; i < ci->n_types; i++) {
235 if (ci->type[i][0] == type) {
241 ci->type[ci->n_types][0] =
type;
242 ci->type[ci->n_types][1] = 1;
247 qsort(Plus->cidx, Plus->n_cidx,
sizeof(
struct Cat_index), cmp_field);
249 G_debug(3,
"Added new category to index");
266 struct Cat_index *ci;
268 G_debug(3,
"dig_cidx_del_cat(): field = %d cat = %d line = %d", field,
273 for (i = 0; i < Plus->n_cidx; i++) {
274 if (Plus->cidx[i].field == field) {
275 ci = &(Plus->cidx[i]);
279 G_warning(
"BUG: Category index not found for field %d.", field);
284 G_debug(3,
"n_cats = %d", ci->n_cats);
285 for (position = 0; position < ci->n_cats; position++) {
286 if (ci->cat[position][0] == cat && ci->cat[position][1] == type &&
287 ci->cat[position][2] == line) {
292 G_debug(4,
"position = %d", position);
294 if (position == ci->n_cats) {
295 G_warning(
"BUG: Category not found in category index.");
300 for (i = position; i < ci->n_cats - 1; i++) {
301 ci->cat[i][0] = ci->cat[i + 1][0];
302 ci->cat[i][1] = ci->cat[i + 1][1];
303 ci->cat[i][2] = ci->cat[i + 1][2];
308 for (i = 0; i < ci->n_types; i++) {
309 if (ci->type[i][0] == type) {
314 G_debug(3,
"Deleted from category index");
326 struct Cat_index *ci;
330 for (f = 0; f < Plus->n_cidx; f++) {
333 ci = &(Plus->cidx[f]);
336 qsort(ci->cat, ci->n_cats, 3 *
sizeof(
int), cmp_cat);
341 for (c = 1; c < ci->n_cats; c++) {
342 if (ci->cat[c][0] != ci->cat[c - 1][0])
345 ci->n_ucats = nucats;
349 qsort(Plus->cidx, Plus->n_cidx,
sizeof(
struct Cat_index), cmp_field);
void G_free(void *buf)
Free allocated memory.
int dig_cidx_add_cat_sorted(struct Plus_head *Plus, int field, int cat, int line, int type)
int dig_cidx_add_cat(struct Plus_head *Plus, int field, int cat, int line, int type)
void dig_cidx_sort(struct Plus_head *Plus)
void dig_cidx_free(struct Plus_head *Plus)
int dig_cidx_init(struct Plus_head *Plus)
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_debug(int level, const char *msg,...)
Print debugging message.
int dig_cidx_del_cat(struct Plus_head *Plus, int field, int cat, int line, int type)