GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
vector/Vlib/list.c
Go to the documentation of this file.
1/*!
2 * \file lib/vector/Vlib/list.c
3 *
4 * \brief Vector library - list definition
5 *
6 * Higher level functions for reading/writing/manipulating vectors.
7 *
8 * SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 *
11 * \author Original author CERL, probably Dave Gerdes or Mike Higgins.
12 * \author Update to GRASS 5.7 Radim Blazek and David D. Gray
13 * \author Update to GRASS 7 Markus Metz
14 */
15
16#include <stdlib.h>
17#include <grass/vector.h>
18
19/**
20 * \brief Creates and initializes a struct ilist.
21 *
22 * This structure is used as container for integer values. The
23 * library routines handle all memory allocation.
24 *
25 * \return pointer to struct ilist
26 * \return NULL on error
27 */
28struct ilist *Vect_new_list(void)
29{
30 struct ilist *p;
31
32 p = (struct ilist *)G_malloc(sizeof(struct ilist));
33
34 if (p) {
35 p->value = NULL;
36 p->n_values = 0;
37 p->alloc_values = 0;
38 }
39
40 return p;
41}
42
43/**
44 * \brief Reset ilist structure.
45 *
46 * To make sure ilist structure is clean to be re-used. List must have
47 * previously been created with Vect_new_list().
48 *
49 * \param[in,out] list pointer to struct ilist
50 *
51 * \return 0
52 */
54{
55 list->n_values = 0;
56
57 return 0;
58}
59
60/**
61 * \brief Frees all memory associated with a struct ilist, including
62 * the struct itself
63 *
64 * \param[in,out] list pointer to ilist structure
65 */
67{
68 if (list) { /* probably a moot test */
69 if (list->alloc_values) {
70 G_free((void *)list->value);
71 }
72 G_free((void *)list);
73 }
74 list = NULL;
75}
76
77/**
78 * \brief Append new item to the end of list if not yet present
79 *
80 * \param[in,out] list pointer to ilist structure
81 * \param val new item to append to the end of list
82 *
83 * \return 0 on success
84 * \return 1 on error
85 */
86int Vect_list_append(struct ilist *list, int val)
87{
88 int i;
89 size_t size;
90
91 if (list == NULL)
92 return 1;
93
94 for (i = 0; i < list->n_values; i++) {
95 if (val == list->value[i])
96 return 0;
97 }
98
99 if (list->n_values == list->alloc_values) {
100 size = (list->n_values + 1000) * sizeof(int);
101 list->value = (int *)G_realloc((void *)list->value, size);
102 list->alloc_values = list->n_values + 1000;
103 }
104
105 list->value[list->n_values] = val;
106 list->n_values++;
107
108 return 0;
109}
110
111/**
112 * \brief Append new items to the end of list if not yet present
113 *
114 * \param[in,out] alist pointer to ilist structure where items will be appended
115 * \param blist pointer to ilist structure with new items
116 *
117 * \return 0 on success
118 * \return 1 on error
119 */
120int Vect_list_append_list(struct ilist *alist, const struct ilist *blist)
121{
122 int i;
123
124 if (alist == NULL || blist == NULL)
125 return 1;
126
127 for (i = 0; i < blist->n_values; i++)
128 Vect_list_append(alist, blist->value[i]);
129
130 return 0;
131}
132
133/**
134 * \brief Remove a given value (item) from list
135 *
136 * \param[in,out] list pointer to ilist structure
137 * \param val to remove
138 *
139 * \return 0 on success
140 * \return 1 on error
141 */
142int Vect_list_delete(struct ilist *list, int val)
143{
144 int i, j;
145
146 if (list == NULL)
147 return 1;
148
149 for (i = 0; i < list->n_values; i++) {
150 if (val == list->value[i]) {
151 for (j = i + 1; j < list->n_values; j++)
152 list->value[j - 1] = list->value[j];
153
154 list->n_values--;
155 return 0;
156 }
157 }
158
159 return 0;
160}
161
162/**
163 * \brief Delete list from existing list
164 *
165 * \param[in,out] alist pointer to original ilist structure,
166 * \param blist pointer to ilist structure with items to delete
167 *
168 * \return 0 on success
169 * \return 1 on error
170 */
171int Vect_list_delete_list(struct ilist *alist, const struct ilist *blist)
172{
173 int i;
174
175 if (alist == NULL || blist == NULL)
176 return 1;
177
178 for (i = 0; i < blist->n_values; i++)
179 Vect_list_delete(alist, blist->value[i]);
180
181 return 0;
182}
183
184/**
185 * \brief Find a given item in the list
186 *
187 * \param list pointer to ilist structure
188 * \param val value of item
189 *
190 * \return 1 if an item is found
191 * \return 0 no found item in the list
192 */
193int Vect_val_in_list(const struct ilist *list, int val)
194{
195 int i;
196
197 if (list == NULL)
198 return 0;
199
200 for (i = 0; i < list->n_values; i++) {
201 if (val == list->value[i])
202 return 1;
203 }
204
205 return 0;
206}
207
208/* box list routines */
209
210/**
211 * \brief Creates and initializes a struct boxlist.
212 *
213 * This structure is used as container for bounding boxes with id. The
214 * library routines handle all memory allocation.
215 *
216 * \param have_boxes if set to 0, the list will hold only ids and no boxes
217 *
218 * \return pointer to struct boxlist
219 * \return NULL on error
220 */
222{
223 struct boxlist *p;
224
225 p = (struct boxlist *)G_malloc(sizeof(struct boxlist));
226
227 if (p) {
228 p->id = NULL;
229 p->box = NULL;
230 p->have_boxes = have_boxes != 0;
231 p->n_values = 0;
232 p->alloc_values = 0;
233 }
234
235 return p;
236}
237
238/**
239 * \brief Reset boxlist structure.
240 *
241 * To make sure boxlist structure is clean to be re-used. List must have
242 * previously been created with Vect_new_boxlist().
243 *
244 * \param[in,out] list pointer to struct boxlist
245 *
246 * \return 0
247 */
249{
250 list->n_values = 0;
251
252 return 0;
253}
254
255/**
256 * \brief Frees all memory associated with a struct boxlist, including
257 * the struct itself
258 *
259 * \param[in,out] list pointer to ilist structure
260 */
262{
263 if (list) { /* probably a moot test */
264 if (list->alloc_values) {
265 G_free((void *)list->id);
266 if (list->box)
267 G_free((void *)list->box);
268 }
269 G_free((void *)list);
270 }
271 list = NULL;
272}
273
274/**
275 * \brief Append new item to the end of list if not yet present
276 *
277 * \param[in,out] list pointer to ilist structure
278 * \param id new item to append to the end of list
279 * \param box bounding box
280 *
281 * \return 0 on success
282 * \return 1 on error
283 */
284int Vect_boxlist_append(struct boxlist *list, int id,
285 const struct bound_box *box)
286{
287 int i;
288 size_t size;
289
290 if (list == NULL)
291 return 1;
292
293 for (i = 0; i < list->n_values; i++) {
294 if (id == list->id[i])
295 return 0;
296 }
297
298 if (list->n_values == list->alloc_values) {
299 size = (list->n_values + 1000) * sizeof(int);
300 list->id = (int *)G_realloc((void *)list->id, size);
301
302 if (list->have_boxes) {
303 size = (list->n_values + 1000) * sizeof(struct bound_box);
304 list->box = (struct bound_box *)G_realloc((void *)list->box, size);
305 }
306
307 list->alloc_values = list->n_values + 1000;
308 }
309
310 list->id[list->n_values] = id;
311 if (list->have_boxes)
312 list->box[list->n_values] = *box;
313 list->n_values++;
314
315 return 0;
316}
317
318/**
319 * \brief Append new items to the end of list if not yet present
320 *
321 * \param[in,out] alist pointer to boxlist structure where items will be
322 * appended \param blist pointer to boxlist structure with new items
323 *
324 * \return 0 on success
325 * \return 1 on error
326 */
328 const struct boxlist *blist)
329{
330 int i;
331
332 if (alist == NULL || blist == NULL)
333 return 1;
334
335 if (blist->have_boxes) {
336 for (i = 0; i < blist->n_values; i++)
337 Vect_boxlist_append(alist, blist->id[i], &blist->box[i]);
338 }
339 else {
340 struct bound_box box;
341
342 box.E = box.W = box.N = box.S = box.T = box.B = 0;
343 for (i = 0; i < blist->n_values; i++)
344 Vect_boxlist_append(alist, blist->id[i], &box);
345 }
346
347 return 0;
348}
349
350/**
351 * \brief Remove a given value (item) from list
352 *
353 * \param[in,out] list pointer to boxlist structure
354 * \param id to remove
355 *
356 * \return 0 on success
357 * \return 1 on error
358 */
359int Vect_boxlist_delete(struct boxlist *list, int id)
360{
361 int i, j;
362
363 if (list == NULL)
364 return 1;
365
366 for (i = 0; i < list->n_values; i++) {
367 if (id == list->id[i]) {
368 for (j = i + 1; j < list->n_values; j++) {
369 list->id[j - 1] = list->id[j];
370 if (list->have_boxes)
371 list->box[j - 1] = list->box[j];
372 }
373
374 list->n_values--;
375 return 0;
376 }
377 }
378
379 return 0;
380}
381
382/**
383 * \brief Delete list from existing list
384 *
385 * \param[in,out] alist pointer to original boxlist structure,
386 * \param blist pointer to boxlist structure with items to delete
387 *
388 * \return 0 on success
389 * \return 1 on error
390 */
392 const struct boxlist *blist)
393{
394 int i;
395
396 if (alist == NULL || blist == NULL)
397 return 1;
398
399 for (i = 0; i < blist->n_values; i++)
401
402 return 0;
403}
404
405/**
406 * \brief Find a given item in the list
407 *
408 * \param list pointer to boxlist structure
409 * \param id value of item
410 *
411 * \return 1 if an item is found
412 * \return 0 no found item in the list
413 */
414int Vect_val_in_boxlist(const struct boxlist *list, int id)
415{
416 int i;
417
418 if (list == NULL)
419 return 0;
420
421 for (i = 0; i < list->n_values; i++) {
422 if (id == list->id[i])
423 return 1;
424 }
425
426 return 0;
427}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_realloc(p, n)
Definition defs/gis.h:138
#define G_malloc(n)
Definition defs/gis.h:136
Bounding box.
Definition dig_structs.h:62
double W
West.
Definition dig_structs.h:78
double T
Top.
Definition dig_structs.h:82
double S
South.
Definition dig_structs.h:70
double N
North.
Definition dig_structs.h:66
double E
East.
Definition dig_structs.h:74
double B
Bottom.
Definition dig_structs.h:86
List of bounding boxes with id.
int alloc_values
Allocated space for items.
int * id
Array of ids.
struct bound_box * box
Array of bounding boxes.
int n_values
Number of items in the list.
int have_boxes
flag to indicate whether bounding boxes should be added
List of integers.
Definition gis.h:712
int n_values
Number of values in the list.
Definition gis.h:720
int * value
Array of values.
Definition gis.h:716
int alloc_values
Allocated space for values.
Definition gis.h:724
Definition manage.h:4
struct boxlist * Vect_new_boxlist(int have_boxes)
Creates and initializes a struct boxlist.
void Vect_destroy_list(struct ilist *list)
Frees all memory associated with a struct ilist, including the struct itself.
int Vect_reset_list(struct ilist *list)
Reset ilist structure.
int Vect_val_in_list(const struct ilist *list, int val)
Find a given item in the list.
int Vect_boxlist_delete(struct boxlist *list, int id)
Remove a given value (item) from list.
int Vect_list_delete(struct ilist *list, int val)
Remove a given value (item) from list.
int Vect_list_append_list(struct ilist *alist, const struct ilist *blist)
Append new items to the end of list if not yet present.
void Vect_destroy_boxlist(struct boxlist *list)
Frees all memory associated with a struct boxlist, including the struct itself.
int Vect_val_in_boxlist(const struct boxlist *list, int id)
Find a given item in the list.
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.
int Vect_list_delete_list(struct ilist *alist, const struct ilist *blist)
Delete list from existing list.
int Vect_reset_boxlist(struct boxlist *list)
Reset boxlist structure.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_boxlist_append_boxlist(struct boxlist *alist, const struct boxlist *blist)
Append new items to the end of list if not yet present.
int Vect_boxlist_delete_boxlist(struct boxlist *alist, const struct boxlist *blist)
Delete list from existing list.
int Vect_list_append(struct ilist *list, int val)
Append new item to the end of list if not yet present.