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