]>
Commit | Line | Data |
---|---|---|
fbd2eda1 GM |
1 | /* do not edit automatically generated by mc from wlists. */ |
2 | /* wlists.mod word lists module. | |
3 | ||
a945c346 | 4 | Copyright (C) 2015-2024 Free Software Foundation, Inc. |
fbd2eda1 GM |
5 | Contributed by Gaius Mulley <gaius@glam.ac.uk>. |
6 | ||
7 | This file is part of GNU Modula-2. | |
8 | ||
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) | |
12 | any later version. | |
13 | ||
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. | |
18 | ||
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/>. */ | |
22 | ||
23 | #include "config.h" | |
24 | #include "system.h" | |
431c0669 | 25 | #include <stdbool.h> |
fbd2eda1 GM |
26 | # if !defined (PROC_D) |
27 | # define PROC_D | |
28 | typedef void (*PROC_t) (void); | |
29 | typedef struct { PROC_t proc; } PROC; | |
30 | # endif | |
31 | ||
32 | # if !defined (TRUE) | |
33 | # define TRUE (1==1) | |
34 | # endif | |
35 | ||
36 | # if !defined (FALSE) | |
37 | # define FALSE (1==0) | |
38 | # endif | |
39 | ||
40 | # include "GStorage.h" | |
41 | #if defined(__cplusplus) | |
42 | # undef NULL | |
43 | # define NULL 0 | |
44 | #endif | |
45 | #define _wlists_H | |
46 | #define _wlists_C | |
47 | ||
48 | # include "GStorage.h" | |
49 | ||
50 | typedef struct wlists_performOperation_p wlists_performOperation; | |
51 | ||
52 | # define maxNoOfElements 5 | |
53 | typedef struct wlists__T1_r wlists__T1; | |
54 | ||
55 | typedef struct wlists__T2_a wlists__T2; | |
56 | ||
57 | typedef wlists__T1 *wlists_wlist; | |
58 | ||
59 | typedef void (*wlists_performOperation_t) (unsigned int); | |
60 | struct wlists_performOperation_p { wlists_performOperation_t proc; }; | |
61 | ||
62 | struct wlists__T2_a { unsigned int array[maxNoOfElements-1+1]; }; | |
63 | struct wlists__T1_r { | |
64 | unsigned int noOfElements; | |
65 | wlists__T2 elements; | |
66 | wlists_wlist next; | |
67 | }; | |
68 | ||
69 | ||
70 | /* | |
71 | initList - creates a new wlist, l. | |
72 | */ | |
73 | ||
74 | extern "C" wlists_wlist wlists_initList (void); | |
75 | ||
76 | /* | |
77 | killList - deletes the complete wlist, l. | |
78 | */ | |
79 | ||
80 | extern "C" void wlists_killList (wlists_wlist *l); | |
81 | ||
82 | /* | |
83 | putItemIntoList - places an WORD, c, into wlist, l. | |
84 | */ | |
85 | ||
86 | extern "C" void wlists_putItemIntoList (wlists_wlist l, unsigned int c); | |
87 | ||
88 | /* | |
89 | getItemFromList - retrieves the nth WORD from wlist, l. | |
90 | */ | |
91 | ||
92 | extern "C" unsigned int wlists_getItemFromList (wlists_wlist l, unsigned int n); | |
93 | ||
94 | /* | |
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. | |
98 | */ | |
99 | ||
100 | extern "C" unsigned int wlists_getIndexOfList (wlists_wlist l, unsigned int c); | |
101 | ||
102 | /* | |
103 | noOfItemsInList - returns the number of items in wlist, l. | |
104 | */ | |
105 | ||
106 | extern "C" unsigned int wlists_noOfItemsInList (wlists_wlist l); | |
107 | ||
108 | /* | |
109 | includeItemIntoList - adds an WORD, c, into a wlist providing | |
110 | the value does not already exist. | |
111 | */ | |
112 | ||
113 | extern "C" void wlists_includeItemIntoList (wlists_wlist l, unsigned int c); | |
114 | ||
115 | /* | |
116 | removeItemFromList - removes a WORD, c, from a wlist. | |
117 | It assumes that this value only appears once. | |
118 | */ | |
119 | ||
120 | extern "C" void wlists_removeItemFromList (wlists_wlist l, unsigned int c); | |
121 | ||
122 | /* | |
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. | |
126 | */ | |
127 | ||
128 | extern "C" void wlists_replaceItemInList (wlists_wlist l, unsigned int n, unsigned int w); | |
129 | ||
130 | /* | |
131 | isItemInList - returns true if a WORD, c, was found in wlist, l. | |
132 | */ | |
133 | ||
431c0669 | 134 | extern "C" bool wlists_isItemInList (wlists_wlist l, unsigned int c); |
fbd2eda1 GM |
135 | |
136 | /* | |
137 | foreachItemInListDo - calls procedure, P, foreach item in wlist, l. | |
138 | */ | |
139 | ||
140 | extern "C" void wlists_foreachItemInListDo (wlists_wlist l, wlists_performOperation p); | |
141 | ||
142 | /* | |
143 | duplicateList - returns a duplicate wlist derived from, l. | |
144 | */ | |
145 | ||
146 | extern "C" wlists_wlist wlists_duplicateList (wlists_wlist l); | |
147 | ||
148 | /* | |
149 | removeItem - remove an element at index, i, from the wlist data type. | |
150 | */ | |
151 | ||
152 | static void removeItem (wlists_wlist p, wlists_wlist l, unsigned int i); | |
153 | ||
154 | ||
155 | /* | |
156 | removeItem - remove an element at index, i, from the wlist data type. | |
157 | */ | |
158 | ||
159 | static void removeItem (wlists_wlist p, wlists_wlist l, unsigned int i) | |
160 | { | |
161 | l->noOfElements -= 1; | |
162 | while (i <= l->noOfElements) | |
163 | { | |
164 | l->elements.array[i-1] = l->elements.array[i+1-1]; | |
165 | i += 1; | |
166 | } | |
167 | if ((l->noOfElements == 0) && (p != NULL)) | |
168 | { | |
169 | p->next = l->next; | |
170 | Storage_DEALLOCATE ((void **) &l, sizeof (wlists__T1)); | |
171 | } | |
172 | } | |
173 | ||
174 | ||
175 | /* | |
176 | initList - creates a new wlist, l. | |
177 | */ | |
178 | ||
179 | extern "C" wlists_wlist wlists_initList (void) | |
180 | { | |
181 | wlists_wlist l; | |
182 | ||
183 | Storage_ALLOCATE ((void **) &l, sizeof (wlists__T1)); | |
184 | l->noOfElements = 0; | |
185 | l->next = NULL; | |
186 | return l; | |
187 | /* static analysis guarentees a RETURN statement will be used before here. */ | |
188 | __builtin_unreachable (); | |
189 | } | |
190 | ||
191 | ||
192 | /* | |
193 | killList - deletes the complete wlist, l. | |
194 | */ | |
195 | ||
196 | extern "C" void wlists_killList (wlists_wlist *l) | |
197 | { | |
198 | if ((*l) != NULL) | |
199 | { | |
200 | if ((*l)->next != NULL) | |
201 | { | |
202 | wlists_killList (&(*l)->next); | |
203 | } | |
204 | Storage_DEALLOCATE ((void **) &(*l), sizeof (wlists__T1)); | |
205 | } | |
206 | } | |
207 | ||
208 | ||
209 | /* | |
210 | putItemIntoList - places an WORD, c, into wlist, l. | |
211 | */ | |
212 | ||
213 | extern "C" void wlists_putItemIntoList (wlists_wlist l, unsigned int c) | |
214 | { | |
215 | if (l->noOfElements < maxNoOfElements) | |
216 | { | |
217 | l->noOfElements += 1; | |
218 | l->elements.array[l->noOfElements-1] = c; | |
219 | } | |
220 | else if (l->next != NULL) | |
221 | { | |
222 | /* avoid dangling else. */ | |
223 | wlists_putItemIntoList (l->next, c); | |
224 | } | |
225 | else | |
226 | { | |
227 | /* avoid dangling else. */ | |
228 | l->next = wlists_initList (); | |
229 | wlists_putItemIntoList (l->next, c); | |
230 | } | |
231 | } | |
232 | ||
233 | ||
234 | /* | |
235 | getItemFromList - retrieves the nth WORD from wlist, l. | |
236 | */ | |
237 | ||
238 | extern "C" unsigned int wlists_getItemFromList (wlists_wlist l, unsigned int n) | |
239 | { | |
240 | while (l != NULL) | |
241 | { | |
242 | if (n <= l->noOfElements) | |
243 | { | |
244 | return l->elements.array[n-1]; | |
245 | } | |
246 | else | |
247 | { | |
248 | n -= l->noOfElements; | |
249 | } | |
250 | l = l->next; | |
251 | } | |
252 | return static_cast<unsigned int> (0); | |
253 | /* static analysis guarentees a RETURN statement will be used before here. */ | |
254 | __builtin_unreachable (); | |
255 | } | |
256 | ||
257 | ||
258 | /* | |
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. | |
262 | */ | |
263 | ||
264 | extern "C" unsigned int wlists_getIndexOfList (wlists_wlist l, unsigned int c) | |
265 | { | |
266 | unsigned int i; | |
267 | ||
268 | if (l == NULL) | |
269 | { | |
270 | return 0; | |
271 | } | |
272 | else | |
273 | { | |
274 | i = 1; | |
275 | while (i <= l->noOfElements) | |
276 | { | |
277 | if (l->elements.array[i-1] == c) | |
278 | { | |
279 | return i; | |
280 | } | |
281 | else | |
282 | { | |
283 | i += 1; | |
284 | } | |
285 | } | |
286 | return l->noOfElements+(wlists_getIndexOfList (l->next, c)); | |
287 | } | |
288 | /* static analysis guarentees a RETURN statement will be used before here. */ | |
289 | __builtin_unreachable (); | |
290 | } | |
291 | ||
292 | ||
293 | /* | |
294 | noOfItemsInList - returns the number of items in wlist, l. | |
295 | */ | |
296 | ||
297 | extern "C" unsigned int wlists_noOfItemsInList (wlists_wlist l) | |
298 | { | |
299 | unsigned int t; | |
300 | ||
301 | if (l == NULL) | |
302 | { | |
303 | return 0; | |
304 | } | |
305 | else | |
306 | { | |
307 | t = 0; | |
308 | do { | |
309 | t += l->noOfElements; | |
310 | l = l->next; | |
311 | } while (! (l == NULL)); | |
312 | return t; | |
313 | } | |
314 | /* static analysis guarentees a RETURN statement will be used before here. */ | |
315 | __builtin_unreachable (); | |
316 | } | |
317 | ||
318 | ||
319 | /* | |
320 | includeItemIntoList - adds an WORD, c, into a wlist providing | |
321 | the value does not already exist. | |
322 | */ | |
323 | ||
324 | extern "C" void wlists_includeItemIntoList (wlists_wlist l, unsigned int c) | |
325 | { | |
326 | if (! (wlists_isItemInList (l, c))) | |
327 | { | |
328 | wlists_putItemIntoList (l, c); | |
329 | } | |
330 | } | |
331 | ||
332 | ||
333 | /* | |
334 | removeItemFromList - removes a WORD, c, from a wlist. | |
335 | It assumes that this value only appears once. | |
336 | */ | |
337 | ||
338 | extern "C" void wlists_removeItemFromList (wlists_wlist l, unsigned int c) | |
339 | { | |
340 | wlists_wlist p; | |
341 | unsigned int i; | |
431c0669 | 342 | bool found; |
fbd2eda1 GM |
343 | |
344 | if (l != NULL) | |
345 | { | |
431c0669 | 346 | found = false; |
fbd2eda1 GM |
347 | p = NULL; |
348 | do { | |
349 | i = 1; | |
350 | while ((i <= l->noOfElements) && (l->elements.array[i-1] != c)) | |
351 | { | |
352 | i += 1; | |
353 | } | |
354 | if ((i <= l->noOfElements) && (l->elements.array[i-1] == c)) | |
355 | { | |
431c0669 | 356 | found = true; |
fbd2eda1 GM |
357 | } |
358 | else | |
359 | { | |
360 | p = l; | |
361 | l = l->next; | |
362 | } | |
363 | } while (! ((l == NULL) || found)); | |
364 | if (found) | |
365 | { | |
366 | removeItem (p, l, i); | |
367 | } | |
368 | } | |
369 | } | |
370 | ||
371 | ||
372 | /* | |
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. | |
376 | */ | |
377 | ||
378 | extern "C" void wlists_replaceItemInList (wlists_wlist l, unsigned int n, unsigned int w) | |
379 | { | |
380 | while (l != NULL) | |
381 | { | |
382 | if (n <= l->noOfElements) | |
383 | { | |
384 | l->elements.array[n-1] = w; | |
385 | } | |
386 | else | |
387 | { | |
388 | n -= l->noOfElements; | |
389 | } | |
390 | l = l->next; | |
391 | } | |
392 | } | |
393 | ||
394 | ||
395 | /* | |
396 | isItemInList - returns true if a WORD, c, was found in wlist, l. | |
397 | */ | |
398 | ||
431c0669 | 399 | extern "C" bool wlists_isItemInList (wlists_wlist l, unsigned int c) |
fbd2eda1 GM |
400 | { |
401 | unsigned int i; | |
402 | ||
403 | do { | |
404 | i = 1; | |
405 | while (i <= l->noOfElements) | |
406 | { | |
407 | if (l->elements.array[i-1] == c) | |
408 | { | |
431c0669 | 409 | return true; |
fbd2eda1 GM |
410 | } |
411 | else | |
412 | { | |
413 | i += 1; | |
414 | } | |
415 | } | |
416 | l = l->next; | |
417 | } while (! (l == NULL)); | |
431c0669 | 418 | return false; |
fbd2eda1 GM |
419 | /* static analysis guarentees a RETURN statement will be used before here. */ |
420 | __builtin_unreachable (); | |
421 | } | |
422 | ||
423 | ||
424 | /* | |
425 | foreachItemInListDo - calls procedure, P, foreach item in wlist, l. | |
426 | */ | |
427 | ||
428 | extern "C" void wlists_foreachItemInListDo (wlists_wlist l, wlists_performOperation p) | |
429 | { | |
430 | unsigned int i; | |
431 | unsigned int n; | |
432 | ||
433 | n = wlists_noOfItemsInList (l); | |
434 | i = 1; | |
435 | while (i <= n) | |
436 | { | |
437 | (*p.proc) (wlists_getItemFromList (l, i)); | |
438 | i += 1; | |
439 | } | |
440 | } | |
441 | ||
442 | ||
443 | /* | |
444 | duplicateList - returns a duplicate wlist derived from, l. | |
445 | */ | |
446 | ||
447 | extern "C" wlists_wlist wlists_duplicateList (wlists_wlist l) | |
448 | { | |
449 | wlists_wlist m; | |
450 | unsigned int n; | |
451 | unsigned int i; | |
452 | ||
453 | m = wlists_initList (); | |
454 | n = wlists_noOfItemsInList (l); | |
455 | i = 1; | |
456 | while (i <= n) | |
457 | { | |
458 | wlists_putItemIntoList (m, wlists_getItemFromList (l, i)); | |
459 | i += 1; | |
460 | } | |
461 | return m; | |
462 | /* static analysis guarentees a RETURN statement will be used before here. */ | |
463 | __builtin_unreachable (); | |
464 | } | |
465 | ||
466 | extern "C" void _M2_wlists_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) | |
467 | { | |
468 | } | |
469 | ||
470 | extern "C" void _M2_wlists_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) | |
471 | { | |
472 | } |