]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/pge-boot/GLists.c
Update copyright years.
[thirdparty/gcc.git] / gcc / m2 / pge-boot / GLists.c
1 /* do not edit automatically generated by mc from Lists. */
2 /* Lists.mod provides an unordered list manipulation package.
3
4 Copyright (C) 2001-2023 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.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 # if !defined (PROC_D)
24 # define PROC_D
25 typedef void (*PROC_t) (void);
26 typedef struct { PROC_t proc; } PROC;
27 # endif
28
29 # if !defined (TRUE)
30 # define TRUE (1==1)
31 # endif
32
33 # if !defined (FALSE)
34 # define FALSE (1==0)
35 # endif
36
37 #include <stddef.h>
38 # include "GStorage.h"
39 #if defined(__cplusplus)
40 # undef NULL
41 # define NULL 0
42 #endif
43 #define _Lists_H
44 #define _Lists_C
45
46 # include "GStorage.h"
47
48 typedef struct SymbolKey_PerformOperation_p SymbolKey_PerformOperation;
49
50 # define MaxNoOfElements 5
51 typedef struct Lists_list_r Lists_list;
52
53 typedef struct Lists__T1_a Lists__T1;
54
55 typedef Lists_list *Lists_List;
56
57 typedef void (*SymbolKey_PerformOperation_t) (unsigned int);
58 struct SymbolKey_PerformOperation_p { SymbolKey_PerformOperation_t proc; };
59
60 struct Lists__T1_a { unsigned int array[MaxNoOfElements-1+1]; };
61 struct Lists_list_r {
62 unsigned int NoOfElements;
63 Lists__T1 Elements;
64 Lists_List Next;
65 };
66
67
68 /*
69 InitList - creates a new list, l.
70 */
71
72 extern "C" void Lists_InitList (Lists_List *l);
73
74 /*
75 KillList - deletes the complete list, l.
76 */
77
78 extern "C" void Lists_KillList (Lists_List *l);
79
80 /*
81 PutItemIntoList - places a WORD, c, into list, l.
82 */
83
84 extern "C" void Lists_PutItemIntoList (Lists_List l, unsigned int c);
85 extern "C" unsigned int Lists_GetItemFromList (Lists_List l, unsigned int n);
86
87 /*
88 GetIndexOfList - returns the index for WORD, c, in list, l.
89 If more than one WORD, c, exists the index
90 for the first is returned.
91 */
92
93 extern "C" unsigned int Lists_GetIndexOfList (Lists_List l, unsigned int c);
94
95 /*
96 NoOfItemsInList - returns the number of items in list, l.
97 (iterative algorithm of the above).
98 */
99
100 extern "C" unsigned int Lists_NoOfItemsInList (Lists_List l);
101
102 /*
103 IncludeItemIntoList - adds a WORD, c, into a list providing
104 the value does not already exist.
105 */
106
107 extern "C" void Lists_IncludeItemIntoList (Lists_List l, unsigned int c);
108
109 /*
110 RemoveItemFromList - removes a WORD, c, from a list.
111 It assumes that this value only appears once.
112 */
113
114 extern "C" void Lists_RemoveItemFromList (Lists_List l, unsigned int c);
115
116 /*
117 IsItemInList - returns true if a WORD, c, was found in list, l.
118 */
119
120 extern "C" unsigned int Lists_IsItemInList (Lists_List l, unsigned int c);
121
122 /*
123 ForeachItemInListDo - calls procedure, P, foreach item in list, l.
124 */
125
126 extern "C" void Lists_ForeachItemInListDo (Lists_List l, SymbolKey_PerformOperation P);
127
128 /*
129 DuplicateList - returns a duplicate list derived from, l.
130 */
131
132 extern "C" Lists_List Lists_DuplicateList (Lists_List l);
133
134 /*
135 RemoveItem - remove an element at index, i, from the list data type.
136 */
137
138 static void RemoveItem (Lists_List p, Lists_List l, unsigned int i);
139
140
141 /*
142 RemoveItem - remove an element at index, i, from the list data type.
143 */
144
145 static void RemoveItem (Lists_List p, Lists_List l, unsigned int i)
146 {
147 l->NoOfElements -= 1;
148 while (i <= l->NoOfElements)
149 {
150 l->Elements.array[i-1] = l->Elements.array[i+1-1];
151 i += 1;
152 }
153 if ((l->NoOfElements == 0) && (p != NULL))
154 {
155 p->Next = l->Next;
156 Storage_DEALLOCATE ((void **) &l, sizeof (Lists_list));
157 }
158 }
159
160
161 /*
162 InitList - creates a new list, l.
163 */
164
165 extern "C" void Lists_InitList (Lists_List *l)
166 {
167 Storage_ALLOCATE ((void **) &(*l), sizeof (Lists_list));
168 (*l)->NoOfElements = 0;
169 (*l)->Next = NULL;
170 }
171
172
173 /*
174 KillList - deletes the complete list, l.
175 */
176
177 extern "C" void Lists_KillList (Lists_List *l)
178 {
179 if ((*l) != NULL)
180 {
181 if ((*l)->Next != NULL)
182 {
183 Lists_KillList (&(*l)->Next);
184 }
185 Storage_DEALLOCATE ((void **) &(*l), sizeof (Lists_list));
186 }
187 }
188
189
190 /*
191 PutItemIntoList - places a WORD, c, into list, l.
192 */
193
194 extern "C" void Lists_PutItemIntoList (Lists_List l, unsigned int c)
195 {
196 if (l->NoOfElements < MaxNoOfElements)
197 {
198 l->NoOfElements += 1;
199 l->Elements.array[l->NoOfElements-1] = c;
200 }
201 else if (l->Next != NULL)
202 {
203 /* avoid dangling else. */
204 Lists_PutItemIntoList (l->Next, c);
205 }
206 else
207 {
208 /* avoid dangling else. */
209 Lists_InitList (&l->Next);
210 Lists_PutItemIntoList (l->Next, c);
211 }
212 }
213
214 extern "C" unsigned int Lists_GetItemFromList (Lists_List l, unsigned int n)
215 {
216 /* iterative solution */
217 while (l != NULL)
218 {
219 if (n <= l->NoOfElements)
220 {
221 return l->Elements.array[n-1];
222 }
223 else
224 {
225 n -= l->NoOfElements;
226 }
227 l = l->Next;
228 }
229 return static_cast<unsigned int> (0);
230 /* static analysis guarentees a RETURN statement will be used before here. */
231 __builtin_unreachable ();
232 }
233
234
235 /*
236 GetIndexOfList - returns the index for WORD, c, in list, l.
237 If more than one WORD, c, exists the index
238 for the first is returned.
239 */
240
241 extern "C" unsigned int Lists_GetIndexOfList (Lists_List l, unsigned int c)
242 {
243 unsigned int i;
244
245 if (l == NULL)
246 {
247 return 0;
248 }
249 else
250 {
251 i = 1;
252 while (i <= l->NoOfElements)
253 {
254 if (l->Elements.array[i-1] == c)
255 {
256 return i;
257 }
258 else
259 {
260 i += 1;
261 }
262 }
263 return l->NoOfElements+(Lists_GetIndexOfList (l->Next, c));
264 }
265 /* static analysis guarentees a RETURN statement will be used before here. */
266 __builtin_unreachable ();
267 }
268
269
270 /*
271 NoOfItemsInList - returns the number of items in list, l.
272 (iterative algorithm of the above).
273 */
274
275 extern "C" unsigned int Lists_NoOfItemsInList (Lists_List l)
276 {
277 unsigned int t;
278
279 if (l == NULL)
280 {
281 return 0;
282 }
283 else
284 {
285 t = 0;
286 do {
287 t += l->NoOfElements;
288 l = l->Next;
289 } while (! (l == NULL));
290 return t;
291 }
292 /* static analysis guarentees a RETURN statement will be used before here. */
293 __builtin_unreachable ();
294 }
295
296
297 /*
298 IncludeItemIntoList - adds a WORD, c, into a list providing
299 the value does not already exist.
300 */
301
302 extern "C" void Lists_IncludeItemIntoList (Lists_List l, unsigned int c)
303 {
304 if (! (Lists_IsItemInList (l, c)))
305 {
306 Lists_PutItemIntoList (l, c);
307 }
308 }
309
310
311 /*
312 RemoveItemFromList - removes a WORD, c, from a list.
313 It assumes that this value only appears once.
314 */
315
316 extern "C" void Lists_RemoveItemFromList (Lists_List l, unsigned int c)
317 {
318 Lists_List p;
319 unsigned int i;
320 unsigned int Found;
321
322 if (l != NULL)
323 {
324 Found = FALSE;
325 p = NULL;
326 do {
327 i = 1;
328 while ((i <= l->NoOfElements) && (l->Elements.array[i-1] != c))
329 {
330 i += 1;
331 }
332 if ((i <= l->NoOfElements) && (l->Elements.array[i-1] == c))
333 {
334 Found = TRUE;
335 }
336 else
337 {
338 p = l;
339 l = l->Next;
340 }
341 } while (! ((l == NULL) || Found));
342 if (Found)
343 {
344 RemoveItem (p, l, i);
345 }
346 }
347 }
348
349
350 /*
351 IsItemInList - returns true if a WORD, c, was found in list, l.
352 */
353
354 extern "C" unsigned int Lists_IsItemInList (Lists_List l, unsigned int c)
355 {
356 unsigned int i;
357
358 do {
359 i = 1;
360 while (i <= l->NoOfElements)
361 {
362 if (l->Elements.array[i-1] == c)
363 {
364 return TRUE;
365 }
366 else
367 {
368 i += 1;
369 }
370 }
371 l = l->Next;
372 } while (! (l == NULL));
373 return FALSE;
374 /* static analysis guarentees a RETURN statement will be used before here. */
375 __builtin_unreachable ();
376 }
377
378
379 /*
380 ForeachItemInListDo - calls procedure, P, foreach item in list, l.
381 */
382
383 extern "C" void Lists_ForeachItemInListDo (Lists_List l, SymbolKey_PerformOperation P)
384 {
385 unsigned int i;
386 unsigned int n;
387
388 n = Lists_NoOfItemsInList (l);
389 i = 1;
390 while (i <= n)
391 {
392 (*P.proc) (Lists_GetItemFromList (l, i));
393 i += 1;
394 }
395 }
396
397
398 /*
399 DuplicateList - returns a duplicate list derived from, l.
400 */
401
402 extern "C" Lists_List Lists_DuplicateList (Lists_List l)
403 {
404 Lists_List m;
405 unsigned int n;
406 unsigned int i;
407
408 Lists_InitList (&m);
409 n = Lists_NoOfItemsInList (l);
410 i = 1;
411 while (i <= n)
412 {
413 Lists_PutItemIntoList (m, Lists_GetItemFromList (l, i));
414 i += 1;
415 }
416 return m;
417 /* static analysis guarentees a RETURN statement will be used before here. */
418 __builtin_unreachable ();
419 }
420
421 extern "C" void _M2_Lists_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
422 {
423 }
424
425 extern "C" void _M2_Lists_finish (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
426 {
427 }