| 1 |
/* do not edit automatically generated by mc from lists. */ |
| 2 |
/* Dynamic list library for pointers. |
| 3 |
Copyright (C) 2015-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of GNU Modula-2. |
| 6 |
|
| 7 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 3, or (at your option) any later |
| 10 |
version. |
| 11 |
|
| 12 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include "system.h" |
| 23 |
#include <stdbool.h> |
| 24 |
# if !defined (PROC_D) |
| 25 |
# define PROC_D |
| 26 |
typedef void (*PROC_t) (void); |
| 27 |
typedef struct { PROC_t proc; } PROC; |
| 28 |
# endif |
| 29 |
|
| 30 |
# if !defined (TRUE) |
| 31 |
# define TRUE (1==1) |
| 32 |
# endif |
| 33 |
|
| 34 |
# if !defined (FALSE) |
| 35 |
# define FALSE (1==0) |
| 36 |
# endif |
| 37 |
|
| 38 |
# include "GStorage.h" |
| 39 |
#if defined(__cplusplus) |
| 40 |
# undef NULL |
| 41 |
# define NULL 0 |
| 42 |
#endif |
| 43 |
#define _lists_C |
| 44 |
|
| 45 |
#include "Glists.h" |
| 46 |
# include "GStorage.h" |
| 47 |
|
| 48 |
typedef struct symbolKey_performOperation_p symbolKey_performOperation; |
| 49 |
|
| 50 |
# define MaxnoOfelements 5 |
| 51 |
typedef struct lists__T1_r lists__T1; |
| 52 |
|
| 53 |
typedef struct lists__T2_a lists__T2; |
| 54 |
|
| 55 |
typedef lists__T1 *lists_list__opaque; |
| 56 |
|
| 57 |
struct lists__T2_a { void * array[MaxnoOfelements-1+1]; }; |
| 58 |
struct lists__T1_r { |
| 59 |
unsigned int noOfelements; |
| 60 |
lists__T2 elements; |
| 61 |
lists_list__opaque next; |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
/* |
| 66 |
initList - creates a new list, l. |
| 67 |
*/ |
| 68 |
|
| 69 |
extern "C" lists_list lists_initList (void); |
| 70 |
|
| 71 |
/* |
| 72 |
killList - deletes the complete list, l. |
| 73 |
*/ |
| 74 |
|
| 75 |
extern "C" void lists_killList (lists_list *l); |
| 76 |
|
| 77 |
/* |
| 78 |
putItemIntoList - places an ADDRESS, c, into list, l. |
| 79 |
*/ |
| 80 |
|
| 81 |
extern "C" void lists_putItemIntoList (lists_list l, void * c); |
| 82 |
|
| 83 |
/* |
| 84 |
getItemFromList - retrieves the nth WORD from list, l. |
| 85 |
*/ |
| 86 |
|
| 87 |
extern "C" void * lists_getItemFromList (lists_list l, unsigned int n); |
| 88 |
|
| 89 |
/* |
| 90 |
getIndexOfList - returns the index for WORD, c, in list, l. |
| 91 |
If more than one WORD, c, exists the index |
| 92 |
for the first is returned. |
| 93 |
*/ |
| 94 |
|
| 95 |
extern "C" unsigned int lists_getIndexOfList (lists_list l, void * c); |
| 96 |
|
| 97 |
/* |
| 98 |
noOfItemsInList - returns the number of items in list, l. |
| 99 |
*/ |
| 100 |
|
| 101 |
extern "C" unsigned int lists_noOfItemsInList (lists_list l); |
| 102 |
|
| 103 |
/* |
| 104 |
includeItemIntoList - adds an ADDRESS, c, into a list providing |
| 105 |
the value does not already exist. |
| 106 |
*/ |
| 107 |
|
| 108 |
extern "C" void lists_includeItemIntoList (lists_list l, void * c); |
| 109 |
|
| 110 |
/* |
| 111 |
removeItemFromList - removes a ADDRESS, c, from a list. |
| 112 |
It assumes that this value only appears once. |
| 113 |
*/ |
| 114 |
|
| 115 |
extern "C" void lists_removeItemFromList (lists_list l, void * c); |
| 116 |
|
| 117 |
/* |
| 118 |
isItemInList - returns true if a ADDRESS, c, was found in list, l. |
| 119 |
*/ |
| 120 |
|
| 121 |
extern "C" bool lists_isItemInList (lists_list l, void * c); |
| 122 |
|
| 123 |
/* |
| 124 |
foreachItemInListDo - calls procedure, P, foreach item in list, l. |
| 125 |
*/ |
| 126 |
|
| 127 |
extern "C" void lists_foreachItemInListDo (lists_list l, symbolKey_performOperation p); |
| 128 |
|
| 129 |
/* |
| 130 |
duplicateList - returns a duplicate list derived from, l. |
| 131 |
*/ |
| 132 |
|
| 133 |
extern "C" lists_list lists_duplicateList (lists_list l); |
| 134 |
|
| 135 |
/* |
| 136 |
removeItem - remove an element at index, i, from the list data type. |
| 137 |
*/ |
| 138 |
|
| 139 |
static void removeItem (lists_list__opaque p, lists_list__opaque l, unsigned int i); |
| 140 |
|
| 141 |
|
| 142 |
/* |
| 143 |
removeItem - remove an element at index, i, from the list data type. |
| 144 |
*/ |
| 145 |
|
| 146 |
static void removeItem (lists_list__opaque p, lists_list__opaque l, unsigned int i) |
| 147 |
{ |
| 148 |
l->noOfelements -= 1; |
| 149 |
while (i <= l->noOfelements) |
| 150 |
{ |
| 151 |
l->elements.array[i-1] = l->elements.array[i+1-1]; |
| 152 |
i += 1; |
| 153 |
} |
| 154 |
if ((l->noOfelements == 0) && (p != NULL)) |
| 155 |
{ |
| 156 |
p->next = l->next; |
| 157 |
Storage_DEALLOCATE ((void **) &l, sizeof (lists__T1)); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
/* |
| 163 |
initList - creates a new list, l. |
| 164 |
*/ |
| 165 |
|
| 166 |
extern "C" lists_list lists_initList (void) |
| 167 |
{ |
| 168 |
lists_list__opaque l; |
| 169 |
|
| 170 |
Storage_ALLOCATE ((void **) &l, sizeof (lists__T1)); |
| 171 |
l->noOfelements = 0; |
| 172 |
l->next = static_cast<lists_list__opaque> (NULL); |
| 173 |
return static_cast<lists_list> (l); |
| 174 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 175 |
__builtin_unreachable (); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/* |
| 180 |
killList - deletes the complete list, l. |
| 181 |
*/ |
| 182 |
|
| 183 |
extern "C" void lists_killList (lists_list *l) |
| 184 |
{ |
| 185 |
if ((*l) != NULL) |
| 186 |
{ |
| 187 |
if (static_cast<lists_list__opaque> ((*l))->next != NULL) |
| 188 |
{ |
| 189 |
lists_killList (reinterpret_cast<lists_list *> (&static_cast<lists_list__opaque> ((*l))->next)); |
| 190 |
} |
| 191 |
Storage_DEALLOCATE ((void **) &(*l), sizeof (lists__T1)); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/* |
| 197 |
putItemIntoList - places an ADDRESS, c, into list, l. |
| 198 |
*/ |
| 199 |
|
| 200 |
extern "C" void lists_putItemIntoList (lists_list l, void * c) |
| 201 |
{ |
| 202 |
if (static_cast<lists_list__opaque> (l)->noOfelements < MaxnoOfelements) |
| 203 |
{ |
| 204 |
static_cast<lists_list__opaque> (l)->noOfelements += 1; |
| 205 |
static_cast<lists_list__opaque> (l)->elements.array[static_cast<lists_list__opaque> (l)->noOfelements-1] = c; |
| 206 |
} |
| 207 |
else if (static_cast<lists_list__opaque> (l)->next != NULL) |
| 208 |
{ |
| 209 |
/* avoid dangling else. */ |
| 210 |
lists_putItemIntoList (static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next), c); |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
/* avoid dangling else. */ |
| 215 |
static_cast<lists_list__opaque> (l)->next = static_cast<lists_list__opaque> (lists_initList ()); |
| 216 |
lists_putItemIntoList (static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next), c); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
/* |
| 222 |
getItemFromList - retrieves the nth WORD from list, l. |
| 223 |
*/ |
| 224 |
|
| 225 |
extern "C" void * lists_getItemFromList (lists_list l, unsigned int n) |
| 226 |
{ |
| 227 |
while (l != NULL) |
| 228 |
{ |
| 229 |
if (n <= static_cast<lists_list__opaque> (l)->noOfelements) |
| 230 |
{ |
| 231 |
return static_cast<lists_list__opaque> (l)->elements.array[n-1]; |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
n -= static_cast<lists_list__opaque> (l)->noOfelements; |
| 236 |
} |
| 237 |
l = static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next); |
| 238 |
} |
| 239 |
return static_cast<void *> (0); |
| 240 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 241 |
__builtin_unreachable (); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
/* |
| 246 |
getIndexOfList - returns the index for WORD, c, in list, l. |
| 247 |
If more than one WORD, c, exists the index |
| 248 |
for the first is returned. |
| 249 |
*/ |
| 250 |
|
| 251 |
extern "C" unsigned int lists_getIndexOfList (lists_list l, void * c) |
| 252 |
{ |
| 253 |
unsigned int i; |
| 254 |
|
| 255 |
if (l == NULL) |
| 256 |
{ |
| 257 |
return 0; |
| 258 |
} |
| 259 |
else |
| 260 |
{ |
| 261 |
i = 1; |
| 262 |
while (i <= static_cast<lists_list__opaque> (l)->noOfelements) |
| 263 |
{ |
| 264 |
if (static_cast<lists_list__opaque> (l)->elements.array[i-1] == c) |
| 265 |
{ |
| 266 |
return i; |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
i += 1; |
| 271 |
} |
| 272 |
} |
| 273 |
return static_cast<lists_list__opaque> (l)->noOfelements+(lists_getIndexOfList (static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next), c)); |
| 274 |
} |
| 275 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 276 |
__builtin_unreachable (); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
/* |
| 281 |
noOfItemsInList - returns the number of items in list, l. |
| 282 |
*/ |
| 283 |
|
| 284 |
extern "C" unsigned int lists_noOfItemsInList (lists_list l) |
| 285 |
{ |
| 286 |
unsigned int t; |
| 287 |
|
| 288 |
if (l == NULL) |
| 289 |
{ |
| 290 |
return 0; |
| 291 |
} |
| 292 |
else |
| 293 |
{ |
| 294 |
t = 0; |
| 295 |
do { |
| 296 |
t += static_cast<lists_list__opaque> (l)->noOfelements; |
| 297 |
l = static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next); |
| 298 |
} while (! (l == NULL)); |
| 299 |
return t; |
| 300 |
} |
| 301 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 302 |
__builtin_unreachable (); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
/* |
| 307 |
includeItemIntoList - adds an ADDRESS, c, into a list providing |
| 308 |
the value does not already exist. |
| 309 |
*/ |
| 310 |
|
| 311 |
extern "C" void lists_includeItemIntoList (lists_list l, void * c) |
| 312 |
{ |
| 313 |
if (! (lists_isItemInList (l, c))) |
| 314 |
{ |
| 315 |
lists_putItemIntoList (l, c); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
/* |
| 321 |
removeItemFromList - removes a ADDRESS, c, from a list. |
| 322 |
It assumes that this value only appears once. |
| 323 |
*/ |
| 324 |
|
| 325 |
extern "C" void lists_removeItemFromList (lists_list l, void * c) |
| 326 |
{ |
| 327 |
lists_list__opaque p; |
| 328 |
unsigned int i; |
| 329 |
bool found; |
| 330 |
|
| 331 |
if (l != NULL) |
| 332 |
{ |
| 333 |
found = false; |
| 334 |
p = static_cast<lists_list__opaque> (NULL); |
| 335 |
do { |
| 336 |
i = 1; |
| 337 |
while ((i <= static_cast<lists_list__opaque> (l)->noOfelements) && (static_cast<lists_list__opaque> (l)->elements.array[i-1] != c)) |
| 338 |
{ |
| 339 |
i += 1; |
| 340 |
} |
| 341 |
if ((i <= static_cast<lists_list__opaque> (l)->noOfelements) && (static_cast<lists_list__opaque> (l)->elements.array[i-1] == c)) |
| 342 |
{ |
| 343 |
found = true; |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
p = static_cast<lists_list__opaque> (l); |
| 348 |
l = static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next); |
| 349 |
} |
| 350 |
} while (! ((l == NULL) || found)); |
| 351 |
if (found) |
| 352 |
{ |
| 353 |
removeItem (p, static_cast<lists_list__opaque> (l), i); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
/* |
| 360 |
isItemInList - returns true if a ADDRESS, c, was found in list, l. |
| 361 |
*/ |
| 362 |
|
| 363 |
extern "C" bool lists_isItemInList (lists_list l, void * c) |
| 364 |
{ |
| 365 |
unsigned int i; |
| 366 |
|
| 367 |
do { |
| 368 |
i = 1; |
| 369 |
while (i <= static_cast<lists_list__opaque> (l)->noOfelements) |
| 370 |
{ |
| 371 |
if (static_cast<lists_list__opaque> (l)->elements.array[i-1] == c) |
| 372 |
{ |
| 373 |
return true; |
| 374 |
} |
| 375 |
else |
| 376 |
{ |
| 377 |
i += 1; |
| 378 |
} |
| 379 |
} |
| 380 |
l = static_cast<lists_list> (static_cast<lists_list__opaque> (l)->next); |
| 381 |
} while (! (l == NULL)); |
| 382 |
return false; |
| 383 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 384 |
__builtin_unreachable (); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
/* |
| 389 |
foreachItemInListDo - calls procedure, P, foreach item in list, l. |
| 390 |
*/ |
| 391 |
|
| 392 |
extern "C" void lists_foreachItemInListDo (lists_list l, symbolKey_performOperation p) |
| 393 |
{ |
| 394 |
unsigned int i; |
| 395 |
unsigned int n; |
| 396 |
|
| 397 |
n = lists_noOfItemsInList (l); |
| 398 |
i = 1; |
| 399 |
while (i <= n) |
| 400 |
{ |
| 401 |
(*p.proc) (lists_getItemFromList (l, i)); |
| 402 |
i += 1; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
/* |
| 408 |
duplicateList - returns a duplicate list derived from, l. |
| 409 |
*/ |
| 410 |
|
| 411 |
extern "C" lists_list lists_duplicateList (lists_list l) |
| 412 |
{ |
| 413 |
lists_list__opaque m; |
| 414 |
unsigned int n; |
| 415 |
unsigned int i; |
| 416 |
|
| 417 |
m = static_cast<lists_list__opaque> (lists_initList ()); |
| 418 |
n = lists_noOfItemsInList (l); |
| 419 |
i = 1; |
| 420 |
while (i <= n) |
| 421 |
{ |
| 422 |
lists_putItemIntoList (static_cast<lists_list> (m), lists_getItemFromList (l, i)); |
| 423 |
i += 1; |
| 424 |
} |
| 425 |
return static_cast<lists_list> (m); |
| 426 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 427 |
__builtin_unreachable (); |
| 428 |
} |
| 429 |
|
| 430 |
extern "C" void _M2_lists_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 431 |
{ |
| 432 |
} |
| 433 |
|
| 434 |
extern "C" void _M2_lists_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 435 |
{ |
| 436 |
} |