native vector ⊏ & /
This commit is contained in:
parent
3891940178
commit
b5468f630b
@ -77,5 +77,6 @@ static inline void c32arr_init() {
|
|||||||
ti[t_c32arr].visit = do_nothing; ti[t_c32slice].visit = slice_visit;
|
ti[t_c32arr].visit = do_nothing; ti[t_c32slice].visit = slice_visit;
|
||||||
ti[t_c32arr].print = arr_print; ti[t_c32slice].print = arr_print;
|
ti[t_c32arr].print = arr_print; ti[t_c32slice].print = arr_print;
|
||||||
ti[t_c32arr].isArr = true; ti[t_c32slice].isArr = true;
|
ti[t_c32arr].isArr = true; ti[t_c32slice].isArr = true;
|
||||||
|
ti[t_i32arr].arrD1 = true; ti[t_i32slice].arrD1 = true;
|
||||||
ti[t_c32arr].canStore = c32arr_canStore;
|
ti[t_c32arr].canStore = c32arr_canStore;
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/h.h
17
src/h.h
@ -98,7 +98,7 @@ char* format_type(u8 u) {
|
|||||||
|
|
||||||
#define FOR_PF(F) F(none, "(unknown fn)") \
|
#define FOR_PF(F) F(none, "(unknown fn)") \
|
||||||
F(add,"+") F(sub,"-") F(mul,"×") F(div,"÷") F(pow,"⋆") F(floor,"⌊") F(ceil,"⌈") F(stile,"|") F(eq,"=") F(ne,"≠") F(le,"≤") F(ge,"≥") F(lt,"<") F(gt,">") F(and,"∧") F(or,"∨") F(not,"¬") F(log,"⋆⁼") /*arith.c*/ \
|
F(add,"+") F(sub,"-") F(mul,"×") F(div,"÷") F(pow,"⋆") F(floor,"⌊") F(ceil,"⌈") F(stile,"|") F(eq,"=") F(ne,"≠") F(le,"≤") F(ge,"≥") F(lt,"<") F(gt,">") F(and,"∧") F(or,"∨") F(not,"¬") F(log,"⋆⁼") /*arith.c*/ \
|
||||||
F(shape,"⥊") F(pick,"⊑") F(ud,"↕") F(pair,"{𝕨‿𝕩}") F(fne,"≢") F(feq,"≡") F(ltack,"⊣") F(rtack,"⊢") F(fmtF,"⍕") F(fmtN,"⍕") /*sfns.c*/ \
|
F(shape,"⥊") F(pick,"⊑") F(ud,"↕") F(pair,"{𝕨‿𝕩}") F(fne,"≢") F(feq,"≡") F(select,"⊏") F(slash,"/") F(ltack,"⊣") F(rtack,"⊢") F(fmtF,"⍕") F(fmtN,"⍕") /*sfns.c*/ \
|
||||||
F(fork,"(fork)") F(atop,"(atop)") F(md1d,"(derived 1-modifier)") F(md2d,"(derived 2-modifier)") /*derv.c*/ \
|
F(fork,"(fork)") F(atop,"(atop)") F(md1d,"(derived 1-modifier)") F(md2d,"(derived 2-modifier)") /*derv.c*/ \
|
||||||
F(type,"•Type") F(decp,"•Decompose") F(primInd,"•PrimInd") F(glyph,"•Glyph") F(fill,"•FillFn") /*sysfn.c*/ \
|
F(type,"•Type") F(decp,"•Decompose") F(primInd,"•PrimInd") F(glyph,"•Glyph") F(fill,"•FillFn") /*sysfn.c*/ \
|
||||||
F(grLen,"•GroupLen") F(grOrd,"•groupOrd") F(asrt,"!") F(sys,"•getsys") F(internal,"•Internal") /*sysfn.c*/
|
F(grLen,"•GroupLen") F(grOrd,"•groupOrd") F(asrt,"!") F(sys,"•getsys") F(internal,"•Internal") /*sysfn.c*/
|
||||||
@ -127,12 +127,12 @@ char* format_pm1(u8 u) {
|
|||||||
}
|
}
|
||||||
enum PrimMd2 {
|
enum PrimMd2 {
|
||||||
pm2_none,
|
pm2_none,
|
||||||
pm2_val, pm2_repeat, pm2_fillBy, pm2_catch, // md2.c
|
pm2_val, pm2_before, pm2_repeat, pm2_fillBy, pm2_catch, // md2.c
|
||||||
};
|
};
|
||||||
char* format_pm2(u8 u) {
|
char* format_pm2(u8 u) {
|
||||||
switch(u) {
|
switch(u) {
|
||||||
default: case pf_none: return"(unknown 1-modifier)";
|
default: case pf_none: return"(unknown 1-modifier)";
|
||||||
case pm2_val: return"⊘"; case pm2_repeat: return"⍟"; case pm2_fillBy: return"•_fillBy_"; case pm2_catch: return"⎊";
|
case pm2_val: return"⊘"; case pm2_before: return"⊸"; case pm2_repeat: return"⍟"; case pm2_fillBy: return"•_fillBy_"; case pm2_catch: return"⎊";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,13 +276,19 @@ void arr_shVec(B x, usz ia) {
|
|||||||
a(x)->sh = &a(x)->ia;
|
a(x)->sh = &a(x)->ia;
|
||||||
}
|
}
|
||||||
bool gotShape[t_COUNT];
|
bool gotShape[t_COUNT];
|
||||||
usz* arr_shAlloc(B x, usz ia, usz r) {
|
usz* arr_shAlloc(B x, usz ia, ur r) {
|
||||||
a(x)->ia = ia;
|
a(x)->ia = ia;
|
||||||
srnk(x,r);
|
srnk(x,r);
|
||||||
if (r>1) return a(x)->sh = ((ShArr*)mm_allocN(fsizeof(ShArr, a, usz, r), t_shape))->a;
|
if (r>1) return a(x)->sh = ((ShArr*)mm_allocN(fsizeof(ShArr, a, usz, r), t_shape))->a;
|
||||||
a(x)->sh = &a(x)->ia;
|
a(x)->sh = &a(x)->ia;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
usz* arr_shAllocR(B x, ur r) { // allocates shape, leaves ia unchanged
|
||||||
|
srnk(x,r);
|
||||||
|
if (r>1) return a(x)->sh = ((ShArr*)mm_allocN(fsizeof(ShArr, a, usz, r), t_shape))->a;
|
||||||
|
a(x)->sh = &a(x)->ia;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
void arr_shCopy(B n, B o) { // copy shape from o to n
|
void arr_shCopy(B n, B o) { // copy shape from o to n
|
||||||
assert(isArr(o));
|
assert(isArr(o));
|
||||||
a(n)->ia = a(o)->ia;
|
a(n)->ia = a(o)->ia;
|
||||||
@ -338,6 +344,7 @@ B m_usz(usz n) { return n==(i32)n? m_i32(n) : m_f64(n); }
|
|||||||
i32 o2i (B x) { if ((i32)x.f!=x.f) thrM("Expected integer"); return (i32)x.f; }
|
i32 o2i (B x) { if ((i32)x.f!=x.f) thrM("Expected integer"); return (i32)x.f; }
|
||||||
usz o2s (B x) { if ((usz)x.f!=x.f) thrM("Expected integer"); return (usz)x.f; }
|
usz o2s (B x) { if ((usz)x.f!=x.f) thrM("Expected integer"); return (usz)x.f; }
|
||||||
i64 o2i64 (B x) { if ((i64)x.f!=x.f) thrM("Expected integer"); return (i64)x.f; }
|
i64 o2i64 (B x) { if ((i64)x.f!=x.f) thrM("Expected integer"); return (i64)x.f; }
|
||||||
|
f64 o2f (B x) { if (!isNum(x)) thrM("Expected integer"); return x.f; }
|
||||||
i32 o2iu (B x) { return isI32(x)? (i32)(u32)x.u : (i32)x.f; }
|
i32 o2iu (B x) { return isI32(x)? (i32)(u32)x.u : (i32)x.f; }
|
||||||
i64 o2i64u(B x) { return (i64)x.f; }
|
i64 o2i64u(B x) { return (i64)x.f; }
|
||||||
bool q_i32(B x) { return isI32(x) || isF64(x)&(x.f==(i32)x.f); }
|
bool q_i32(B x) { return isI32(x) || isF64(x)&(x.f==(i32)x.f); }
|
||||||
@ -377,6 +384,7 @@ typedef struct TypeInfo {
|
|||||||
B2v visit; // call mm_visit for all referents
|
B2v visit; // call mm_visit for all referents
|
||||||
B2B decompose; // consumes; must return a HArr
|
B2B decompose; // consumes; must return a HArr
|
||||||
bool isArr;
|
bool isArr;
|
||||||
|
bool arrD1; // is always an array with depth 1
|
||||||
} TypeInfo;
|
} TypeInfo;
|
||||||
TypeInfo ti[t_COUNT];
|
TypeInfo ti[t_COUNT];
|
||||||
#define TI(x) (ti[v(x)->type])
|
#define TI(x) (ti[v(x)->type])
|
||||||
@ -412,6 +420,7 @@ static inline void hdr_init() {
|
|||||||
ti[i].m1_d = def_m1_d;
|
ti[i].m1_d = def_m1_d;
|
||||||
ti[i].m2_d = def_m2_d;
|
ti[i].m2_d = def_m2_d;
|
||||||
ti[i].isArr = false;
|
ti[i].isArr = false;
|
||||||
|
ti[i].arrD1 = false;
|
||||||
ti[i].identity = def_identity;
|
ti[i].identity = def_identity;
|
||||||
ti[i].decompose = def_decompose;
|
ti[i].decompose = def_decompose;
|
||||||
ti[i].slice = def_slice;
|
ti[i].slice = def_slice;
|
||||||
|
|||||||
@ -46,6 +46,12 @@ HArr* toHArr(B x) {
|
|||||||
return r.c;
|
return r.c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NOINLINE void harr_pfree(B x, usz am) { // am - item after last written
|
||||||
|
B* p = harr_ptr(x);
|
||||||
|
for (usz i = 0; i < am; i++) dec(p[i]);
|
||||||
|
mm_free(v(x));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
B m_caB(usz ia, B* a) {
|
B m_caB(usz ia, B* a) {
|
||||||
|
|||||||
@ -71,5 +71,6 @@ static inline void i32arr_init() {
|
|||||||
ti[t_i32arr].visit = do_nothing; ti[t_i32slice].visit = slice_visit;
|
ti[t_i32arr].visit = do_nothing; ti[t_i32slice].visit = slice_visit;
|
||||||
ti[t_i32arr].print = arr_print; ti[t_i32slice].print = arr_print;
|
ti[t_i32arr].print = arr_print; ti[t_i32slice].print = arr_print;
|
||||||
ti[t_i32arr].isArr = true; ti[t_i32slice].isArr = true;
|
ti[t_i32arr].isArr = true; ti[t_i32slice].isArr = true;
|
||||||
|
ti[t_i32arr].arrD1 = true; ti[t_i32slice].arrD1 = true;
|
||||||
ti[t_i32arr].canStore = i32arr_canStore;
|
ti[t_i32arr].canStore = i32arr_canStore;
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/main.c
19
src/main.c
@ -96,21 +96,21 @@ int main() {
|
|||||||
|
|
||||||
// fake runtime
|
// fake runtime
|
||||||
B fruntime[] = {
|
B fruntime[] = {
|
||||||
/* +-×÷⋆√⌊⌈|¬ */ bi_add , bi_sub , bi_mul , bi_div, bi_pow, bi_N , bi_floor, bi_ceil, bi_stile, bi_not,
|
/* +-×÷⋆√⌊⌈|¬ */ bi_add , bi_sub , bi_mul , bi_div, bi_pow , bi_N , bi_floor, bi_ceil, bi_stile, bi_not,
|
||||||
/* ∧∨<>≠=≤≥≡≢ */ bi_and , bi_or , bi_lt , bi_gt , bi_ne , bi_eq, bi_le , bi_ge , bi_feq , bi_fne,
|
/* ∧∨<>≠=≤≥≡≢ */ bi_and , bi_or , bi_lt , bi_gt , bi_ne , bi_eq , bi_le , bi_ge , bi_feq , bi_fne,
|
||||||
/* ⊣⊢⥊∾≍↑↓↕«» */ bi_ltack, bi_rtack , bi_shape, bi_N , bi_N , bi_N , bi_N , bi_ud , bi_N , bi_N,
|
/* ⊣⊢⥊∾≍↑↓↕«» */ bi_ltack, bi_rtack , bi_shape, bi_N , bi_N , bi_N , bi_N , bi_ud , bi_N , bi_N,
|
||||||
/* ⌽⍉/⍋⍒⊏⊑⊐⊒∊ */ bi_N , bi_N , bi_N , bi_N , bi_N , bi_N , bi_pick , bi_N , bi_N , bi_N,
|
/* ⌽⍉/⍋⍒⊏⊑⊐⊒∊ */ bi_N , bi_N , bi_slash, bi_N , bi_N , bi_select, bi_pick , bi_N , bi_N , bi_N,
|
||||||
/* ⍷⊔!˙˜˘¨⌜⁼´ */ bi_N , bi_N , bi_asrt , bi_N , bi_N , bi_N , bi_each , bi_tbl , bi_N , bi_fold,
|
/* ⍷⊔!˙˜˘¨⌜⁼´ */ bi_N , bi_N , bi_asrt , bi_N , bi_N , bi_N , bi_each , bi_tbl , bi_N , bi_fold,
|
||||||
/* ˝`∘○⊸⟜⌾⊘◶⎉ */ bi_N , bi_scan , bi_N , bi_N , bi_N , bi_N , bi_N , bi_val , bi_N , bi_N,
|
/* ˝`∘○⊸⟜⌾⊘◶⎉ */ bi_N , bi_scan , bi_N , bi_N , bi_before, bi_N , bi_N , bi_val , bi_N , bi_N,
|
||||||
/* ⚇⍟⎊ */ bi_N , bi_repeat, bi_catch
|
/* ⚇⍟⎊ */ bi_N , bi_repeat, bi_catch
|
||||||
};
|
};
|
||||||
bool rtComplete[] = {
|
bool rtComplete[] = {
|
||||||
/* +-×÷⋆√⌊⌈|¬ */ 1,1,1,1,1,0,1,1,1,1,
|
/* +-×÷⋆√⌊⌈|¬ */ 1,1,1,1,1,0,1,1,1,1,
|
||||||
/* ∧∨<>≠=≤≥≡≢ */ 1,1,1,1,1,1,1,1,1,1,
|
/* ∧∨<>≠=≤≥≡≢ */ 1,1,1,1,1,1,1,1,1,1,
|
||||||
/* ⊣⊢⥊∾≍↑↓↕«» */ 1,1,0,0,0,0,0,0,0,0,
|
/* ⊣⊢⥊∾≍↑↓↕«» */ 1,1,0,0,0,0,0,0,0,0,
|
||||||
/* ⌽⍉/⍋⍒⊏⊑⊐⊒∊ */ 0,0,0,0,0,0,0,0,0,0,
|
/* ⌽⍉/⍋⍒⊏⊑⊐⊒∊ */ 0,0,1,0,0,1,0,0,0,0,
|
||||||
/* ⍷⊔!˙˜˘¨⌜⁼´ */ 0,0,1,0,0,0,1,1,0,1,
|
/* ⍷⊔!˙˜˘¨⌜⁼´ */ 0,0,1,0,0,0,1,1,0,1,
|
||||||
/* ˝`∘○⊸⟜⌾⊘◶⎉ */ 0,1,0,0,0,0,0,1,0,0,
|
/* ˝`∘○⊸⟜⌾⊘◶⎉ */ 0,1,0,0,1,0,0,1,0,0,
|
||||||
/* ⚇⍟⎊ */ 0,1,1
|
/* ⚇⍟⎊ */ 0,1,1
|
||||||
};
|
};
|
||||||
assert(sizeof(fruntime)/sizeof(B) == rtLen);
|
assert(sizeof(fruntime)/sizeof(B) == rtLen);
|
||||||
@ -134,6 +134,8 @@ int main() {
|
|||||||
rt_sortDsc = rtObjGet(rtObjRaw, 11); gc_add(rt_sortDsc);
|
rt_sortDsc = rtObjGet(rtObjRaw, 11); gc_add(rt_sortDsc);
|
||||||
rt_merge = rtObjGet(rtObjRaw, 13); gc_add(rt_merge);
|
rt_merge = rtObjGet(rtObjRaw, 13); gc_add(rt_merge);
|
||||||
rt_undo = rtObjGet(rtObjRaw, 48); gc_add(rt_undo);
|
rt_undo = rtObjGet(rtObjRaw, 48); gc_add(rt_undo);
|
||||||
|
rt_select = rtObjGet(rtObjRaw, 35); gc_add(rt_select);
|
||||||
|
rt_slash = rtObjGet(rtObjRaw, 32); gc_add(rt_slash);
|
||||||
|
|
||||||
for (usz i = 0; i < runtimeLen; i++) {
|
for (usz i = 0; i < runtimeLen; i++) {
|
||||||
#ifdef ALL_RUNTIME
|
#ifdef ALL_RUNTIME
|
||||||
@ -141,6 +143,7 @@ int main() {
|
|||||||
#else
|
#else
|
||||||
B r = rtComplete[i]? inc(fruntime[i]) : rtObjGet(rtObjRaw, i);
|
B r = rtComplete[i]? inc(fruntime[i]) : rtObjGet(rtObjRaw, i);
|
||||||
#endif
|
#endif
|
||||||
|
if (isNothing(r)) { printf("· in runtime!\n"); exit(1); }
|
||||||
r = rtPerf_wrap(r);
|
r = rtPerf_wrap(r);
|
||||||
runtimeH.a[i] = r;
|
runtimeH.a[i] = r;
|
||||||
if (isVal(r)) v(r)->flags|= i+1;
|
if (isVal(r)) v(r)->flags|= i+1;
|
||||||
|
|||||||
11
src/md2.c
11
src/md2.c
@ -94,14 +94,21 @@ B repeat_c1(B d, B x) { REPEAT_T(c1,{} ); }
|
|||||||
B repeat_c2(B d, B w, B x) { REPEAT_T(c2,dec(w), inc(w), ); }
|
B repeat_c2(B d, B w, B x) { REPEAT_T(c2,dec(w), inc(w), ); }
|
||||||
#undef REPEAT_T
|
#undef REPEAT_T
|
||||||
|
|
||||||
|
B before_c1(B d, B x) {
|
||||||
|
return c2(c(Md2D,d)->g, c1(c(Md2D,d)->f, inc(x)), x);
|
||||||
|
}
|
||||||
|
B before_c2(B d, B w, B x) {
|
||||||
|
return c2(c(Md2D,d)->g, c1(c(Md2D,d)->f, w), x);
|
||||||
|
}
|
||||||
|
|
||||||
#define ba(NAME) bi_##NAME = mm_alloc(sizeof(Md2), t_md2BI, ftag(MD2_TAG)); c(Md2,bi_##NAME)->c2 = NAME##_c2; c(Md2,bi_##NAME)->c1 = NAME##_c1; c(Md2,bi_##NAME)->extra=pm2_##NAME; gc_add(bi_##NAME);
|
#define ba(NAME) bi_##NAME = mm_alloc(sizeof(Md2), t_md2BI, ftag(MD2_TAG)); c(Md2,bi_##NAME)->c2 = NAME##_c2; c(Md2,bi_##NAME)->c1 = NAME##_c1; c(Md2,bi_##NAME)->extra=pm2_##NAME; gc_add(bi_##NAME);
|
||||||
#define bd(NAME) bi_##NAME = mm_alloc(sizeof(Md2), t_md2BI, ftag(MD2_TAG)); c(Md2,bi_##NAME)->c2 = NAME##_c2; c(Md2,bi_##NAME)->c1 = c1_invalid; c(Md1,bi_##NAME)->extra=pm2_##NAME; gc_add(bi_##NAME);
|
#define bd(NAME) bi_##NAME = mm_alloc(sizeof(Md2), t_md2BI, ftag(MD2_TAG)); c(Md2,bi_##NAME)->c2 = NAME##_c2; c(Md2,bi_##NAME)->c1 = c1_invalid; c(Md1,bi_##NAME)->extra=pm2_##NAME; gc_add(bi_##NAME);
|
||||||
#define bm(NAME) bi_##NAME = mm_alloc(sizeof(Md2), t_md2BI, ftag(MD2_TAG)); c(Md2,bi_##NAME)->c2 = c2_invalid;c(Md2,bi_##NAME)->c1 = NAME##_c1; c(Md1,bi_##NAME)->extra=pm2_##NAME; gc_add(bi_##NAME);
|
#define bm(NAME) bi_##NAME = mm_alloc(sizeof(Md2), t_md2BI, ftag(MD2_TAG)); c(Md2,bi_##NAME)->c2 = c2_invalid;c(Md2,bi_##NAME)->c1 = NAME##_c1; c(Md1,bi_##NAME)->extra=pm2_##NAME; gc_add(bi_##NAME);
|
||||||
|
|
||||||
void print_md2_def(B x) { printf("%s", format_pm2(c(Md1,x)->extra)); }
|
void print_md2_def(B x) { printf("%s", format_pm2(c(Md1,x)->extra)); }
|
||||||
|
|
||||||
B bi_val, bi_repeat, bi_fillBy, bi_catch;
|
B bi_val, bi_repeat, bi_before, bi_fillBy, bi_catch;
|
||||||
static inline void md2_init() { ba(val) ba(repeat) ba(fillBy) ba(catch)
|
static inline void md2_init() { ba(val) ba(repeat) ba(before) ba(fillBy) ba(catch)
|
||||||
ti[t_md2BI].print = print_md2_def;
|
ti[t_md2BI].print = print_md2_def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
116
src/sfns.c
116
src/sfns.c
@ -235,14 +235,15 @@ B fne_c1(B t, B x) {
|
|||||||
}
|
}
|
||||||
u64 depth(B x) { // doesn't consume
|
u64 depth(B x) { // doesn't consume
|
||||||
if (!isArr(x)) return 0;
|
if (!isArr(x)) return 0;
|
||||||
u64 r = 1;
|
if (TI(x).arrD1) return 1;
|
||||||
|
u64 r = 0;
|
||||||
usz ia = a(x)->ia;
|
usz ia = a(x)->ia;
|
||||||
BS2B xgetU = TI(x).getU;
|
BS2B xgetU = TI(x).getU;
|
||||||
for (usz i = 0; i < ia; i++) {
|
for (usz i = 0; i < ia; i++) {
|
||||||
u64 n = depth(xgetU(x,i))+1;
|
u64 n = depth(xgetU(x,i));
|
||||||
if (n>r) r = n;
|
if (n>r) r = n;
|
||||||
}
|
}
|
||||||
return r;
|
return r+1;
|
||||||
}
|
}
|
||||||
B feq_c1(B t, B x) {
|
B feq_c1(B t, B x) {
|
||||||
u64 r = depth(x);
|
u64 r = depth(x);
|
||||||
@ -266,14 +267,119 @@ B funBI_identity(B x) {
|
|||||||
return inc(c(BFn,x)->ident);
|
return inc(c(BFn,x)->ident);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
B rt_select;
|
||||||
|
B select_c1(B t, B x) {
|
||||||
|
if (!isArr(x)) thrM("⊏: Argument cannot be an atom");
|
||||||
|
ur xr = rnk(x);
|
||||||
|
if (xr==0) thrM("⊏: Argument cannot be rank 0");
|
||||||
|
if (a(x)->sh[0]==0) thrM("⊏: Argument shape cannot start with 0");
|
||||||
|
inc(x);
|
||||||
|
B r = TI(x).slice(x,0);
|
||||||
|
usz* sh = arr_shAllocR(r, xr-1);
|
||||||
|
usz ia = 1;
|
||||||
|
for (i32 i = 1; i < xr; i++) {
|
||||||
|
if (sh) sh[i-1] = a(x)->sh[i];
|
||||||
|
ia*= a(x)->sh[i];
|
||||||
|
}
|
||||||
|
a(r)->ia = ia;
|
||||||
|
dec(x);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
B select_c2(B t, B w, B x) {
|
||||||
|
if (isArr(w) && isArr(x) && rnk(w)==1 && rnk(x)==1) {
|
||||||
|
usz wia = a(w)->ia;
|
||||||
|
usz xia = a(x)->ia;
|
||||||
|
B xf = getFill(inc(x));
|
||||||
|
HArr_p r = m_harrc(w);
|
||||||
|
BS2B wgetU = TI(w).getU;
|
||||||
|
BS2B xget = TI(x).get;
|
||||||
|
for (usz i = 0; i < wia; i++) {
|
||||||
|
B cw = wgetU(w, i);
|
||||||
|
if (!isNum(cw)) { harr_pfree(r.b, i); goto base; }
|
||||||
|
f64 c = o2f(cw);
|
||||||
|
if (c<0) c+= xia;
|
||||||
|
if (c!=(usz)c | c>=xia) { harr_pfree(r.b, i); goto base; }
|
||||||
|
r.a[i] = xget(x, c);
|
||||||
|
}
|
||||||
|
dec(w); dec(x);
|
||||||
|
return withFill(r.b,xf);
|
||||||
|
}
|
||||||
|
base:
|
||||||
|
return c2(rt_select, w, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
i64 isum(B x) { // doesn't consume; assumes is array; may error
|
||||||
|
BS2B xgetU = TI(x).getU;
|
||||||
|
i64 r = 0;
|
||||||
|
usz xia = a(x)->ia;
|
||||||
|
for (usz i = 0; i < xia; i++) r+= o2f(xgetU(x,i)); // TODO error on overflow and non-integers or something
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
B rt_slash;
|
||||||
|
B slash_c1(B t, B x) {
|
||||||
|
if (!isArr(x)) thrM("/: Argument must be a list");
|
||||||
|
if (rnk(x)!=1) thrM("/: Argument must have rank 1");
|
||||||
|
i64 s = isum(x);
|
||||||
|
if(s<0) thrM("/: Argument must consist of natural numbers");
|
||||||
|
usz xia = a(x)->ia;
|
||||||
|
BS2B xgetU = TI(x).getU;
|
||||||
|
usz ri = 0;
|
||||||
|
if (xia<I32_MAX) {
|
||||||
|
B r = m_i32arrv(s); i32* rp = i32arr_ptr(r);
|
||||||
|
for (usz i = 0; i < xia; i++) {
|
||||||
|
usz c = o2s(xgetU(x, i));
|
||||||
|
for (usz j = 0; j < c; j++) rp[ri++] = i;
|
||||||
|
}
|
||||||
|
dec(x);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
HArr_p r = m_harrv(s);
|
||||||
|
for (usz i = 0; i < xia; i++) {
|
||||||
|
usz c = o2s(xgetU(x, i));
|
||||||
|
for (usz j = 0; j < c; j++) r.a[ri++] = m_i32(i);
|
||||||
|
}
|
||||||
|
dec(x);
|
||||||
|
return withFill(r.b,m_f64(0));
|
||||||
|
}
|
||||||
|
B slash_c2(B t, B w, B x) {
|
||||||
|
if (isArr(w) && isArr(x) && rnk(w)==1 && rnk(x)==1 && depth(w)==1) {
|
||||||
|
usz wia = a(w)->ia;
|
||||||
|
usz xia = a(x)->ia;
|
||||||
|
B xf = getFill(inc(x));
|
||||||
|
if (wia!=xia) thrM("/: Lengths of components of 𝕨 must match 𝕩");
|
||||||
|
usz ria = isum(w);
|
||||||
|
HArr_p r = m_harrv(ria);
|
||||||
|
BS2B wgetU = TI(w).getU;
|
||||||
|
BS2B xgetU = TI(x).getU;
|
||||||
|
usz ri = 0;
|
||||||
|
for (usz i = 0; i < wia; i++) {
|
||||||
|
B cw = wgetU(w, i);
|
||||||
|
if (isNum(cw)) {
|
||||||
|
f64 cf = o2f(cw);
|
||||||
|
usz c = (usz)cf;
|
||||||
|
if (cf!=c) goto base; // TODO clean up half-written r
|
||||||
|
if (c) {
|
||||||
|
B cx = xgetU(x, i);
|
||||||
|
for (usz j = 0; j < c; j++) r.a[ri++] = inc(cx);
|
||||||
|
}
|
||||||
|
} else { dec(cw); goto base; }
|
||||||
|
}
|
||||||
|
dec(w); dec(x);
|
||||||
|
return withFill(r.b,xf);
|
||||||
|
}
|
||||||
|
base:
|
||||||
|
return c2(rt_slash, w, x);
|
||||||
|
}
|
||||||
|
|
||||||
#define ba(N) bi_##N = mm_alloc(sizeof(BFn), t_funBI, ftag(FUN_TAG)); c(Fun,bi_##N)->c2 = N##_c2 ;c(Fun,bi_##N)->c1 = N##_c1 ; c(Fun,bi_##N)->extra=pf_##N; c(BFn,bi_##N)->ident=bi_N; gc_add(bi_##N);
|
#define ba(N) bi_##N = mm_alloc(sizeof(BFn), t_funBI, ftag(FUN_TAG)); c(Fun,bi_##N)->c2 = N##_c2 ;c(Fun,bi_##N)->c1 = N##_c1 ; c(Fun,bi_##N)->extra=pf_##N; c(BFn,bi_##N)->ident=bi_N; gc_add(bi_##N);
|
||||||
#define bd(N) bi_##N = mm_alloc(sizeof(BFn), t_funBI, ftag(FUN_TAG)); c(Fun,bi_##N)->c2 = N##_c2 ;c(Fun,bi_##N)->c1 = c1_invalid; c(Fun,bi_##N)->extra=pf_##N; c(BFn,bi_##N)->ident=bi_N; gc_add(bi_##N);
|
#define bd(N) bi_##N = mm_alloc(sizeof(BFn), t_funBI, ftag(FUN_TAG)); c(Fun,bi_##N)->c2 = N##_c2 ;c(Fun,bi_##N)->c1 = c1_invalid; c(Fun,bi_##N)->extra=pf_##N; c(BFn,bi_##N)->ident=bi_N; gc_add(bi_##N);
|
||||||
#define bm(N) bi_##N = mm_alloc(sizeof(BFn), t_funBI, ftag(FUN_TAG)); c(Fun,bi_##N)->c2 = c2_invalid;c(Fun,bi_##N)->c1 = N##_c1 ; c(Fun,bi_##N)->extra=pf_##N; c(BFn,bi_##N)->ident=bi_N; gc_add(bi_##N);
|
#define bm(N) bi_##N = mm_alloc(sizeof(BFn), t_funBI, ftag(FUN_TAG)); c(Fun,bi_##N)->c2 = c2_invalid;c(Fun,bi_##N)->c1 = N##_c1 ; c(Fun,bi_##N)->extra=pf_##N; c(BFn,bi_##N)->ident=bi_N; gc_add(bi_##N);
|
||||||
|
|
||||||
void print_fun_def(B x) { printf("%s", format_pf(c(Fun,x)->extra)); }
|
void print_fun_def(B x) { printf("%s", format_pf(c(Fun,x)->extra)); }
|
||||||
|
|
||||||
B bi_shape, bi_pick, bi_ud, bi_pair, bi_fne, bi_feq, bi_ltack, bi_rtack, bi_fmtF, bi_fmtN;
|
B bi_shape, bi_pick, bi_ud, bi_pair, bi_fne, bi_feq, bi_select, bi_slash, bi_ltack, bi_rtack, bi_fmtF, bi_fmtN;
|
||||||
static inline void sfns_init() { ba(shape) ba(pick) bm(ud) ba(pair) ba(fne) ba(feq) ba(ltack) ba(rtack) bm(fmtF) bm(fmtN)
|
static inline void sfns_init() { ba(shape) ba(pick) bm(ud) ba(pair) ba(fne) ba(feq) ba(select) ba(slash) ba(ltack) ba(rtack) bm(fmtF) bm(fmtN)
|
||||||
ti[t_funBI].print = print_fun_def;
|
ti[t_funBI].print = print_fun_def;
|
||||||
ti[t_funBI].identity = funBI_identity;
|
ti[t_funBI].identity = funBI_identity;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user