PIPS
array.c
Go to the documentation of this file.
1 /*
2 
3  $Id: array.c 1387 2020-07-06 08:36:33Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of NewGen.
8 
9  NewGen is free software: you can redistribute it and/or modify it under the
10  terms of the GNU General Public License as published by the Free Software
11  Foundation, either version 3 of the License, or any later version.
12 
13  NewGen is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16  License for more details.
17 
18  You should have received a copy of the GNU General Public License along with
19  NewGen. If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 #ifdef HAVE_CONFIG_H
23  #include "config.h"
24 #endif
25 
26 #include <stdlib.h>
27 #include "genC.h"
28 
29 #include "newgen_include.h"
30 
31 #define GEN_ARRAY_SIZE_INCREMENT (50)
32 
34  size_t size;
35  int nitems;
36  void ** array;
37 };
38 
40 gen_array_make(size_t size)
41 {
42  gen_array_t a;
43  size_t i;
44  if (size<=0) size= GEN_ARRAY_SIZE_INCREMENT; /* default size */
45  a = (gen_array_t) malloc(sizeof(struct _gen_array_chunk_t));
46  message_assert("array ok", a);
47  a->size = size;
48  a->nitems = 0; /* number of items stored */
49  a->array = (void**) malloc(size*sizeof(void*));
50  message_assert("malloc ok", a->array);
51  for (i=0; i<size; i++) a->array[i] = (void*) NULL;
52  return a;
53 }
54 
55 static void
57 {
59  size_t nsize = ((min%N)==0)?min:((int)(min/N) +1)*N;
60  size_t i;
61  /* int nsize = a->size+GEN_ARRAY_SIZE_INCREMENT, i;*/
62  a->array = (void**) realloc(a->array, nsize*sizeof(void*));
63  message_assert("realloc ok", a->array);
64  for (i=a->size; i<nsize; i++)
65  a->array[i] = (void*) NULL;
66  a->size = nsize;
67 }
68 
69 void
71 {
72  gen_free_area(a->array, a->size*sizeof(void*));
73  gen_free_area((void**)a, sizeof(struct _gen_array_chunk_t));
74 }
75 
76 void
78 {
79  size_t i;
80  for (i=0; i<a->size; i++)
81  if (a->array[i])
82  free(a->array[i]); /* what is it? */
83  gen_array_free(a);
84 }
85 
86 void
87 gen_array_addto(gen_array_t a, size_t i, void * what)
88 {
89  if (i>=a->size) gen_array_resize(a,i+1);
90  message_assert("valid index", /* 0<=i && */ i < a->size);
91  if (a->array[i]!=(void *)NULL) a->nitems--;
92  a->array[i] = what;
93  if (a->array[i]!=(void *)NULL) a->nitems++;
94 }
95 
96 void
98 {
99  message_assert("valid index", /* 0<=i && */ i < a->size);
100  if (a->array[i]!=(void *)NULL) a->nitems--;
101  a->array[i] = (void *)NULL;
102 }
103 
104 void
106 {
107  gen_array_addto(a, a->nitems, what);
108 }
109 
110 void
111 gen_array_dupaddto(gen_array_t a, size_t i, void * what)
112 {
113  gen_array_addto(a, i, strdup(what));
114 }
115 
116 void
118 {
119  gen_array_append(a, strdup(what));
120 }
121 
122 /* Observers...
123  */
124 void **
126 {
127  return a->array;
128 }
129 
130 size_t
132 {
133  return a->nitems;
134 }
135 
136 size_t
138 {
139  return a->size;
140 }
141 
142 void *
143 gen_array_item(const gen_array_t a, size_t i)
144 {
145  message_assert("valid index", /* 0<=i && */ i < a->size);
146  return a->array[i];
147 }
148 
149 /* Sort: assumes that the items are the first ones.
150  */
151 static int
152 gen_array_cmp(const void * a1, const void * a2)
153 {
154  return strcmp(* (char **) a1, * (char **) a2);
155 }
156 
157 void
158 gen_array_sort_with_cmp(gen_array_t a, int (*cmp)(const void *, const void *))
159 {
160  qsort(a->array, a->nitems, sizeof(void *), cmp);
161 }
162 
163 void
165 {
167 }
168 
170 gen_array_from_list(list /* of string */ ls)
171 {
173  MAP(STRING, s, gen_array_dupappend(a, s), ls);
174  return a;
175 }
176 
177 list // of string
179 {
180  list ls = NIL;
181  GEN_ARRAY_FOREACH(string, s, a)
182  ls = CONS(string, strdup(s), ls);
183  return ls;
184 }
185 
186 /* Join a string array with a string separator.
187 
188  @param array is the string array
189  @param separator is the string separator
190 
191  @return a string in a concatenate buffer, so it needs to be strdup()ed
192  quickly if it is expected to last some time in the caller...
193 
194  It is similar to the join() string method in Python. Using the function
195  with ["foo", "bar", "daurade"] and "," should return the string
196  "foo,bar,daurade".
197 */
198 string string_array_join(gen_array_t array, string separator)
199 {
200  string join = "";
201  bool first_iteration = true;
202 
203  GEN_ARRAY_FOREACH(string, s, array)
204  {
205  if (! first_iteration)
206  join = concatenate(join, separator, NULL);
207  else
208  first_iteration = false;
209  join = concatenate(join, s, NULL);
210  }
211 
212  return join;
213 }
void const char const char const int
void gen_array_sort_with_cmp(gen_array_t a, int(*cmp)(const void *, const void *))
Definition: array.c:158
size_t gen_array_nitems(const gen_array_t a)
Definition: array.c:131
void gen_array_full_free(gen_array_t a)
Definition: array.c:77
size_t gen_array_size(const gen_array_t a)
Definition: array.c:137
gen_array_t gen_array_make(size_t size)
declarations...
Definition: array.c:40
string string_array_join(gen_array_t array, string separator)
Join a string array with a string separator.
Definition: array.c:198
void ** gen_array_pointer(const gen_array_t a)
Observers...
Definition: array.c:125
void gen_array_addto(gen_array_t a, size_t i, void *what)
Definition: array.c:87
void gen_array_dupaddto(gen_array_t a, size_t i, void *what)
Definition: array.c:111
list list_from_gen_array(gen_array_t a)
Definition: array.c:178
void gen_array_sort(gen_array_t a)
Definition: array.c:164
gen_array_t gen_array_from_list(list ls)
Definition: array.c:170
void gen_array_remove(gen_array_t a, size_t i)
Definition: array.c:97
void * gen_array_item(const gen_array_t a, size_t i)
Definition: array.c:143
static void gen_array_resize(gen_array_t a, int min)
Definition: array.c:56
void gen_array_append(gen_array_t a, void *what)
Definition: array.c:105
static int gen_array_cmp(const void *a1, const void *a2)
Sort: assumes that the items are the first ones.
Definition: array.c:152
#define GEN_ARRAY_SIZE_INCREMENT
Definition: array.c:31
void gen_array_free(gen_array_t a)
Definition: array.c:70
void gen_array_dupappend(gen_array_t a, void *what)
Definition: array.c:117
#define min(a, b)
#define STRING(x)
Definition: genC.h:87
void * malloc(YYSIZE_T)
void free(void *)
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
void gen_free_area(void **p, int size)
free an area.
Definition: list.c:775
#define MAP(_map_CASTER, _map_item, _map_code, _map_list)
Apply/map an instruction block on all the elements of a list (old fashioned)
Definition: newgen_list.h:226
#define GEN_ARRAY_FOREACH(type, s, array)
Definition: newgen_array.h:50
struct _gen_array_chunk_t * gen_array_t
Definition: newgen_array.h:24
#define message_assert(msg, ex)
Definition: newgen_assert.h:47
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
char * strdup()
static entity array
void ** array
Definition: array.c:36
size_t size
Definition: array.c:34
The structure used to build lists in NewGen.
Definition: newgen_list.h:41