]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/mc-boot/Gwlists.c
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / mc-boot / Gwlists.c
1 /* do not edit automatically generated by mc from wlists. */
2 /* wlists.mod word 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 _wlists_H
45 #define _wlists_C
46
47 # include "GStorage.h"
48
49 typedef struct wlists_performOperation_p wlists_performOperation;
50
51 # define maxNoOfElements 5
52 typedef struct wlists__T1_r wlists__T1;
53
54 typedef struct wlists__T2_a wlists__T2;
55
56 typedef wlists__T1 *wlists_wlist;
57
58 typedef void (*wlists_performOperation_t) (unsigned int);
59 struct wlists_performOperation_p { wlists_performOperation_t proc; };
60
61 struct wlists__T2_a { unsigned int array[maxNoOfElements-1+1]; };
62 struct wlists__T1_r {
63 unsigned int noOfElements;
64 wlists__T2 elements;
65 wlists_wlist next;
66 };
67
68
69 /*
70 initList - creates a new wlist, l.
71 */
72
73 extern "C" wlists_wlist wlists_initList (void);
74
75 /*
76 killList - deletes the complete wlist, l.
77 */
78
79 extern "C" void wlists_killList (wlists_wlist *l);
80
81 /*
82 putItemIntoList - places an WORD, c, into wlist, l.
83 */
84
85 extern "C" void wlists_putItemIntoList (wlists_wlist l, unsigned int c);
86
87 /*
88 getItemFromList - retrieves the nth WORD from wlist, l.
89 */
90
91 extern "C" unsigned int wlists_getItemFromList (wlists_wlist l, unsigned int n);
92
93 /*
94 getIndexOfList - returns the index for WORD, c, in wlist, l.
95 If more than one WORD, c, exists the index
96 for the first is returned.
97 */
98
99 extern "C" unsigned int wlists_getIndexOfList (wlists_wlist l, unsigned int c);
100
101 /*
102 noOfItemsInList - returns the number of items in wlist, l.
103 */
104
105 extern "C" unsigned int wlists_noOfItemsInList (wlists_wlist l);
106
107 /*
108 includeItemIntoList - adds an WORD, c, into a wlist providing
109 the value does not already exist.
110 */
111
112 extern "C" void wlists_includeItemIntoList (wlists_wlist l, unsigned int c);
113
114 /*
115 removeItemFromList - removes a WORD, c, from a wlist.
116 It assumes that this value only appears once.
117 */
118
119 extern "C" void wlists_removeItemFromList (wlists_wlist l, unsigned int c);
120
121 /*
122 replaceItemInList - replace the nth WORD in wlist, l.
123 The first item in a wlists is at index, 1.
124 If the index, n, is out of range nothing is changed.
125 */
126
127 extern "C" void wlists_replaceItemInList (wlists_wlist l, unsigned int n, unsigned int w);
128
129 /*
130 isItemInList - returns true if a WORD, c, was found in wlist, l.
131 */
132
133 extern "C" unsigned int wlists_isItemInList (wlists_wlist l, unsigned int c);
134
135 /*
136 foreachItemInListDo - calls procedure, P, foreach item in wlist, l.
137 */
138
139 extern "C" void wlists_foreachItemInListDo (wlists_wlist l, wlists_performOperation p);
140
141 /*
142 duplicateList - returns a duplicate wlist derived from, l.
143 */
144
145 extern "C" wlists_wlist wlists_duplicateList (wlists_wlist l);
146
147 /*
148 removeItem - remove an element at index, i, from the wlist data type.
149 */
150
151 static void removeItem (wlists_wlist p, wlists_wlist l, unsigned int i);
152
153
154 /*
155 removeItem - remove an element at index, i, from the wlist data type.
156 */
157
158 static void removeItem (wlists_wlist p, wlists_wlist l, unsigned int i)
159 {
160 l->noOfElements -= 1;
161 while (i <= l->noOfElements)
162 {
163 l->elements.array[i-1] = l->elements.array[i+1-1];
164 i += 1;
165 }
166 if ((l->noOfElements == 0) && (p != NULL))
167 {
168 p->next = l->next;
169 Storage_DEALLOCATE ((void **) &l, sizeof (wlists__T1));
170 }
171 }
172
173
174 /*
175 initList - creates a new wlist, l.
176 */
177
178 extern "C" wlists_wlist wlists_initList (void)
179 {
180 wlists_wlist l;
181
182 Storage_ALLOCATE ((void **) &l, sizeof (wlists__T1));
183 l->noOfElements = 0;
184 l->next = NULL;
185 return l;
186 /* static analysis guarentees a RETURN statement will be used before here. */
187 __builtin_unreachable ();
188 }
189
190
191 /*
192 killList - deletes the complete wlist, l.
193 */
194
195 extern "C" void wlists_killList (wlists_wlist *l)
196 {
197 if ((*l) != NULL)
198 {
199 if ((*l)->next != NULL)
200 {
201 wlists_killList (&(*l)->next);
202 }
203 Storage_DEALLOCATE ((void **) &(*l), sizeof (wlists__T1));
204 }
205 }
206
207
208 /*
209 putItemIntoList - places an WORD, c, into wlist, l.
210 */
211
212 extern "C" void wlists_putItemIntoList (wlists_wlist l, unsigned int c)
213 {
214 if (l->noOfElements < maxNoOfElements)
215 {
216 l->noOfElements += 1;
217 l->elements.array[l->noOfElements-1] = c;
218 }
219 else if (l->next != NULL)
220 {
221 /* avoid dangling else. */
222 wlists_putItemIntoList (l->next, c);
223 }
224 else
225 {
226 /* avoid dangling else. */
227 l->next = wlists_initList ();
228 wlists_putItemIntoList (l->next, c);
229 }
230 }
231
232
233 /*
234 getItemFromList - retrieves the nth WORD from wlist, l.
235 */
236
237 extern "C" unsigned int wlists_getItemFromList (wlists_wlist l, unsigned int n)
238 {
239 while (l != NULL)
240 {
241 if (n <= l->noOfElements)
242 {
243 return l->elements.array[n-1];
244 }
245 else
246 {
247 n -= l->noOfElements;
248 }
249 l = l->next;
250 }
251 return static_cast<unsigned int> (0);
252 /* static analysis guarentees a RETURN statement will be used before here. */
253 __builtin_unreachable ();
254 }
255
256
257 /*
258 getIndexOfList - returns the index for WORD, c, in wlist, l.
259 If more than one WORD, c, exists the index
260 for the first is returned.
261 */
262
263 extern "C" unsigned int wlists_getIndexOfList (wlists_wlist l, unsigned int c)
264 {
265 unsigned int i;
266
267 if (l == NULL)
268 {
269 return 0;
270 }
271 else
272 {
273 i = 1;
274 while (i <= l->noOfElements)
275 {
276 if (l->elements.array[i-1] == c)
277 {
278 return i;
279 }
280 else
281 {
282 i += 1;
283 }
284 }
285 return l->noOfElements+(wlists_getIndexOfList (l->next, c));
286 }
287 /* static analysis guarentees a RETURN statement will be used before here. */
288 __builtin_unreachable ();
289 }
290
291
292 /*
293 noOfItemsInList - returns the number of items in wlist, l.
294 */
295
296 extern "C" unsigned int wlists_noOfItemsInList (wlists_wlist l)
297 {
298 unsigned int t;
299
300 if (l == NULL)
301 {
302 return 0;
303 }
304 else
305 {
306 t = 0;
307 do {
308 t += l->noOfElements;
309 l = l->next;
310 } while (! (l == NULL));
311 return t;
312 }
313 /* static analysis guarentees a RETURN statement will be used before here. */
314 __builtin_unreachable ();
315 }
316
317
318 /*
319 includeItemIntoList - adds an WORD, c, into a wlist providing
320 the value does not already exist.
321 */
322
323 extern "C" void wlists_includeItemIntoList (wlists_wlist l, unsigned int c)
324 {
325 if (! (wlists_isItemInList (l, c)))
326 {
327 wlists_putItemIntoList (l, c);
328 }
329 }
330
331
332 /*
333 removeItemFromList - removes a WORD, c, from a wlist.
334 It assumes that this value only appears once.
335 */
336
337 extern "C" void wlists_removeItemFromList (wlists_wlist l, unsigned int c)
338 {
339 wlists_wlist p;
340 unsigned int i;
341 unsigned int found;
342
343 if (l != NULL)
344 {
345 found = FALSE;
346 p = NULL;
347 do {
348 i = 1;
349 while ((i <= l->noOfElements) && (l->elements.array[i-1] != c))
350 {
351 i += 1;
352 }
353 if ((i <= l->noOfElements) && (l->elements.array[i-1] == c))
354 {
355 found = TRUE;
356 }
357 else
358 {
359 p = l;
360 l = l->next;
361 }
362 } while (! ((l == NULL) || found));
363 if (found)
364 {
365 removeItem (p, l, i);
366 }
367 }
368 }
369
370
371 /*
372 replaceItemInList - replace the nth WORD in wlist, l.
373 The first item in a wlists is at index, 1.
374 If the index, n, is out of range nothing is changed.
375 */
376
377 extern "C" void wlists_replaceItemInList (wlists_wlist l, unsigned int n, unsigned int w)
378 {
379 while (l != NULL)
380 {
381 if (n <= l->noOfElements)
382 {
383 l->elements.array[n-1] = w;
384 }
385 else
386 {
387 n -= l->noOfElements;
388 }
389 l = l->next;
390 }
391 }
392
393
394 /*
395 isItemInList - returns true if a WORD, c, was found in wlist, l.
396 */
397
398 extern "C" unsigned int wlists_isItemInList (wlists_wlist l, unsigned int c)
399 {
400 unsigned int i;
401
402 do {
403 i = 1;
404 while (i <= l->noOfElements)
405 {
406 if (l->elements.array[i-1] == c)
407 {
408 return TRUE;
409 }
410 else
411 {
412 i += 1;
413 }
414 }
415 l = l->next;
416 } while (! (l == NULL));
417 return FALSE;
418 /* static analysis guarentees a RETURN statement will be used before here. */
419 __builtin_unreachable ();
420 }
421
422
423 /*
424 foreachItemInListDo - calls procedure, P, foreach item in wlist, l.
425 */
426
427 extern "C" void wlists_foreachItemInListDo (wlists_wlist l, wlists_performOperation p)
428 {
429 unsigned int i;
430 unsigned int n;
431
432 n = wlists_noOfItemsInList (l);
433 i = 1;
434 while (i <= n)
435 {
436 (*p.proc) (wlists_getItemFromList (l, i));
437 i += 1;
438 }
439 }
440
441
442 /*
443 duplicateList - returns a duplicate wlist derived from, l.
444 */
445
446 extern "C" wlists_wlist wlists_duplicateList (wlists_wlist l)
447 {
448 wlists_wlist m;
449 unsigned int n;
450 unsigned int i;
451
452 m = wlists_initList ();
453 n = wlists_noOfItemsInList (l);
454 i = 1;
455 while (i <= n)
456 {
457 wlists_putItemIntoList (m, wlists_getItemFromList (l, i));
458 i += 1;
459 }
460 return m;
461 /* static analysis guarentees a RETURN statement will be used before here. */
462 __builtin_unreachable ();
463 }
464
465 extern "C" void _M2_wlists_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
466 {
467 }
468
469 extern "C" void _M2_wlists_finish (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
470 {
471 }