~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/vector/Vlib/list.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
 
/**
3
 
 * \file list.c
 
2
/*!
 
3
 * \file lib/vector/Vlib/list.c
4
4
 *
5
5
 * \brief Vector library - list definition
6
6
 *
7
7
 * Higher level functions for reading/writing/manipulating vectors.
8
8
 *
9
 
 * (C) 2001-2008 by the GRASS Development Team
 
9
 * (C) 2001-2009 by the GRASS Development Team
10
10
 *
11
11
 * This program is free software under the GNU General Public
12
12
 * License (>=v2). Read the file COPYING that comes with GRASS
13
13
 * for details.
14
14
 *
15
15
 * \author Original author CERL, probably Dave Gerdes or Mike Higgins.
16
 
 * Update to GRASS 5.7 Radim Blazek and David D. Gray
17
 
 *
18
 
 * \date 2001-2008
 
16
 * \author Update to GRASS 5.7 Radim Blazek and David D. Gray
 
17
 * \author Update to GRASS 7 Markus Metz
19
18
 */
20
19
 
21
20
#include <stdlib.h>
22
 
#include <grass/Vect.h>
23
 
#include <grass/gis.h>
 
21
#include <grass/vector.h>
24
22
 
25
23
/**
26
24
 * \brief Creates and initializes a struct ilist.
68
66
 * the struct itself
69
67
 *
70
68
 * \param[in,out] list pointer to ilist structure
71
 
 *
72
 
 * \return 0
73
69
 */
74
 
int Vect_destroy_list(struct ilist *list)
 
