2014-09-05 23:55:16 +04:00
|
|
|
/* j/5/jam.c
|
2013-09-29 00:21:18 +04:00
|
|
|
**
|
|
|
|
*/
|
|
|
|
#include "all.h"
|
2014-09-04 07:10:43 +04:00
|
|
|
|
2018-05-30 07:29:53 +03:00
|
|
|
typedef struct {
|
|
|
|
c3_w a_w;
|
|
|
|
c3_w b_w;
|
|
|
|
c3_w bit_w;
|
|
|
|
c3_w* wor_w;
|
|
|
|
} _jam_buf;
|
|
|
|
|
|
|
|
static void
|
|
|
|
_jam_buf_grow(_jam_buf* buf_u, c3_w mor_w)
|
|
|
|
{
|
|
|
|
c3_w wan_w = buf_u->bit_w + mor_w;
|
|
|
|
|
|
|
|
if ( wan_w < mor_w ) {
|
|
|
|
// overflowed c3_w bits
|
|
|
|
u3m_bail(c3__fail);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
|
2018-05-30 07:29:53 +03:00
|
|
|
if ( wan_w > buf_u->a_w ) {
|
|
|
|
c3_w old_w, new_w, c_w = 0;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2018-05-30 07:29:53 +03:00
|
|
|
old_w = buf_u->a_w >> 5;
|
|
|
|
if ( (old_w << 5) != buf_u->a_w ) {
|
|
|
|
++old_w;
|
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2018-05-30 07:29:53 +03:00
|
|
|
// fibonacci growth
|
|
|
|
while ( c_w < wan_w ) {
|
|
|
|
c_w = buf_u->a_w + buf_u->b_w;
|
|
|
|
buf_u->b_w = buf_u->a_w;
|
|
|
|
buf_u->a_w = c_w;
|
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2018-05-30 07:29:53 +03:00
|
|
|
new_w = c_w >> 5;
|
|
|
|
if ( (new_w << 5) != c_w ) {
|
|
|
|
++new_w;
|
|
|
|
}
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2018-05-30 07:29:53 +03:00
|
|
|
buf_u->wor_w = u3a_realloc(buf_u->wor_w, new_w * sizeof(c3_w));
|
|
|
|
memset(buf_u->wor_w + old_w, 0, (new_w - old_w) * sizeof(c3_w));
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2018-05-30 07:29:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_jam_buf_chop(_jam_buf* buf_u, c3_w met_w, u3_noun a)
|
|
|
|
{
|
|
|
|
c3_w bit_w = buf_u->bit_w;
|
|
|
|
_jam_buf_grow(buf_u, met_w);
|
|
|
|
u3r_chop(0, 0, met_w, bit_w, buf_u->wor_w, a);
|
|
|
|
buf_u->bit_w += met_w;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_jam_buf_atom(_jam_buf* buf_u, u3_noun a)
|
|
|
|
{
|
|
|
|
u3_noun mat = u3qe_mat(a);
|
|
|
|
_jam_buf_chop(buf_u, u3h(mat), u3t(mat));
|
|
|
|
u3z(mat);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u3_noun
|
|
|
|
_jam_buf_top(u3_noun a)
|
|
|
|
{
|
|
|
|
u3p(u3h_root) har_p = u3h_new();
|
|
|
|
c3_o nor_o = u3a_is_north(u3R);
|
2018-05-30 19:49:04 +03:00
|
|
|
c3_y wis_y = c3_wiseof(u3_noun);
|
2018-05-30 07:29:53 +03:00
|
|
|
c3_ys mov = ( c3y == nor_o ? -wis_y : wis_y );
|
|
|
|
c3_ys off = ( c3y == nor_o ? 0 : -wis_y );
|
2018-05-30 19:49:04 +03:00
|
|
|
u3_noun* top, *don = u3to(u3_noun, u3R->cap_p + off);
|
2018-05-30 07:29:53 +03:00
|
|
|
_jam_buf buf_u;
|
|
|
|
u3_weak c;
|
|
|
|
c3_o cel_o;
|
2018-05-30 19:49:04 +03:00
|
|
|
c3_w* sal_w, len_w;
|
2018-05-30 07:29:53 +03:00
|
|
|
|
2018-05-30 21:10:58 +03:00
|
|
|
buf_u.a_w = 144; // fib(12) is small enough to be reasonably fast to allocate.
|
|
|
|
buf_u.b_w = 89; // fib(11) is needed to get fib(13).
|
2018-05-30 19:49:04 +03:00
|
|
|
len_w = buf_u.a_w >> 5;
|
|
|
|
if ( (len_w << 5) != buf_u.a_w ) {
|
|
|
|
++len_w;
|
2018-01-25 00:16:44 +03:00
|
|
|
}
|
2018-05-30 19:49:04 +03:00
|
|
|
buf_u.wor_w = u3a_calloc(len_w, sizeof(c3_w));
|
2018-05-30 07:29:53 +03:00
|
|
|
buf_u.bit_w = 0;
|
|
|
|
|
|
|
|
u3R->cap_p += mov;
|
2018-05-30 19:49:04 +03:00
|
|
|
top = u3to(u3_noun, u3R->cap_p + off);
|
|
|
|
*top = a;
|
|
|
|
|
|
|
|
while ( top != don ) {
|
|
|
|
a = *top;
|
|
|
|
cel_o = u3du(a);
|
|
|
|
c = u3h_git(har_p, a);
|
|
|
|
if ( u3_none != c ) {
|
|
|
|
if ( c3y == cel_o ) {
|
|
|
|
_jam_buf_chop(&buf_u, 2, 3);
|
|
|
|
_jam_buf_atom(&buf_u, c);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( u3r_met(0, a) <= u3r_met(0, c) ) {
|
|
|
|
_jam_buf_chop(&buf_u, 1, 0);
|
|
|
|
_jam_buf_atom(&buf_u, a);
|
2018-03-15 03:53:12 +03:00
|
|
|
}
|
2018-05-30 07:29:53 +03:00
|
|
|
else {
|
2018-05-30 19:49:04 +03:00
|
|
|
_jam_buf_chop(&buf_u, 2, 3);
|
|
|
|
_jam_buf_atom(&buf_u, c);
|
2018-01-25 00:16:44 +03:00
|
|
|
}
|
2018-05-30 19:49:04 +03:00
|
|
|
}
|
|
|
|
u3R->cap_p -= mov;
|
|
|
|
top = u3to(u3_noun, u3R->cap_p + off);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
u3h_put(har_p, a, buf_u.bit_w);
|
|
|
|
if ( c3n == cel_o ) {
|
|
|
|
_jam_buf_chop(&buf_u, 1, 0);
|
|
|
|
_jam_buf_atom(&buf_u, a);
|
2018-05-30 07:29:53 +03:00
|
|
|
u3R->cap_p -= mov;
|
2018-05-30 19:49:04 +03:00
|
|
|
top = u3to(u3_noun, u3R->cap_p + off);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_jam_buf_chop(&buf_u, 2, 1);
|
|
|
|
*top = u3t(a);
|
|
|
|
|
|
|
|
u3R->cap_p += mov;
|
|
|
|
top = u3to(u3_noun, u3R->cap_p + off);
|
|
|
|
*top = u3h(a);
|
|
|
|
}
|
2018-01-25 00:16:44 +03:00
|
|
|
}
|
2018-05-30 07:29:53 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 19:49:04 +03:00
|
|
|
len_w = buf_u.bit_w >> 5;
|
|
|
|
if ( (len_w << 5) != buf_u.bit_w ) {
|
2018-05-30 07:29:53 +03:00
|
|
|
++len_w;
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
2018-05-30 07:29:53 +03:00
|
|
|
sal_w = u3a_slab(len_w);
|
2018-05-30 21:10:58 +03:00
|
|
|
memcpy(sal_w, buf_u.wor_w, len_w*sizeof(c3_w));
|
2018-05-30 07:29:53 +03:00
|
|
|
u3a_free(buf_u.wor_w);
|
|
|
|
u3h_free(har_p);
|
2018-05-30 21:22:59 +03:00
|
|
|
return u3a_moot(sal_w);
|
2018-05-30 07:29:53 +03:00
|
|
|
}
|
2018-03-15 03:53:12 +03:00
|
|
|
|
2018-05-30 07:29:53 +03:00
|
|
|
/* functions
|
|
|
|
*/
|
2018-03-15 03:53:12 +03:00
|
|
|
u3_noun
|
|
|
|
u3qe_jam(u3_atom a)
|
|
|
|
{
|
2018-05-30 07:29:53 +03:00
|
|
|
return _jam_buf_top(a);
|
2018-03-15 03:53:12 +03:00
|
|
|
}
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
2014-11-06 22:13:57 +03:00
|
|
|
u3we_jam(u3_noun cor)
|
2013-09-29 00:21:18 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun a;
|
2013-09-29 00:21:18 +04:00
|
|
|
|
2014-11-18 00:56:51 +03:00
|
|
|
if ( (u3_none == (a = u3r_at(u3x_sam, cor))) ) {
|
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_jam(a);
|
2013-09-29 00:21:18 +04:00
|
|
|
}
|
|
|
|
}
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_atom
|
2014-11-06 22:13:57 +03:00
|
|
|
u3ke_jam(u3_noun a)
|
2014-09-05 23:55:16 +04:00
|
|
|
{
|
2014-11-06 22:13:57 +03:00
|
|
|
u3_atom b = u3qe_jam(a);
|
2014-09-05 23:55:16 +04:00
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3z(a);
|
2014-09-05 23:55:16 +04:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|