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 | 44260 | void lbm_set_extensions_init(void) { | |
37 | 44260 | lbm_add_extension("set-insert", ext_set_insert); | |
38 | 44260 | lbm_add_extension("set-union", ext_set_union); | |
39 | 44260 | } | |
40 | |||
41 | 91243 | static lbm_value set_insert(lbm_value set, lbm_value val) { | |
42 | |||
43 | 91243 | lbm_value end = ENC_SYM_NIL; | |
44 | 91243 | lbm_value start = ENC_SYM_NIL; | |
45 | |||
46 | 91243 | lbm_value curr = set; | |
47 |
2/2✓ Branch 0 taken 149724 times.
✓ Branch 1 taken 90989 times.
|
240713 | while (lbm_is_cons(curr)) { |
48 | 149724 | lbm_value h = lbm_car(curr); | |
49 |
2/2✓ Branch 0 taken 171 times.
✓ Branch 1 taken 149553 times.
|
149724 | if (struct_eq(lbm_car(curr), val)) { |
50 | 171 | return set; | |
51 | } | ||
52 | 149553 | lbm_value cell = lbm_cons(h, ENC_SYM_NIL); | |
53 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 149470 times.
|
149553 | ABORT_ON_MERROR(cell); |
54 |
2/2✓ Branch 0 taken 10864 times.
✓ Branch 1 taken 138606 times.
|
149470 | if (end == ENC_SYM_NIL) { |
55 | 10864 | end = cell; | |
56 | 10864 | start = cell; | |
57 | } else { | ||
58 | 138606 | lbm_set_cdr(end, cell); | |
59 | 138606 | end = cell; | |
60 | } | ||
61 | 149470 | curr = lbm_cdr(curr); | |
62 | } | ||
63 | 90989 | lbm_value v = lbm_cons(val, ENC_SYM_NIL); | |
64 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 90959 times.
|
90989 | ABORT_ON_MERROR(v); |
65 |
2/2✓ Branch 0 taken 80179 times.
✓ Branch 1 taken 10780 times.
|
90959 | if (end == ENC_SYM_NIL) { |
66 | 80179 | start = v; | |
67 | } else { | ||
68 | 10780 | lbm_set_cdr(end, v); | |
69 | } | ||
70 | 90959 | return start; | |
71 | } | ||
72 | |||
73 | /* extends a copy of the input set with the new element. */ | ||
74 | 80691 | static lbm_value ext_set_insert(lbm_value *args, lbm_uint argn) { | |
75 | 80691 | lbm_value res = ENC_SYM_TERROR; | |
76 |
4/4✓ Branch 0 taken 80463 times.
✓ Branch 1 taken 228 times.
✓ Branch 2 taken 80458 times.
✓ Branch 3 taken 5 times.
|
80691 | if (argn == 2 && lbm_is_list(args[0])) { |
77 | 80458 | res = set_insert(args[0], args[1]); | |
78 | } | ||
79 | 80691 | return res; | |
80 | } | ||
81 | |||
82 | |||
83 | 1555 | static lbm_value ext_set_union(lbm_value *args, lbm_uint argn) { | |
84 | 1555 | lbm_value res = ENC_SYM_TERROR; | |
85 |
6/6✓ Branch 0 taken 1327 times.
✓ Branch 1 taken 228 times.
✓ Branch 2 taken 1319 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 1258 times.
✓ Branch 5 taken 61 times.
|
1555 | if (argn == 2 && lbm_is_list(args[0]) && lbm_is_list(args[1])) { |
86 | 1258 | lbm_value curr = args[0]; | |
87 | 1258 | lbm_value set = args[1]; | |
88 | |||
89 |
2/2✓ Branch 0 taken 10785 times.
✓ Branch 1 taken 1175 times.
|
11960 | while (lbm_is_cons(curr)) { |
90 | 10785 | set = set_insert(set, lbm_car(curr)); | |
91 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 10702 times.
|
10785 | ABORT_ON_MERROR(set); |
92 | 10702 | curr = lbm_cdr(curr); | |
93 | } | ||
94 | 1175 | return set; | |
95 | } | ||
96 | 297 | return res; | |
97 | } | ||
98 |