#include "../core.h" #include "mut.h" #include "each.h" static inline B mv(B* p, usz n) { B r = p [n]; p [n] = m_f64(0); return r; } static inline B hmv(HArr_p p, usz n) { B r = p.a[n]; p.a[n] = m_f64(0); return r; } B eachd_fn(B fo, B w, B x, FC2 f) { ur wr, xr; // if rank is 0, respective w/x will be disclosed if (isArr(w)) { wr=RNK(w); if (wr==0) w = TO_GET(w,0); } else wr=0; if (isArr(x)) { xr=RNK(x); if (xr==0) x = TO_GET(x,0); } else xr=0; bool wg = wr>xr; ur rM = wg? wr : xr; ur rm = wg? xr : wr; 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; if (rw|rx && (wr==xr | rm==0)) { HArr_p r = harr_parts(REUSE(rw? w : x)); usz ria = r.c->ia; if (ria>0) { if (wr==0) { incBy(w, ria-1); for(usz i=0; itype) { case t_fillarr: { dec(c(FillArr,x)->fill); c(FillArr,x)->fill = bi_noFill; xp = fillarrv_ptr(a(x)); break; } case t_harr: { xp = harr_ptr(x); break; } case t_fillslice: { FillSlice* s = c(FillSlice,x); Arr* p = s->p; if (p->refc==1 && ((p->type==t_fillarr && ((FillArr*)p)->a==s->a) || (p->type==t_harr && harrv_ptr(p)==s->a)) && PIA(p)==ia) { ur sr = PRNK(s); if (sr<=1) { arr_shErase(p, sr); } else { usz* ssh = PSH(s); if (ssh != PSH(p)) arr_shReplace(p, sr, ptr_inc(shObjS(ssh))); } x = taga(ptr_inc(p)); value_free((Value*)s); goto re_reuse; } else goto base; } default: goto base; } REUSE(x); for (; i < ia; i++) xp[i] = f(fo, mv(xp, i)); return any_squeeze(x); } base:; M_HARR(r, ia) void* xp = tyany_ptr(x); switch(TI(x,elType)) { default: UD; case el_B: { SGet(x); { for (; i