simpler batch function calls

This commit is contained in:
dzaima 2021-05-02 21:46:28 +03:00
parent c9eab08569
commit 5d0342e647
3 changed files with 25 additions and 23 deletions

14
src/h.h
View File

@ -428,17 +428,17 @@ typedef struct Fun {
BBB2B c2;
} Fun;
NOINLINE B c1_invalid(B f, B x) { thrM("This function can't be called monadically"); }
NOINLINE B c2_invalid(B f, B w, B x) { thrM("This function can't be called dyadically"); }
NOINLINE B c1_rare(B f, B x) { dec(x);
B c1_rare(B f, B x) { dec(x);
if (isMd(f)) thrM("Calling a modifier");
return inc(VALIDATE(f));
}
NOINLINE B c2_rare(B f, B w, B x) { dec(w); dec(x);
B c2_rare(B f, B w, B x) { dec(w); dec(x);
if (isMd(f)) thrM("Calling a modifier");
return inc(VALIDATE(f));
}
BB2B c1fn(B f);
BBB2B c2fn(B f);
B c1(B f, B x) { // BQN-call f monadically; consumes x
if (isFun(f)) return VALIDATE(c(Fun,f)->c1(f, x));
return c1_rare(f, x);
@ -447,12 +447,6 @@ B c2(B f, B w, B x) { // BQN-call f dyadically; consumes w,x
if (isFun(f)) return VALIDATE(c(Fun,f)->c2(f, w, x));
return c2_rare(f, w, x);
}
B c1_modifier(B f, B w, B x) {
dec(w); dec(x);
thrM("Calling a modifier");
}
BB2B c1fn(B f);
BBB2B c2fn(B f);
typedef struct Md1 {

View File

@ -62,12 +62,14 @@ B tbl_c2(B d, B w, B x) { B f = c(Md1D,d)->f;
BS2B wgetU = TI(w).getU;
BS2B xget = TI(x).get;
BBB2B fc2 = c2fn(f);
usz ri = 0;
HArr_p r = m_harrs(ria, &ri);
for (usz wi = 0; wi < wia; wi++) {
B cw = wgetU(w,wi);
for (usz xi = 0; xi < xia; xi++,ri++) {
r.a[ri] = c2(f, inc(cw), xget(x,xi));
r.a[ri] = fc2(f, inc(cw), xget(x,xi));
}
}
usz* rsh = harr_fa(r, rr);
@ -104,14 +106,15 @@ B scan_c1(B d, B x) { B f = c(Md1D,d)->f;
usz i = 0;
HArr_p r = reuse? harr_parts(x) : m_harrs(a(x)->ia, &i);
BS2B xget = reuse? TI(x).getU : TI(x).get;
BBB2B fc2 = c2fn(f);
if (xr==1) {
r.a[i] = xget(x,0); i++;
for (i = 1; i < ia; i++) r.a[i] = c2(f, inc(r.a[i-1]), xget(x,i));
for (i = 1; i < ia; i++) r.a[i] = fc2(f, inc(r.a[i-1]), xget(x,i));
} else {
usz csz = arr_csz(x);
for (; i < csz; i++) r.a[i] = xget(x,i);
for (; i < ia; i++) r.a[i] = c2(f, inc(r.a[i-csz]), xget(x,i));
for (; i < ia; i++) r.a[i] = fc2(f, inc(r.a[i-csz]), xget(x,i));
}
return withFill(reuse? x : harr_fcd(r, x), xf);
}
@ -123,6 +126,7 @@ B scan_c2(B d, B w, B x) { B f = c(Md1D,d)->f;
usz i = 0;
HArr_p r = reuse? harr_parts(x) : m_harrs(a(x)->ia, &i);
BS2B xget = reuse? TI(x).getU : TI(x).get;
BBB2B fc2 = c2fn(f);
if (isArr(w)) {
ur wr = rnk(w); usz* wsh = a(w)->sh; BS2B wget = TI(w).get;
@ -130,14 +134,14 @@ B scan_c2(B d, B w, B x) { B f = c(Md1D,d)->f;
if (memcmp(wsh, xsh+1, wr)) thrM("`: Shape of 𝕨 must match the cell of 𝕩");
if (ia==0) return x;
usz csz = arr_csz(x);
for (; i < csz; i++) r.a[i] = c2(f, wget(w,i), xget(x,i));
for (; i < ia; i++) r.a[i] = c2(f, inc(r.a[i-csz]), xget(x,i));
for (; i < csz; i++) r.a[i] = fc2(f, wget(w,i), xget(x,i));
for (; i < ia; i++) r.a[i] = fc2(f, inc(r.a[i-csz]), xget(x,i));
dec(w);
} else {
if (xr!=1) thrM("`: Shape of 𝕨 must match the cell of 𝕩");
if (ia==0) return x;
B pr = r.a[0] = c2(f, w, xget(x,0)); i++;
for (; i < ia; i++) r.a[i] = pr = c2(f, inc(pr), xget(x,i));
B pr = r.a[0] = fc2(f, w, xget(x,0)); i++;
for (; i < ia; i++) r.a[i] = pr = fc2(f, inc(pr), xget(x,i));
}
return withFill(reuse? x : harr_fcd(r, x), wf);
}
@ -155,7 +159,8 @@ B fold_c1(B d, B x) { B f = c(Md1D,d)->f;
}
BS2B xget = TI(x).get;
B c = xget(x, ia-1);
for (usz i = ia-1; i>0; i--) c = c2(f, xget(x, i-1), c);
BBB2B fc2 = c2fn(f);
for (usz i = ia-1; i>0; i--) c = fc2(f, xget(x, i-1), c);
dec(x);
return c;
}
@ -164,7 +169,8 @@ B fold_c2(B d, B w, B x) { B f = c(Md1D,d)->f;
usz ia = a(x)->ia;
B c = w;
BS2B xget = TI(x).get;
for (usz i = ia; i>0; i--) c = c2(f, xget(x, i-1), c);
BBB2B fc2 = c2fn(f);
for (usz i = ia; i>0; i--) c = fc2(f, xget(x, i-1), c);
dec(x);
return c;
}

View File

@ -209,10 +209,12 @@ static inline void hdr_init() {
assert((MD1_TAG>>1) == (MD2_TAG>>1)); // just to be sure it isn't changed incorrectly, `isMd` depends on this
}
B md_c1(B t, B x) { thrM("Cannot call a modifier"); }
B md_c2(B t, B w, B x) { thrM("Cannot call a modifier"); }
B arr_c1(B t, B x) { return inc(t); }
B arr_c2(B t, B w, B x) { return inc(t); }
static NOINLINE B c1_invalid(B f, B x) { thrM("This function can't be called monadically"); }
static NOINLINE B c2_invalid(B f, B w, B x) { thrM("This function can't be called dyadically"); }
static B md_c1(B t, B x) { thrM("Cannot call a modifier"); }
static B md_c2(B t, B w, B x) { thrM("Cannot call a modifier"); }
static B arr_c1(B t, B x) { return inc(t); }
static B arr_c2(B t, B w, B x) { return inc(t); }
BB2B c1fn(B f) {
if (isFun(f)) return c(Fun,f)->c1;
if (isMd(f)) return md_c1;