Branch data 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-2024 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_H
46 : : #define _Lists_C
47 : :
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;
58 : :
59 : : typedef void (*SymbolKey_PerformOperation_t) (unsigned int);
60 : : struct SymbolKey_PerformOperation_p { SymbolKey_PerformOperation_t proc; };
61 : :
62 : : struct Lists__T1_a { unsigned int array[MaxNoOfElements-1+1]; };
63 : : struct Lists_list_r {
64 : : unsigned int NoOfElements;
65 : : Lists__T1 Elements;
66 : : Lists_List Next;
67 : : };
68 : :
69 : :
70 : : /*
71 : : InitList - creates a new list, l.
72 : : */
73 : :
74 : : extern "C" void Lists_InitList (Lists_List *l);
75 : :
76 : : /*
77 : : KillList - deletes the complete list, l.
78 : : */
79 : :
80 : : extern "C" void Lists_KillList (Lists_List *l);
81 : :
82 : : /*
83 : : PutItemIntoList - places a WORD, c, into list, l.
84 : : */
85 : :
86 : : extern "C" void Lists_PutItemIntoList (Lists_List l, unsigned int c);
87 : : extern "C" unsigned int Lists_GetItemFromList (Lists_List l, unsigned int n);
88 : :
89 : : /*
90 : : GetIndexOfList - returns the index for WORD, c, in list, l.
91 : : If more than one WORD, c, exists the index
92 : : for the first is returned.
93 : : */
94 : :
95 : : extern "C" unsigned int Lists_GetIndexOfList (Lists_List l, unsigned int c);
96 : :
97 : : /*
98 : : NoOfItemsInList - returns the number of items in list, l.
99 : : (iterative algorithm of the above).
100 : : */
101 : :
102 : : extern "C" unsigned int Lists_NoOfItemsInList (Lists_List l);
103 : :
104 : : /*
105 : : IncludeItemIntoList - adds a WORD, c, into a list providing
106 : : the value does not already exist.
107 : : */
108 : :
109 : : extern "C" void Lists_IncludeItemIntoList (Lists_List l, unsigned int c);
110 : :
111 : : /*
112 : : RemoveItemFromList - removes a WORD, c, from a list.
113 : : It assumes that this value only appears once.
114 : : */
115 : :
116 : : extern "C" void Lists_RemoveItemFromList (Lists_List l, unsigned int c);
117 : :
118 : : /*
119 : : IsItemInList - returns true if a WORD, c, was found in list, l.
120 : : */
121 : :
122 : : extern "C" bool Lists_IsItemInList (Lists_List l, unsigned int c);
123 : :
124 : : /*
125 : : ForeachItemInListDo - calls procedure, P, foreach item in list, l.
126 : : */
127 : :
128 : : extern "C" void Lists_ForeachItemInListDo (Lists_List l, SymbolKey_PerformOperation P);
129 : :
130 : : /*
131 : : DuplicateList - returns a duplicate list derived from, l.
132 : : */
133 : :
134 : : extern "C" Lists_List Lists_DuplicateList (Lists_List l);
135 : :
136 : : /*
137 : : RemoveItem - remove an element at index, i, from the list data type.
138 : : */
139 : :
140 : : static void RemoveItem (Lists_List p, Lists_List l, unsigned int i);
141 : :
142 : :
143 : : /*
144 : : RemoveItem - remove an element at index, i, from the list data type.
145 : : */
146 : :
147 : 330009 : static void RemoveItem (Lists_List p, Lists_List l, unsigned int i)
148 : : {
149 : 330009 : l->NoOfElements -= 1;
150 : 488323 : while (i <= l->NoOfElements)
151 : : {
152 : 158314 : l->Elements.array[i-1] = l->Elements.array[i+1-1];
153 : 158314 : i += 1;
154 : : }
155 : 330009 : if ((l->NoOfElements == 0) && (p != NULL))
156 : : {
157 : 2676 : p->Next = l->Next;
158 : 2676 : Storage_DEALLOCATE ((void **) &l, sizeof (Lists_list));
159 : : }
160 : 330009 : }
161 : :
162 : :
163 : : /*
164 : : InitList - creates a new list, l.
165 : : */
166 : :
167 : 53038061 : extern "C" void Lists_InitList (Lists_List *l)
168 : : {
169 : 53038061 : Storage_ALLOCATE ((void **) &(*l), sizeof (Lists_list));
170 : 53038061 : (*l)->NoOfElements = 0;
171 : 53038061 : (*l)->Next = NULL;
172 : 53038061 : }
173 : :
174 : :
175 : : /*
176 : : KillList - deletes the complete list, l.
177 : : */
178 : :
179 : 524260 : extern "C" void Lists_KillList (Lists_List *l)
180 : : {
181 : 524260 : if ((*l) != NULL)
182 : : {
183 : 524260 : if ((*l)->Next != NULL)
184 : : {
185 : 9357 : Lists_KillList (&(*l)->Next);
186 : : }
187 : 524260 : Storage_DEALLOCATE ((void **) &(*l), sizeof (Lists_list));
188 : : }
189 : 524260 : }
190 : :
191 : :
192 : : /*
193 : : PutItemIntoList - places a WORD, c, into list, l.
194 : : */
195 : :
196 : 23059617 : extern "C" void Lists_PutItemIntoList (Lists_List l, unsigned int c)
197 : : {
198 : 43588946 : if (l->NoOfElements < MaxNoOfElements)
199 : : {
200 : 23059617 : l->NoOfElements += 1;
201 : 23059617 : l->Elements.array[l->NoOfElements-1] = c;
202 : : }
203 : 20529329 : else if (l->Next != NULL)
204 : : {
205 : : /* avoid dangling else. */
206 : : Lists_PutItemIntoList (l->Next, c);
207 : : }
208 : : else
209 : : {
210 : : /* avoid dangling else. */
211 : 1268141 : Lists_InitList (&l->Next);
212 : 1268141 : Lists_PutItemIntoList (l->Next, c);
213 : : }
214 : 23059617 : }
215 : :
216 : 868017712 : extern "C" unsigned int Lists_GetItemFromList (Lists_List l, unsigned int n)
217 : : {
218 : : /* iterative solution */
219 : 2021132968 : while (l != NULL)
220 : : {
221 : 2013146680 : if (n <= l->NoOfElements)
222 : : {
223 : 860031424 : return l->Elements.array[n-1];
224 : : }
225 : : else
226 : : {
227 : 1153115256 : n -= l->NoOfElements;
228 : : }
229 : 1153115256 : l = l->Next;
230 : : }
231 : : return static_cast<unsigned int> (0);
232 : : /* static analysis guarentees a RETURN statement will be used before here. */
233 : : __builtin_unreachable ();
234 : : }
235 : :
236 : :
237 : : /*
238 : : GetIndexOfList - returns the index for WORD, c, in list, l.
239 : : If more than one WORD, c, exists the index
240 : : for the first is returned.
241 : : */
242 : :
243 : 0 : extern "C" unsigned int Lists_GetIndexOfList (Lists_List l, unsigned int c)
244 : : {
245 : 0 : unsigned int i;
246 : :
247 : 0 : if (l == NULL)
248 : : {
249 : : return 0;
250 : : }
251 : : else
252 : : {
253 : : i = 1;
254 : 0 : while (i <= l->NoOfElements)
255 : : {
256 : 0 : if (l->Elements.array[i-1] == c)
257 : : {
258 : 0 : return i;
259 : : }
260 : : else
261 : : {
262 : 0 : i += 1;
263 : : }
264 : : }
265 : 0 : return l->NoOfElements+(Lists_GetIndexOfList (l->Next, c));
266 : : }
267 : : /* static analysis guarentees a RETURN statement will be used before here. */
268 : : __builtin_unreachable ();
269 : : }
270 : :
271 : :
272 : : /*
273 : : NoOfItemsInList - returns the number of items in list, l.
274 : : (iterative algorithm of the above).
275 : : */
276 : :
277 : 92573052 : extern "C" unsigned int Lists_NoOfItemsInList (Lists_List l)
278 : : {
279 : 92573052 : unsigned int t;
280 : :
281 : 92573052 : if (l == NULL)
282 : : {
283 : : return 0;
284 : : }
285 : : else
286 : : {
287 : : t = 0;
288 : 304988255 : do {
289 : 304988255 : t += l->NoOfElements;
290 : 304988255 : l = l->Next;
291 : 304988255 : } while (! (l == NULL));
292 : 92570710 : return t;
293 : : }
294 : : /* static analysis guarentees a RETURN statement will be used before here. */
295 : : __builtin_unreachable ();
296 : : }
297 : :
298 : :
299 : : /*
300 : : IncludeItemIntoList - adds a WORD, c, into a list providing
301 : : the value does not already exist.
302 : : */
303 : :
304 : 4927301 : extern "C" void Lists_IncludeItemIntoList (Lists_List l, unsigned int c)
305 : : {
306 : 4927301 : if (! (Lists_IsItemInList (l, c)))
307 : : {
308 : 4705761 : Lists_PutItemIntoList (l, c);
309 : : }
310 : 4927301 : }
311 : :
312 : :
313 : : /*
314 : : RemoveItemFromList - removes a WORD, c, from a list.
315 : : It assumes that this value only appears once.
316 : : */
317 : :
318 : 358374 : extern "C" void Lists_RemoveItemFromList (Lists_List l, unsigned int c)
319 : : {
320 : 358374 : Lists_List p;
321 : 358374 : unsigned int i;
322 : 358374 : bool Found;
323 : :
324 : 358374 : if (l != NULL)
325 : : {
326 : : Found = false;
327 : : p = NULL;
328 : 401123 : do {
329 : 401123 : i = 1;
330 : 647034 : while ((i <= l->NoOfElements) && (l->Elements.array[i-1] != c))
331 : : {
332 : 245911 : i += 1;
333 : : }
334 : 401123 : if ((i <= l->NoOfElements) && (l->Elements.array[i-1] == c))
335 : : {
336 : : Found = true;
337 : : }
338 : : else
339 : : {
340 : 71114 : p = l;
341 : 71114 : l = l->Next;
342 : : }
343 : 401123 : } while (! ((l == NULL) || Found));
344 : 358374 : if (Found)
345 : : {
346 : 330009 : RemoveItem (p, l, i);
347 : : }
348 : : }
349 : 358374 : }
350 : :
351 : :
352 : : /*
353 : : IsItemInList - returns true if a WORD, c, was found in list, l.
354 : : */
355 : :
356 : 15037978 : extern "C" bool Lists_IsItemInList (Lists_List l, unsigned int c)
357 : : {
358 : 23713679 : unsigned int i;
359 : :
360 : 23713679 : do {
361 : 23713679 : i = 1;
362 : 75754278 : while (i <= l->NoOfElements)
363 : : {
364 : 52783022 : if (l->Elements.array[i-1] == c)
365 : : {
366 : : return true;
367 : : }
368 : : else
369 : : {
370 : 52040599 : i += 1;
371 : : }
372 : : }
373 : 22971256 : l = l->Next;
374 : 22971256 : } while (! (l == NULL));
375 : : return false;
376 : : /* static analysis guarentees a RETURN statement will be used before here. */
377 : : __builtin_unreachable ();
378 : : }
379 : :
380 : :
381 : : /*
382 : : ForeachItemInListDo - calls procedure, P, foreach item in list, l.
383 : : */
384 : :
385 : 3953676 : extern "C" void Lists_ForeachItemInListDo (Lists_List l, SymbolKey_PerformOperation P)
386 : : {
387 : 3953676 : unsigned int i;
388 : 3953676 : unsigned int n;
389 : :
390 : 3953676 : n = Lists_NoOfItemsInList (l);
391 : 3953676 : i = 1;
392 : 16562625 : while (i <= n)
393 : : {
394 : 8655279 : (*P.proc) (Lists_GetItemFromList (l, i));
395 : 8655273 : i += 1;
396 : : }
397 : 3953670 : }
398 : :
399 : :
400 : : /*
401 : : DuplicateList - returns a duplicate list derived from, l.
402 : : */
403 : :
404 : 4544 : extern "C" Lists_List Lists_DuplicateList (Lists_List l)
405 : : {
406 : 4544 : Lists_List m;
407 : 4544 : unsigned int n;
408 : 4544 : unsigned int i;
409 : :
410 : 4544 : Lists_InitList (&m);
411 : 4544 : n = Lists_NoOfItemsInList (l);
412 : 4544 : i = 1;
413 : 11901 : while (i <= n)
414 : : {
415 : 2813 : Lists_PutItemIntoList (m, Lists_GetItemFromList (l, i));
416 : 2813 : i += 1;
417 : : }
418 : 4544 : return m;
419 : : /* static analysis guarentees a RETURN statement will be used before here. */
420 : : __builtin_unreachable ();
421 : : }
422 : :
423 : 16817 : extern "C" void _M2_Lists_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
424 : : {
425 : 16817 : }
426 : :
427 : 0 : extern "C" void _M2_Lists_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[])
428 : : {
429 : 0 : }
|