2013-09-29 00:21:18 +04:00
|
|
|
/* j/5/loss.c
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
#include "all.h"
|
2014-09-04 07:10:43 +04:00
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
/* functions
|
|
|
|
*/
|
2014-09-06 00:13:24 +04:00
|
|
|
typedef struct _u3_loss { // loss problem
|
|
|
|
u3_noun hel; // a as a list
|
2014-04-22 05:22:11 +04:00
|
|
|
c3_w lel_w; // length of a
|
2013-09-29 00:21:18 +04:00
|
|
|
c3_w lev_w; // length of b
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun* hev; // b as an array
|
|
|
|
u3_noun sev; // b as a set of lists
|
2013-09-29 00:21:18 +04:00
|
|
|
c3_w kct_w; // candidate count
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun* kad; // candidate array
|
|
|
|
} u3_loss;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
// free loss object
|
|
|
|
//
|
2014-04-22 05:22:11 +04:00
|
|
|
static void
|
2014-09-06 00:13:24 +04:00
|
|
|
_flem(u3_loss* loc_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3z(loc_u->sev);
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
c3_w i_w;
|
|
|
|
|
|
|
|
for ( i_w = 0; i_w < loc_u->kct_w; i_w++ ) {
|
2014-09-06 00:13:24 +04:00
|
|
|
u3z(loc_u->kad[i_w]);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(loc_u->hev);
|
|
|
|
free(loc_u->kad);
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract lcs - XX don't use the stack like this
|
|
|
|
//
|
2014-09-06 00:13:24 +04:00
|
|
|
static u3_noun
|
|
|
|
_lext(u3_loss* loc_u, u3_noun kad)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
if ( u3_nul == kad ) {
|
|
|
|
return u3_nul;
|
2013-09-29 00:21:18 +04:00
|
|
|
} else {
|
2014-11-06 03:20:01 +03:00
|
|
|
return u3nc(u3k(loc_u->hev[u3r_word(0, u3h(kad))]),
|
2014-09-06 00:13:24 +04:00
|
|
|
_lext(loc_u, u3t(kad)));
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 05:22:11 +04:00
|
|
|
// extract lcs
|
2013-09-29 00:21:18 +04:00
|
|
|
//
|
2014-09-06 00:13:24 +04:00
|
|
|
static u3_noun
|
|
|
|
_lexs(u3_loss* loc_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
if ( 0 == loc_u->kct_w ) {
|
2014-09-06 00:13:24 +04:00
|
|
|
return u3_nul;
|
2014-11-06 22:13:57 +03:00
|
|
|
} else return u3kb_flop(_lext(loc_u, loc_u->kad[loc_u->kct_w - 1]));
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// initialize loss object
|
|
|
|
//
|
|
|
|
static void
|
2014-09-06 00:13:24 +04:00
|
|
|
_lemp(u3_loss* loc_u,
|
|
|
|
u3_noun hel,
|
|
|
|
u3_noun hev)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
loc_u->hel = hel;
|
2014-11-06 22:13:57 +03:00
|
|
|
loc_u->lel_w = u3kb_lent(u3k(hel));
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
// Read hev into array.
|
|
|
|
{
|
|
|
|
c3_w i_w;
|
|
|
|
|
2014-11-06 22:13:57 +03:00
|
|
|
loc_u->hev = c3_malloc(u3kb_lent(u3k(hev)) * sizeof(u3_noun));
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
for ( i_w = 0; u3_nul != hev; i_w++ ) {
|
|
|
|
loc_u->hev[i_w] = u3h(hev);
|
|
|
|
hev = u3t(hev);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
loc_u->lev_w = i_w;
|
|
|
|
}
|
|
|
|
loc_u->kct_w = 0;
|
2014-04-02 04:47:01 +04:00
|
|
|
loc_u->kad = c3_malloc(
|
2013-09-29 00:21:18 +04:00
|
|
|
(1 + c3_min(loc_u->lev_w, loc_u->lel_w)) *
|
2014-09-06 00:13:24 +04:00
|
|
|
sizeof(u3_noun));
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
// Compute equivalence classes.
|
|
|
|
//
|
2014-09-06 00:13:24 +04:00
|
|
|
loc_u->sev = u3_nul;
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
c3_w i_w;
|
|
|
|
|
|
|
|
for ( i_w = 0; i_w < loc_u->lev_w; i_w++ ) {
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun how = loc_u->hev[i_w];
|
|
|
|
u3_noun hav;
|
|
|
|
u3_noun teg;
|
|
|
|
|
2014-11-06 22:13:57 +03:00
|
|
|
hav = u3kdb_get(u3k(loc_u->sev), u3k(how));
|
2014-11-06 03:20:01 +03:00
|
|
|
teg = u3nc(u3i_words(1, &i_w),
|
2014-11-06 02:36:30 +03:00
|
|
|
(hav == u3_none) ? u3_nul : hav);
|
2014-11-06 22:13:57 +03:00
|
|
|
loc_u->sev = u3kdb_put(loc_u->sev, u3k(how), teg);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply
|
|
|
|
//
|
|
|
|
static void
|
2014-09-06 00:13:24 +04:00
|
|
|
_lune(u3_loss* loc_u,
|
2013-09-29 00:21:18 +04:00
|
|
|
c3_w inx_w,
|
|
|
|
c3_w goy_w)
|
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun kad;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-06 03:20:01 +03:00
|
|
|
kad = u3nc(u3i_words(1, &goy_w),
|
2014-09-06 00:13:24 +04:00
|
|
|
(inx_w == 0) ? u3_nul
|
|
|
|
: u3k(loc_u->kad[inx_w - 1]));
|
2013-09-29 00:21:18 +04:00
|
|
|
if ( loc_u->kct_w == inx_w ) {
|
|
|
|
c3_assert(loc_u->kct_w < (1 << 31));
|
|
|
|
loc_u->kct_w++;
|
|
|
|
} else {
|
2014-09-06 00:13:24 +04:00
|
|
|
u3z(loc_u->kad[inx_w]);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
loc_u->kad[inx_w] = kad;
|
2014-04-22 05:22:11 +04:00
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
// extend fits top
|
|
|
|
//
|
2014-11-06 06:10:22 +03:00
|
|
|
static u3_noun
|
2014-09-06 00:13:24 +04:00
|
|
|
_hink(u3_loss* loc_u,
|
2013-09-29 00:21:18 +04:00
|
|
|
c3_w inx_w,
|
|
|
|
c3_w goy_w)
|
|
|
|
{
|
2014-11-05 04:18:47 +03:00
|
|
|
return __
|
2013-09-29 00:21:18 +04:00
|
|
|
( (loc_u->kct_w == inx_w) ||
|
2014-11-06 03:20:01 +03:00
|
|
|
(u3r_word(0, u3h(loc_u->kad[inx_w])) > goy_w) );
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// extend fits bottom
|
|
|
|
//
|
2014-11-06 06:10:22 +03:00
|
|
|
static u3_noun
|
2014-09-06 00:13:24 +04:00
|
|
|
_lonk(u3_loss* loc_u,
|
2013-09-29 00:21:18 +04:00
|
|
|
c3_w inx_w,
|
|
|
|
c3_w goy_w)
|
|
|
|
{
|
2014-11-05 04:18:47 +03:00
|
|
|
return __
|
2013-09-29 00:21:18 +04:00
|
|
|
( (0 == inx_w) ||
|
2014-11-06 03:20:01 +03:00
|
|
|
(u3r_word(0, u3h(loc_u->kad[inx_w - 1])) < goy_w) );
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// search for first index >= inx_w and <= max_w that fits
|
|
|
|
// the hink and lonk criteria.
|
|
|
|
//
|
2014-11-06 06:10:22 +03:00
|
|
|
static u3_noun
|
2014-09-06 00:13:24 +04:00
|
|
|
_binka(u3_loss* loc_u,
|
2013-09-29 00:21:18 +04:00
|
|
|
c3_w* inx_w,
|
|
|
|
c3_w max_w,
|
|
|
|
c3_w goy_w)
|
|
|
|
{
|
|
|
|
while ( *inx_w <= max_w ) {
|
2014-11-05 04:18:47 +03:00
|
|
|
if ( c3n == _lonk(loc_u, *inx_w, goy_w) ) {
|
|
|
|
return c3n;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2014-11-05 04:18:47 +03:00
|
|
|
if ( c3y == _hink(loc_u, *inx_w, goy_w) ) {
|
|
|
|
return c3y;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
else ++*inx_w;
|
|
|
|
}
|
2014-11-05 04:18:47 +03:00
|
|
|
return c3n;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
#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.
|
|
|
|
//
|
2014-11-06 06:10:22 +03:00
|
|
|
static u3_noun
|
2014-09-06 00:13:24 +04:00
|
|
|
_bink(u3_loss* loc_u,
|
2013-09-29 00:21:18 +04:00
|
|
|
c3_w* inx_w,
|
|
|
|
c3_w max_w,
|
|
|
|
c3_w goy_w)
|
|
|
|
{
|
|
|
|
c3_assert(max_w >= *inx_w);
|
|
|
|
|
|
|
|
if ( max_w == *inx_w ) {
|
2014-11-05 04:18:47 +03:00
|
|
|
if ( c3n == _lonk(loc_u, *inx_w, goy_w) ) {
|
|
|
|
return c3n;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2014-11-05 04:18:47 +03:00
|
|
|
if ( c3y == _hink(loc_u, *inx_w, goy_w) ) {
|
|
|
|
return c3y;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
++*inx_w;
|
2014-11-05 04:18:47 +03:00
|
|
|
return c3n;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2014-04-22 05:22:11 +04:00
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
else {
|
|
|
|
c3_w mid_w = *inx_w + ((max_w - *inx_w) / 2);
|
|
|
|
|
2014-11-05 04:18:47 +03:00
|
|
|
if ( (c3n == _lonk(loc_u, mid_w, goy_w)) ||
|
|
|
|
(c3y == _hink(loc_u, mid_w, goy_w)) )
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
|
|
|
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
|
2014-09-06 00:13:24 +04:00
|
|
|
_merg(u3_loss* loc_u,
|
2013-09-29 00:21:18 +04:00
|
|
|
c3_w inx_w,
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun gay)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
if ( (u3_nul == gay) || (inx_w > loc_u->kct_w) ) {
|
2013-09-29 00:21:18 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun i_gay = u3h(gay);
|
2014-11-06 03:20:01 +03:00
|
|
|
c3_w goy_w = u3r_word(0, i_gay);
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun bik;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
|
|
|
bik = _bink(loc_u, &inx_w, loc_u->kct_w, goy_w);
|
2014-04-22 05:22:11 +04:00
|
|
|
|
2014-11-05 04:18:47 +03:00
|
|
|
if ( c3y == bik ) {
|
2014-09-06 00:13:24 +04:00
|
|
|
_merg(loc_u, inx_w + 1, u3t(gay));
|
2013-09-29 00:21:18 +04:00
|
|
|
_lune(loc_u, inx_w, goy_w);
|
|
|
|
}
|
|
|
|
else {
|
2014-09-06 00:13:24 +04:00
|
|
|
_merg(loc_u, inx_w, u3t(gay));
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// compute lcs
|
|
|
|
//
|
|
|
|
static void
|
2014-09-06 00:13:24 +04:00
|
|
|
_loss(u3_loss* loc_u)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
while ( u3_nul != loc_u->hel ) {
|
|
|
|
u3_noun i_hel = u3h(loc_u->hel);
|
2014-11-06 22:13:57 +03:00
|
|
|
u3_noun guy = u3kdb_get(u3k(loc_u->sev), u3k(i_hel));
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-06 02:36:30 +03:00
|
|
|
if ( u3_none != guy ) {
|
2014-11-06 22:13:57 +03:00
|
|
|
u3_noun gay = u3kb_flop(guy);
|
2014-04-22 05:22:11 +04:00
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
_merg(loc_u, 0, gay);
|
2014-09-06 00:13:24 +04:00
|
|
|
u3z(gay);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
loc_u->hel = u3t(loc_u->hel);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
2014-11-06 22:13:57 +03:00
|
|
|
u3qe_loss(
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun hel,
|
|
|
|
u3_noun hev)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_loss loc_u;
|
|
|
|
u3_noun lcs;
|
2013-11-12 11:09:11 +04:00
|
|
|
|
2013-09-29 00:21:18 +04:00
|
|
|
_lemp(&loc_u, hel, hev);
|
|
|
|
_loss(&loc_u);
|
|
|
|
lcs = _lexs(&loc_u);
|
|
|
|
|
|
|
|
_flem(&loc_u);
|
|
|
|
return lcs;
|
|
|
|
}
|
|
|
|
|
2014-11-06 06:10:22 +03:00
|
|
|
static u3_noun
|
2014-09-06 00:13:24 +04:00
|
|
|
_listp(u3_noun lix)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-04-22 05:22:11 +04:00
|
|
|
while ( 1 ) {
|
2014-11-05 04:18:47 +03:00
|
|
|
if ( u3_nul == lix ) return c3y;
|
|
|
|
if ( c3n == u3du(lix) ) return c3n;
|
2014-09-06 00:13:24 +04:00
|
|
|
lix = u3t(lix);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
2014-11-06 22:13:57 +03:00
|
|
|
u3we_loss(u3_noun cor)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun hel, hev;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-18 00:56:51 +03:00
|
|
|
if ( (u3_none == (hel = u3r_at(u3x_sam_2, cor))) ||
|
|
|
|
(u3_none == (hev = u3r_at(u3x_sam_3, cor))) ||
|
2014-11-05 04:18:47 +03:00
|
|
|
(c3n == _listp(hel)) ||
|
|
|
|
(c3n == _listp(hev)) )
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-11-06 03:20:01 +03:00
|
|
|
return u3m_bail(c3__fail);
|
2013-09-29 00:21:18 +04:00
|
|
|
} else {
|
2014-11-06 22:13:57 +03:00
|
|
|
return u3qe_loss(hel, hev);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|