GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
vector/Vlib/list.c
Go to the documentation of this file.
1 
2 /*!
3  * \file lib/vector/Vlib/list.c
4  *
5  * \brief Vector library - list definition
6  *
7  * Higher level functions for reading/writing/manipulating vectors.
8  *
9  * (C) 2001-2009 by the GRASS Development Team
10  *
11  * This program is free software under the GNU General Public
12  * License (>=v2). Read the file COPYING that comes with GRASS
13  * for details.
14  *
15  * \author Original author CERL, probably Dave Gerdes or Mike Higgins.
16  * \author Update to GRASS 5.7 Radim Blazek and David D. Gray
17  * \author Update to GRASS 7 Markus Metz
18  */
19 
20 #include <stdlib.h>
21 #include <grass/vector.h>
22 
23 /**
24  * \brief Creates and initializes a struct ilist.
25  *
26  * This structure is used as container for integer values. The
27  * library routines handle all memory allocation.
28  *
29  * \return pointer to struct ilist
30  * \return NULL on error
31  */
32 struct ilist *Vect_new_list(void)
33 {
34  struct ilist *p;
35 
36  p = (struct ilist *)G_malloc(sizeof(struct ilist));
37 
38  if (p) {
39  p->value = NULL;
40  p->n_values = 0;
41  p->alloc_values = 0;
42  }
43 
44  return p;
45 }
46 
47 /**
48  * \brief Reset ilist structure.
49  *
50  * To make sure ilist structure is clean to be re-used. List must have
51  * previously been created with Vect_new_list().
52  *
53  * \param[in,out] list pointer to struct ilist
54  *
55  * \return 0
56  */
58 {
59  list->n_values = 0;
60 
61  return 0;
62 }
63 
64 /**
65  * \brief Frees all memory associated with a struct ilist, including
66  * the struct itself
67  *
68  * \param[in,out] list pointer to ilist structure
69  */
71 {
72  if (list) { /* probably a moot test */
73  if (list->alloc_values) {
74  G_free((void *)list->value);
75  }
76  G_free((void *)list);
77  }
78  list = NULL;
79 }
80 
81 /**
82  * \brief Append new item to the end of list if not yet present
83  *
84  * \param[in,out] list pointer to ilist structure
85  * \param val new item to append to the end of list
86  *
87  * \return 0 on success
88  * \return 1 on error
89  */
90 int Vect_list_append(struct ilist *list, int val)
91 {
92  int i;
93  size_t size;
94 
95  if (list == NULL)
96  return 1;
97 
98  for (i = 0; i < list->n_values; i++) {
99  if (val == list->value[i])
100  return 0;
101  }
102 
103  if (list->n_values == list->alloc_values) {
104  size = (list->n_values + 1000) * sizeof(int);
105  list->value = (int *)G_realloc((void *)list->value, size);
106  list->alloc_values = list->n_values + 1000;
107  }
108 
109  list->value[list->n_values] = val;
110  list->n_values++;
111 
112  return 0;
113 }
114 
115 /**
116  * \brief Append new items to the end of list if not yet present
117  *
118  * \param[in,out] alist pointer to ilist structure where items will be appended
119  * \param blist pointer to ilist structure with new items
120  *
121  * \return 0 on success
122  * \return 1 on error
123  */
124 int Vect_list_append_list(struct ilist *alist, const struct ilist *blist)
125 {
126  int i;
127 
128  if (alist == NULL || blist == NULL)
129  return 1;
130 
131  for (i = 0; i < blist->n_values; i++)
132  Vect_list_append(alist, blist->value[i]);
133 
134  return 0;
135 }
136 
137 /**
138  * \brief Remove a given value (item) from list
139  *
140  * \param[in,out] list pointer to ilist structure
141  * \param val to remove
142  *
143  * \return 0 on success
144  * \return 1 on error
145  */
146 int Vect_list_delete(struct ilist *list, int val)
147 {
148  int i, j;
149 
150  if (list == NULL)
151  return 1;
152 
153  for (i = 0; i < list->n_values; i++) {
154  if (val == list->value[i]) {
155  for (j = i + 1; j < list->n_values; j++)
156  list->value[j - 1] = list->value[j];
157 
158  list->n_values--;
159  return 0;
160  }
161  }
162 
163  return 0;
164 }
165 
166 /**
167  * \brief Delete list from existing list
168  *
169  * \param[in,out] alist pointer to original ilist structure,
170  * \param blist pointer to ilist structure with items to delete
171  *
172  * \return 0 on success
173  * \return 1 on error
174  */
175 int Vect_list_delete_list(struct ilist *alist, const struct ilist *blist)
176 {
177  int i;
178 
179  if (alist == NULL || blist == NULL)
180  return 1;
181 
182  for (i = 0; i < blist->n_values; i++)
183  Vect_list_delete(alist, blist->value[i]);
184 
185  return 0;
186 }
187 
188 /**
189  * \brief Find a given item in the list
190  *
191  * \param list pointer to ilist structure
192  * \param val value of item
193  *
194  * \return 1 if an item is found
195  * \return 0 no found item in the list
196 */
197 int Vect_val_in_list(const struct ilist *list, int val)
198 {
199  int i;
200 
201  if (list == NULL)
202  return 0;
203 
204  for (i = 0; i < list->n_values; i++) {
205  if (val == list->value[i])
206  return 1;
207  }
208 
209  return 0;
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  */
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  */
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  */
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 }
struct boxlist * Vect_new_boxlist(int have_boxes)
Creates and initializes a struct boxlist.
#define G_malloc(n)
Definition: defs/gis.h:112
int Vect_list_append_list(struct ilist *alist, const struct ilist *blist)
Append new items to the end of list if not yet present.
Bounding box.
Definition: dig_structs.h:65
int Vect_val_in_boxlist(const struct boxlist *list, int id)
Find a given item in the list.
int * id
Array of ids.
Definition: dig_structs.h:1755
int Vect_boxlist_append(struct boxlist *list, int id, const struct bound_box *box)
Append new item to the end of list if not yet present.
double W
West.
Definition: dig_structs.h:82
void Vect_destroy_list(struct ilist *list)
Frees all memory associated with a struct ilist, including the struct itself.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_list_delete(struct ilist *list, int val)
Remove a given value (item) from list.
void Vect_destroy_boxlist(struct boxlist *list)
Frees all memory associated with a struct boxlist, including the struct itself.
int n_values
Number of values in the list.
Definition: gis.h:698
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
double E
East.
Definition: dig_structs.h:78
#define NULL
Definition: ccmath.h:32
int n_values
Number of items in the list.
Definition: dig_structs.h:1767
struct bound_box * box
Array of bounding boxes.
Definition: dig_structs.h:1759
int Vect_boxlist_delete_boxlist(struct boxlist *alist, const struct boxlist *blist)
Delete list from existing list.
double N
North.
Definition: dig_structs.h:70
double B
Bottom.
Definition: dig_structs.h:90
int Vect_reset_list(struct ilist *list)
Reset ilist structure.
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.
int Vect_boxlist_append_boxlist(struct boxlist *alist, const struct boxlist *blist)
Append new items to the end of list if not yet present.
double T
Top.
Definition: dig_structs.h:86
int alloc_values
Allocated space for values.
Definition: gis.h:702
int have_boxes
flag to indicate whether bounding boxes should be added
Definition: dig_structs.h:1763
int alloc_values
Allocated space for items.
Definition: dig_structs.h:1771
Definition: manage.h:4
List of bounding boxes with id.
Definition: dig_structs.h:1750
double S
South.
Definition: dig_structs.h:74
int Vect_val_in_list(const struct ilist *list, int val)
Find a given item in the list.
#define G_realloc(p, n)
Definition: defs/gis.h:114
List of integers.
Definition: gis.h:689
int * value
Array of values.
Definition: gis.h:694
int Vect_reset_boxlist(struct boxlist *list)
Reset boxlist structure.
int Vect_list_delete_list(struct ilist *alist, const struct ilist *blist)
Delete list from existing list.
int Vect_boxlist_delete(struct boxlist *list, int id)
Remove a given value (item) from list.