| 1 |
/* do not edit automatically generated by mc from Lists. */ |
| 2 |
/* Lists.mod provides an unordered list manipulation package. |
| 3 |
|
| 4 |
Copyright (C) 2001-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 <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 <stddef.h> |
| 39 |
# include "GStorage.h" |
| 40 |
#if defined(__cplusplus) |
| 41 |
# undef NULL |
| 42 |
# define NULL 0 |
| 43 |
#endif |
| 44 |
#define _Lists_C |
| 45 |
|
| 46 |
#include "GLists.h" |
| 47 |
# include "GStorage.h" |
| 48 |
|
| 49 |
typedef struct SymbolKey_PerformOperation_p SymbolKey_PerformOperation; |
| 50 |
|
| 51 |
# define MaxNoOfElements 5 |
| 52 |
typedef struct Lists_list_r Lists_list; |
| 53 |
|
| 54 |
typedef struct Lists__T1_a Lists__T1; |
| 55 |
|
| 56 |
typedef Lists_list *Lists_List__opaque; |
| 57 |
|
| 58 |
struct Lists__T1_a { unsigned int array[MaxNoOfElements-1+1]; }; |
| 59 |
struct Lists_list_r { |
| 60 |
unsigned int NoOfElements; |
| 61 |
Lists__T1 Elements; |
| 62 |
Lists_List__opaque Next; |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
/* |
| 67 |
InitList - creates a new list, l. |
| 68 |
*/ |
| 69 |
|
| 70 |
extern "C" void Lists_InitList (Lists_List *l); |
| 71 |
|
| 72 |
/* |
| 73 |
KillList - deletes the complete list, l. |
| 74 |
*/ |
| 75 |
|
| 76 |
extern "C" void Lists_KillList (Lists_List *l); |
| 77 |
|
| 78 |
/* |
| 79 |
PutItemIntoList - places a WORD, c, into list, l. |
| 80 |
*/ |
| 81 |
|
| 82 |
extern "C" void Lists_PutItemIntoList (Lists_List l, unsigned int c); |
| 83 |
extern "C" unsigned int Lists_GetItemFromList (Lists_List l, unsigned int n); |
| 84 |
|
| 85 |
/* |
| 86 |
GetIndexOfList - returns the index for WORD, c, in list, l. |
| 87 |
If more than one WORD, c, exists the index |
| 88 |
for the first is returned. |
| 89 |
*/ |
| 90 |
|
| 91 |
extern "C" unsigned int Lists_GetIndexOfList (Lists_List l, unsigned int c); |
| 92 |
|
| 93 |
/* |
| 94 |
NoOfItemsInList - returns the number of items in list, l. |
| 95 |
(iterative algorithm of the above). |
| 96 |
*/ |
| 97 |
|
| 98 |
extern "C" unsigned int Lists_NoOfItemsInList (Lists_List l); |
| 99 |
|
| 100 |
/* |
| 101 |
IncludeItemIntoList - adds a WORD, c, into a list providing |
| 102 |
the value does not already exist. |
| 103 |
*/ |
| 104 |
|
| 105 |
extern "C" void Lists_IncludeItemIntoList (Lists_List l, unsigned int c); |
| 106 |
|
| 107 |
/* |
| 108 |
RemoveItemFromList - removes a WORD, c, from a list. |
| 109 |
It assumes that this value only appears once. |
| 110 |
*/ |
| 111 |
|
| 112 |
extern "C" void Lists_RemoveItemFromList (Lists_List l, unsigned int c); |
| 113 |
|
| 114 |
/* |
| 115 |
IsItemInList - returns true if a WORD, c, was found in list, l. |
| 116 |
*/ |
| 117 |
|
| 118 |
extern "C" bool Lists_IsItemInList (Lists_List l, unsigned int c); |
| 119 |
|
| 120 |
/* |
| 121 |
ForeachItemInListDo - calls procedure, P, foreach item in list, l. |
| 122 |
*/ |
| 123 |
|
| 124 |
extern "C" void Lists_ForeachItemInListDo (Lists_List l, SymbolKey_PerformOperation P); |
| 125 |
|
| 126 |
/* |
| 127 |
DuplicateList - returns a duplicate list derived from, l. |
| 128 |
*/ |
| 129 |
|
| 130 |
extern "C" Lists_List Lists_DuplicateList (Lists_List l); |
| 131 |
|
| 132 |
/* |
| 133 |
RemoveItem - remove an element at index, i, from the list data type. |
| 134 |
*/ |
| 135 |
|
| 136 |
static void RemoveItem (Lists_List__opaque p, Lists_List__opaque l, unsigned int i); |
| 137 |
|
| 138 |
|
| 139 |
/* |
| 140 |
RemoveItem - remove an element at index, i, from the list data type. |
| 141 |
*/ |
| 142 |
|
| 143 |
static void RemoveItem (Lists_List__opaque p, Lists_List__opaque l, unsigned int i) |
| 144 |
{ |
| 145 |
l->NoOfElements -= 1; |
| 146 |
while (i <= l->NoOfElements) |
| 147 |
{ |
| 148 |
l->Elements.array[i-1] = l->Elements.array[i+1-1]; |
| 149 |
i += 1; |
| 150 |
} |
| 151 |
if ((l->NoOfElements == 0) && (p != NULL)) |
| 152 |
{ |
| 153 |
p->Next = l->Next; |
| 154 |
Storage_DEALLOCATE ((void **) &l, sizeof (Lists_list)); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/* |
| 160 |
InitList - creates a new list, l. |
| 161 |
*/ |
| 162 |
|
| 163 |
extern "C" void Lists_InitList (Lists_List *l) |
| 164 |
{ |
| 165 |
Storage_ALLOCATE ((void **) &(*l), sizeof (Lists_list)); |
| 166 |
static_cast<Lists_List__opaque> ((*l))->NoOfElements = 0; |
| 167 |
static_cast<Lists_List__opaque> ((*l))->Next = static_cast<Lists_List__opaque> (NULL); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/* |
| 172 |
KillList - deletes the complete list, l. |
| 173 |
*/ |
| 174 |
|
| 175 |
extern "C" void Lists_KillList (Lists_List *l) |
| 176 |
{ |
| 177 |
if ((*l) != NULL) |
| 178 |
{ |
| 179 |
if (static_cast<Lists_List__opaque> ((*l))->Next != NULL) |
| 180 |
{ |
| 181 |
Lists_KillList (reinterpret_cast<Lists_List *> (&static_cast<Lists_List__opaque> ((*l))->Next)); |
| 182 |
} |
| 183 |
Storage_DEALLOCATE ((void **) &(*l), sizeof (Lists_list)); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/* |
| 189 |
PutItemIntoList - places a WORD, c, into list, l. |
| 190 |
*/ |
| 191 |
|
| 192 |
extern "C" void Lists_PutItemIntoList (Lists_List l, unsigned int c) |
| 193 |
{ |
| 194 |
if (static_cast<Lists_List__opaque> (l)->NoOfElements < MaxNoOfElements) |
| 195 |
{ |
| 196 |
static_cast<Lists_List__opaque> (l)->NoOfElements += 1; |
| 197 |
static_cast<Lists_List__opaque> (l)->Elements.array[static_cast<Lists_List__opaque> (l)->NoOfElements-1] = c; |
| 198 |
} |
| 199 |
else if (static_cast<Lists_List__opaque> (l)->Next != NULL) |
| 200 |
{ |
| 201 |
/* avoid dangling else. */ |
| 202 |
Lists_PutItemIntoList (static_cast<Lists_List> (static_cast<Lists_List__opaque> (l)->Next), c); |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
/* avoid dangling else. */ |
| 207 |
Lists_InitList (reinterpret_cast<Lists_List *> (&static_cast<Lists_List__opaque> (l)->Next)); |
| 208 |
Lists_PutItemIntoList (static_cast<Lists_List> (static_cast<Lists_List__opaque> (l)->Next), c); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
extern "C" unsigned int Lists_GetItemFromList (Lists_List l, unsigned int n) |
| 213 |
{ |
| 214 |
/* iterative solution */ |
| 215 |
while (l != NULL) |
| 216 |
{ |
| 217 |
if (n <= static_cast<Lists_List__opaque> (l)->NoOfElements) |
| 218 |
{ |
| 219 |
return static_cast<Lists_List__opaque> (l)->Elements.array[n-1]; |
| 220 |
} |
| 221 |
else |
| 222 |
{ |
| 223 |
n -= static_cast<Lists_List__opaque> (l)->NoOfElements; |
| 224 |
} |
| 225 |
l = static_cast<Lists_List> (static_cast<Lists_List__opaque> (l)->Next); |
| 226 |
} |
| 227 |
return static_cast<unsigned int> (0); |
| 228 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 229 |
__builtin_unreachable (); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
/* |
| 234 |
GetIndexOfList - returns the index for WORD, c, in list, l. |
| 235 |
If more than one WORD, c, exists the index |
| 236 |
for the first is returned. |
| 237 |
*/ |
| 238 |
|
| 239 |
extern "C" unsigned int Lists_GetIndexOfList (Lists_List l, unsigned int c) |
| 240 |
{ |
| 241 |
unsigned int i; |
| 242 |
|
| 243 |
if (l == NULL) |
| 244 |
{ |
| 245 |
return 0; |
| 246 |
} |
| 247 |
else |
| 248 |
{ |
| 249 |
i = 1; |
| 250 |
while (i <= static_cast<Lists_List__opaque> (l)->NoOfElements) |
| 251 |
{ |
| 252 |
if (static_cast<Lists_List__opaque> (l)->Elements.array[i-1] == c) |
| 253 |
{ |
| 254 |
return i; |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
i += 1; |
| 259 |
} |
| 260 |
} |
| 261 |
return static_cast<Lists_List__opaque> (l)->NoOfElements+(Lists_GetIndexOfList (static_cast<Lists_List> (static_cast<Lists_List__opaque> (l)->Next), c)); |
| 262 |
} |
| 263 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 264 |
__builtin_unreachable (); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/* |
| 269 |
NoOfItemsInList - returns the number of items in list, l. |
| 270 |
(iterative algorithm of the above). |
| 271 |
*/ |
| 272 |
|
| 273 |
extern "C" unsigned int Lists_NoOfItemsInList (Lists_List l) |
| 274 |
{ |
| 275 |
unsigned int t; |
| 276 |
|
| 277 |
if (l == NULL) |
| 278 |
{ |
| 279 |
return 0; |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
t = 0; |
| 284 |
do { |
| 285 |
t += static_cast<Lists_List__opaque> (l)->NoOfElements; |
| 286 |
l = static_cast<Lists_List> (static_cast<Lists_List__opaque> (l)->Next); |
| 287 |
} while (! (l == NULL)); |
| 288 |
return t; |
| 289 |
} |
| 290 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 291 |
__builtin_unreachable (); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
/* |
| 296 |
IncludeItemIntoList - adds a WORD, c, into a list providing |
| 297 |
the value does not already exist. |
| 298 |
*/ |
| 299 |
|
| 300 |
extern "C" void Lists_IncludeItemIntoList (Lists_List l, unsigned int c) |
| 301 |
{ |
| 302 |
if (! (Lists_IsItemInList (l, c))) |
| 303 |
{ |
| 304 |
Lists_PutItemIntoList (l, c); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
/* |
| 310 |
RemoveItemFromList - removes a WORD, c, from a list. |
| 311 |
It assumes that this value only appears once. |
| 312 |
*/ |
| 313 |
|
| 314 |
extern "C" void Lists_RemoveItemFromList (Lists_List l, unsigned int c) |
| 315 |
{ |
| 316 |
Lists_List__opaque p; |
| 317 |
unsigned int i; |
| 318 |
bool Found; |
| 319 |
|
| 320 |
if (l != NULL) |
| 321 |
{ |
| 322 |
Found = false; |
| 323 |
p = static_cast<Lists_List__opaque> (NULL); |
| 324 |
do { |
| 325 |
i = 1; |
| 326 |
while ((i <= static_cast<Lists_List__opaque> (l)->NoOfElements) && (static_cast<Lists_List__opaque> (l)->Elements.array[i-1] != c)) |
| 327 |
{ |
| 328 |
i += 1; |
| 329 |
} |
| 330 |
if ((i <= static_cast<Lists_List__opaque> (l)->NoOfElements) && (static_cast<Lists_List__opaque> (l)->Elements.array[i-1] == c)) |
| 331 |
{ |
| 332 |
Found = true; |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
p = static_cast<Lists_List__opaque> (l); |
| 337 |
l = static_cast<Lists_List> (static_cast<Lists_List__opaque> (l)->Next); |
| 338 |
} |
| 339 |
} while (! ((l == NULL) || Found)); |
| 340 |
if (Found) |
| 341 |
{ |
| 342 |
RemoveItem (p, static_cast<Lists_List__opaque> (l), i); |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
/* |
| 349 |
IsItemInList - returns true if a WORD, c, was found in list, l. |
| 350 |
*/ |
| 351 |
|
| 352 |
extern "C" bool Lists_IsItemInList (Lists_List l, unsigned int c) |
| 353 |
{ |
| 354 |
unsigned int i; |
| 355 |
|
| 356 |
do { |
| 357 |
i = 1; |
| 358 |
while (i <= static_cast<Lists_List__opaque> (l)->NoOfElements) |
| 359 |
{ |
| 360 |
if (static_cast<Lists_List__opaque> (l)->Elements.array[i-1] == c) |
| 361 |
{ |
| 362 |
return true; |
| 363 |
} |
| 364 |
else |
| 365 |
{ |
| 366 |
i += 1; |
| 367 |
} |
| 368 |
} |
| 369 |
l = static_cast<Lists_List> (static_cast<Lists_List__opaque> (l)->Next); |
| 370 |
} while (! (l == NULL)); |
| 371 |
return false; |
| 372 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 373 |
__builtin_unreachable (); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
/* |
| 378 |
ForeachItemInListDo - calls procedure, P, foreach item in list, l. |
| 379 |
*/ |
| 380 |
|
| 381 |
extern "C" void Lists_ForeachItemInListDo (Lists_List l, SymbolKey_PerformOperation P) |
| 382 |
{ |
| 383 |
unsigned int i; |
| 384 |
unsigned int n; |
| 385 |
|
| 386 |
n = Lists_NoOfItemsInList (l); |
| 387 |
i = 1; |
| 388 |
while (i <= n) |
| 389 |
{ |
| 390 |
(*P.proc) (Lists_GetItemFromList (l, i)); |
| 391 |
i += 1; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
/* |
| 397 |
DuplicateList - returns a duplicate list derived from, l. |
| 398 |
*/ |
| 399 |
|
| 400 |
extern "C" Lists_List Lists_DuplicateList (Lists_List l) |
| 401 |
{ |
| 402 |
Lists_List__opaque m; |
| 403 |
unsigned int n; |
| 404 |
unsigned int i; |
| 405 |
|
| 406 |
Lists_InitList (reinterpret_cast<Lists_List *> (&m)); |
| 407 |
n = Lists_NoOfItemsInList (l); |
| 408 |
i = 1; |
| 409 |
while (i <= n) |
| 410 |
{ |
| 411 |
Lists_PutItemIntoList (static_cast<Lists_List> (m), Lists_GetItemFromList (l, i)); |
| 412 |
i += 1; |
| 413 |
} |
| 414 |
return static_cast<Lists_List> (m); |
| 415 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 416 |
__builtin_unreachable (); |
| 417 |
} |
| 418 |
|
| 419 |
extern "C" void _M2_Lists_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) |
| 420 |
{ |
| 421 |
} |
| 422 |
|
| 423 |
extern "C" void _M2_Lists_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) |
| 424 |
{ |
| 425 |
} |