GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-602118adcc
ilist.c
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * MODULE: gis library
5  *
6  * AUTHOR(S): Original author CERL, probably Dave Gerdes.
7  * Update to GRASS 5.7 Radim Blazek.
8  *
9  * PURPOSE: Lower level functions for reading and manipulating integer list
10  *
11  * COPYRIGHT: (C) 2001 by the GRASS Development Team
12  *
13  * This program is free software under the GNU General Public
14  * License (>=v2). Read the file COPYING that comes with GRASS
15  * for details.
16  *
17  *****************************************************************************/
18 #include <stdlib.h>
19 #include <grass/gis.h>
20 
21 /**
22  * \brief Free allocated memory of an integer list
23  *
24  * \param list The pointer to an integer list
25  *
26  * */
27 void G_free_ilist(struct ilist *list)
28 {
29  if (list->value)
30  G_free(list->value);
31  G_free(list);
32 }
33 
34 /**
35  * \brief Return a new integer list.
36  *
37  * G_fatal_error() will be invoked by the
38  * allocation function.
39  *
40  * \return list The pointer to a new allocated integer list
41  *
42  * */
43 struct ilist *G_new_ilist(void)
44 {
45  struct ilist *l = G_malloc(sizeof(struct ilist));
46 
47  l->value = NULL;
48  G_init_ilist(l);
49  return l;
50 }
51 
52 /**
53  * \brief Init an integer list and free allocated memory
54  *
55  * \param list The pointer to an integer list
56  *
57  * */
58 void G_init_ilist(struct ilist *list)
59 {
60  if (list->value)
61  G_free(list->value);
62  list->value = NULL;
63  list->n_values = 0;
64  list->alloc_values = 0;
65 }
66 
67 /**
68  * \brief Add item to ilist
69  *
70  * This function adds an integer to the list but does not check for duplicates.
71  * In case reallocation fails, G_fatal_error() will be invoked by the
72  * allocation function.
73  *
74  * \param list The ilist pointer
75  * \param val The value to attach
76  *
77  * */
78 void G_ilist_add(struct ilist *list, int val)
79 {
80  if (list->n_values == list->alloc_values) {
81  size_t size = (list->n_values + 1000) * sizeof(int);
82  void *p = G_realloc((void *)list->value, size);
83 
84  list->value = (int *)p;
85  list->alloc_values = list->n_values + 1000;
86  }
87 
88  list->value[list->n_values] = val;
89  list->n_values++;
90 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_realloc(p, n)
Definition: defs/gis.h:96
#define G_malloc(n)
Definition: defs/gis.h:94
struct ilist * G_new_ilist(void)
Return a new integer list.
Definition: ilist.c:43
void G_init_ilist(struct ilist *list)
Init an integer list and free allocated memory.
Definition: ilist.c:58
void G_ilist_add(struct ilist *list, int val)
Add item to ilist.
Definition: ilist.c:78
void G_free_ilist(struct ilist *list)
Free allocated memory of an integer list.
Definition: ilist.c:27
double l
Definition: r_raster.c:39
List of integers.
Definition: gis.h:709
Definition: manage.h:4