urbit/j/c/po.c

109 lines
2.1 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-06 03:20:01 +03:00
if ( !_(u3a_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-11-06 03:20:01 +03:00
u3r_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
2014-11-06 22:13:57 +03:00
u3wcp_ins(
2014-09-06 00:13:24 +04:00
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-18 00:56:51 +03:00
if ( (c3n == u3r_mean(cor, u3x_sam, &a, u3x_con_sam, &x, 0)) ||
2014-11-05 04:18:47 +03:00
(c3n == u3du(x)) ||
(c3n == u3ud(buf = u3h(x))) ||
(c3n == u3ud(a)) )
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
return u3m_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
2014-11-06 22:13:57 +03:00
u3wcp_ind(
2014-09-06 00:13:24 +04:00
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-18 00:56:51 +03:00
if ( (c3n == u3r_mean(cor, u3x_sam, &a, u3x_con_sam, &x, 0)) ||
2014-11-05 04:18:47 +03:00
(c3n == u3du(x)) ||
(c3n == u3ud(buf = u3t(x))) ||
(c3n == u3ud(a)) )
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
return u3m_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
2014-11-06 22:13:57 +03:00
u3wcp_tos(
2014-09-06 00:13:24 +04:00
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-18 00:56:51 +03:00
if ( (c3n == u3r_mean(cor, u3x_sam, &a, u3x_con_sam, &x, 0)) ||
2014-11-05 04:18:47 +03:00
(c3n == u3du(x)) ||
(c3n == u3ud(buf = u3h(x))) ||
(c3n == u3ud(a)) ||
(a >= 256) )
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
}
2013-09-29 00:21:18 +04:00
else {
c3_y byt_y[3];
2014-11-06 03:20:01 +03:00
u3r_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
2014-11-06 22:13:57 +03:00
u3wcp_tod(
2014-09-06 00:13:24 +04:00
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-18 00:56:51 +03:00
if ( (c3n == u3r_mean(cor, u3x_sam, &a, u3x_con_sam, &x, 0)) ||
2014-11-05 04:18:47 +03:00
(c3n == u3du(x)) ||
(c3n == u3ud(buf = u3t(x))) ||
(c3n == u3ud(a)) ||
(a >= 256) )
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__exit);
2013-09-29 00:21:18 +04:00
} else {
c3_y byt_y[3];
2014-11-06 03:20:01 +03:00
u3r_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));
}
}