1 /* do not edit automatically generated by mc from alists. */
2 /* alists.mod address lists module.
4 Copyright (C) 2015-2022 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/>. */
25 # if !defined (PROC_D)
27 typedef void (*PROC_t
) (void);
28 typedef struct { PROC_t proc
; } PROC
;
39 # include "GStorage.h"
40 #if defined(__cplusplus)
47 # include "GStorage.h"
49 typedef struct alists_performOperation_p alists_performOperation
;
51 # define MaxnoOfelements 5
52 typedef struct alists__T1_r alists__T1
;
54 typedef struct alists__T2_a alists__T2
;
56 typedef alists__T1
*alists_alist
;
58 typedef void (*alists_performOperation_t
) (void *);
59 struct alists_performOperation_p
{ alists_performOperation_t proc
; };
61 struct alists__T2_a
{ void * array
[MaxnoOfelements
-1+1]; };
63 unsigned int noOfelements
;
70 initList - creates a new alist, l.
73 extern "C" alists_alist
alists_initList (void);
76 killList - deletes the complete alist, l.
79 extern "C" void alists_killList (alists_alist
*l
);
82 putItemIntoList - places an ADDRESS, c, into alist, l.
85 extern "C" void alists_putItemIntoList (alists_alist l
, void * c
);
88 getItemFromList - retrieves the nth WORD from alist, l.
91 extern "C" void * alists_getItemFromList (alists_alist l
, unsigned int n
);
94 getIndexOfList - returns the index for WORD, c, in alist, l.
95 If more than one WORD, c, exists the index
96 for the first is returned.
99 extern "C" unsigned int alists_getIndexOfList (alists_alist l
, void * c
);
102 noOfItemsInList - returns the number of items in alist, l.
105 extern "C" unsigned int alists_noOfItemsInList (alists_alist l
);
108 includeItemIntoList - adds an ADDRESS, c, into a alist providing
109 the value does not already exist.
112 extern "C" void alists_includeItemIntoList (alists_alist l
, void * c
);
115 removeItemFromList - removes a ADDRESS, c, from a alist.
116 It assumes that this value only appears once.
119 extern "C" void alists_removeItemFromList (alists_alist l
, void * c
);
122 isItemInList - returns true if a ADDRESS, c, was found in alist, l.
125 extern "C" unsigned int alists_isItemInList (alists_alist l
, void * c
);
128 foreachItemInListDo - calls procedure, P, foreach item in alist, l.
131 extern "C" void alists_foreachItemInListDo (alists_alist l
, alists_performOperation p
);
134 duplicateList - returns a duplicate alist derived from, l.
137 extern "C" alists_alist
alists_duplicateList (alists_alist l
);
140 removeItem - remove an element at index, i, from the alist data type.
143 static void removeItem (alists_alist p
, alists_alist l
, unsigned int i
);
147 removeItem - remove an element at index, i, from the alist data type.
150 static void removeItem (alists_alist p
, alists_alist l
, unsigned int i
)
152 l
->noOfelements
-= 1;
153 while (i
<= l
->noOfelements
)
155 l
->elements
.array
[i
-1] = l
->elements
.array
[i
+1-1];
158 if ((l
->noOfelements
== 0) && (p
!= NULL
))
161 Storage_DEALLOCATE ((void **) &l
, sizeof (alists__T1
));
167 initList - creates a new alist, l.
170 extern "C" alists_alist
alists_initList (void)
174 Storage_ALLOCATE ((void **) &l
, sizeof (alists__T1
));
178 /* static analysis guarentees a RETURN statement will be used before here. */
179 __builtin_unreachable ();
184 killList - deletes the complete alist, l.
187 extern "C" void alists_killList (alists_alist
*l
)
191 if ((*l
)->next
!= NULL
)
193 alists_killList (&(*l
)->next
);
195 Storage_DEALLOCATE ((void **) &(*l
), sizeof (alists__T1
));
201 putItemIntoList - places an ADDRESS, c, into alist, l.
204 extern "C" void alists_putItemIntoList (alists_alist l
, void * c
)
206 if (l
->noOfelements
< MaxnoOfelements
)
208 l
->noOfelements
+= 1;
209 l
->elements
.array
[l
->noOfelements
-1] = c
;
211 else if (l
->next
!= NULL
)
213 /* avoid dangling else. */
214 alists_putItemIntoList (l
->next
, c
);
218 /* avoid dangling else. */
219 l
->next
= alists_initList ();
220 alists_putItemIntoList (l
->next
, c
);
226 getItemFromList - retrieves the nth WORD from alist, l.
229 extern "C" void * alists_getItemFromList (alists_alist l
, unsigned int n
)
233 if (n
<= l
->noOfelements
)
235 return l
->elements
.array
[n
-1];
239 n
-= l
->noOfelements
;
243 return reinterpret_cast<void *> (0);
244 /* static analysis guarentees a RETURN statement will be used before here. */
245 __builtin_unreachable ();
250 getIndexOfList - returns the index for WORD, c, in alist, l.
251 If more than one WORD, c, exists the index
252 for the first is returned.
255 extern "C" unsigned int alists_getIndexOfList (alists_alist l
, void * c
)
266 while (i
<= l
->noOfelements
)
268 if (l
->elements
.array
[i
-1] == c
)
277 return l
->noOfelements
+(alists_getIndexOfList (l
->next
, c
));
279 /* static analysis guarentees a RETURN statement will be used before here. */
280 __builtin_unreachable ();
285 noOfItemsInList - returns the number of items in alist, l.
288 extern "C" unsigned int alists_noOfItemsInList (alists_alist l
)
300 t
+= l
->noOfelements
;
302 } while (! (l
== NULL
));
305 /* static analysis guarentees a RETURN statement will be used before here. */
306 __builtin_unreachable ();
311 includeItemIntoList - adds an ADDRESS, c, into a alist providing
312 the value does not already exist.
315 extern "C" void alists_includeItemIntoList (alists_alist l
, void * c
)
317 if (! (alists_isItemInList (l
, c
)))
319 alists_putItemIntoList (l
, c
);
325 removeItemFromList - removes a ADDRESS, c, from a alist.
326 It assumes that this value only appears once.
329 extern "C" void alists_removeItemFromList (alists_alist l
, void * c
)
341 while ((i
<= l
->noOfelements
) && (l
->elements
.array
[i
-1] != c
))
345 if ((i
<= l
->noOfelements
) && (l
->elements
.array
[i
-1] == c
))
354 } while (! ((l
== NULL
) || found
));
357 removeItem (p
, l
, i
);
364 isItemInList - returns true if a ADDRESS, c, was found in alist, l.
367 extern "C" unsigned int alists_isItemInList (alists_alist l
, void * c
)
373 while (i
<= l
->noOfelements
)
375 if (l
->elements
.array
[i
-1] == c
)
385 } while (! (l
== NULL
));
387 /* static analysis guarentees a RETURN statement will be used before here. */
388 __builtin_unreachable ();
393 foreachItemInListDo - calls procedure, P, foreach item in alist, l.
396 extern "C" void alists_foreachItemInListDo (alists_alist l
, alists_performOperation p
)
401 n
= alists_noOfItemsInList (l
);
405 (*p
.proc
) (alists_getItemFromList (l
, i
));
412 duplicateList - returns a duplicate alist derived from, l.
415 extern "C" alists_alist
alists_duplicateList (alists_alist l
)
421 m
= alists_initList ();
422 n
= alists_noOfItemsInList (l
);
426 alists_putItemIntoList (m
, alists_getItemFromList (l
, i
));
430 /* static analysis guarentees a RETURN statement will be used before here. */
431 __builtin_unreachable ();
434 extern "C" void _M2_alists_init (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])
438 extern "C" void _M2_alists_finish (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])