1 /* do not edit automatically generated by mc from alists. */
2 /* alists.mod address lists module.
4 Copyright (C) 2015-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius@glam.ac.uk>.
7 This file is part of GNU Modula-2.
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)
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.
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/>. */
26 # if !defined (PROC_D)
28 typedef void (*PROC_t
) (void);
29 typedef struct { PROC_t proc
; } PROC
;
40 # include "GStorage.h"
41 #if defined(__cplusplus)
48 # include "GStorage.h"
50 typedef struct alists_performOperation_p alists_performOperation
;
52 # define MaxnoOfelements 5
53 typedef struct alists__T1_r alists__T1
;
55 typedef struct alists__T2_a alists__T2
;
57 typedef alists__T1
*alists_alist__opaque
;
59 struct alists__T2_a
{ void * array
[MaxnoOfelements
-1+1]; };
61 unsigned int noOfelements
;
63 alists_alist__opaque next
;
68 initList - creates a new alist, l.
71 extern "C" alists_alist
alists_initList (void);
74 killList - deletes the complete alist, l.
77 extern "C" void alists_killList (alists_alist
*l
);
80 putItemIntoList - places an ADDRESS, c, into alist, l.
83 extern "C" void alists_putItemIntoList (alists_alist l
, void * c
);
86 getItemFromList - retrieves the nth WORD from alist, l.
89 extern "C" void * alists_getItemFromList (alists_alist l
, unsigned int n
);
92 getIndexOfList - returns the index for WORD, c, in alist, l.
93 If more than one WORD, c, exists the index
94 for the first is returned.
97 extern "C" unsigned int alists_getIndexOfList (alists_alist l
, void * c
);
100 noOfItemsInList - returns the number of items in alist, l.
103 extern "C" unsigned int alists_noOfItemsInList (alists_alist l
);
106 includeItemIntoList - adds an ADDRESS, c, into a alist providing
107 the value does not already exist.
110 extern "C" void alists_includeItemIntoList (alists_alist l
, void * c
);
113 removeItemFromList - removes a ADDRESS, c, from a alist.
114 It assumes that this value only appears once.
117 extern "C" void alists_removeItemFromList (alists_alist l
, void * c
);
120 isItemInList - returns true if a ADDRESS, c, was found in alist, l.
123 extern "C" bool alists_isItemInList (alists_alist l
, void * c
);
126 foreachItemInListDo - calls procedure, P, foreach item in alist, l.
129 extern "C" void alists_foreachItemInListDo (alists_alist l
, alists_performOperation p
);
132 duplicateList - returns a duplicate alist derived from, l.
135 extern "C" alists_alist
alists_duplicateList (alists_alist l
);
138 equalList - returns TRUE if left contains the same information as right.
141 extern "C" bool alists_equalList (alists_alist left
, alists_alist right
);
144 removeItem - remove an element at index, i, from the alist data type.
147 static void removeItem (alists_alist__opaque p
, alists_alist__opaque l
, unsigned int i
);
151 removeItem - remove an element at index, i, from the alist data type.
154 static void removeItem (alists_alist__opaque p
, alists_alist__opaque l
, unsigned int i
)
156 l
->noOfelements
-= 1;
157 while (i
<= l
->noOfelements
)
159 l
->elements
.array
[i
-1] = l
->elements
.array
[i
+1-1];
162 if ((l
->noOfelements
== 0) && (p
!= NULL
))
165 Storage_DEALLOCATE ((void **) &l
, sizeof (alists__T1
));
171 initList - creates a new alist, l.
174 extern "C" alists_alist
alists_initList (void)
176 alists_alist__opaque l
;
178 Storage_ALLOCATE ((void **) &l
, sizeof (alists__T1
));
180 l
->next
= static_cast<alists_alist__opaque
> (NULL
);
181 return static_cast<alists_alist
> (l
);
182 /* static analysis guarentees a RETURN statement will be used before here. */
183 __builtin_unreachable ();
188 killList - deletes the complete alist, l.
191 extern "C" void alists_killList (alists_alist
*l
)
195 if (static_cast<alists_alist__opaque
> ((*l
))->next
!= NULL
)
197 alists_killList (reinterpret_cast<alists_alist
*> (&static_cast<alists_alist__opaque
> ((*l
))->next
));
199 Storage_DEALLOCATE ((void **) &(*l
), sizeof (alists__T1
));
205 putItemIntoList - places an ADDRESS, c, into alist, l.
208 extern "C" void alists_putItemIntoList (alists_alist l
, void * c
)
210 if (static_cast<alists_alist__opaque
> (l
)->noOfelements
< MaxnoOfelements
)
212 static_cast<alists_alist__opaque
> (l
)->noOfelements
+= 1;
213 static_cast<alists_alist__opaque
> (l
)->elements
.array
[static_cast<alists_alist__opaque
> (l
)->noOfelements
-1] = c
;
215 else if (static_cast<alists_alist__opaque
> (l
)->next
!= NULL
)
217 /* avoid dangling else. */
218 alists_putItemIntoList (static_cast<alists_alist
> (static_cast<alists_alist__opaque
> (l
)->next
), c
);
222 /* avoid dangling else. */
223 static_cast<alists_alist__opaque
> (l
)->next
= static_cast<alists_alist__opaque
> (alists_initList ());
224 alists_putItemIntoList (static_cast<alists_alist
> (static_cast<alists_alist__opaque
> (l
)->next
), c
);
230 getItemFromList - retrieves the nth WORD from alist, l.
233 extern "C" void * alists_getItemFromList (alists_alist l
, unsigned int n
)
237 if (n
<= static_cast<alists_alist__opaque
> (l
)->noOfelements
)
239 return static_cast<alists_alist__opaque
> (l
)->elements
.array
[n
-1];
243 n
-= static_cast<alists_alist__opaque
> (l
)->noOfelements
;
245 l
= static_cast<alists_alist
> (static_cast<alists_alist__opaque
> (l
)->next
);
247 return static_cast<void *> (0);
248 /* static analysis guarentees a RETURN statement will be used before here. */
249 __builtin_unreachable ();
254 getIndexOfList - returns the index for WORD, c, in alist, l.
255 If more than one WORD, c, exists the index
256 for the first is returned.
259 extern "C" unsigned int alists_getIndexOfList (alists_alist l
, void * c
)
270 while (i
<= static_cast<alists_alist__opaque
> (l
)->noOfelements
)
272 if (static_cast<alists_alist__opaque
> (l
)->elements
.array
[i
-1] == c
)
281 return static_cast<alists_alist__opaque
> (l
)->noOfelements
+(alists_getIndexOfList (static_cast<alists_alist
> (static_cast<alists_alist__opaque
> (l
)->next
), c
));
283 /* static analysis guarentees a RETURN statement will be used before here. */
284 __builtin_unreachable ();
289 noOfItemsInList - returns the number of items in alist, l.
292 extern "C" unsigned int alists_noOfItemsInList (alists_alist l
)
304 t
+= static_cast<alists_alist__opaque
> (l
)->noOfelements
;
305 l
= static_cast<alists_alist
> (static_cast<alists_alist__opaque
> (l
)->next
);
306 } while (! (l
== NULL
));
309 /* static analysis guarentees a RETURN statement will be used before here. */
310 __builtin_unreachable ();
315 includeItemIntoList - adds an ADDRESS, c, into a alist providing
316 the value does not already exist.
319 extern "C" void alists_includeItemIntoList (alists_alist l
, void * c
)
321 if (! (alists_isItemInList (l
, c
)))
323 alists_putItemIntoList (l
, c
);
329 removeItemFromList - removes a ADDRESS, c, from a alist.
330 It assumes that this value only appears once.
333 extern "C" void alists_removeItemFromList (alists_alist l
, void * c
)
335 alists_alist__opaque p
;
342 p
= static_cast<alists_alist__opaque
> (NULL
);
345 while ((i
<= static_cast<alists_alist__opaque
> (l
)->noOfelements
) && (static_cast<alists_alist__opaque
> (l
)->elements
.array
[i
-1] != c
))
349 if ((i
<= static_cast<alists_alist__opaque
> (l
)->noOfelements
) && (static_cast<alists_alist__opaque
> (l
)->elements
.array
[i
-1] == c
))
355 p
= static_cast<alists_alist__opaque
> (l
);
356 l
= static_cast<alists_alist
> (static_cast<alists_alist__opaque
> (l
)->next
);
358 } while (! ((l
== NULL
) || found
));
361 removeItem (p
, static_cast<alists_alist__opaque
> (l
), i
);
368 isItemInList - returns true if a ADDRESS, c, was found in alist, l.
371 extern "C" bool alists_isItemInList (alists_alist l
, void * c
)
377 while (i
<= static_cast<alists_alist__opaque
> (l
)->noOfelements
)
379 if (static_cast<alists_alist__opaque
> (l
)->elements
.array
[i
-1] == c
)
388 l
= static_cast<alists_alist
> (static_cast<alists_alist__opaque
> (l
)->next
);
389 } while (! (l
== NULL
));
391 /* static analysis guarentees a RETURN statement will be used before here. */
392 __builtin_unreachable ();
397 foreachItemInListDo - calls procedure, P, foreach item in alist, l.
400 extern "C" void alists_foreachItemInListDo (alists_alist l
, alists_performOperation p
)
405 n
= alists_noOfItemsInList (l
);
409 (*p
.proc
) (alists_getItemFromList (l
, i
));
416 duplicateList - returns a duplicate alist derived from, l.
419 extern "C" alists_alist
alists_duplicateList (alists_alist l
)
421 alists_alist__opaque m
;
425 m
= static_cast<alists_alist__opaque
> (alists_initList ());
426 n
= alists_noOfItemsInList (l
);
430 alists_putItemIntoList (static_cast<alists_alist
> (m
), alists_getItemFromList (l
, i
));
433 return static_cast<alists_alist
> (m
);
434 /* static analysis guarentees a RETURN statement will be used before here. */
435 __builtin_unreachable ();
440 equalList - returns TRUE if left contains the same information as right.
443 extern "C" bool alists_equalList (alists_alist left
, alists_alist right
)
449 leftn
= alists_noOfItemsInList (left
);
450 rightn
= alists_noOfItemsInList (right
);
456 if (alists_isItemInList (right
, alists_getItemFromList (left
, i
)))
471 /* static analysis guarentees a RETURN statement will be used before here. */
472 __builtin_unreachable ();
475 extern "C" void _M2_alists_init (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])
479 extern "C" void _M2_alists_fini (__attribute__((unused
)) int argc
, __attribute__((unused
)) char *argv
[], __attribute__((unused
)) char *envp
[])