Relative pointers in hashtable.

This commit is contained in:
C. Guy Yarvin 2014-11-03 17:02:55 -08:00
parent c7b4917a75
commit 49bf64733d
10 changed files with 106 additions and 96 deletions

18
g/a.c
View File

@ -1327,24 +1327,25 @@ u3_ca_moot(c3_w* sal_w)
return u3_co_to_pug(u3_co_outa(nov_w));
}
#if 0
/* _ca_detect(): in u3_ca_detect().
*/
static c3_d
_ca_detect(u3_ch_root* har_u, u3_noun fum, u3_noun som, c3_d axe_d)
_ca_detect(u3p(u3_ch_root) har_p, u3_noun fum, u3_noun som, c3_d axe_d)
{
while ( 1 ) {
if ( som == fum ) {
return axe_d;
}
else if ( u3_ne(u3du(fum)) || (u3_none != u3_ch_get(har_u, fum)) ) {
else if ( u3_ne(u3du(fum)) || (u3_none != u3_ch_get(har_p, fum)) ) {
return 0;
}
else {
c3_d eax_d;
u3_ch_put(har_u, fum, 0);
u3_ch_put(har_p, fum, 0);
if ( 0 != (eax_d = _ca_detect(har_u, u3h(fum), som, 2ULL * axe_d)) ) {
if ( 0 != (eax_d = _ca_detect(har_p, u3h(fum), som, 2ULL * axe_d)) ) {
return u3_yes;
}
else {
@ -1362,14 +1363,15 @@ _ca_detect(u3_ch_root* har_u, u3_noun fum, u3_noun som, c3_d axe_d)
c3_d
u3_ca_detect(u3_noun fum, u3_noun som)
{
u3_ch_root* har_u = u3_ch_new();
c3_o ret_o;
u3p(u3_ch_root) har_p = u3_ch_new();
c3_o ret_o;
ret_o = _ca_detect(har_u, fum, som, 1);
u3_ch_free(har_u);
ret_o = _ca_detect(har_p, fum, som, 1);
u3_ch_free(har_p);
return ret_o;
}
#endif
/* u3_ca_mint(): finish a measured proto-atom.
*/

4
g/e.c
View File

@ -864,8 +864,8 @@ u3_ce_init(c3_o chk_o)
void
u3_ce_grab(c3_c* cap_c, u3_noun som, ...) // terminate with u3_none
{
// u3_ch_free(u3R->cax.har_u);
// u3R->cax.har_u = u3_ch_new();
// u3_ch_free(u3R->cax.har_p);
// u3R->cax.har_p = u3_ch_new();
u3_cv_mark();
u3_cm_mark();

66
g/h.c
View File

@ -9,17 +9,18 @@ static void* _ch_some_new(c3_w lef_w);
/* u3_ch_new(): create hashtable.
*/
u3_ch_root*
u3p(u3_ch_root)
u3_ch_new(void)
{
u3_ch_root* har_u = u3_ca_walloc(c3_wiseof(u3_ch_root));
u3_ch_root* har_u = u3_ca_walloc(c3_wiseof(u3_ch_root));
u3p(u3_ch_root) har_p = u3of(u3_ch_root, har_u);
c3_w i_w;
har_u->clk_w = 0;
for ( i_w = 0; i_w < 64; i_w++ ) {
har_u->sot_w[i_w] = 0;
}
return har_u;
return har_p;
}
/* _ch_popcount(): number of bits set in word. A standard intrinsic.
@ -183,13 +184,14 @@ _ch_some_add(void* han_v, c3_w lef_w, c3_w rem_w, u3_noun kev)
** `key` is RETAINED; `val` is transferred.
*/
void
u3_ch_put(u3_ch_root* har_u, u3_noun key, u3_noun val)
u3_ch_put(u3p(u3_ch_root) har_p, u3_noun key, u3_noun val)
{
u3_noun kev = u3nc(u3k(key), val);
c3_w mug_w = u3_cr_mug(key);
c3_w inx_w = (mug_w >> 25);
c3_w rem_w = (mug_w & ((1 << 25) - 1));
c3_w sot_w = har_u->sot_w[inx_w];
u3_ch_root* har_u = u3to(u3_ch_root, har_p);
u3_noun kev = u3nc(u3k(key), val);
c3_w mug_w = u3_cr_mug(key);
c3_w inx_w = (mug_w >> 25);
c3_w rem_w = (mug_w & ((1 << 25) - 1));
c3_w sot_w = har_u->sot_w[inx_w];
if ( u3_so(u3_ch_slot_is_null(sot_w)) ) {
har_u->sot_w[inx_w] = u3_ch_noun_to_slot(kev);
@ -272,11 +274,12 @@ _ch_node_hum(u3_ch_node* han_u, c3_w lef_w, c3_w rem_w, c3_w mug_w)
** `key` is RETAINED.
*/
c3_o
u3_ch_hum(u3_ch_root* har_u, c3_w mug_w)
u3_ch_hum(u3p(u3_ch_root) har_p, c3_w mug_w)
{
c3_w inx_w = (mug_w >> 25);
c3_w rem_w = (mug_w & ((1 << 25) - 1));
c3_w sot_w = har_u->sot_w[inx_w];
u3_ch_root* har_u = u3to(u3_ch_root, har_p);
c3_w inx_w = (mug_w >> 25);
c3_w rem_w = (mug_w & ((1 << 25) - 1));
c3_w sot_w = har_u->sot_w[inx_w];
if ( u3_so(u3_ch_slot_is_null(sot_w)) ) {
return u3_no;
@ -358,12 +361,13 @@ _ch_node_get(u3_ch_node* han_u, c3_w lef_w, c3_w rem_w, u3_noun key)
** `key` is RETAINED.
*/
u3_weak
u3_ch_get(u3_ch_root* har_u, u3_noun key)
u3_ch_get(u3p(u3_ch_root) har_p, u3_noun key)
{
c3_w mug_w = u3_cr_mug(key);
c3_w inx_w = (mug_w >> 25);
c3_w rem_w = (mug_w & ((1 << 25) - 1));
c3_w sot_w = har_u->sot_w[inx_w];
u3_ch_root* har_u = u3to(u3_ch_root, har_p);
c3_w mug_w = u3_cr_mug(key);
c3_w inx_w = (mug_w >> 25);
c3_w rem_w = (mug_w & ((1 << 25) - 1));
c3_w sot_w = har_u->sot_w[inx_w];
if ( u3_so(u3_ch_slot_is_null(sot_w)) ) {
return u3_none;
@ -446,12 +450,13 @@ _ch_node_gut(u3_ch_node* han_u, c3_w lef_w, c3_w rem_w, u3_noun key)
** `key` is RETAINED.
*/
u3_weak
u3_ch_gut(u3_ch_root* har_u, u3_noun key)
u3_ch_gut(u3p(u3_ch_root) har_p, u3_noun key)
{
c3_w mug_w = u3_cr_mug(key);
c3_w inx_w = (mug_w >> 25);
c3_w rem_w = (mug_w & ((1 << 25) - 1));
c3_w sot_w = har_u->sot_w[inx_w];
u3_ch_root* har_u = u3to(u3_ch_root, har_p);
c3_w mug_w = u3_cr_mug(key);
c3_w inx_w = (mug_w >> 25);
c3_w rem_w = (mug_w & ((1 << 25) - 1));
c3_w sot_w = har_u->sot_w[inx_w];
if ( u3_so(u3_ch_slot_is_null(sot_w)) ) {
return u3_none;
@ -521,9 +526,10 @@ _ch_free_node(u3_ch_node* han_u, c3_w lef_w)
/* u3_ch_free(): free hashtable.
*/
void
u3_ch_free(u3_ch_root* har_u)
u3_ch_free(u3p(u3_ch_root) har_p)
{
c3_w i_w;
u3_ch_root* har_u = u3to(u3_ch_root, har_p);
c3_w i_w;
for ( i_w = 0; i_w < 64; i_w++ ) {
c3_w sot_w = har_u->sot_w[i_w];
@ -587,9 +593,10 @@ _ch_walk_node(u3_ch_node* han_u, c3_w lef_w, void (*fun_f)(u3_noun))
/* u3_ch_walk(): walk hashtable for gc.
*/
void
u3_ch_walk(u3_ch_root* har_u, void (*fun_f)(u3_noun))
u3_ch_walk(u3p(u3_ch_root) har_p, void (*fun_f)(u3_noun))
{
c3_w i_w;
u3_ch_root* har_u = u3to(u3_ch_root, har_p);
c3_w i_w;
for ( i_w = 0; i_w < 64; i_w++ ) {
c3_w sot_w = har_u->sot_w[i_w];
@ -655,9 +662,10 @@ _ch_mark_node(u3_ch_node* han_u, c3_w lef_w)
/* u3_ch_mark(): mark hashtable for gc.
*/
void
u3_ch_mark(u3_ch_root* har_u)
u3_ch_mark(u3p(u3_ch_root) har_p)
{
c3_w i_w;
u3_ch_root* har_u = u3to(u3_ch_root, har_p);
c3_w i_w;
for ( i_w = 0; i_w < 64; i_w++ ) {
c3_w sot_w = har_u->sot_w[i_w];

22
g/j.c
View File

@ -294,7 +294,7 @@ _cj_warm_fend(u3_noun bat)
u3_cs_road* rod_u = u3R;
while ( 1 ) {
u3_weak jaw = u3_ch_gut(rod_u->jed.har_u, bat);
u3_weak jaw = u3_ch_gut(rod_u->jed.har_p, bat);
if ( u3_none != jaw ) {
return jaw;
@ -431,7 +431,7 @@ _cj_warm_ream_be(c3_l jax_l,
u3_noun bat,
u3_noun huc)
{
u3_ch_put(u3R->jed.har_u,
u3_ch_put(u3R->jed.har_p,
bat,
u3nq(jax_l,
u3k(pax),
@ -468,7 +468,7 @@ _cj_warm_ream_un(u3_noun soh)
u3_noun cax;
c3_l jax_l;
if ( u3_none != (cax = u3_ch_get(u3R->jed.har_u, u3h(u3h(sab)))) ) {
if ( u3_none != (cax = u3_ch_get(u3R->jed.har_p, u3h(u3h(sab)))) ) {
jax_l = u3h(cax);
u3z(cax);
}
@ -538,8 +538,8 @@ _cj_warm_ream(void)
void
u3_cj_ream(void)
{
u3_ch_free(u3R->jed.har_u);
u3R->jed.har_u = u3_ch_new();
u3_ch_free(u3R->jed.har_p);
u3R->jed.har_p = u3_ch_new();
_cj_warm_ream();
}
@ -572,7 +572,7 @@ _cj_warm_mine(u3_noun clu, u3_noun cor)
c3_l jax_l = _cj_boil_mine(mop, cor);
// fprintf(stderr, "warm: bat %x\r\n", u3_cr_mug(bat));
u3_ch_put(u3R->jed.har_u,
u3_ch_put(u3R->jed.har_p,
bat,
u3nq(jax_l,
u3k(pax),
@ -611,7 +611,7 @@ _cj_find(u3_noun bat)
u3_cs_road* rod_u = u3R;
while ( 1 ) {
u3_weak jaw = u3_ch_gut(rod_u->jed.har_u, bat);
u3_weak jaw = u3_ch_gut(rod_u->jed.har_p, bat);
if ( u3_none != jaw ) {
u3_assure(u3_co_is_cat(u3h(jaw)));
@ -619,7 +619,7 @@ _cj_find(u3_noun bat)
#if 0
if ( rod_u != u3R ) {
fprintf(stderr, "got: %x in %p/%p, %d\r\n",
bat, rod_u, rod_u->jed.har_u, jax);
bat, rod_u, rod_u->jed.har_p, jax);
}
#endif
return (c3_l)u3h(jaw);
@ -975,7 +975,7 @@ _cj_warm_reap(u3_noun kev)
u3_ca_use(tab),
u3_cr_mug(xac));
#endif
u3_ch_put(u3R->jed.har_u, tab, xac);
u3_ch_put(u3R->jed.har_p, tab, xac);
u3z(tab);
}
}
@ -983,8 +983,8 @@ _cj_warm_reap(u3_noun kev)
/* u3_cj_reap(): promote jet state. RETAINS.
*/
void
u3_cj_reap(u3_noun das, u3_ch_root* har_u)
u3_cj_reap(u3_noun das, u3p(u3_ch_root) har_p)
{
_cj_cold_reap_in(u3t(das));
u3_ch_walk(har_u, _cj_warm_reap);
u3_ch_walk(har_p, _cj_warm_reap);
}

18
g/m.c
View File

@ -342,8 +342,8 @@ _boot_south(c3_w* mem_w, c3_w siz_w, c3_w len_w)
static void
_boot_parts(void)
{
u3R->cax.har_u = u3_ch_new();
u3R->jed.har_u = u3_ch_new();
u3R->cax.har_p = u3_ch_new();
u3R->jed.har_p = u3_ch_new();
u3R->jed.das = u3nc(u3_nul, u3_nul);
}
@ -352,14 +352,14 @@ _boot_parts(void)
void
u3_cm_mark(void)
{
u3_ch_mark(u3R->jed.har_u);
u3_ch_mark(u3R->jed.har_p);
u3_ca_mark_noun(u3R->jed.das);
u3_ca_mark_noun(u3R->ski.flu);
u3_ca_mark_noun(u3R->bug.tax);
u3_ca_mark_noun(u3R->bug.mer);
u3_ca_mark_noun(u3R->pro.don);
u3_ca_mark_noun(u3R->pro.day);
u3_ch_mark(u3R->cax.har_u);
u3_ch_mark(u3R->cax.har_p);
}
/* u3_cm_boot(): instantiate or activate image.
@ -392,8 +392,8 @@ u3_cm_boot(c3_o nuu_o, c3_o bug_o)
void
u3_cm_clear(void)
{
u3_ch_free(u3R->cax.har_u);
u3_ch_free(u3R->jed.har_u);
u3_ch_free(u3R->cax.har_p);
u3_ch_free(u3R->jed.har_p);
u3_ca_lose(u3R->jed.das);
}
@ -662,14 +662,14 @@ u3_cm_hate(c3_w pad_w)
u3_noun
u3_cm_love(u3_noun pro)
{
u3_noun das = u3R->jed.das;
u3_ch_root* har_u = u3R->jed.har_u;
u3_noun das = u3R->jed.das;
u3p(u3_ch_root) har_p = u3R->jed.har_p;
u3_cm_fall();
pro = u3_ca_take(pro);
u3_cj_reap(das, har_u);
u3_cj_reap(das, har_p);
u3R->cap_p = u3R->ear_p;
u3R->ear_p = 0;

20
g/z.c
View File

@ -12,7 +12,7 @@ u3_cz_find(u3_mote fun, u3_noun one)
u3_noun key = u3nc(fun, u3k(one));
u3_noun val;
val = u3_ch_get(u3R->cax.har_u, key);
val = u3_ch_get(u3R->cax.har_p, key);
u3z(key);
return val;
}
@ -22,7 +22,7 @@ u3_cz_find_2(u3_mote fun, u3_noun one, u3_noun two)
u3_noun key = u3nt(fun, u3k(one), u3k(two));
u3_noun val;
val = u3_ch_get(u3R->cax.har_u, key);
val = u3_ch_get(u3R->cax.har_p, key);
u3z(key);
return val;
}
@ -32,7 +32,7 @@ u3_cz_find_3(u3_mote fun, u3_noun one, u3_noun two, u3_noun tri)
u3_noun key = u3nq(fun, u3k(one), u3k(two), u3k(tri));
u3_noun val;
val = u3_ch_get(u3R->cax.har_u, key);
val = u3_ch_get(u3R->cax.har_p, key);
u3z(key);
return val;
}
@ -42,7 +42,7 @@ u3_cz_find_4(u3_mote fun, u3_noun one, u3_noun two, u3_noun tri, u3_noun qua)
u3_noun key = u3nc(fun, u3nq(u3k(one), u3k(two), u3k(tri), u3k(qua)));
u3_noun val;
val = u3_ch_get(u3R->cax.har_u, key);
val = u3_ch_get(u3R->cax.har_p, key);
u3z(key);
return val;
}
@ -54,7 +54,7 @@ u3_cz_save(u3_mote fun, u3_noun one, u3_noun val)
{
u3_noun key = u3nc(fun, u3k(one));
u3_ch_put(u3R->cax.har_u, key, u3k(val));
u3_ch_put(u3R->cax.har_p, key, u3k(val));
u3z(key);
return val;
}
@ -63,7 +63,7 @@ u3_cz_save_2(u3_mote fun, u3_noun one, u3_noun two, u3_noun val)
{
u3_noun key = u3nt(fun, u3k(one), u3k(two));
u3_ch_put(u3R->cax.har_u, key, u3k(val));
u3_ch_put(u3R->cax.har_p, key, u3k(val));
u3z(key);
return val;
}
@ -72,7 +72,7 @@ u3_cz_save_3(u3_mote fun, u3_noun one, u3_noun two, u3_noun tri, u3_noun val)
{
u3_noun key = u3nq(fun, u3k(one), u3k(two), u3k(tri));
u3_ch_put(u3R->cax.har_u, key, u3k(val));
u3_ch_put(u3R->cax.har_p, key, u3k(val));
u3z(key);
return val;
}
@ -86,7 +86,7 @@ u3_cz_save_4(u3_mote fun,
{
u3_noun key = u3nc(fun, u3nq(u3k(one), u3k(two), u3k(tri), u3k(qua)));
u3_ch_put(u3R->cax.har_u, key, u3k(val));
u3_ch_put(u3R->cax.har_p, key, u3k(val));
u3z(key);
return val;
}
@ -97,13 +97,13 @@ u3_noun
u3_cz_uniq(u3_noun som)
{
u3_noun key = u3nc(c3__uniq, u3k(som));
u3_noun val = u3_ch_get(u3R->cax.har_u, key);
u3_noun val = u3_ch_get(u3R->cax.har_p, key);
if ( u3_none != val ) {
u3z(key); u3z(som); return val;
}
else {
u3_ch_put(u3R->cax.har_u, key, u3k(som));
u3_ch_put(u3R->cax.har_p, key, u3k(som));
return som;
}
}

View File

@ -8,7 +8,7 @@
**/
/* u3_ch_new(): create hashtable.
*/
u3_ch_root*
u3p(u3_ch_root)
u3_ch_new(void);
/* u3_ch_put(): insert in hashtable.
@ -16,33 +16,33 @@
** `key` is RETAINED; `val` is transferred.
*/
void
u3_ch_put(u3_ch_root* har_u, u3_noun key, u3_noun val);
u3_ch_put(u3p(u3_ch_root) har_p, u3_noun key, u3_noun val);
/* u3_ch_get(): read from hashtable.
**
** `key` is RETAINED.
*/
u3_weak
u3_ch_get(u3_ch_root* har_u, u3_noun key);
u3_ch_get(u3p(u3_ch_root) har_p, u3_noun key);
/* u3_ch_gut(): read from hashtable, unifying key nouns.
**
** `key` is RETAINED.
*/
u3_weak
u3_ch_gut(u3_ch_root* har_u, u3_noun key);
u3_ch_gut(u3p(u3_ch_root) har_p, u3_noun key);
/* u3_ch_free(): free hashtable.
*/
void
u3_ch_free(u3_ch_root* har_u);
u3_ch_free(u3p(u3_ch_root) har_p);
/* u3_ch_mark(): mark hashtable for gc.
*/
void
u3_ch_mark(u3_ch_root* har_u);
u3_ch_mark(u3p(u3_ch_root) har_p);
/* u3_ch_walk(): traverse hashtable with key, value fn; RETAINS.
*/
void
u3_ch_walk(u3_ch_root* har_u, void (*fun_f)(u3_noun));
u3_ch_walk(u3p(u3_ch_root) har_p, void (*fun_f)(u3_noun));

View File

@ -66,4 +66,4 @@
/* u3_cj_reap(): promote jet state. RETAINS.
*/
void
u3_cj_reap(u3_noun das, u3_ch_root* har_u);
u3_cj_reap(u3_noun das, u3p(u3_ch_root) har_p);

View File

@ -150,8 +150,8 @@
} all;
struct { // jet dashboard
u3_ch_root* har_u; // jet index (old style)
u3_noun das; // dashboard (new style)
u3p(u3_ch_root) har_p; // jet index (old style)
u3_noun das; // dashboard (new style)
} jed;
struct { // namespace
@ -170,7 +170,7 @@
} pro;
struct { // memoization
u3_ch_root* har_u; // (map (pair term noun) noun)
u3p(u3_ch_root) har_p; // (map (pair term noun) noun)
} cax;
} u3_cs_road;
typedef u3_cs_road u3_road;

View File

@ -8,10 +8,10 @@
/* functions
*/
static u3_noun
_jam_in(u3_ch_root* har_u, u3_atom, u3_atom, u3_noun);
_jam_in(u3p(u3_ch_root) har_p, u3_atom, u3_atom, u3_noun);
static u3_noun
_jam_in_pair(u3_ch_root* har_u,
_jam_in_pair(u3p(u3_ch_root) har_p,
u3_atom h_a,
u3_atom t_a,
u3_atom b,
@ -19,14 +19,14 @@
{
u3_noun w = u3nc(u3nc(2, 1), u3k(l));
u3_noun x = u3_cqa_add(2, b);
u3_noun d = _jam_in(har_u, h_a, x, w);
u3_noun d = _jam_in(har_p, h_a, x, w);
u3_noun p_d, q_d, r_d;
u3_noun r;
u3_cr_trel(d, &p_d, &q_d, &r_d);
{
u3_noun y = u3_cqa_add(x, p_d);
u3_noun e = _jam_in(har_u, t_a, y, q_d);
u3_noun e = _jam_in(har_p, t_a, y, q_d);
u3_noun p_e, q_e, r_e;
u3_cr_trel(e, &p_e, &q_e, &r_e);
@ -48,7 +48,7 @@
}
static u3_noun
_jam_in_flat(u3_ch_root* har_u,
_jam_in_flat(u3p(u3_ch_root) har_p,
u3_atom a,
u3_noun l)
{
@ -63,7 +63,7 @@
}
static u3_noun
_jam_in_ptr(u3_ch_root* har_u,
_jam_in_ptr(u3p(u3_ch_root) har_p,
u3_atom u_c,
u3_noun l)
{
@ -80,29 +80,29 @@
}
static u3_noun
_jam_in(u3_ch_root* har_u,
_jam_in(u3p(u3_ch_root) har_p,
u3_noun a,
u3_atom b,
u3_noun l)
{
u3_noun c = u3_ch_get(har_u, a);
u3_noun c = u3_ch_get(har_p, a);
u3_noun x;
if ( u3_none == c ) {
u3_ch_put(har_u, a, u3k(b));
u3_ch_put(har_p, a, u3k(b));
if ( u3_yes == u3ud(a) ) {
x = _jam_in_flat(har_u, a, l);
x = _jam_in_flat(har_p, a, l);
} else {
x = _jam_in_pair(har_u, u3h(a), u3t(a), b, l);
x = _jam_in_pair(har_p, u3h(a), u3t(a), b, l);
}
}
else {
if ( u3_yes == u3ud(a) && u3_cr_met(0, a) <= u3_cr_met(0, c) ) {
x = _jam_in_flat(har_u, a, l);
x = _jam_in_flat(har_p, a, l);
}
else {
x = _jam_in_ptr(har_u, c, l);
x = _jam_in_ptr(har_p, c, l);
}
}
return x;
@ -111,15 +111,15 @@
u3_noun
u3_cqe_jam(u3_atom a)
{
u3_ch_root* har_u = u3_ch_new();
u3p(u3_ch_root) har_p = u3_ch_new();
u3_noun x = _jam_in(har_u, a, 0, u3_nul);
u3_noun x = _jam_in(har_p, a, 0, u3_nul);
u3_noun q = u3_cqb_flop(u3h(u3t(x)));
u3_noun r = u3_cqc_can(0, q);
u3z(x);
u3z(q);
u3_ch_free(har_u);
u3_ch_free(har_p);
return r;
}
u3_noun