| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2024, 2025 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 3 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "extensions/set_extensions.h" | ||
| 19 | |||
| 20 | #include "extensions.h" | ||
| 21 | #include "fundamental.h" | ||
| 22 | |||
| 23 | #ifdef LBM_OPT_SET_EXTENSIONS_SIZE | ||
| 24 | #pragma GCC optimize ("-Os") | ||
| 25 | #endif | ||
| 26 | #ifdef LBM_OPT_SET_EXTENSIONS_SIZE_AGGRESSIVE | ||
| 27 | #pragma GCC optimize ("-Oz") | ||
| 28 | #endif | ||
| 29 | |||
| 30 | |||
| 31 | #define ABORT_ON_MERROR(X) if ((X) == ENC_SYM_MERROR) return ENC_SYM_MERROR; | ||
| 32 | |||
| 33 | static lbm_value ext_set_insert(lbm_value *args, lbm_uint argn); | ||
| 34 | static lbm_value ext_set_union(lbm_value *args, lbm_uint argn); | ||
| 35 | |||
| 36 | 66906 | void lbm_set_extensions_init(void) { | |
| 37 | 66906 | lbm_add_extension("set-insert", ext_set_insert); | |
| 38 | 66906 | lbm_add_extension("set-union", ext_set_union); | |
| 39 | 66906 | } | |
| 40 | |||
| 41 | 272469 | static lbm_value set_insert(lbm_value set, lbm_value val) { | |
| 42 | |||
| 43 | 272469 | lbm_value end = ENC_SYM_NIL; | |
| 44 | 272469 | lbm_value start = ENC_SYM_NIL; | |
| 45 | |||
| 46 | 272469 | lbm_value curr = set; | |
| 47 |
2/2✓ Branch 0 taken 447072 times.
✓ Branch 1 taken 271959 times.
|
719031 | while (lbm_is_cons(curr)) { |
| 48 | 447072 | lbm_value h = lbm_car(curr); | |
| 49 |
2/2✓ Branch 0 taken 261 times.
✓ Branch 1 taken 446811 times.
|
447072 | if (struct_eq(lbm_car(curr), val)) { |
| 50 | 261 | return set; | |
| 51 | } | ||
| 52 | 446811 | lbm_value cell = lbm_cons(h, ENC_SYM_NIL); | |
| 53 |
2/2✓ Branch 0 taken 249 times.
✓ Branch 1 taken 446562 times.
|
446811 | ABORT_ON_MERROR(cell); |
| 54 |
2/2✓ Branch 0 taken 31836 times.
✓ Branch 1 taken 414726 times.
|
446562 | if (end == ENC_SYM_NIL) { |
| 55 | 31836 | end = cell; | |
| 56 | 31836 | start = cell; | |
| 57 | } else { | ||
| 58 | 414726 | lbm_set_cdr(end, cell); | |
| 59 | 414726 | end = cell; | |
| 60 | } | ||
| 61 | 446562 | curr = lbm_cdr(curr); | |
| 62 | } | ||
| 63 | 271959 | lbm_value v = lbm_cons(val, ENC_SYM_NIL); | |
| 64 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 271869 times.
|
271959 | ABORT_ON_MERROR(v); |
| 65 |
2/2✓ Branch 0 taken 240285 times.
✓ Branch 1 taken 31584 times.
|
271869 | if (end == ENC_SYM_NIL) { |
| 66 | 240285 | start = v; | |
| 67 | } else { | ||
| 68 | 31584 | lbm_set_cdr(end, v); | |
| 69 | } | ||
| 70 | 271869 | return start; | |
| 71 | } | ||
| 72 | |||
| 73 | /* extends a copy of the input set with the new element. */ | ||
| 74 | 241149 | static lbm_value ext_set_insert(lbm_value *args, lbm_uint argn) { | |
| 75 | 241149 | lbm_value res = ENC_SYM_TERROR; | |
| 76 |
4/4✓ Branch 0 taken 240801 times.
✓ Branch 1 taken 348 times.
✓ Branch 2 taken 240786 times.
✓ Branch 3 taken 15 times.
|
241149 | if (argn == 2 && lbm_is_list(args[0])) { |
| 77 | 240786 | res = set_insert(args[0], args[1]); | |
| 78 | } | ||
| 79 | 241149 | return res; | |
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | 3993 | static lbm_value ext_set_union(lbm_value *args, lbm_uint argn) { | |
| 84 | 3993 | lbm_value res = ENC_SYM_TERROR; | |
| 85 |
6/6✓ Branch 0 taken 3645 times.
✓ Branch 1 taken 348 times.
✓ Branch 2 taken 3621 times.
✓ Branch 3 taken 24 times.
✓ Branch 4 taken 3522 times.
✓ Branch 5 taken 99 times.
|
3993 | if (argn == 2 && lbm_is_list(args[0]) && lbm_is_list(args[1])) { |
| 86 | 3522 | lbm_value curr = args[0]; | |
| 87 | 3522 | lbm_value set = args[1]; | |
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 31683 times.
✓ Branch 1 taken 3273 times.
|
34956 | while (lbm_is_cons(curr)) { |
| 90 | 31683 | set = set_insert(set, lbm_car(curr)); | |
| 91 |
2/2✓ Branch 0 taken 249 times.
✓ Branch 1 taken 31434 times.
|
31683 | ABORT_ON_MERROR(set); |
| 92 | 31434 | curr = lbm_cdr(curr); | |
| 93 | } | ||
| 94 | 3273 | return set; | |
| 95 | } | ||
| 96 | 471 | return res; | |
| 97 | } | ||
| 98 |