70
void Vect_destroy_list(struct ilist *list)
75
71
{
76
72
    if (list) {                 /* probably a moot test */
77
73
        if (list->alloc_values) {
80
76
        G_free((void *)list);
81
77
    }
82
78
    list = NULL;
83
 
 
84
 
    return 0;
85
79
}
86
80
 
87
81
/**
88
82
 * \brief Append new item to the end of list if not yet present 
89
83
 *
90
84
 * \param[in,out] list pointer to ilist structure
91
 
 * \param[in] val new item to append to the end of list
 
85
 * \param val new item to append to the end of list
92
86
 *
93
87
 * \return 0 on success
94
88
 * \return 1 on error
122
116
 * \brief Append new items to the end of list if not yet present 
123
117
 *
124
118
 * \param[in,out] alist pointer to ilist structure where items will be appended
125
 
 * \param[in] blist pointer to ilist structure with new items
 
119
 * \param blist pointer to ilist structure with new items
126
120
 *
127
121
 * \return 0 on success
128
122
 * \return 1 on error
129
123
 */
130
 
int Vect_list_append_list(struct ilist *alist, struct ilist *blist)
 
124
int Vect_list_append_list(struct ilist *alist, const struct ilist *blist)
131
125
{
132
126
    int i;
133
127
 
144
138
 * \brief Remove a given value (item) from list
145
139
 *
146
140
 * \param[in,out] list pointer to ilist structure
147
 
 * \param[in] val to remove
 
141
 * \param val to remove
148
142
 *
149
143
 * \return 0 on success
150
144
 * \return 1 on error
173
167
 * \brief Delete list from existing list 
174
168
 *
175
169
 * \param[in,out] alist pointer to original ilist structure,
176
 
 * \param[in] blist pointer to ilist structure with items to delete
 
170
 * \param blist pointer to ilist structure with items to delete
177
171
 *
178
172
 * \return 0 on success
179
173
 * \return 1 on error
180
174
 */
181
 
int Vect_list_delete_list(struct ilist *alist, struct ilist *blist)
 
175
int Vect_list_delete_list(struct ilist *alist, const struct ilist *blist)
182
176
{
183
177
    int i;
184
178
 
194
188
/**
195
189
 * \brief Find a given item in the list
196
190
 *
197
 
 * \param[in] list pointer to ilist structure
198
 
 * \param[in] val value of item
 
191
 * \param list pointer to ilist structure
 
192
 * \param val value of item
199
193
 *
200
194
 * \return 1 if an item is found
201
195
 * \return 0 no found item in the list
202
196
*/
203
 
int Vect_val_in_list(struct ilist *list, int val)
 
197
int Vect_val_in_list(const struct ilist *list, int val)
204
198
{
205
199
    int i;
206
200
 
214
208
 
215
209
    return 0;
216
210
}
 
211
 
 
212
/* box list routines */
 
213
 
 
214
/**
 
215
 * \brief Creates and initializes a struct boxlist.
 
216
 *
 
217
 * This structure is used as container for bounding boxes with id. The
 
218
 * library routines handle all memory allocation.
 
219
 *
 
220
 * \param have_boxes if set to 0, the list will hold only ids and no boxes
 
221
 * 
 
222
 * \return pointer to struct boxlist
 
223
 * \return NULL on error
 
224
 */
 
225
struct boxlist *Vect_new_boxlist(int have_boxes)
 
226
{
 
227
    struct boxlist *p;
 
228
 
 
229
    p = (struct boxlist *)G_malloc(sizeof(struct boxlist));
 
230
 
 
231
    if (p) {
 
232
        p->id = NULL;
 
233
        p->box = NULL;
 
234
        p->have_boxes = have_boxes != 0;
 
235
        p->n_values = 0;
 
236
        p->alloc_values = 0;
 
237
    }
 
238
 
 
239
    return p;
 
240
}
 
241
 
 
242
/**
 
243
 * \brief Reset boxlist structure.
 
244
 *
 
245
 * To make sure boxlist structure is clean to be re-used. List must have
 
246
 * previously been created with Vect_new_boxlist().
 
247
 *
 
248
 * \param[in,out] list pointer to struct boxlist
 
249
 * 
 
250
 * \return 0
 
251
 */
 
252
int Vect_reset_boxlist(struct boxlist *list)
 
253
{
 
254
    list->n_values = 0;
 
255
 
 
256
    return 0;
 
257
}
 
258
 
 
259
/**
 
260
 * \brief Frees all memory associated with a struct boxlist, including
 
261
 * the struct itself
 
262
 *
 
263
 * \param[in,out] list pointer to ilist structure
 
264
 */
 
265
void Vect_destroy_boxlist(struct boxlist *list)
 
266
{
 
267
    if (list) {                 /* probably a moot test */
 
268
        if (list->alloc_values) {
 
269
            G_free((void *)list->id);
 
270
            if (list->box)
 
271
                G_free((void *)list->box);
 
272
        }
 
273
        G_free((void *)list);
 
274
    }
 
275
    list = NULL;
 
276
}
 
277
 
 
278
/**
 
279
 * \brief Append new item to the end of list if not yet present 
 
280
 *
 
281
 * \param[in,out] list pointer to ilist structure
 
282
 * \param id new item to append to the end of list
 
283
 * \param box bounding box
 
284
 *
 
285
 * \return 0 on success
 
286
 * \return 1 on error
 
287
 */
 
288
int Vect_boxlist_append(struct boxlist *list, int id, const struct bound_box *box)
 
289
{
 
290
    int i;
 
291
    size_t size;
 
292
 
 
293
    if (list == NULL)
 
294
        return 1;
 
295
 
 
296
    for (i = 0; i < list->n_values; i++) {
 
297
        if (id == list->id[i])
 
298
            return 0;
 
299
    }
 
300
 
 
301
    if (list->n_values == list->alloc_values) {
 
302
        size = (list->n_values + 1000) * sizeof(int);
 
303
        list->id = (int *)G_realloc((void *)list->id, size);
 
304
 
 
305
        if (list->have_boxes) {
 
306
            size = (list->n_values + 1000) * sizeof(struct bound_box);
 
307
            list->box = (struct bound_box *)G_realloc((void *)list->box, size);
 
308
        }
 
309
 
 
310
        list->alloc_values = list->n_values + 1000;
 
311
    }
 
312
 
 
313
    list->id[list->n_values] = id;
 
314
    if (list->have_boxes)
 
315
        list->box[list->n_values] = *box;
 
316
    list->n_values++;
 
317
 
 
318
    return 0;
 
319
}
 
320
 
 
321
/**
 
322
 * \brief Append new items to the end of list if not yet present 
 
323
 *
 
324
 * \param[in,out] alist pointer to boxlist structure where items will be appended
 
325
 * \param blist pointer to boxlist structure with new items
 
326
 *
 
327
 * \return 0 on success
 
328
 * \return 1 on error
 
329
 */
 
330
int Vect_boxlist_append_boxlist(struct boxlist *alist, const struct boxlist *blist)
 
331
{
 
332
    int i;
 
333
 
 
334
    if (alist == NULL || blist == NULL)
 
335
        return 1;
 
336
 
 
337
    if (blist->have_boxes) {
 
338
        for (i = 0; i < blist->n_values; i++)
 
339
            Vect_boxlist_append(alist, blist->id[i], &blist->box[i]);
 
340
    }
 
341
    else {
 
342
        struct bound_box box;
 
343
 
 
344
        box.E = box.W = box.N = box.S = box.T = box.B = 0;
 
345
        for (i = 0; i < blist->n_values; i++)
 
346
            Vect_boxlist_append(alist, blist->id[i], &box);
 
347
    }
 
348
 
 
349
    return 0;
 
350
}
 
351
 
 
352
/**
 
353
 * \brief Remove a given value (item) from list
 
354
 *
 
355
 * \param[in,out] list pointer to boxlist structure
 
356
 * \param id to remove
 
357
 *
 
358
 * \return 0 on success
 
359
 * \return 1 on error
 
360
 */
 
361
int Vect_boxlist_delete(struct boxlist *list, int id)
 
362
{
 
363
    int i, j;
 
364
 
 
365
    if (list == NULL)
 
366
        return 1;
 
367
 
 
368
    for (i = 0; i < list->n_values; i++) {
 
369
        if (id == list->id[i]) {
 
370
            for (j = i + 1; j < list->n_values; j++) {
 
371
                list->id[j - 1] = list->id[j];
 
372
                if (list->have_boxes)
 
373
                    list->box[j - 1] = list->box[j];
 
374
            }
 
375
 
 
376
            list->n_values--;
 
377
            return 0;
 
378
        }
 
379
    }
 
380
 
 
381
    return 0;
 
382
}
 
383
 
 
384
/**
 
385
 * \brief Delete list from existing list 
 
386
 *
 
387
 * \param[in,out] alist pointer to original boxlist structure,
 
388
 * \param blist pointer to boxlist structure with items to delete
 
389
 *
 
390
 * \return 0 on success
 
391
 * \return 1 on error
 
392
 */
 
393
int Vect_boxlist_delete_boxlist(struct boxlist *alist, const struct boxlist *blist)
 
394
{
 
395
    int i;
 
396
 
 
397
    if (alist == NULL || blist == NULL)
 
398
        return 1;
 
399
 
 
400
    for (i = 0; i < blist->n_values; i++)
 
401
        Vect_boxlist_delete(alist, blist->id[i]);
 
402
 
 
403
    return 0;
 
404
}
 
405
 
 
406
/**
 
407
 * \brief Find a given item in the list
 
408
 *
 
409
 * \param list pointer to boxlist structure
 
410
 * \param id value of item
 
411
 *
 
412
 * \return 1 if an item is found
 
413
 * \return 0 no found item in the list
 
414
*/
 
415
int Vect_val_in_boxlist(const struct boxlist *list, int id)
 
416
{
 
417
    int i;
 
418
 
 
419
    if (list == NULL)
 
420
        return 0;
 
421
 
 
422
    for (i = 0; i < list->n_values; i++) {
 
423
        if (id == list->id[i])
 
424
            return 1;
 
425
    }
 
426
 
 
427
    return 0;
 
428
}