| 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 | 22317 | void lbm_set_extensions_init(void) { | |
| 37 | 22317 | lbm_add_extension("set-insert", ext_set_insert); | |
| 38 | 22317 | lbm_add_extension("set-union", ext_set_union); | |
| 39 | 22317 | } | |
| 40 | |||
| 41 | 90823 | static lbm_value set_insert(lbm_value set, lbm_value val) { | |
| 42 | |||
| 43 | 90823 | lbm_value end = ENC_SYM_NIL; | |
| 44 | 90823 | lbm_value start = ENC_SYM_NIL; | |
| 45 | |||
| 46 | 90823 | lbm_value curr = set; | |
| 47 |
2/2✓ Branch 0 taken 149024 times.
✓ Branch 1 taken 90653 times.
|
239677 | while (lbm_is_cons(curr)) { |
| 48 | 149024 | lbm_value h = lbm_car(curr); | |
| 49 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 148937 times.
|
149024 | if (struct_eq(lbm_car(curr), val)) { |
| 50 | 87 | return set; | |
| 51 | } | ||
| 52 | 148937 | lbm_value cell = lbm_cons(h, ENC_SYM_NIL); | |
| 53 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 148854 times.
|
148937 | ABORT_ON_MERROR(cell); |
| 54 |
2/2✓ Branch 0 taken 10612 times.
✓ Branch 1 taken 138242 times.
|
148854 | if (end == ENC_SYM_NIL) { |
| 55 | 10612 | end = cell; | |
| 56 | 10612 | start = cell; | |
| 57 | } else { | ||
| 58 | 138242 | lbm_set_cdr(end, cell); | |
| 59 | 138242 | end = cell; | |
| 60 | } | ||
| 61 | 148854 | curr = lbm_cdr(curr); | |
| 62 | } | ||
| 63 | 90653 | lbm_value v = lbm_cons(val, ENC_SYM_NIL); | |
| 64 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 90623 times.
|
90653 | ABORT_ON_MERROR(v); |
| 65 |
2/2✓ Branch 0 taken 80095 times.
✓ Branch 1 taken 10528 times.
|
90623 | if (end == ENC_SYM_NIL) { |
| 66 | 80095 | start = v; | |
| 67 | } else { | ||
| 68 | 10528 | lbm_set_cdr(end, v); | |
| 69 | } | ||
| 70 | 90623 | return start; | |
| 71 | } | ||
| 72 | |||
| 73 | /* extends a copy of the input set with the new element. */ | ||
| 74 | 80383 | static lbm_value ext_set_insert(lbm_value *args, lbm_uint argn) { | |
| 75 | 80383 | lbm_value res = ENC_SYM_TERROR; | |
| 76 |
4/4✓ Branch 0 taken 80267 times.
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 80262 times.
✓ Branch 3 taken 5 times.
|
80383 | if (argn == 2 && lbm_is_list(args[0])) { |
| 77 | 80262 | res = set_insert(args[0], args[1]); | |
| 78 | } | ||
| 79 | 80383 | return res; | |
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | 1331 | static lbm_value ext_set_union(lbm_value *args, lbm_uint argn) { | |
| 84 | 1331 | lbm_value res = ENC_SYM_TERROR; | |
| 85 |
6/6✓ Branch 0 taken 1215 times.
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 1207 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 1174 times.
✓ Branch 5 taken 33 times.
|
1331 | if (argn == 2 && lbm_is_list(args[0]) && lbm_is_list(args[1])) { |
| 86 | 1174 | lbm_value curr = args[0]; | |
| 87 | 1174 | lbm_value set = args[1]; | |
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 10561 times.
✓ Branch 1 taken 1091 times.
|
11652 | while (lbm_is_cons(curr)) { |
| 90 | 10561 | set = set_insert(set, lbm_car(curr)); | |
| 91 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 10478 times.
|
10561 | ABORT_ON_MERROR(set); |
| 92 | 10478 | curr = lbm_cdr(curr); | |
| 93 | } | ||
| 94 | 1091 | return set; | |
| 95 | } | ||
| 96 | 157 | return res; | |
| 97 | } | ||
| 98 |