1 /* do not edit automatically generated by mc from Lists. */
2 /* Lists.mod provides an unordered list manipulation package.
4 Copyright (C) 2001-2022 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.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/>. */
23 # if !defined (PROC_D)
25 typedef void (*PROC_t
) (void);
26 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_list_r Lists_list
;
53 typedef struct Lists__T1_a Lists__T1
;
55 typedef Lists_list
*Lists_List
;
57 typedef void (*SymbolKey_PerformOperation_t
) (unsigned int);
58 struct SymbolKey_PerformOperation_p
{ SymbolKey_PerformOperation_t proc
; };
60 struct Lists__T1_a
{ unsigned int array
[MaxNoOfElements
-1+1]; };
62 unsigned int NoOfElements
;
69 InitList - creates a new list, l.
72 extern "C" void Lists_InitList (Lists_List
*l
);
75 KillList - deletes the complete list, l.
78 extern "C" void Lists_KillList (Lists_List
*l
);
81 PutItemIntoList - places a WORD, c, into list, l.
84 extern "C" void Lists_PutItemIntoList (Lists_List l
, unsigned int c
);
85 extern "C" unsigned int Lists_GetItemFromList (Lists_List l
, unsigned int n
);
88 GetIndexOfList - returns the index for WORD, c, in list, l.
89 If more than one WORD, c, exists the index
90 for the first is returned.
93 extern "C" unsigned int Lists_GetIndexOfList (Lists_List l
, unsigned int c
);
96 NoOfItemsInList - returns the number of items in list, l.
97 (iterative algorithm of the above).
100 extern "C" unsigned int Lists_NoOfItemsInList (Lists_List l
);
103 IncludeItemIntoList - adds a WORD, c, into a list providing
104 the value does not already exist.
107 extern "C" void Lists_IncludeItemIntoList (Lists_List l
, unsigned int c
);
110 RemoveItemFromList - removes a WORD, c, from a list.
111 It assumes that this value only appears once.
114 extern "C" void Lists_RemoveItemFromList (Lists_List l
, unsigned int c
);
117 IsItemInList - returns true if a WORD, c, was found in list, l.
120 extern "C" unsigned int Lists_IsItemInList (Lists_List l
, unsigned int c
);
123 ForeachItemInListDo - calls procedure, P, foreach item in list, l.
126 extern "C" void Lists_ForeachItemInListDo (Lists_List l
, SymbolKey_PerformOperation P
);
129 DuplicateList - returns a duplicate list derived from, l.
132 extern "C" Lists_List
Lists_DuplicateList (Lists_List l
);
135 RemoveItem - remove an element at index, i, from the list data type.
138 static void RemoveItem (Lists_List p
, Lists_List l
, unsigned int i
);
142 RemoveItem - remove an element at index, i, from the list data type.
145 static void RemoveItem (Lists_List p
, Lists_List l
, unsigned int i
)
147 l
->NoOfElements
-= 1;
148 while (i
<= l
->NoOfElements
)
150 l
->Elements
.array
[i
-1] = l
->Elements
.array
[i
+1-1];
153 if ((l
->NoOfElements
== 0) && (p
!= NULL
))
156 Storage_DEALLOCATE ((void **) &l
, sizeof (Lists_list
));
162 InitList - creates a new list, l.
165 extern "C" void Lists_InitList (Lists_List
*l
)
167 Storage_ALLOCATE ((void **) &(*l
), sizeof (Lists_list
));
168 (*l
)->NoOfElements
= 0;
174 KillList - deletes the complete list, l.
177 extern "C" void Lists_KillList (Lists_List
*l
)
181 if ((*l
)->Next
!= NULL
)
183 Lists_KillList (&(*l
)->Next
);
185 Storage_DEALLOCATE ((void **) &(*l
), sizeof (Lists_list
));
191 PutItemIntoList - places a WORD, c, into list, l.
194 extern "C" void Lists_PutItemIntoList (Lists_List l
, unsigned int c
)
196 if (l
->NoOfElements
< MaxNoOfElements
)
198 l
->NoOfElements
+= 1;
199 l
->Elements
.array
[l
->NoOfElements
-1] = c
;
201 else if (l
->Next
!= NULL
)
203 /* avoid dangling else. */
204 Lists_PutItemIntoList (l
->Next
, c
);
208 /* avoid dangling else. */
209 Lists_InitList (&l
->Next
);
210 Lists_PutItemIntoList (l
->Next
, c
);
214 extern "C" unsigned int Lists_GetItemFromList (Lists_List l
, unsigned int n
)
216 /* iterative solution */
219 if (n
<= l
->NoOfElements
)
221 return l
->Elements
.array
[n
-1];
225 n
-= l
->NoOfElements
;
229 return static_cast<unsigned int> (0);
230 /* static analysis guarentees a RETURN statement will be used before here. */
231 __builtin_unreachable ();
236 GetIndexOfList - returns the index for WORD, c, in list, l.
237 If more than one WORD, c, exists the index
238 for the first is returned.
241 extern "C" unsigned int Lists_GetIndexOfList (Lists_List l
, unsigned int c
)
252 while (i
<= l
->NoOfElements
)
254 if (l
->Elements
.array
[i
-1] == c
)
263 return l
->NoOfElements
+(Lists_GetIndexOfList (l
->Next
, c
));
265 /* static analysis guarentees a RETURN statement will be used before here. */
266 __builtin_unreachable ();
271 NoOfItemsInList - returns the number of items in list, l.
272 (iterative algorithm of the above).
275 extern "C" unsigned int Lists_NoOfItemsInList (Lists_List l
)
287 t
+= l
->NoOfElements
;
289 } while (! (l
== NULL
));
292 /* static analysis guarentees a RETURN statement will be used before here. */
293 __builtin_unreachable ();
298 IncludeItemIntoList - adds a WORD, c, into a list providing
299 the value does not already exist.
302 extern "C" void Lists_IncludeItemIntoList (Lists_List l
, unsigned int c
)
304 if (! (Lists_IsItemInList (l
, c
)))
306 Lists_PutItemIntoList (l
, c
);
312 RemoveItemFromList - removes a WORD, c, from a list.
313 It assumes that this value only appears once.
316 extern "C" void Lists_RemoveItemFromList (Lists_List l
, unsigned int c
)
328 while ((i
<= l
->NoOfElements
) && (l
->Elements
.array
[i
-1] != c
))
332 if ((i
<= l
->NoOfElements
) && (l
->Elements
.array
[i
-1] == c
))
341 } while (! ((l
== NULL
) || Found
));
344 RemoveItem (p
, l
, i
);
351 IsItemInList - returns true if a WORD, c, was found in list, l.
354 extern "C" unsigned int Lists_IsItemInList (Lists_List l
, unsigned int c
)
360 while (i
<= l
->NoOfElements
)
362 if (l
->Elements
.array
[i
-1] == c
)
372 } while (! (l
== NULL
));
374 /* static analysis guarentees a RETURN statement will be used before here. */
375 __builtin_unreachable ();
380 ForeachItemInListDo - calls procedure, P, foreach item in list, l.
383 extern "C" void Lists_ForeachItemInListDo (Lists_List l
, SymbolKey_PerformOperation P
)
388 n
= Lists_NoOfItemsInList (l
);
392 (*P
.proc
) (Lists_GetItemFromList (l
, i
));
399 DuplicateList - returns a duplicate list derived from, l.
402 extern "C" Lists_List
Lists_DuplicateList (Lists_List l
)
409 n
= Lists_NoOfItemsInList (l
);
413 Lists_PutItemIntoList (m
, Lists_GetItemFromList (l
, i
));
417 /* static analysis guarentees a RETURN statement will be used before here. */
418 __builtin_unreachable ();
421 extern "C" void _M2_Lists_init (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])
425 extern "C" void _M2_Lists_finish (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])