use formatter for •Show, fix some refcounts

This commit is contained in:
dzaima 2021-05-20 19:45:47 +03:00
parent e90701af9f
commit 2d60dd4f1a
5 changed files with 32 additions and 19 deletions

View File

@ -225,8 +225,8 @@ B indexOf_c1(B t, B x) {
i32 ctr = 0; i32 ctr = 0;
for (usz i = 0; i < xia; i++) { for (usz i = 0; i < xia; i++) {
bool had; u64 p = mk_b2i(&map, xgetU(x,i), &had); bool had; u64 p = mk_b2i(&map, xgetU(x,i), &had);
if (had) rp[i] = map->ent[p].val; if (had) rp[i] = map->a[p].val;
else rp[i] = map->ent[p].val = ctr++; else rp[i] = map->a[p].val = ctr++;
} }
free_b2i(map); free_b2i(map);
dec(x); dec(x);
@ -255,7 +255,7 @@ B memberOf_c2(B t, B w, B x) {
for (usz i = 0; i < xia; i++) mk_Sb(&set, xgetU(x,i), &had); for (usz i = 0; i < xia; i++) mk_Sb(&set, xgetU(x,i), &had);
i32* rp; B r = m_i32arrv(&rp, wia); i32* rp; B r = m_i32arrv(&rp, wia);
for (usz i = 0; i < wia; i++) rp[i] = has_Sb(set, wgetU(w,i)); for (usz i = 0; i < wia; i++) rp[i] = has_Sb(set, wgetU(w,i));
dec(w);dec(x); dec(w);dec(x); free_Sb(set);
return r; return r;
} }

View File

