| 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 | 29167 | void lbm_set_extensions_init(void) { | |
| 37 | 29167 | lbm_add_extension("set-insert", ext_set_insert); | |
| 38 | 29167 | lbm_add_extension("set-union", ext_set_union); | |
| 39 | 29167 | } | |
| 40 | |||
| 41 | 271749 | static lbm_value set_insert(lbm_value set, lbm_value val) { | |
| 42 | |||
| 43 | 271749 | lbm_value end = ENC_SYM_NIL; | |
| 44 | 271749 | lbm_value start = ENC_SYM_NIL; | |
| 45 | |||
| 46 | 271749 | lbm_value curr = set; | |
| 47 |
2/2✓ Branch 1 taken 445872 times.
✓ Branch 2 taken 271383 times.
|
717255 | while (lbm_is_cons(curr)) { |
| 48 | 445872 | lbm_value h = lbm_car(curr); | |
| 49 |
2/2✓ Branch 2 taken 117 times.
✓ Branch 3 taken 445755 times.
|
445872 | if (struct_eq(lbm_car(curr), val, 0)) { |
| 50 | 117 | return set; | |
| 51 | } | ||
| 52 | 445755 | lbm_value cell = lbm_cons(h, ENC_SYM_NIL); | |
| 53 |
2/2✓ Branch 0 taken 249 times.
✓ Branch 1 taken 445506 times.
|
445755 | ABORT_ON_MERROR(cell); |
| 54 |
2/2✓ Branch 0 taken 31404 times.
✓ Branch 1 taken 414102 times.
|
445506 | if (end == ENC_SYM_NIL) { |
| 55 | 31404 | end = cell; | |
| 56 | 31404 | start = cell; | |
| 57 | } else { | ||
| 58 | 414102 | lbm_set_cdr(end, cell); | |
| 59 | 414102 | end = cell; | |
| 60 | } | ||
| 61 | 445506 | curr = lbm_cdr(curr); | |
| 62 | } | ||
| 63 | 271383 | lbm_value v = lbm_cons(val, ENC_SYM_NIL); | |
| 64 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 271293 times.
|
271383 | ABORT_ON_MERROR(v); |
| 65 |
2/2✓ Branch 0 taken 240141 times.
✓ Branch 1 taken 31152 times.
|
271293 | if (end == ENC_SYM_NIL) { |
| 66 | 240141 | start = v; | |
| 67 | } else { | ||
| 68 | 31152 | lbm_set_cdr(end, v); | |
| 69 | } | ||
| 70 | 271293 | return start; | |
| 71 | } | ||
| 72 | |||
| 73 | /* extends a copy of the input set with the new element. */ | ||
| 74 | 240621 | static lbm_value ext_set_insert(lbm_value *args, lbm_uint argn) { | |
| 75 | 240621 | lbm_value res = ENC_SYM_TERROR; | |
| 76 |
4/4✓ Branch 0 taken 240465 times.
✓ Branch 1 taken 156 times.
✓ Branch 3 taken 240450 times.
✓ Branch 4 taken 15 times.
|
240621 | if (argn == 2 && lbm_is_list(args[0])) { |
| 77 | 240450 | res = set_insert(args[0], args[1]); | |
| 78 | } | ||
| 79 | 240621 | return res; | |
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | 3609 | static lbm_value ext_set_union(lbm_value *args, lbm_uint argn) { | |
| 84 | 3609 | lbm_value res = ENC_SYM_TERROR; | |
| 85 |
6/6✓ Branch 0 taken 3453 times.
✓ Branch 1 taken 156 times.
✓ Branch 3 taken 3429 times.
✓ Branch 4 taken 24 times.
✓ Branch 6 taken 3378 times.
✓ Branch 7 taken 51 times.
|
3609 | if (argn == 2 && lbm_is_list(args[0]) && lbm_is_list(args[1])) { |
| 86 | 3378 | lbm_value curr = args[0]; | |
| 87 | 3378 | lbm_value set = args[1]; | |
| 88 | |||
| 89 |
2/2✓ Branch 1 taken 31299 times.
✓ Branch 2 taken 3129 times.
|
34428 | while (lbm_is_cons(curr)) { |
| 90 | 31299 | set = set_insert(set, lbm_car(curr)); | |
| 91 |
2/2✓ Branch 0 taken 249 times.
✓ Branch 1 taken 31050 times.
|
31299 | ABORT_ON_MERROR(set); |
| 92 | 31050 | curr = lbm_cdr(curr); | |
| 93 | } | ||
| 94 | 3129 | return set; | |
| 95 | } | ||
| 96 | 231 | return res; | |
| 97 | } | ||
| 98 |