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