| 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 | 66394 | void lbm_set_extensions_init(void) { | |
| 37 | 66394 | lbm_add_extension("set-insert", ext_set_insert); | |
| 38 | 66394 | lbm_add_extension("set-union", ext_set_union); | |
| 39 | 66394 | } | |
| 40 | |||
| 41 | 91663 | static lbm_value set_insert(lbm_value set, lbm_value val) { | |
| 42 | |||
| 43 | 91663 | lbm_value end = ENC_SYM_NIL; | |
| 44 | 91663 | lbm_value start = ENC_SYM_NIL; | |
| 45 | |||
| 46 | 91663 | lbm_value curr = set; | |
| 47 |
2/2✓ Branch 0 taken 150424 times.
✓ Branch 1 taken 91325 times.
|
241749 | while (lbm_is_cons(curr)) { |
| 48 | 150424 | lbm_value h = lbm_car(curr); | |
| 49 |
2/2✓ Branch 0 taken 255 times.
✓ Branch 1 taken 150169 times.
|
150424 | if (struct_eq(lbm_car(curr), val)) { |
| 50 | 255 | return set; | |
| 51 | } | ||
| 52 | 150169 | lbm_value cell = lbm_cons(h, ENC_SYM_NIL); | |
| 53 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 150086 times.
|
150169 | ABORT_ON_MERROR(cell); |
| 54 |
2/2✓ Branch 0 taken 11116 times.
✓ Branch 1 taken 138970 times.
|
150086 | if (end == ENC_SYM_NIL) { |
| 55 | 11116 | end = cell; | |
| 56 | 11116 | start = cell; | |
| 57 | } else { | ||
| 58 | 138970 | lbm_set_cdr(end, cell); | |
| 59 | 138970 | end = cell; | |
| 60 | } | ||
| 61 | 150086 | curr = lbm_cdr(curr); | |
| 62 | } | ||
| 63 | 91325 | lbm_value v = lbm_cons(val, ENC_SYM_NIL); | |
| 64 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 91295 times.
|
91325 | ABORT_ON_MERROR(v); |
| 65 |
2/2✓ Branch 0 taken 80263 times.
✓ Branch 1 taken 11032 times.
|
91295 | if (end == ENC_SYM_NIL) { |
| 66 | 80263 | start = v; | |
| 67 | } else { | ||
| 68 | 11032 | lbm_set_cdr(end, v); | |
| 69 | } | ||
| 70 | 91295 | return start; | |
| 71 | } | ||
| 72 | |||
| 73 | /* extends a copy of the input set with the new element. */ | ||
| 74 | 80999 | static lbm_value ext_set_insert(lbm_value *args, lbm_uint argn) { | |
| 75 | 80999 | lbm_value res = ENC_SYM_TERROR; | |
| 76 |
4/4✓ Branch 0 taken 80659 times.
✓ Branch 1 taken 340 times.
✓ Branch 2 taken 80654 times.
✓ Branch 3 taken 5 times.
|
80999 | if (argn == 2 && lbm_is_list(args[0])) { |
| 77 | 80654 | res = set_insert(args[0], args[1]); | |
| 78 | } | ||
| 79 | 80999 | return res; | |
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | 1779 | static lbm_value ext_set_union(lbm_value *args, lbm_uint argn) { | |
| 84 | 1779 | lbm_value res = ENC_SYM_TERROR; | |
| 85 |
6/6✓ Branch 0 taken 1439 times.
✓ Branch 1 taken 340 times.
✓ Branch 2 taken 1431 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 1342 times.
✓ Branch 5 taken 89 times.
|
1779 | if (argn == 2 && lbm_is_list(args[0]) && lbm_is_list(args[1])) { |
| 86 | 1342 | lbm_value curr = args[0]; | |
| 87 | 1342 | lbm_value set = args[1]; | |
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 11009 times.
✓ Branch 1 taken 1259 times.
|
12268 | while (lbm_is_cons(curr)) { |
| 90 | 11009 | set = set_insert(set, lbm_car(curr)); | |
| 91 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 10926 times.
|
11009 | ABORT_ON_MERROR(set); |
| 92 | 10926 | curr = lbm_cdr(curr); | |
| 93 | } | ||
| 94 | 1259 | return set; | |
| 95 | } | ||
| 96 | 437 | return res; | |
| 97 | } | ||
| 98 |