rename unit creation methods

This commit is contained in:
dzaima 2023-04-29 15:47:45 +03:00
parent 88c19cedec
commit 3b580cbd0c
16 changed files with 59 additions and 57 deletions

View File

@ -159,7 +159,7 @@ TRIG(sin,Sin) TRIG(cos,Cos) TRIG(tan,Tan)
#undef TRIG
#undef MATH
B lt_c1(B t, B x) { return m_atomUnit(x); }
B lt_c1(B t, B x) { return m_unit(x); }
B eq_c1(B t, B x) { if (isAtm(x)) { decA(x); return m_i32(0); } B r = m_i32(RNK(x)); decG(x); return r; }
B ne_c1(B t, B x) { if (isAtm(x)) { decA(x); return m_i32(1); } B r = m_f64(*SH(x)); decG(x); return r; }

View File

@ -275,8 +275,8 @@ static ur cell_rank(f64 r, f64 k) { // ⎉k over arg rank r
static NOINLINE B c1wrap(B f, B x) { B r = c1(f, x); return isAtm(r)? m_atomUnit(r) : r; }
static NOINLINE B c2wrap(B f, B w, B x) { B r = c2(f, w, x); return isAtm(r)? m_atomUnit(r) : r; }
static NOINLINE B c1wrap(B f, B x) { B r = c1(f, x); return isAtm(r)? m_unit(r) : r; }
static NOINLINE B c2wrap(B f, B w, B x) { B r = c2(f, w, x); return isAtm(r)? m_unit(r) : r; }
// monadic ˘ & ⎉
B for_cells_c1(B f, u32 xr, u32 cr, u32 k, B x, u32 chr) { // F⎉cr x, with array x, xr>0, 0≤cr<xr, k≡xr-cr
assert(isArr(x) && xr>0 && k>0 && cr<xr);
@ -287,7 +287,7 @@ B for_cells_c1(B f, u32 xr, u32 cr, u32 k, B x, u32 chr) { // F⎉cr x, with arr
switch(rtid) {
case n_lt:
if (cam==0) goto noCells; // toCells/toKCells don't set outer array fill
return k==1 && RNK(x)>1? toCells(x) : k==0? m_atomUnit(x) : toKCells(x, k);
return k==1 && RNK(x)>1? toCells(x) : k==0? m_unit(x) : toKCells(x, k);
case n_select:
if (IA(x)==0) goto noSpecial;
if (cr==0) goto base;

View File

@ -69,7 +69,7 @@ B ud_c1(B t, B x) {
usz xia = IA(x);
if (RNK(x)!=1) thrF("↕: Argument must be either an integer or integer list (had rank %i)", RNK(x));
if (xia>UR_MAX) thrF("↕: Result rank too large (%s≡≠𝕩)", xia);
if (xia==0) { decG(x); return m_unit(emptyIVec()); }
if (xia==0) { decG(x); return m_funit(emptyIVec()); }
usz sh[xia]; // stack allocation of rank items
i32 pos[xia];
usz ria = 1;
@ -106,7 +106,7 @@ B ud_c2(B t, B w, B x) {
if (isArr(w)) {
if (RNK(w)>1) thrM("↕: 𝕨 must have rank at most 1");
wia = IA(w);
if (wia==0) { decG(w); return isArr(x)? x : m_atomUnit(x); }
if (wia==0) { decG(w); return isArr(x)? x : m_unit(x); }
}
ur xr;
if (isAtm(x) || (xr=RNK(x))<wia) thrM("↕: Length of 𝕨 must be at most rank of 𝕩");

View File

@ -337,7 +337,7 @@ B insert_c1(Md1D* d, B x) { B f = d->f;
if (xia==0) { SLOW2("!𝕎˝𝕩", f, x); return m1c1(rt_insert, f, x); }
if (isFun(f)) {
u8 rtid = v(f)->flags-1;
if (RNK(x)==1 && isPervasiveDyExt(f)) return m_atomUnit(fold_c1(d, x));
if (RNK(x)==1 && isPervasiveDyExt(f)) return m_unit(fold_c1(d, x));
if (rtid == n_join) {
ur xr = RNK(x);
if (xr==1) return x;
@ -365,7 +365,7 @@ B insert_c2(Md1D* d, B w, B x) { B f = d->f;
if (isFun(f)) {
if (RNK(x)==1 && isPervasiveDyExt(f)) {
if (isAtm(w)) {
to_fold: return m_atomUnit(fold_c2(d, w, x));
to_fold: return m_unit(fold_c2(d, w, x));
}
if (RNK(w)==0) {
B w0=w; w = IGet(w,0); decG(w0);

View File

@ -328,7 +328,7 @@ B GRADE_CAT(c1)(B t, B x) {
B GRADE_CAT(c2)(B t, B w, B x) {
if (isAtm(w) || RNK(w)==0) thrM(GRADE_CHR": 𝕨 must have rank≥1");
if (isAtm(x)) x = m_atomUnit(x);
if (isAtm(x)) x = m_unit(x);
ur wr = RNK(w);
ur xr = RNK(x);

View File

@ -61,8 +61,8 @@ B tbl_c1(Md1D* d, B x) {
B slash_c2(B t, B w, B x);
B shape_c2(B t, B w, B x);
B tbl_c2(Md1D* d, B w, B x) { B f = d->f;
if (isAtm(w)) w = m_atomUnit(w);
if (isAtm(x)) x = m_atomUnit(x);
if (isAtm(w)) w = m_unit(w);
if (isAtm(x)) x = m_unit(x);
ur wr = RNK(w); usz wia = IA(w);
ur xr = RNK(x); usz xia = IA(x);
ur rr = wr+xr; usz ria = uszMul(wia, xia);

View File

@ -98,7 +98,7 @@ static NOINLINE B2 splitCells(B n, B p, u8 mode) { // 0:∊ 1:⊐ 2:⊒
#define ARG_P (mode? "𝕨" : "𝕩")
if (isAtm(p) || RNK(p)==0) thrF("%U: %U cannot have rank 0", SYMB, ARG_P);
ur pr = RNK(p);
if (isAtm(n)) n = m_atomUnit(n);
if (isAtm(n)) n = m_unit(n);
ur nr = RNK(n);
if (nr < pr-1) thrF("%U: Rank of %U must be at least the cell rank of %U (%H ≡ ≢𝕨, %H ≡ ≢𝕩)", SYMB, ARG_N, ARG_P, mode?p:n, mode?n:p);
ur pcr = pr-1;

View File

@ -118,7 +118,7 @@ B select_c2(B t, B w, B x) {
B xv = IGet(x, wi);
B rb;
if (isNum(xf) || isC32(xf)) {
rb = m_atomUnit(xv);
rb = m_unit(xv);
} else if (noFill(xf)) {
rb = m_hunit(xv);
} else {

View File

@ -35,28 +35,6 @@ static Arr* take_impl(usz ria, B x) { // consumes x; returns v↑⥊𝕩 without
}
}
B m_vec1(B a) {
if (isF64(a)) {
i32 i = (i32)a.f;
if (RARE(a.f != i)) { f64* rp; B r = m_f64arrv(&rp, 1); rp[0] = a.f; return r; }
else if (q_ibit(i)) { u64* rp; B r = m_bitarrv(&rp, 1); rp[0] = i; return r; }
else if (i == (i8 )i) { i8* rp; B r = m_i8arrv (&rp, 1); rp[0] = i; return r; }
else if (i == (i16)i) { i16* rp; B r = m_i16arrv(&rp, 1); rp[0] = i; return r; }
else { i32* rp; B r = m_i32arrv(&rp, 1); rp[0] = i; return r; }
}
if (isC32(a)) {
u32 c = o2cG(a);
if (LIKELY(c<U8_MAX )) { u8* rp; B r = m_c8arrv (&rp, 1); rp[0] = c; return r; }
else if (LIKELY(c<U16_MAX)) { u16* rp; B r = m_c16arrv(&rp, 1); rp[0] = c; return r; }
else { u32* rp; B r = m_c32arrv(&rp, 1); rp[0] = c; return r; }
}
Arr* ra = arr_shVec(m_fillarrp(1));
fillarr_ptr(ra)[0] = a;
fillarr_setFill(ra, m_f64(0));
NOGC_E;
fillarr_setFill(ra, asFill(inc(a)));
return taga(ra);
}
FORCE_INLINE B m_vec2Base(B a, B b, bool fills) {
if (isAtm(a)&isAtm(b)) {
@ -156,7 +134,7 @@ B shape_c2(B t, B w, B x) {
nr = 1;
sh = NULL;
} else {
if (isAtm(w)) w = m_atomUnit(w);
if (isAtm(w)) w = m_unit(w);
if (RNK(w)>1) thrM("⥊: 𝕨 must have rank at most 1");
if (IA(w)>UR_MAX) thrM("⥊: Result rank too large");
nr = IA(w);
@ -214,7 +192,7 @@ B shape_c2(B t, B w, B x) {
if (sh) sh->a[unkPos] = item;
nia = uszMul(nia, item);
if (fill) {
if (!isArr(x)) x = m_atomUnit(x);
if (!isArr(x)) x = m_unit(x);
x = taga(arr_shVec(take_impl(nia, x)));
xia = nia;
}
@ -410,7 +388,7 @@ static B recPick(B w, B x) { // doesn't consume
B pick_c2(B t, B w, B x) {
if (RARE(isAtm(x))) {
if (isArr(w) && RNK(w)==1 && IA(w)==0) { dec(w); return x; }
x = m_atomUnit(x);
x = m_unit(x);
}
if (isNum(w)) {
if (RNK(x)!=1) thrF("⊑: 𝕩 must be a list when 𝕨 is a number (%H ≡ ≢𝕩)", x);
@ -625,7 +603,7 @@ NOINLINE B takedrop_highrank(bool take, B w, B x) {
}
#define TAKEDROP_INIT(TAKE) \
if (!isArr(x)) x = m_atomUnit(x); \
if (!isArr(x)) x = m_unit(x); \
if (!isNum(w)) return takedrop_highrank(TAKE, w, x); \
Arr* a; \
i64 wv = o2i64(w); \
@ -910,11 +888,11 @@ B join_c1(B t, B x) {
}
}
B join_c2(B t, B w, B x) {
if (isAtm(w)) w = m_atomUnit(w);
if (isAtm(w)) w = m_unit(w);
ur wr = RNK(w);
if (isAtm(x)) {
if (wr==1 && inplace_add(w, x)) return w;
x = m_atomUnit(x);
x = m_unit(x);
}
ur xr = RNK(x);
B f = fill_both(w, x);
@ -985,8 +963,8 @@ B couple_c1(B t, B x) {
}
B couple_c2(B t, B w, B x) {
if (isAtm(w)&isAtm(x)) return m_vec2(w, x);
if (isAtm(w)) w = m_atomUnit(w);
if (isAtm(x)) x = m_atomUnit(x);
if (isAtm(w)) w = m_unit(w);
if (isAtm(x)) x = m_unit(x);
if (!eqShape(w, x)) thrF("≍: 𝕨 and 𝕩 must have equal shapes (%H ≡ ≢𝕨, %H ≡ ≢𝕩)", w, x);
usz ia = IA(w);
ur wr = RNK(w);
@ -1025,7 +1003,7 @@ B shiftb_c1(B t, B x) {
}
B shiftb_c2(B t, B w, B x) {
if (isAtm(x) || RNK(x)==0) thrM("»: 𝕩 cannot be a scalar");
if (isAtm(w)) w = m_atomUnit(w);
if (isAtm(w)) w = m_unit(w);
shift_check(w, x);
B f = fill_both(w, x);
usz wia = IA(w);
@ -1051,7 +1029,7 @@ B shifta_c1(B t, B x) {
}
B shifta_c2(B t, B w, B x) {
if (isAtm(x) || RNK(x)==0) thrM("«: 𝕩 cannot be a scalar");
if (isAtm(w)) w = m_atomUnit(w);
if (isAtm(w)) w = m_unit(w);
shift_check(w, x);
B f = fill_both(w, x);
usz wia = IA(w);
@ -1378,7 +1356,7 @@ NOINLINE B enclose_im(B t, B x) {
return r;
}
B enclose_uc1(B t, B o, B x) {
return enclose_im(t, c1(o, m_atomUnit(x)));
return enclose_im(t, c1(o, m_unit(x)));
}
void sfns_init(void) {

View File

@ -641,7 +641,7 @@ B slash_c2(B t, B w, B x) {
if (wr>1) thrF("/: Simple 𝕨 must have rank 0 or 1 (%i≡=𝕨)", wr);
if (wr<1) { B v=IGet(w, 0); decG(w); w=v; goto atom; }
wia = IA(w);
if (wia==0) { decG(w); return isArr(x)? x : m_atomUnit(x); }
if (wia==0) { decG(w); return isArr(x)? x : m_unit(x); }
} else {
atom:
if (!q_i32(w)) goto base;

View File

@ -1382,8 +1382,8 @@ B bitop2(B f, B w, B x, enum BitOp2 op, char* name) {
ow = t[0]; rw = t[1]; xw = t[2]; ww = t[3];
}
if (isAtm(x)) x = m_atomUnit(x);
if (isAtm(w)) w = m_atomUnit(w);
if (isAtm(x)) x = m_unit(x);
if (isAtm(w)) w = m_unit(w);
ur wr=RNK(w); usz* wsh = SH(w); u64 s = wr==0? ww : ww*(u64)wsh[wr-1];
ur xr=RNK(x); usz* sh = SH(x); u64 t = xr==0? xw : xw*(u64) sh[xr-1];
bool negw = 0; // Negate 𝕨 to subtract from 𝕩

View File

@ -124,7 +124,7 @@ static Arr* transpose_noshape(B* px, usz ia, usz w, usz h) {
}
B transp_c1(B t, B x) {
if (RARE(isAtm(x))) return m_atomUnit(x);
if (RARE(isAtm(x))) return m_unit(x);
ur xr = RNK(x);
if (xr<=1) return x;
@ -164,7 +164,7 @@ B transp_c2(B t, B w, B x) {
if (isArr(w)) {
if (RNK(w)>1) thrM("⍉: 𝕨 must have rank at most 1");
wia = IA(w);
if (wia==0) { decG(w); return isArr(x)? x : m_atomUnit(x); }
if (wia==0) { decG(w); return isArr(x)? x : m_unit(x); }
}
ur xr;
if (isAtm(x) || (xr=RNK(x))<wia) thrM("⍉: Length of 𝕨 must be at most rank of 𝕩");

View File

@ -128,7 +128,7 @@ static B md1D_identity(B t) {
Md1D* d = c(Md1D, t);
if (d->m1==c(Md1,bi_tbl)) {
B i = TI(d->f,identity)(d->f);
return q_N(i)? i : m_atomUnit(i);
return q_N(i)? i : m_unit(i);
}
return bi_N;
}

View File

@ -161,7 +161,7 @@ B withFill(B x, B fill) { // consumes both
}
NOINLINE B m_unit(B x) {
NOINLINE B m_funit(B x) {
B xf = asFill(inc(x));
if (noFill(xf)) return m_hunit(x);
FillArr* r = m_arr(fsizeof(FillArr,a,B,1), t_fillarr, 1);
@ -170,7 +170,8 @@ NOINLINE B m_unit(B x) {
r->a[0] = x;
return taga(r);
}
NOINLINE B m_atomUnit(B x) {
NOINLINE B m_unit(B x) {
u64 data; assert(sizeof(f64)<=8);
u8 t; u64 sz;
if (isNum(x)) {
@ -185,10 +186,33 @@ NOINLINE B m_atomUnit(B x) {
if (xi==(u8 )xi) { u8 v=xi; memcpy(&data, &v, sz=sizeof(v)); t=t_c8arr; }
else if (xi==(u16)xi) { u16 v=xi; memcpy(&data, &v, sz=sizeof(v)); t=t_c16arr; }
else { u32 v=xi; memcpy(&data, &v, sz=sizeof(v)); t=t_c32arr; }
} else return m_unit(x);
} else return m_funit(x);
TyArr* r = m_arr(offsetof(TyArr,a) + sizeof(u64), t, 1);
*((u64*)r->a) = data;
FINISH_OVERALLOC(r, offsetof(TyArr,a)+sz, offsetof(TyArr,a)+sizeof(u64));
arr_shAtm((Arr*)r);
return taga(r);
}
NOINLINE B m_vec1(B a) {
if (isF64(a)) {
i32 i = (i32)a.f;
if (RARE(a.f != i)) { f64* rp; B r = m_f64arrv(&rp, 1); rp[0] = a.f; return r; }
else if (q_ibit(i)) { u64* rp; B r = m_bitarrv(&rp, 1); rp[0] = i; return r; }
else if (i == (i8 )i) { i8* rp; B r = m_i8arrv (&rp, 1); rp[0] = i; return r; }
else if (i == (i16)i) { i16* rp; B r = m_i16arrv(&rp, 1); rp[0] = i; return r; }
else { i32* rp; B r = m_i32arrv(&rp, 1); rp[0] = i; return r; }
}
if (isC32(a)) {
u32 c = o2cG(a);
if (LIKELY(c<U8_MAX )) { u8* rp; B r = m_c8arrv (&rp, 1); rp[0] = c; return r; }
else if (LIKELY(c<U16_MAX)) { u16* rp; B r = m_c16arrv(&rp, 1); rp[0] = c; return r; }
else { u32* rp; B r = m_c32arrv(&rp, 1); rp[0] = c; return r; }
}
Arr* ra = arr_shVec(m_fillarrp(1));
fillarr_ptr(ra)[0] = a;
fillarr_setFill(ra, m_f64(0));
NOGC_E;
fillarr_setFill(ra, asFill(inc(a)));
return taga(ra);
}

View File

@ -98,8 +98,8 @@ static Arr* m_fillarr0p(usz ia) { // zero-initialized fillarr, with both fill &
return r;
}
B m_funit(B x); // consumes
B m_unit(B x); // consumes
B m_atomUnit(B x); // consumes
static bool fillEqualsGetFill(B fill, B obj) { // returns whether `fill` equals the fill of `obj`
return fillEqual(fill, getFillN(obj));

View File

@ -11,7 +11,7 @@ B eachd_fn(B fo, B w, B x, FC2 f) {
bool wg = wr>xr;
ur rM = wg? wr : xr;
ur rm = wg? xr : wr;
if (rM==0) return m_unit(f(fo, w, x));
if (rM==0) return m_funit(f(fo, w, x));
if (rm && !eqShPart(SH(w), SH(x), rm)) thrF("Mapping: Expected equal shape prefix (%H ≡ ≢𝕨, %H ≡ ≢𝕩)", w, x);
bool rw = rM==wr && reusable(w) && TY(w)==t_harr; // dereferencing is safe as rank>0 from rM==
bool rx = rM==xr && reusable(x) && TY(x)==t_harr;