]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/mc-boot/Galists.c
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / mc-boot / Galists.c
1 /* do not edit automatically generated by mc from alists. */
2 /* alists.mod address lists module.
3
4 Copyright (C) 2015-2023 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius@glam.ac.uk>.
6
7 This file is part of GNU Modula-2.
8
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Modula-2; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 # if !defined (PROC_D)
26 # define PROC_D
27 typedef void (*PROC_t) (void);
28 typedef struct { PROC_t proc; } PROC;
29 # endif
30
31 # if !defined (TRUE)
32 # define TRUE (1==1)
33 # endif
34
35 # if !defined (FALSE)
36 # define FALSE (1==0)
37 # endif
38
39 # include "GStorage.h"
40 #if defined(__cplusplus)
41 # undef NULL
42 # define NULL 0
43 #endif
44 #define _alists_H
45 #define _alists_C
46
47 # include "GStorage.h"
48
49 typedef struct alists_performOperation_p alists_performOperation;
50
51 # define MaxnoOfelements 5
52 typedef struct alists__T1_r alists__T1;
53
54 typedef struct alists__T2_a alists__T2;
55
56 typedef alists__T1 *alists_alist;
57
58 typedef void (*alists_performOperation_t) (void *);
59 struct alists_performOperation_p { alists_performOperation_t proc; };
60
61 struct alists__T2_a { void * array[MaxnoOfelements-1+1]; };
62 struct alists__T1_r {
63 unsigned int noOfelements;
64 alists__T2 elements;
65 alists_alist next;
66 };
67
68
69 /*
70 initList - creates a new alist, l.
71 */
72
73 extern "C" alists_alist alists_initList (void);
74
75 /*
76 killList - deletes the complete alist, l.
77 */
78
79 extern "C" void alists_killList (alists_alist *l);
80
81 /*
82 putItemIntoList - places an ADDRESS, c, into alist, l.
83 */
84
85 extern "C" void alists_putItemIntoList (alists_alist l, void * c);
86
87 /*
88 getItemFromList - retrieves the nth WORD from alist, l.
89 */
90
91 extern "C" void * alists_getItemFromList (alists_alist l, unsigned int n);
92
93 /*
94 getIndexOfList - returns the index for WORD, c, in alist, l.
95 If more than one WORD, c, exists the index
96 for the first is returned.
97 */
98
99 extern "C" unsigned int alists_getIndexOfList (alists_alist l, void * c);
100
101 /*
102 noOfItemsInList - returns the number of items in alist, l.
103 */
104
105 extern "C" unsigned int alists_noOfItemsInList (alists_alist l);
106
107 /*
108 includeItemIntoList - adds an ADDRESS, c, into a alist providing
109 the value does not already exist.
110 */
111
112 extern "C" void alists_includeItemIntoList (alists_alist l, void * c);
113
114 /*
115 removeItemFromList - removes a ADDRESS, c, from a alist.
116 It assumes that this value only appears once.
117 */
118
119 extern "C" void alists_removeItemFromList (alists_alist l, void * c);
120
121 /*
122 isItemInList - returns true if a ADDRESS, c, was found in alist, l.
123 */
124
125 extern "C" unsigned int alists_isItemInList (alists_alist l, void * c);
126
127 /*
128 foreachItemInListDo - calls procedure, P, foreach item in alist, l.
129 */
130
131 extern "C" void alists_foreachItemInListDo (alists_alist l, alists_performOperation p);
132
133 /*
134 duplicateList - returns a duplicate alist derived from, l.
135 */
136
137 extern "C" alists_alist alists_duplicateList (alists_alist l);
138
139 /*
140 removeItem - remove an element at index, i, from the alist data type.
141 */
142
143 static void removeItem (alists_alist p, alists_alist l, unsigned int i);
144
145
146 /*
147 removeItem - remove an element at index, i, from the alist data type.
148 */
149
150 static void removeItem (alists_alist p, alists_alist l, unsigned int i)
151 {
152 l->noOfelements -= 1;
153 while (i <= l->noOfelements)
154 {
155 l->elements.array[i-1] = l->elements.array[i+1-1];
156 i += 1;
157 }
158 if ((l->noOfelements == 0) && (p != NULL))
159 {
160 p->next = l->next;
161 Storage_DEALLOCATE ((void **) &l, sizeof (alists__T1));
162 }
163 }
164
165
166 /*
167 initList - creates a new alist, l.
168 */
169
170 extern "C" alists_alist alists_initList (void)
171 {
172 alists_alist l;
173
174 Storage_ALLOCATE ((void **) &l, sizeof (alists__T1));
175 l->noOfelements = 0;
176 l->next = NULL;
177 return l;
178 /* static analysis guarentees a RETURN statement will be used before here. */
179 __builtin_unreachable ();
180 }
181
182
183 /*
184 killList - deletes the complete alist, l.
185 */
186
187 extern "C" void alists_killList (alists_alist *l)
188 {
189 if ((*l) != NULL)
190 {
191 if ((*l)->next != NULL)
192 {
193 alists_killList (&(*l)->next);
194 }
195 Storage_DEALLOCATE ((void **) &(*l), sizeof (alists__T1));
196 }
197 }
198
199
200 /*
201 putItemIntoList - places an ADDRESS, c, into alist, l.
202 */
203
204 extern "C" void alists_putItemIntoList (alists_alist l, void * c)
205 {
206 if (l->noOfelements < MaxnoOfelements)
207 {
208 l->noOfelements += 1;
209 l->elements.array[l->noOfelements-1] = c;
210 }
211 else if (l->next != NULL)
212 {
213 /* avoid dangling else. */
214 alists_putItemIntoList (l->next, c);
215 }
216 else
217 {
218 /* avoid dangling else. */
219 l->next = alists_initList ();
220 alists_putItemIntoList (l->next, c);
221 }
222 }
223
224
225 /*
226 getItemFromList - retrieves the nth WORD from alist, l.
227 */
228
229 extern "C" void * alists_getItemFromList (alists_alist l, unsigned int n)
230 {
231 while (l != NULL)
232 {
233 if (n <= l->noOfelements)
234 {
235 return l->elements.array[n-1];
236 }
237 else
238 {
239 n -= l->noOfelements;
240 }
241 l = l->next;
242 }
243 return reinterpret_cast<void *> (0);
244 /* static analysis guarentees a RETURN statement will be used before here. */
245 __builtin_unreachable ();
246 }
247
248
249 /*
250 getIndexOfList - returns the index for WORD, c, in alist, l.
251 If more than one WORD, c, exists the index
252 for the first is returned.
253 */
254
255 extern "C" unsigned int alists_getIndexOfList (alists_alist l, void * c)
256 {
257 unsigned int i;
258
259 if (l == NULL)
260 {
261 return 0;
262 }
263 else
264 {
265 i = 1;
266 while (i <= l->noOfelements)
267 {
268 if (l->elements.array[i-1] == c)
269 {
270 return i;
271 }
272 else
273 {
274 i += 1;
275 }
276 }
277 return l->noOfelements+(alists_getIndexOfList (l->next, c));
278 }
279 /* static analysis guarentees a RETURN statement will be used before here. */
280 __builtin_unreachable ();
281 }
282
283
284 /*
285 noOfItemsInList - returns the number of items in alist, l.
286 */
287
288 extern "C" unsigned int alists_noOfItemsInList (alists_alist l)
289 {
290 unsigned int t;
291
292 if (l == NULL)
293 {
294 return 0;
295 }
296 else
297 {
298 t = 0;
299 do {
300 t += l->noOfelements;
301 l = l->next;
302 } while (! (l == NULL));
303 return t;
304 }
305 /* static analysis guarentees a RETURN statement will be used before here. */
306 __builtin_unreachable ();
307 }
308
309
310 /*
311 includeItemIntoList - adds an ADDRESS, c, into a alist providing
312 the value does not already exist.
313 */
314
315 extern "C" void alists_includeItemIntoList (alists_alist l, void * c)
316 {
317 if (! (alists_isItemInList (l, c)))
318 {
319 alists_putItemIntoList (l, c);
320 }
321 }
322
323
324 /*
325 removeItemFromList - removes a ADDRESS, c, from a alist.
326 It assumes that this value only appears once.
327 */
328
329 extern "C" void alists_removeItemFromList (alists_alist l, void * c)
330 {
331 alists_alist p;
332 unsigned int i;
333 unsigned int found;
334
335 if (l != NULL)
336 {
337 found = FALSE;
338 p = NULL;
339 do {
340 i = 1;
341 while ((i <= l->noOfelements) && (l->elements.array[i-1] != c))
342 {
343 i += 1;
344 }
345 if ((i <= l->noOfelements) && (l->elements.array[i-1] == c))
346 {
347 found = TRUE;
348 }
349 else
350 {
351 p = l;
352 l = l->next;
353 }
354 } while (! ((l == NULL) || found));
355 if (found)
356 {
357 removeItem (p, l, i);
358 }
359 }
360 }
361
362
363 /*
364 isItemInList - returns true if a ADDRESS, c, was found in alist, l.
365 */
366
367 extern "C" unsigned int alists_isItemInList (alists_alist l, void * c)
368 {
369 unsigned int i;
370
371 do {
372 i = 1;
373 while (i <= l->noOfelements)
374 {
375 if (l->elements.array[i-1] == c)
376 {
377 return TRUE;
378 }
379 else
380 {
381 i += 1;
382 }
383 }
384 l = l->next;
385 } while (! (l == NULL));
386 return FALSE;
387 /* static analysis guarentees a RETURN statement will be used before here. */
388 __builtin_unreachable ();
389 }
390
391
392 /*
393 foreachItemInListDo - calls procedure, P, foreach item in alist, l.
394 */
395
396 extern "C" void alists_foreachItemInListDo (alists_alist l, alists_performOperation p)
397 {
398 unsigned int i;
399 unsigned int n;
400
401 n = alists_noOfItemsInList (l);
402 i = 1;
403 while (i <= n)
404 {
405 (*p.proc) (alists_getItemFromList (l, i));
406 i += 1;
407 }
408 }
409
410
411 /*
412 duplicateList - returns a duplicate alist derived from, l.
413 */
414
415 extern "C" alists_alist alists_duplicateList (alists_alist l)
416 {
417 alists_alist m;
418 unsigned int n;
419 unsigned int i;
420
421 m = alists_initList ();
422 n = alists_noOfItemsInList (l);
423 i = 1;
424 while (i <= n)
425 {
426 alists_putItemIntoList (m, alists_getItemFromList (l, i));
427 i += 1;
428 }
429 return m;
430 /* static analysis guarentees a RETURN statement will be used before here. */
431 __builtin_unreachable ();
432 }
433
434 extern "C" void _M2_alists_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
435 {
436 }
437
438 extern "C" void _M2_alists_finish (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
439 {
440 }