File: cindex.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (329 lines) | stat: -rw-r--r-- 7,841 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*****************************************************************************
*
* MODULE:       Vector library 
*   	    	
* AUTHOR(S):    Radim Blazek
*
* PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
*
* COPYRIGHT:    (C) 2001 by the GRASS Development Team
*
*               This program is free software under the GNU General Public
*   	    	License (>=v2). Read the file COPYING that comes with GRASS
*   	    	for details.
*
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "gis.h"
#include "Vect.h"

/* 
*  dig_cidx_init ()
*  initit cat index
*
*  returns 1 OK
*          0 on error      
*/
int 
dig_cidx_init ( struct Plus_head *Plus) 
{
    G_debug(3, "dig_cidx_init()");
    
    Plus->n_cidx = 0;
    Plus->a_cidx = 5;
    Plus->cidx = (struct Cat_index*) G_malloc(  Plus->a_cidx * sizeof( struct Cat_index ) );
    if (!Plus->cidx ) return 0;
    Plus->cidx_up_to_date = 0;
    return 1;
}

/* Free category index */
void
dig_cidx_free ( struct Plus_head *Plus) 
{
    int i;
    struct Cat_index *ci;
    
    G_debug(2, "dig_cidx_free()");
    for ( i = 0; i < Plus->n_cidx; i++ ) {
	ci = &(Plus->cidx[0]);
	free ( ci->cat);
	ci->cat = NULL;
	ci->field = ci->n_cats = ci->a_cats = ci->n_types = 0;
    }
    Plus->n_cidx = 0;
    Plus->cidx_up_to_date = 0;
}

/* 
*  dig_cidx_add_cat ()
*  add new field - cat - line record, space is allocated if necessary 
*
*  returns 1 OK
*          0 on error      
*/
int 
dig_cidx_add_cat ( struct Plus_head *Plus, int field, int cat, int line, int type) 
{
    int i, si, found;
    struct Cat_index *ci;
    
    G_debug(3, "dig_cidx_add_cat(): field = %d cat = %d line = %d type = %d", field, cat, line, type);

    /* Find field or add new */ 
    si = -1;
    for ( i = 0; i < Plus->n_cidx; i++ ) {
	if ( Plus->cidx[i].field == field ) {
	    si = i;
	}
    }
    if ( si == -1 ) { /* not found add new */
	if ( Plus->n_cidx == Plus->a_cidx ) {
	    Plus->a_cidx += 10;
	    Plus->cidx = (struct Cat_index*) G_realloc ( Plus->cidx, Plus->a_cidx * sizeof( struct Cat_index ) );
	    if (!Plus->cidx) return 0;
	}
	si = Plus->n_cidx;
	ci = &(Plus->cidx[si]);
       	ci->field = field;
       	ci->n_cats = ci->a_cats = 0;
       	ci->cat = NULL;
       	ci->n_types = 0;
       	ci->offset = 0;
	Plus->n_cidx++;
    }

    /* Add new cat - line record */
    ci = &(Plus->cidx[si]);
    if ( ci->n_cats == ci->a_cats ) {
	ci->a_cats += 5000;
	ci->cat = (int *) G_realloc ( ci->cat, ci->a_cats * 3 * sizeof(int) );
    }

    ci->cat[ci->n_cats][0] = cat;
    ci->cat[ci->n_cats][1] = type;
    ci->cat[ci->n_cats][2] = line;
    ci->n_cats++;

    /* Add type */
    found = 0;
    for ( i = 0; i < ci->n_types; i++ ) {
	if ( ci->type[i][0] == type ) {
	    ci->type[i][1]++;
	    found = 1;
	}
    }
    if ( ! found ) {
	ci->type[ci->n_types][0] = type;
	ci->type[ci->n_types][1] = 1;
	ci->n_types++;
    }

    return 1;
}

/* Compare by cat */
static int cmp_cat ( const void *pa, const void *pb )
{
    int *p1 = (int*) pa;
    int *p2 = (int*) pb;

    if ( p1[0] < p2[0] ) return -1;
    if ( p1[0] > p2[0] ) return 1;
    return 0;
}

/* Compare by field */
static int cmp_field ( const void *pa, const void *pb )
{
    struct Cat_index *p1 = (struct Cat_index*) pa;
    struct Cat_index *p2 = (struct Cat_index*) pb;

    if ( p1->field < p2->field ) return -1;
    if ( p1->field > p2->field ) return 1;
    return 0;
}

