1 /* do not edit automatically generated by mc from lists. */
2 /* Dynamic list library for pointers.
3 Copyright (C) 2015-2025 Free Software Foundation, Inc.
5 This file is part of GNU Modula-2.
7 GNU Modula-2 is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
24 # if !defined (PROC_D)
26 typedef void (*PROC_t
) (void);
27 typedef struct { PROC_t proc
; } PROC
;
38 # include "GStorage.h"
39 #if defined(__cplusplus)
46 # include "GStorage.h"
48 typedef struct symbolKey_performOperation_p symbolKey_performOperation
;
50 # define MaxnoOfelements 5
51 typedef struct lists__T1_r lists__T1
;
53 typedef struct lists__T2_a lists__T2
;
55 typedef lists__T1
*lists_list__opaque
;
57 struct lists__T2_a
{ void * array
[MaxnoOfelements
-1+1]; };
59 unsigned int noOfelements
;
61 lists_list__opaque next
;
66 initList - creates a new list, l.
69 extern "C" lists_list
lists_initList (void);
72 killList - deletes the complete list, l.
75 extern "C" void lists_killList (lists_list
*l
);
78 putItemIntoList - places an ADDRESS, c, into list, l.
81 extern "C" void lists_putItemIntoList (lists_list l
, void * c
);
84 getItemFromList - retrieves the nth WORD from list, l.
87 extern "C" void * lists_getItemFromList (lists_list l
, unsigned int n
);
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.
95 extern "C" unsigned int lists_getIndexOfList (lists_list l
, void * c
);
98 noOfItemsInList - returns the number of items in list, l.
101 extern "C" unsigned int lists_noOfItemsInList (lists_list l
);
104 includeItemIntoList - adds an ADDRESS, c, into a list providing
105 the value does not already exist.
108 extern "C" void lists_includeItemIntoList (lists_list l
, void * c
);
111 removeItemFromList - removes a ADDRESS, c, from a list.
112 It assumes that this value only appears once.
115 extern "C" void lists_removeItemFromList (lists_list l
, void * c
);
118 isItemInList - returns true if a ADDRESS, c, was found in list, l.
121 extern "C" bool lists_isItemInList (lists_list l
, void * c
);
124 foreachItemInListDo - calls procedure, P, foreach item in list, l.
127 extern "C" void lists_foreachItemInListDo (lists_list l
, symbolKey_performOperation p
);
130 duplicateList - returns a duplicate list derived from, l.
133 extern "C" lists_list
lists_duplicateList (lists_list l
);
136 removeItem - remove an element at index, i, from the list data type.
139 static void removeItem (lists_list__opaque p
, lists_list__opaque l
, unsigned int i
);
143 removeItem - remove an element at index, i, from the list data type.
146 static void removeItem (lists_list__opaque p
, lists_list__opaque l
, unsigned int i
)
148 l
->noOfelements
-= 1;
149 while (i
<= l
->noOfelements
)
151 l
->elements
.array
[i
-1] = l
->elements
.array
[i
+1-1];
154 if ((l
->noOfelements
== 0) && (p
!= NULL
))
157 Storage_DEALLOCATE ((void **) &l
, sizeof (lists__T1
));
163 initList - creates a new list, l.
166 extern "C" lists_list
lists_initList (void)
168 lists_list__opaque l
;
170 Storage_ALLOCATE ((void **) &l
, sizeof (lists__T1
));
172 l
->next
= static_cast<lists_list__opaque
> (NULL
);
173 return static_cast<lists_list
> (l
);
174 /* static analysis guarentees a RETURN statement will be used before here. */
175 __builtin_unreachable ();
180 killList - deletes the complete list, l.
183 extern "C" void lists_killList (lists_list
*l
)
187 if (static_cast<lists_list__opaque
> ((*l
))->next
!= NULL
)
189 lists_killList (reinterpret_cast<lists_list
*> (&static_cast<lists_list__opaque
> ((*l
))->next
));
191 Storage_DEALLOCATE ((void **) &(*l
), sizeof (lists__T1
));
197 putItemIntoList - places an ADDRESS, c, into list, l.
200 extern "C" void lists_putItemIntoList (lists_list l
, void * c
)
202 if (static_cast<lists_list__opaque
> (l
)->noOfelements
< MaxnoOfelements
)
204 static_cast<lists_list__opaque
> (l
)->noOfelements
+= 1;
205 static_cast<lists_list__opaque
> (l
)->elements
.array
[static_cast<lists_list__opaque
> (l
)->noOfelements
-1] = c
;
207 else if (static_cast<lists_list__opaque
> (l
)->next
!= NULL
)
209 /* avoid dangling else. */
210 lists_putItemIntoList (static_cast<lists_list
> (static_cast<lists_list__opaque
> (l
)->next
), c
);
214 /* avoid dangling else. */
215 static_cast<lists_list__opaque
> (l
)->next
= static_cast<lists_list__opaque
> (lists_initList ());
216 lists_putItemIntoList (static_cast<lists_list
> (static_cast<lists_list__opaque
> (l
)->next
), c
);
222 getItemFromList - retrieves the nth WORD from list, l.
225 extern "C" void * lists_getItemFromList (lists_list l
, unsigned int n
)
229 if (n
<= static_cast<lists_list__opaque
> (l
)->noOfelements
)
231 return static_cast<lists_list__opaque
> (l
)->elements
.array
[n
-1];
235 n
-= static_cast<lists_list__opaque
> (l
)->noOfelements
;
237 l
= static_cast<lists_list
> (static_cast<lists_list__opaque
> (l
)->next
);
239 return static_cast<void *> (0);
240 /* static analysis guarentees a RETURN statement will be used before here. */
241 __builtin_unreachable ();
246 getIndexOfList - returns the index for WORD, c, in list, l.
247 If more than one WORD, c, exists the index
248 for the first is returned.
251 extern "C" unsigned int lists_getIndexOfList (lists_list l
, void * c
)
262 while (i
<= static_cast<lists_list__opaque
> (l
)->noOfelements
)
264 if (static_cast<lists_list__opaque
> (l
)->elements
.array
[i
-1] == c
)
273 return static_cast<lists_list__opaque
> (l
)->noOfelements
+(lists_getIndexOfList (static_cast<lists_list
> (static_cast<lists_list__opaque
> (l
)->next
), c
));
275 /* static analysis guarentees a RETURN statement will be used before here. */
276 __builtin_unreachable ();
281 noOfItemsInList - returns the number of items in list, l.
284 extern "C" unsigned int lists_noOfItemsInList (lists_list l
)
296 t
+= static_cast<lists_list__opaque
> (l
)->noOfelements
;
297 l
= static_cast<lists_list
> (static_cast<lists_list__opaque
> (l
)->next
);
298 } while (! (l
== NULL
));
301 /* static analysis guarentees a RETURN statement will be used before here. */
302 __builtin_unreachable ();
307 includeItemIntoList - adds an ADDRESS, c, into a list providing
308 the value does not already exist.
311 extern "C" void lists_includeItemIntoList (lists_list l
, void * c
)
313 if (! (lists_isItemInList (l
, c
)))
315 lists_putItemIntoList (l
, c
);
321 removeItemFromList - removes a ADDRESS, c, from a list.
322 It assumes that this value only appears once.
325 extern "C" void lists_removeItemFromList (lists_list l
, void * c
)
327 lists_list__opaque p
;
334 p
= static_cast<lists_list__opaque
> (NULL
);
337 while ((i
<= static_cast<lists_list__opaque
> (l
)->noOfelements
) && (static_cast<lists_list__opaque
> (l
)->elements
.array
[i
-1] != c
))
341 if ((i
<= static_cast<lists_list__opaque
> (l
)->noOfelements
) && (static_cast<lists_list__opaque
> (l
)->elements
.array
[i
-1] == c
))
347 p
= static_cast<lists_list__opaque
> (l
);
348 l
= static_cast<lists_list
> (static_cast<lists_list__opaque
> (l
)->next
);
350 } while (! ((l
== NULL
) || found
));
353 removeItem (p
, static_cast<lists_list__opaque
> (l
), i
);
360 isItemInList - returns true if a ADDRESS, c, was found in list, l.
363 extern "C" bool lists_isItemInList (lists_list l
, void * c
)
369 while (i
<= static_cast<lists_list__opaque
> (l
)->noOfelements
)
371 if (static_cast<lists_list__opaque
> (l
)->elements
.array
[i
-1] == c
)
380 l
= static_cast<lists_list
> (static_cast<lists_list__opaque
> (l
)->next
);
381 } while (! (l
== NULL
));
383 /* static analysis guarentees a RETURN statement will be used before here. */
384 __builtin_unreachable ();
389 foreachItemInListDo - calls procedure, P, foreach item in list, l.
392 extern "C" void lists_foreachItemInListDo (lists_list l
, symbolKey_performOperation p
)
397 n
= lists_noOfItemsInList (l
);
401 (*p
.proc
) (lists_getItemFromList (l
, i
));
408 duplicateList - returns a duplicate list derived from, l.
411 extern "C" lists_list
lists_duplicateList (lists_list l
)
413 lists_list__opaque m
;
417 m
= static_cast<lists_list__opaque
> (lists_initList ());
418 n
= lists_noOfItemsInList (l
);
422 lists_putItemIntoList (static_cast<lists_list
> (m
), lists_getItemFromList (l
, i
));
425 return static_cast<lists_list
> (m
);
426 /* static analysis guarentees a RETURN statement will be used before here. */
427 __builtin_unreachable ();
430 extern "C" void _M2_lists_init (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])
434 extern "C" void _M2_lists_fini (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])