3
* \file lib/vector/Vlib/list.c
5
5
* \brief Vector library - list definition
7
7
* Higher level functions for reading/writing/manipulating vectors.
9
* (C) 2001-2008 by the GRASS Development Team
9
* (C) 2001-2009 by the GRASS Development Team
11
11
* This program is free software under the GNU General Public
12
12
* License (>=v2). Read the file COPYING that comes with GRASS
15
15
* \author Original author CERL, probably Dave Gerdes or Mike Higgins.
16
* Update to GRASS 5.7 Radim Blazek and David D. Gray
16
* \author Update to GRASS 5.7 Radim Blazek and David D. Gray
17
* \author Update to GRASS 7 Markus Metz
21
20
#include <stdlib.h>
22
#include <grass/Vect.h>
23
#include <grass/gis.h>
21
#include <grass/vector.h>
26
24
* \brief Creates and initializes a struct ilist.
212
/* box list routines */
215
* \brief Creates and initializes a struct boxlist.
217
* This structure is used as container for bounding boxes with id. The
218
* library routines handle all memory allocation.
220
* \param have_boxes if set to 0, the list will hold only ids and no boxes
222
* \return pointer to struct boxlist
223
* \return NULL on error
225
struct boxlist *Vect_new_boxlist(int have_boxes)
229
p = (struct boxlist *)G_malloc(sizeof(struct boxlist));
234
p->have_boxes = have_boxes != 0;
243
* \brief Reset boxlist structure.
245
* To make sure boxlist structure is clean to be re-used. List must have
246
* previously been created with Vect_new_boxlist().
248
* \param[in,out] list pointer to struct boxlist
252
int Vect_reset_boxlist(struct boxlist *list)
260
* \brief Frees all memory associated with a struct boxlist, including
263
* \param[in,out] list pointer to ilist structure
265
void Vect_destroy_boxlist(struct boxlist *list)
267
if (list) { /* probably a moot test */
268
if (list->alloc_values) {
269
G_free((void *)list->id);
271
G_free((void *)list->box);
273
G_free((void *)list);
279
* \brief Append new item to the end of list if not yet present
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
285
* \return 0 on success
288
int Vect_boxlist_append(struct boxlist *list, int id, const struct bound_box *box)
296
for (i = 0; i < list->n_values; i++) {
297
if (id == list->id[i])
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);
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);
310
list->alloc_values = list->n_values + 1000;
313
list->id[list->n_values] = id;
314
if (list->have_boxes)
315
list->box[list->n_values] = *box;
322
* \brief Append new items to the end of list if not yet present
324
* \param[in,out] alist pointer to boxlist structure where items will be appended
325
* \param blist pointer to boxlist structure with new items
327
* \return 0 on success
330
int Vect_boxlist_append_boxlist(struct boxlist *alist, const struct boxlist *blist)
334
if (alist == NULL || blist == NULL)
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]);
342
struct bound_box box;
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);
353
* \brief Remove a given value (item) from list
355
* \param[in,out] list pointer to boxlist structure
356
* \param id to remove
358
* \return 0 on success
361
int Vect_boxlist_delete(struct boxlist *list, int id)
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];
385
* \brief Delete list from existing list
387
* \param[in,out] alist pointer to original boxlist structure,
388
* \param blist pointer to boxlist structure with items to delete
390
* \return 0 on success
393
int Vect_boxlist_delete_boxlist(struct boxlist *alist, const struct boxlist *blist)
397
if (alist == NULL || blist == NULL)
400
for (i = 0; i < blist->n_values; i++)
401
Vect_boxlist_delete(alist, blist->id[i]);
407
* \brief Find a given item in the list
409
* \param list pointer to boxlist structure
410
* \param id value of item
412
* \return 1 if an item is found
413
* \return 0 no found item in the list
415
int Vect_val_in_boxlist(const struct boxlist *list, int id)
422
for (i = 0; i < list->n_values; i++) {
423
if (id == list->id[i])