/* 
*  dig_cidx_add_cat_sorted ()
*  add new field - cat - line record to sorted category index, space is allocated if necessary 
*  
*  returns 1 OK
*          0 on error      
*/
int 
dig_cidx_add_cat_sorted ( struct Plus_head *Plus, int field, int cat, int line, int type) 
{
    int i, si, found, *catp, position;
    struct Cat_index *ci;
    
    G_debug(3, "dig_cidx_add_cat_sorted(): field = %d cat = %d line = %d type = %d", field, cat, line, type);

    /* Find field or add new */ 
    si = -1;
    for ( i = 0; i < Plus->n_cidx; i++ ) {
	if ( Plus->cidx[i].field == field ) {
	    si = i;
	}
    }
    if ( si == -1 ) { /* not found add new */
	if ( Plus->n_cidx == Plus->a_cidx ) {
	    Plus->a_cidx += 10;
	    Plus->cidx = (struct Cat_index*) G_realloc ( Plus->cidx, Plus->a_cidx * sizeof( struct Cat_index ) );
	    if (!Plus->cidx) return 0;
	}
	si = Plus->n_cidx;
	ci = &(Plus->cidx[si]);
       	ci->field = field;
       	ci->n_cats = ci->a_cats = 0;
       	ci->cat = NULL;
       	ci->n_types = 0;
       	ci->offset = 0;
	Plus->n_cidx++;
    }

    /* Add new cat - line record */
    ci = &(Plus->cidx[si]);
    if ( ci->n_cats == ci->a_cats ) {
	ci->a_cats += 5000;
	ci->cat = (int *) G_realloc ( ci->cat, ci->a_cats * 3 * sizeof(int) );
    }
    
    /* Find position */
    for ( position = 0; position < ci->n_cats; position++ ) {
	if ( ci->cat[position][0] >= cat ) {
	    break;
	}
    }
    
    G_debug (4, "position = %d", position );

    /* Move */
    for ( i = ci->n_cats; i > position; i-- ) {
	ci->cat[i][0] = ci->cat[i-1][0];
	ci->cat[i][1] = ci->cat[i-1][1];
	ci->cat[i][2] = ci->cat[i-1][2];
    }

    ci->cat[position][0] = cat;
    ci->cat[position][1] = type;
    ci->cat[position][2] = line;
    ci->n_cats++;

    /* Add type */
    found = 0;
    for ( i = 0; i < ci->n_types; i++ ) {
	if ( ci->type[i][0] == type ) {
	    ci->type[i][1]++;
	    found = 1;
	}
    }
    if ( ! found ) {
	ci->type[ci->n_types][0] = type;
	ci->type[ci->n_types][1] = 1;
	ci->n_types++;
    }
    
    /* Sort by field */
    qsort ( Plus->cidx, Plus->n_cidx, sizeof(struct Cat_index), cmp_field );
    
    G_debug (3, "Added new category to index" );

    return 1;
}

/* 
*  dig_cidx_del_cat ()
*  delete old field - cat - line record from _sorted_ category index
*
*  returns 1 OK
*          0 on error      
*/
int 
dig_cidx_del_cat ( struct Plus_head *Plus, int field, int cat, int line, int type ) 
{
    int    i, position;
    struct Cat_index *ci;

    G_debug(3, "dig_cidx_del_cat(): field = %d cat = %d line = %d", field, cat, line);

    /* Find field or add new */ 
    ci = NULL;
    for ( i = 0; i < Plus->n_cidx; i++ ) {
	if ( Plus->cidx[i].field == field ) {
	    ci = &(Plus->cidx[i]);
	}
    }
    if ( ci == NULL ) { /* should not happen */
	G_warning ( "BUG: Category index not found for field %d.", field );
	return 0;
    }

    /* Find position */
    G_debug(3, "n_cats = %d", ci->n_cats );
    for ( position = 0; position < ci->n_cats; position++ ) {
	if ( ci->cat[position][0] == cat && ci->cat[position][1] == type && ci->cat[position][2] == line ) {
	    break;
	}
    }
    
    G_debug (4, "position = %d" );

    if ( position == ci->n_cats ) {
	G_warning ( "BUG: Category not found in category index." );
	return 0;
    }

    /* Delete */
    for ( i = position; i < ci->n_cats-1; i++ ) {
	ci->cat[i][0] = ci->cat[i+1][0];
	ci->cat[i][1] = ci->cat[i+1][1];
	ci->cat[i][2] = ci->cat[i+1][2];
    }

    ci->n_cats--;
    
    for ( i = 0; i < ci->n_types; i++ ) {
	if ( ci->type[i][0] == type ) {
	    ci->type[i][1]--;
	}
    }

    G_debug (3, "Deleted from category index" );
    return 1;
}

/* 
*  dig_cidx_sort ()
*  sort all records in cat index  
*
*/
void 
dig_cidx_sort ( struct Plus_head *Plus ) 
{
    int f;
    struct Cat_index *ci;
    
    G_debug(2, "dig_cidx_sort()" );

    for ( f = 0; f < Plus->n_cidx; f++ ) {
	int c, nucats = 0;
	
	ci = &(Plus->cidx[f]);
    
	/* Sort by category */
	qsort ( ci->cat, ci->n_cats, 3*sizeof(int), cmp_cat );

	/* Calculate number of unique cats */
	if ( ci->n_cats > 0 ) nucats++;
	for ( c = 1; c < ci->n_cats; c++ ) {
	    if ( ci->cat[c][0] != ci->cat[c-1][0] )  nucats++;
	}
	ci->n_ucats = nucats;
    }	
    
    /* Sort by field */
    qsort ( Plus->cidx, Plus->n_cidx, sizeof(struct Cat_index), cmp_field );
}