1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/// @file
#include "jets/k.h"
#include "jets/q.h"
#include "jets/w.h"
#include "noun.h"
typedef struct _u3_loss { // loss problem
u3_noun hel; // a as a list
c3_w lel_w; // length of a
c3_w lev_w; // length of b
u3_noun* hev; // b as an array
u3_noun sev; // b as a set of lists
c3_w kct_w; // candidate count
u3_noun* kad; // candidate array
} u3_loss;
// free loss object
//
static void
_flem(u3_loss* loc_u)
{
u3z(loc_u->sev);
{
c3_w i_w;
for ( i_w = 0; i_w < loc_u->kct_w; i_w++ ) {
u3z(loc_u->kad[i_w]);
}
}
u3a_free(loc_u->hev);
u3a_free(loc_u->kad);
}
// extract lcs - XX don't use the stack like this
//
static u3_noun
_lext(u3_loss* loc_u,
u3_noun kad)
{
if ( u3_nul == kad ) {
return u3_nul;
} else {
return u3nc(u3k(loc_u->hev[u3r_word(0, u3h(kad))]),
_lext(loc_u, u3t(kad)));
}
}
// extract lcs
//
static u3_noun
_lexs(u3_loss* loc_u)
{
if ( 0 == loc_u->kct_w ) {
return u3_nul;
} else return u3kb_flop(_lext(loc_u, loc_u->kad[loc_u->kct_w - 1]));
}
// initialize loss object
//
static void
_lemp(u3_loss* loc_u,
u3_noun hel,
u3_noun hev)
{
loc_u->hel = hel;
loc_u->lel_w = u3kb_lent(u3k(hel));
// Read hev into array.
{
c3_w i_w;
loc_u->hev = u3a_malloc(u3kb_lent(u3k(hev)) * sizeof(u3_noun));
for ( i_w = 0; u3_nul != hev; i_w++ ) {
loc_u->hev[i_w] = u3h(hev);
hev = u3t(hev);
}
loc_u->lev_w = i_w;
}
loc_u->kct_w = 0;
loc_u->kad = u3a_malloc((1 + c3_min(loc_u->lev_w, loc_u->lel_w)) *
sizeof(u3_noun));
// Compute equivalence classes.
//
loc_u->sev = u3_nul;
{
c3_w i_w;
for ( i_w = 0; i_w < loc_u->lev_w; i_w++ ) {
u3_noun how = loc_u->hev[i_w];
u3_noun hav;
u3_noun teg;
hav = u3kdb_get(u3k(loc_u->sev), u3k(how));
teg = u3nc(u3i_words(1, &i_w),
(hav == u3_none) ? u3_nul : hav);
loc_u->sev = u3kdb_put(loc_u->sev, u3k(how), teg);
}
}
}
// apply
//
static void
_lune(u3_loss* loc_u,
c3_w inx_w,
c3_w goy_w)
{
u3_noun kad;
kad = u3nc(u3i_words(1, &goy_w),
(inx_w == 0) ? u3_nul
: u3k(loc_u->kad[inx_w - 1]));
if ( loc_u->kct_w == inx_w ) {
u3_assert(loc_u->kct_w < (1 << 31));
loc_u->kct_w++;
} else {
u3z(loc_u->kad[inx_w]);
}
loc_u->kad[inx_w] = kad;
}
// extend fits top
//
static u3_noun
_hink(u3_loss* loc_u,
c3_w inx_w,
c3_w goy_w)
{
return __
( (loc_u->kct_w == inx_w) ||
(u3r_word(0, u3h(loc_u->kad[inx_w])) > goy_w) );
}
// extend fits bottom
//
static u3_noun
_lonk(u3_loss* loc_u,
c3_w inx_w,
c3_w goy_w)
{
return __
( (0 == inx_w) ||
(u3r_word(0, u3h(loc_u->kad[inx_w - 1])) < goy_w) );
}
#if 0
// search for first index >= inx_w and <= max_w that fits
// the hink and lonk criteria.
//
static u3_noun
_binka(u3_loss* loc_u,
c3_w* inx_w,
c3_w max_w,
c3_w goy_w)
{
while ( *inx_w <= max_w ) {
if ( c3n == _lonk(loc_u, *inx_w, goy_w) ) {
return c3n;
}
if ( c3y == _hink(loc_u, *inx_w, goy_w) ) {
return c3y;
}
else ++*inx_w;
}
return c3n;
}
#endif
// search for lowest index >= inx_w and <= max_w for which
// both hink(inx_w) and lonk(inx_w) are true. lonk is false
// if inx_w is too high, hink is false if it is too low.
//
static u3_noun
_bink(u3_loss* loc_u,
c3_w* inx_w,
c3_w max_w,
c3_w goy_w)
{
u3_assert(max_w >= *inx_w);
if ( max_w == *inx_w ) {
if ( c3n == _lonk(loc_u, *inx_w, goy_w) ) {
return c3n;
}
if ( c3y == _hink(loc_u, *inx_w, goy_w) ) {
return c3y;
}
else {
++*inx_w;
return c3n;
}
}
else {
c3_w mid_w = *inx_w + ((max_w - *inx_w) / 2);
if ( (c3n == _lonk(loc_u, mid_w, goy_w)) ||
(c3y == _hink(loc_u, mid_w, goy_w)) )
{
return _bink(loc_u, inx_w, mid_w, goy_w);
} else {
*inx_w = mid_w + 1;
return _bink(loc_u, inx_w, max_w, goy_w);
}
}
}
static void
_merg(u3_loss* loc_u,
c3_w inx_w,
u3_noun gay)
{
if ( (u3_nul == gay) || (inx_w > loc_u->kct_w) ) {
return;
}
else {
u3_noun i_gay = u3h(gay);
c3_w goy_w = u3r_word(0, i_gay);
u3_noun bik;
bik = _bink(loc_u, &inx_w, loc_u->kct_w, goy_w);
if ( c3y == bik ) {
_merg(loc_u, inx_w + 1, u3t(gay));
_lune(loc_u, inx_w, goy_w);
}
else {
_merg(loc_u, inx_w, u3t(gay));
}
}
}
// compute lcs
//
static void
_loss(u3_loss* loc_u)
{
while ( u3_nul != loc_u->hel ) {
u3_noun i_hel = u3h(loc_u->hel);
u3_noun guy = u3kdb_get(u3k(loc_u->sev), u3k(i_hel));
if ( u3_none != guy ) {
u3_noun gay = u3kb_flop(guy);
_merg(loc_u, 0, gay);
u3z(gay);
}
loc_u->hel = u3t(loc_u->hel);
}
}
u3_noun
u3qe_loss(u3_noun hel,
u3_noun hev)
{
u3_loss loc_u;
u3_noun lcs;
_lemp(&loc_u, hel, hev);
_loss(&loc_u);
lcs = _lexs(&loc_u);
_flem(&loc_u);
return lcs;
}
static u3_noun
_listp(u3_noun lix)
{
while ( 1 ) {
if ( u3_nul == lix ) return c3y;
if ( c3n == u3du(lix) ) return c3n;
lix = u3t(lix);
}
}
u3_noun
u3we_loss(u3_noun cor)
{
u3_noun hel, hev;
if ( (u3_none == (hel = u3r_at(u3x_sam_2, cor))) ||
(u3_none == (hev = u3r_at(u3x_sam_3, cor))) ||
(c3n == _listp(hel)) ||
(c3n == _listp(hev)) )
{
return u3m_bail(c3__fail);
} else {
return u3qe_loss(hel, hev);
}
}
|