GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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()
44 {
45  struct ilist *l = G_malloc(sizeof(struct ilist));
46  l->value = NULL;
47  G_init_ilist(l);
48  return l;
49 }
50 
51 /**
52  * \brief Init an integer list and free allocated memory
53  *
54  * \param list The pointer to an integer list
55  *
56  * */
57 void G_init_ilist(struct ilist *list)
58 {
59  if(list->value)
60  G_free(list->value);
61  list->value = NULL;
62  list->n_values = 0;
63  list->alloc_values = 0;
64 }
65 
66 /**
67  * \brief Add item to ilist
68  *
69  * This function adds an integer to the list but does not check for duplicates.
70  * In case reallocation fails, G_fatal_error() will be invoked by the
71  * allocation function.
72  *
73  * \param list The ilist pointer
74  * \param val The value to attach
75  *
76  * */
77 void G_ilist_add(struct ilist *list, int val)
78 {
79  if (list->n_values == list->alloc_values) {
80  size_t size = (list->n_values + 1000) * sizeof(int);
81  void *p = G_realloc((void *)list->value, size);
82 
83  list->value = (int *)p;
84  list->alloc_values = list->n_values + 1000;
85  }
86 
87  list->value[list->n_values] = val;
88  list->n_values++;
89 }
#define G_malloc(n)
Definition: defs/gis.h:112
int n_values
Number of values in the list.
Definition: gis.h:709
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
#define NULL
Definition: ccmath.h:32
double l
Definition: r_raster.c:39
void G_free_ilist(struct ilist *list)
Free allocated memory of an integer list.
Definition: ilist.c:27
int alloc_values
Allocated space for values.
Definition: gis.h:713
Definition: manage.h:4
void G_init_ilist(struct ilist *list)
Init an integer list and free allocated memory.
Definition: ilist.c:57
#define G_realloc(p, n)
Definition: defs/gis.h:114
List of integers.
Definition: gis.h:700
int * value
Array of values.
Definition: gis.h:705
struct ilist * G_new_ilist()
Return a new integer list.
Definition: ilist.c:43
void G_ilist_add(struct ilist *list, int val)
Add item to ilist.
Definition: ilist.c:77