1 /* do not edit automatically generated by mc from wlists. */
2 /* wlists.mod word lists module.
4 Copyright (C) 2015-2024 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 wlists_performOperation_p wlists_performOperation
;
52 # define maxNoOfElements 5
53 typedef struct wlists__T1_r wlists__T1
;
55 typedef struct wlists__T2_a wlists__T2
;
57 typedef wlists__T1
*wlists_wlist
;
59 typedef void (*wlists_performOperation_t
) (unsigned int);
60 struct wlists_performOperation_p
{ wlists_performOperation_t proc
; };
62 struct wlists__T2_a
{ unsigned int array
[maxNoOfElements
-1+1]; };
64 unsigned int noOfElements
;
71 initList - creates a new wlist, l.
74 extern "C" wlists_wlist
wlists_initList (void);
77 killList - deletes the complete wlist, l.
80 extern "C" void wlists_killList (wlists_wlist
*l
);
83 putItemIntoList - places an WORD, c, into wlist, l.
86 extern "C" void wlists_putItemIntoList (wlists_wlist l
, unsigned int c
);
89 getItemFromList - retrieves the nth WORD from wlist, l.
92 extern "C" unsigned int wlists_getItemFromList (wlists_wlist l
, unsigned int n
);
95 getIndexOfList - returns the index for WORD, c, in wlist, l.
96 If more than one WORD, c, exists the index
97 for the first is returned.
100 extern "C" unsigned int wlists_getIndexOfList (wlists_wlist l
, unsigned int c
);
103 noOfItemsInList - returns the number of items in wlist, l.
106 extern "C" unsigned int wlists_noOfItemsInList (wlists_wlist l
);
109 includeItemIntoList - adds an WORD, c, into a wlist providing
110 the value does not already exist.
113 extern "C" void wlists_includeItemIntoList (wlists_wlist l
, unsigned int c
);
116 removeItemFromList - removes a WORD, c, from a wlist.
117 It assumes that this value only appears once.
120 extern "C" void wlists_removeItemFromList (wlists_wlist l
, unsigned int c
);
123 replaceItemInList - replace the nth WORD in wlist, l.
124 The first item in a wlists is at index, 1.
125 If the index, n, is out of range nothing is changed.
128 extern "C" void wlists_replaceItemInList (wlists_wlist l
, unsigned int n
, unsigned int w
);
131 isItemInList - returns true if a WORD, c, was found in wlist, l.
134 extern "C" bool wlists_isItemInList (wlists_wlist l
, unsigned int c
);
137 foreachItemInListDo - calls procedure, P, foreach item in wlist, l.
140 extern "C" void wlists_foreachItemInListDo (wlists_wlist l
, wlists_performOperation p
);
143 duplicateList - returns a duplicate wlist derived from, l.
146 extern "C" wlists_wlist
wlists_duplicateList (wlists_wlist l
);
149 removeItem - remove an element at index, i, from the wlist data type.
152 static void removeItem (wlists_wlist p
, wlists_wlist l
, unsigned int i
);
156 removeItem - remove an element at index, i, from the wlist data type.
159 static void removeItem (wlists_wlist p
, wlists_wlist l
, unsigned int i
)
161 l
->noOfElements
-= 1;
162 while (i
<= l
->noOfElements
)
164 l
->elements
.array
[i
-1] = l
->elements
.array
[i
+1-1];
167 if ((l
->noOfElements
== 0) && (p
!= NULL
))
170 Storage_DEALLOCATE ((void **) &l
, sizeof (wlists__T1
));
176 initList - creates a new wlist, l.
179 extern "C" wlists_wlist
wlists_initList (void)
183 Storage_ALLOCATE ((void **) &l
, sizeof (wlists__T1
));
187 /* static analysis guarentees a RETURN statement will be used before here. */
188 __builtin_unreachable ();
193 killList - deletes the complete wlist, l.
196 extern "C" void wlists_killList (wlists_wlist
*l
)
200 if ((*l
)->next
!= NULL
)
202 wlists_killList (&(*l
)->next
);
204 Storage_DEALLOCATE ((void **) &(*l
), sizeof (wlists__T1
));
210 putItemIntoList - places an WORD, c, into wlist, l.
213 extern "C" void wlists_putItemIntoList (wlists_wlist l
, unsigned int c
)
215 if (l
->noOfElements
< maxNoOfElements
)
217 l
->noOfElements
+= 1;
218 l
->elements
.array
[l
->noOfElements
-1] = c
;
220 else if (l
->next
!= NULL
)
222 /* avoid dangling else. */
223 wlists_putItemIntoList (l
->next
, c
);
227 /* avoid dangling else. */
228 l
->next
= wlists_initList ();
229 wlists_putItemIntoList (l
->next
, c
);
235 getItemFromList - retrieves the nth WORD from wlist, l.
238 extern "C" unsigned int wlists_getItemFromList (wlists_wlist l
, unsigned int n
)
242 if (n
<= l
->noOfElements
)
244 return l
->elements
.array
[n
-1];
248 n
-= l
->noOfElements
;
252 return static_cast<unsigned int> (0);
253 /* static analysis guarentees a RETURN statement will be used before here. */
254 __builtin_unreachable ();
259 getIndexOfList - returns the index for WORD, c, in wlist, l.
260 If more than one WORD, c, exists the index
261 for the first is returned.
264 extern "C" unsigned int wlists_getIndexOfList (wlists_wlist l
, unsigned int c
)
275 while (i
<= l
->noOfElements
)
277 if (l
->elements
.array
[i
-1] == c
)
286 return l
->noOfElements
+(wlists_getIndexOfList (l
->next
, c
));
288 /* static analysis guarentees a RETURN statement will be used before here. */
289 __builtin_unreachable ();
294 noOfItemsInList - returns the number of items in wlist, l.
297 extern "C" unsigned int wlists_noOfItemsInList (wlists_wlist l
)
309 t
+= l
->noOfElements
;
311 } while (! (l
== NULL
));
314 /* static analysis guarentees a RETURN statement will be used before here. */
315 __builtin_unreachable ();
320 includeItemIntoList - adds an WORD, c, into a wlist providing
321 the value does not already exist.
324 extern "C" void wlists_includeItemIntoList (wlists_wlist l
, unsigned int c
)
326 if (! (wlists_isItemInList (l
, c
)))
328 wlists_putItemIntoList (l
, c
);
334 removeItemFromList - removes a WORD, c, from a wlist.
335 It assumes that this value only appears once.
338 extern "C" void wlists_removeItemFromList (wlists_wlist l
, unsigned int c
)
350 while ((i
<= l
->noOfElements
) && (l
->elements
.array
[i
-1] != c
))
354 if ((i
<= l
->noOfElements
) && (l
->elements
.array
[i
-1] == c
))
363 } while (! ((l
== NULL
) || found
));
366 removeItem (p
, l
, i
);
373 replaceItemInList - replace the nth WORD in wlist, l.
374 The first item in a wlists is at index, 1.
375 If the index, n, is out of range nothing is changed.
378 extern "C" void wlists_replaceItemInList (wlists_wlist l
, unsigned int n
, unsigned int w
)
382 if (n
<= l
->noOfElements
)
384 l
->elements
.array
[n
-1] = w
;
388 n
-= l
->noOfElements
;
396 isItemInList - returns true if a WORD, c, was found in wlist, l.
399 extern "C" bool wlists_isItemInList (wlists_wlist l
, unsigned int c
)
405 while (i
<= l
->noOfElements
)
407 if (l
->elements
.array
[i
-1] == c
)
417 } while (! (l
== NULL
));
419 /* static analysis guarentees a RETURN statement will be used before here. */
420 __builtin_unreachable ();
425 foreachItemInListDo - calls procedure, P, foreach item in wlist, l.
428 extern "C" void wlists_foreachItemInListDo (wlists_wlist l
, wlists_performOperation p
)
433 n
= wlists_noOfItemsInList (l
);
437 (*p
.proc
) (wlists_getItemFromList (l
, i
));
444 duplicateList - returns a duplicate wlist derived from, l.
447 extern "C" wlists_wlist
wlists_duplicateList (wlists_wlist l
)
453 m
= wlists_initList ();
454 n
= wlists_noOfItemsInList (l
);
458 wlists_putItemIntoList (m
, wlists_getItemFromList (l
, i
));
462 /* static analysis guarentees a RETURN statement will be used before here. */
463 __builtin_unreachable ();
466 extern "C" void _M2_wlists_init (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])
470 extern "C" void _M2_wlists_fini (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])