urbit/j/3/po.c

109 lines
2.2 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* j/3/po.c
**
** This file is in the public domain.
*/
#include "all.h"
2014-09-04 07:10:43 +04:00
2013-09-29 00:21:18 +04:00
// good old linear search
//
2014-09-06 00:13:24 +04:00
static u3_noun
_po_find(u3_noun buf, u3_noun a)
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
if ( !_(u3_ca_is_cat(a)) ) {
2014-09-06 00:13:24 +04:00
return u3_nul;
2013-09-29 00:21:18 +04:00
}
else {
c3_w i_w;
c3_w a_w = a;
for ( i_w = 0; i_w < 256; i_w++ ) {
c3_y byt_y[3];
c3_w but_w;
2014-09-06 00:13:24 +04:00
u3_cr_bytes((i_w * 3), 3, byt_y, buf);
2013-09-29 00:21:18 +04:00
but_w = (byt_y[0] | (byt_y[1] << 8) | (byt_y[2] << 16));
if ( but_w == a_w ) {
2014-09-06 00:13:24 +04:00
return u3nc(u3_nul, i_w);
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
return u3_nul;
2013-09-29 00:21:18 +04:00
}
}
2014-09-06 00:13:24 +04:00
u3_noun
u3_cwcp_ins(
u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun x, a, buf;
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( (c3n == u3_cr_mean(cor, u3_cv_sam, &a, u3_cv_con_sam, &x, 0)) ||
(c3n == u3du(x)) ||
(c3n == u3ud(buf = u3h(x))) ||
(c3n == u3ud(a)) )
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
return u3_cm_bail(c3__exit);
2013-09-29 00:21:18 +04:00
} else {
return _po_find(buf, a);
}
}
2014-09-06 00:13:24 +04:00
u3_noun
u3_cwcp_ind(
u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun x, a, buf;
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( (c3n == u3_cr_mean(cor, u3_cv_sam, &a, u3_cv_con_sam, &x, 0)) ||
(c3n == u3du(x)) ||
(c3n == u3ud(buf = u3t(x))) ||
(c3n == u3ud(a)) )
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
return u3_cm_bail(c3__exit);
2013-09-29 00:21:18 +04:00
} else {
return _po_find(buf, a);
}
}
2014-09-06 00:13:24 +04:00
u3_noun
u3_cwcp_tos(
u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun x, a, buf;
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( (c3n == u3_cr_mean(cor, u3_cv_sam, &a, u3_cv_con_sam, &x, 0)) ||
(c3n == u3du(x)) ||
(c3n == u3ud(buf = u3h(x))) ||
(c3n == u3ud(a)) ||
(a >= 256) )
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
return u3_cm_bail(c3__exit);
}
2013-09-29 00:21:18 +04:00
else {
c3_y byt_y[3];
2014-09-06 00:13:24 +04:00
u3_cr_bytes((a * 3), 3, byt_y, buf);
2013-09-29 00:21:18 +04:00
return (byt_y[0] | (byt_y[1] << 8) | (byt_y[2] << 16));
}
}
2014-09-06 00:13:24 +04:00
u3_noun
u3_cwcp_tod(
u3_noun cor)
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun x, a, buf;
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( (c3n == u3_cr_mean(cor, u3_cv_sam, &a, u3_cv_con_sam, &x, 0)) ||
(c3n == u3du(x)) ||
(c3n == u3ud(buf = u3t(x))) ||
(c3n == u3ud(a)) ||
(a >= 256) )
2013-09-29 00:21:18 +04:00
{
2014-09-06 00:13:24 +04:00
return u3_cm_bail(c3__exit);
2013-09-29 00:21:18 +04:00
} else {
c3_y byt_y[3];
2014-09-06 00:13:24 +04:00
u3_cr_bytes((a * 3), 3, byt_y, buf);
2013-09-29 00:21:18 +04:00
return (byt_y[0] | (byt_y[1] << 8) | (byt_y[2] << 16));
}
}