1 /* do not edit automatically generated by mc from wlists. */
2 /* wlists.mod word lists module.
4 Copyright (C) 2015-2023 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 wlists_performOperation_p wlists_performOperation
;
51 # define maxNoOfElements 5
52 typedef struct wlists__T1_r wlists__T1
;
54 typedef struct wlists__T2_a wlists__T2
;
56 typedef wlists__T1
*wlists_wlist
;
58 typedef void (*wlists_performOperation_t
) (unsigned int);
59 struct wlists_performOperation_p
{ wlists_performOperation_t proc
; };
61 struct wlists__T2_a
{ unsigned int array
[maxNoOfElements
-1+1]; };
63 unsigned int noOfElements
;
70 initList - creates a new wlist, l.
73 extern "C" wlists_wlist
wlists_initList (void);
76 killList - deletes the complete wlist, l.
79 extern "C" void wlists_killList (wlists_wlist
*l
);
82 putItemIntoList - places an WORD, c, into wlist, l.
85 extern "C" void wlists_putItemIntoList (wlists_wlist l
, unsigned int c
);
88 getItemFromList - retrieves the nth WORD from wlist, l.
91 extern "C" unsigned int wlists_getItemFromList (wlists_wlist l
, unsigned int n
);
94 getIndexOfList - returns the index for WORD, c, in wlist, l.
95 If more than one WORD, c, exists the index
96 for the first is returned.
99 extern "C" unsigned int wlists_getIndexOfList (wlists_wlist l
, unsigned int c
);
102 noOfItemsInList - returns the number of items in wlist, l.
105 extern "C" unsigned int wlists_noOfItemsInList (wlists_wlist l
);
108 includeItemIntoList - adds an WORD, c, into a wlist providing
109 the value does not already exist.
112 extern "C" void wlists_includeItemIntoList (wlists_wlist l
, unsigned int c
);
115 removeItemFromList - removes a WORD, c, from a wlist.
116 It assumes that this value only appears once.
119 extern "C" void wlists_removeItemFromList (wlists_wlist l
, unsigned int c
);
122 replaceItemInList - replace the nth WORD in wlist, l.
123 The first item in a wlists is at index, 1.
124 If the index, n, is out of range nothing is changed.
127 extern "C" void wlists_replaceItemInList (wlists_wlist l
, unsigned int n
, unsigned int w
);
130 isItemInList - returns true if a WORD, c, was found in wlist, l.
133 extern "C" unsigned int wlists_isItemInList (wlists_wlist l
, unsigned int c
);
136 foreachItemInListDo - calls procedure, P, foreach item in wlist, l.
139 extern "C" void wlists_foreachItemInListDo (wlists_wlist l
, wlists_performOperation p
);
142 duplicateList - returns a duplicate wlist derived from, l.
145 extern "C" wlists_wlist
wlists_duplicateList (wlists_wlist l
);
148 removeItem - remove an element at index, i, from the wlist data type.
151 static void removeItem (wlists_wlist p
, wlists_wlist l
, unsigned int i
);
155 removeItem - remove an element at index, i, from the wlist data type.
158 static void removeItem (wlists_wlist p
, wlists_wlist l
, unsigned int i
)
160 l
->noOfElements
-= 1;
161 while (i
<= l
->noOfElements
)
163 l
->elements
.array
[i
-1] = l
->elements
.array
[i
+1-1];
166 if ((l
->noOfElements
== 0) && (p
!= NULL
))
169 Storage_DEALLOCATE ((void **) &l
, sizeof (wlists__T1
));
175 initList - creates a new wlist, l.
178 extern "C" wlists_wlist
wlists_initList (void)
182 Storage_ALLOCATE ((void **) &l
, sizeof (wlists__T1
));
186 /* static analysis guarentees a RETURN statement will be used before here. */
187 __builtin_unreachable ();
192 killList - deletes the complete wlist, l.
195 extern "C" void wlists_killList (wlists_wlist
*l
)
199 if ((*l
)->next
!= NULL
)
201 wlists_killList (&(*l
)->next
);
203 Storage_DEALLOCATE ((void **) &(*l
), sizeof (wlists__T1
));
209 putItemIntoList - places an WORD, c, into wlist, l.
212 extern "C" void wlists_putItemIntoList (wlists_wlist l
, unsigned int c
)
214 if (l
->noOfElements
< maxNoOfElements
)
216 l
->noOfElements
+= 1;
217 l
->elements
.array
[l
->noOfElements
-1] = c
;
219 else if (l
->next
!= NULL
)
221 /* avoid dangling else. */
222 wlists_putItemIntoList (l
->next
, c
);
226 /* avoid dangling else. */
227 l
->next
= wlists_initList ();
228 wlists_putItemIntoList (l
->next
, c
);
234 getItemFromList - retrieves the nth WORD from wlist, l.
237 extern "C" unsigned int wlists_getItemFromList (wlists_wlist l
, unsigned int n
)
241 if (n
<= l
->noOfElements
)
243 return l
->elements
.array
[n
-1];
247 n
-= l
->noOfElements
;
251 return static_cast<unsigned int> (0);
252 /* static analysis guarentees a RETURN statement will be used before here. */
253 __builtin_unreachable ();
258 getIndexOfList - returns the index for WORD, c, in wlist, l.
259 If more than one WORD, c, exists the index
260 for the first is returned.
263 extern "C" unsigned int wlists_getIndexOfList (wlists_wlist l
, unsigned int c
)
274 while (i
<= l
->noOfElements
)
276 if (l
->elements
.array
[i
-1] == c
)
285 return l
->noOfElements
+(wlists_getIndexOfList (l
->next
, c
));
287 /* static analysis guarentees a RETURN statement will be used before here. */
288 __builtin_unreachable ();
293 noOfItemsInList - returns the number of items in wlist, l.
296 extern "C" unsigned int wlists_noOfItemsInList (wlists_wlist l
)
308 t
+= l
->noOfElements
;
310 } while (! (l
== NULL
));
313 /* static analysis guarentees a RETURN statement will be used before here. */
314 __builtin_unreachable ();
319 includeItemIntoList - adds an WORD, c, into a wlist providing
320 the value does not already exist.
323 extern "C" void wlists_includeItemIntoList (wlists_wlist l
, unsigned int c
)
325 if (! (wlists_isItemInList (l
, c
)))
327 wlists_putItemIntoList (l
, c
);
333 removeItemFromList - removes a WORD, c, from a wlist.
334 It assumes that this value only appears once.
337 extern "C" void wlists_removeItemFromList (wlists_wlist l
, unsigned int c
)
349 while ((i
<= l
->noOfElements
) && (l
->elements
.array
[i
-1] != c
))
353 if ((i
<= l
->noOfElements
) && (l
->elements
.array
[i
-1] == c
))
362 } while (! ((l
== NULL
) || found
));
365 removeItem (p
, l
, i
);
372 replaceItemInList - replace the nth WORD in wlist, l.
373 The first item in a wlists is at index, 1.
374 If the index, n, is out of range nothing is changed.
377 extern "C" void wlists_replaceItemInList (wlists_wlist l
, unsigned int n
, unsigned int w
)
381 if (n
<= l
->noOfElements
)
383 l
->elements
.array
[n
-1] = w
;
387 n
-= l
->noOfElements
;
395 isItemInList - returns true if a WORD, c, was found in wlist, l.
398 extern "C" unsigned int wlists_isItemInList (wlists_wlist l
, unsigned int c
)
404 while (i
<= l
->noOfElements
)
406 if (l
->elements
.array
[i
-1] == c
)
416 } while (! (l
== NULL
));
418 /* static analysis guarentees a RETURN statement will be used before here. */
419 __builtin_unreachable ();
424 foreachItemInListDo - calls procedure, P, foreach item in wlist, l.
427 extern "C" void wlists_foreachItemInListDo (wlists_wlist l
, wlists_performOperation p
)
432 n
= wlists_noOfItemsInList (l
);
436 (*p
.proc
) (wlists_getItemFromList (l
, i
));
443 duplicateList - returns a duplicate wlist derived from, l.
446 extern "C" wlists_wlist
wlists_duplicateList (wlists_wlist l
)
452 m
= wlists_initList ();
453 n
= wlists_noOfItemsInList (l
);
457 wlists_putItemIntoList (m
, wlists_getItemFromList (l
, i
));
461 /* static analysis guarentees a RETURN statement will be used before here. */
462 __builtin_unreachable ();
465 extern "C" void _M2_wlists_init (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])
469 extern "C" void _M2_wlists_finish (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])