@ -41,17 +41,17 @@ typedef struct Map {
u64 pop; // count of defined entries u64 pop; // count of defined entries
u64 mask; // sz-1 u64 mask; // sz-1
u64 sz; // count of allocated entries, a power of 2 u64 sz; // count of allocated entries, a power of 2
Ent ent[]; Ent a[];
} Map; } Map;
Map* N(m) (u64 sz) { Map* N(m) (u64 sz) {
assert(sz && (sz & sz-1)==0); assert(sz && (sz & sz-1)==0);
Map* r = mm_allocN(fsizeof(Map,ent,Ent,sz), t_hashmap); Map* r = mm_allocN(fsizeof(Map,a,Ent,sz), t_hashmap);
#ifdef HDEF #ifdef HDEF
for (u64 i = 0; i < sz; i++) r->ent[i].hash = HDEF; for (u64 i = 0; i < sz; i++) r->a[i].hash = HDEF;
#endif #endif
#ifdef KDEF #ifdef KDEF
for (u64 i = 0; i < sz; i++) r->ent[i].key = KDEF; for (u64 i = 0; i < sz; i++) r->a[i].key = KDEF;
#endif #endif
r->sz = sz; r->sz = sz;
r->mask = sz-1; r->mask = sz-1;
@ -69,7 +69,7 @@ static inline u64 N(find) (Map* m, KT k, u64 h1, u64 h2, bool* had) {
u64 mask = m->mask; u64 mask = m->mask;
u64 p = h1 & mask; u64 p = h1 & mask;
while (true) { while (true) {
Ent e = m->ent[p]; Ent e = m->a[p];
if (e.hash==h2 IFKEY(&& LIKELY(EQUAL(e.key, k)))) return p; if (e.hash==h2 IFKEY(&& LIKELY(EQUAL(e.key, k)))) return p;
if (EMPTY(e.hash, e.key)) { *had=false; return p; } if (EMPTY(e.hash, e.key)) { *had=false; return p; }
if (RARE(p++==mask)) p = 0; if (RARE(p++==mask)) p = 0;
@ -87,11 +87,11 @@ static inline void N(qins) (Map* m, u64 h1, HT h2, KT k IFVAL(, VT v)) { // if g
u64 mask = m->mask; u64 mask = m->mask;
u64 p = h1 & mask; u64 p = h1 & mask;
while (true) { while (true) {
u64 ch2 = m->ent[p].hash; u64 ch2 = m->a[p].hash;
if (EMPTY(ch2, m->ent[p].key)) { if (EMPTY(ch2, m->a[p].key)) {
m->ent[p].hash = h2; m->a[p].hash = h2;
IFKEY(m->ent[p].key = k); IFKEY(m->a[p].key = k);
IFVAL(m->ent[p].val = v); IFVAL(m->a[p].val = v);
m->pop++; m->pop++;
return; return;
} }
@ -103,7 +103,7 @@ void N(dbl) (Map** m) {
u64 psz = pm->sz; u64 psz = pm->sz;
Map* nm = N(m)(psz*2); Map* nm = N(m)(psz*2);
for (u64 i = 0; i < psz; i++) { for (u64 i = 0; i < psz; i++) {
Ent e = pm->ent[i]; Ent e = pm->a[i];
if(!EMPTY(e.hash, e.key)) N(qins)(nm, H1R(e.key, e.hash), e.hash, e.key IFVAL(, e.val)); if(!EMPTY(e.hash, e.key)) N(qins)(nm, H1R(e.key, e.hash), e.hash, e.key IFVAL(, e.val));
} }
mm_free((Value*)pm); mm_free((Value*)pm);
@ -117,15 +117,15 @@ static inline u64 N(mk) (Map** mp, KT k, bool* had) {
u64 h1 = H1(k); u64 h2 = H2(k, h1); u64 h1 = H1(k); u64 h2 = H2(k, h1);
u64 p = N(find)(m, k, h1, h2, had); u64 p = N(find)(m, k, h1, h2, had);
if (*had) return p; if (*had) return p;
m->ent[p].hash = h2; m->a[p].hash = h2;
IFKEY(m->ent[p].key = k); IFKEY(m->a[p].key = k);
m->pop++; m->pop++;
return p; return p;
} }
#ifdef VALS #ifdef VALS
static inline bool N(ins) (Map** mp, KT k, VT v) { // returns whether element was replaced static inline bool N(ins) (Map** mp, KT k, VT v) { // returns whether element was replaced
bool had; u64 p = N(mk)(mp, k, &had); bool had; u64 p = N(mk)(mp, k, &had);
(*mp)->ent[p].val = v; (*mp)->a[p].val = v;
return had; return had;
} }
#endif #endif

View File

@ -182,6 +182,7 @@ static inline void load_init() {
load_fmt = fget(fmtR, 0); gc_add(load_fmt); load_fmt = fget(fmtR, 0); gc_add(load_fmt);
load_repr = fget(fmtR, 1); gc_add(load_repr); load_repr = fget(fmtR, 1); gc_add(load_repr);
dec(fmtR); dec(fmtR);
dec(fmtM);
#endif #endif
gc_enable(); gc_enable();
#endif // NO_COMP #endif // NO_COMP

View File

@ -200,7 +200,7 @@ int main(int argc, char* argv[]) {
#ifdef FORMATTER #ifdef FORMATTER
B resFmt = bqn_fmt(res); B resFmt = bqn_fmt(res);
printRaw(resFmt); dec(resFmt); printRaw(resFmt); dec(resFmt);
printf("\n"); putchar('\n');
#else #else
print(res); putchar('\n'); fflush(stdout); print(res); putchar('\n'); fflush(stdout);
dec(res); dec(res);
@ -213,6 +213,9 @@ int main(int argc, char* argv[]) {
} }
popCatch(); popCatch();
} }
#ifdef HEAP_VERIFY
heapVerify();
#endif
rtPerf_print(); rtPerf_print();
CTR_FOR(CTR_PRINT) CTR_FOR(CTR_PRINT)
// printf("done\n");fflush(stdout); while(1); // printf("done\n");fflush(stdout); while(1);

View File

@ -143,7 +143,16 @@ B internal_c2(B t, B w, B x) {
} }
B sys_c1(B t, B x); B sys_c1(B t, B x);
B out_c1(B t, B x) { printRaw(x); putchar('\n'); return x; } B out_c1(B t, B x) { printRaw(x); putchar('\n'); return x; }
B show_c1(B t, B x) { print (x); putchar('\n'); return x; } B show_c1(B t, B x) {
#ifdef FORMATTER
B fmt = bqn_fmt(inc(x));
printRaw(fmt); dec(fmt);
#else
print(x);
#endif
putchar('\n');
return x;
}
B bqn_c1(B t, B x) { B bqn_c1(B t, B x) {
if (isAtm(x) || rnk(x)!=1) thrM("•BQN: Argument must be a character vector"); if (isAtm(x) || rnk(x)!=1) thrM("•BQN: Argument must be a character vector");