diff --git a/src/README.md b/src/README.md index c9395a8b..31b35e3d 100644 --- a/src/README.md +++ b/src/README.md @@ -170,16 +170,25 @@ B result = taga(r); u32* rp; B r = m_c32arrv(%rp, 10); // 10-char string // etc for m_(i8|i16|i32|c8|c16|c32|f64)arr[vcp] -usz ri=0; HArr_p r = m_harrs(123, &ri); // allocate 123-item arbitrary type array -// write items with r.a[ri++] or equivalent. Whenever GC could happen, ri must point to after the end of the currently set items -// then do one of these to finish the array: -B result = harr_fv(r); // sets shape to a vector -B result = harr_fc(r, x); // copies the shape of x, doesn't consume x -B result = harr_fcd(r, x); // copies the shape of x and consumes it -usz* sh = harr_fa(r, 4); // allocate shape for a rank 4 array. To get the result `B` object, just do r.b -// If at any point you want to free the object before finishing it, use harr_abandon(r) -// If you're sure GC cannot happen before all items in the array are set, you can use: (U means "unsafe") +// arbitrary object arrays: +// initialized with all elements being 0.0s, which you can replace with `r.a[i]=val`, and get the result with `r.b`; simple, but may not be optimal +HArr_p r = m_harr0v(10); // new 10-item vector +HArr_p r = m_harr0c(10, x); // new 10-item array with the same shape as x +HArr_p r = m_harr0p(10); // new 10-item array without any set shape. Use the arr_shWhatever(r.c, …) + +// safe known size array creation without preinitialization: +M_HARR(r, 123) // allocate a 123-item arbitrary object array +HARR_ADD(r, i, val); // write val to the next position in the array. The 'i' variable is just a hint, all calls must be consecutive either way +HARR_ADDA(r, val); // the above but without needing the useless 'i' parameter +// then do one of these to get the finished object: +B result = HARR_FV(r); // sets shape to a vector +B result = HARR_FC(r, x); // copies the shape of x, doesn't consume x +B result = HARR_FCD(r, x); // copies the shape of x and consumes it +usz* sh = HARR_FA(r, 4); // allocate shape for a rank 4 array. To get the result `B` object, do HARR_O(r).b later +// If at any point you want to free the object before finishing it, do HARR_ABANDON(r) + +// If you're sure GC cannot happen (that includes no allocating) before all items in the array are set, you can use: HArr_p r = m_harrUv(10); // 10-item vector HArr_p r = m_harrUc(10, x); // 10-item array with the same shape as x HArr_p r = m_harrUp(10); // 10-item array without any set shape. Use the arr_shWhatever(r.c, …) diff --git a/src/builtins/internal.c b/src/builtins/internal.c index f3ab148b..49f20147 100644 --- a/src/builtins/internal.c +++ b/src/builtins/internal.c @@ -241,10 +241,10 @@ static B unshare(B x) { case t_c32arr: return taga(cpyC32Arr(inc(x))); case t_f64arr: return taga(cpyF64Arr(inc(x))); case t_harr: { - HArr_p r = m_harrUc(x); B* xp = harr_ptr(x); - for (usz i = 0; i < xia; i++) r.a[i] = unshare(xp[i]); - return r.b; + M_HARR(r, xia) + for (usz i = 0; i < xia; i++) HARR_ADD(r, i, unshare(xp[i])); + return HARR_FC(r, x); } case t_fillarr: { Arr* r = m_fillarrp(xia); arr_shCopy(r, x); diff --git a/src/builtins/md1.c b/src/builtins/md1.c index 6977f2f0..dc73817c 100644 --- a/src/builtins/md1.c +++ b/src/builtins/md1.c @@ -55,22 +55,21 @@ B tbl_c2(Md1D* d, B w, B x) { B f = d->f; SGet(x) BBB2B fc2 = c2fn(f); - usz ri = 0; - HArr_p r = m_harrs(ria, &ri); + M_HARR(r, ria) for (usz wi = 0; wi < wia; wi++) { B cw = GetU(w,wi); - for (usz xi = 0; xi < xia; xi++,ri++) { - r.a[ri] = fc2(f, inc(cw), Get(x,xi)); + for (usz xi = 0; xi < xia; xi++) { + HARR_ADDA(r, fc2(f, inc(cw), Get(x,xi))); } } - usz* rsh = harr_fa(r, rr); + usz* rsh = HARR_FA(r, rr); if (rsh) { memcpy(rsh , a(w)->sh, wr*sizeof(usz)); memcpy(rsh+wr, a(x)->sh, xr*sizeof(usz)); } dec(w); dec(x); - if (EACH_FILLS) return homFil2(f, r.b, wf, xf); - return r.b; + if (EACH_FILLS) return homFil2(f, HARR_O(r).b, wf, xf); + return HARR_O(r).b; } B each_c1(Md1D* d, B x) { B f = d->f; @@ -137,20 +136,21 @@ B scan_c1(Md1D* d, B x) { B f = d->f; SLOW2("π•Ž` 𝕩", f, x); bool reuse = v(x)->type==t_harr && reusable(x); - usz i = 0; - HArr_p r = reuse? harr_parts(REUSE(x)) : m_harrs(a(x)->ia, &i); + HArr_p r = reuse? harr_parts(REUSE(x)) : m_harr0c(x); AS2B xget = reuse? TI(x,getU) : TI(x,get); Arr* xa = a(x); BBB2B fc2 = c2fn(f); if (xr==1) { - r.a[i] = xget(xa,0); i++; - for (i = 1; i < ia; i++) r.a[i] = fc2(f, inc(r.a[i-1]), xget(xa,i)); + r.a[0] = xget(xa,0); + for (usz i=1; if; if (isAtm(x) || rnk(x)==0) thrM("`: 𝕩 cannot have rank 0"); @@ -184,7 +184,7 @@ B scan_c2(Md1D* d, B w, B x) { B f = d->f; bool reuse = (v(x)->type==t_harr && reusable(x)) | !ia; usz i = 0; - HArr_p r = reuse? harr_parts(REUSE(x)) : m_harrs(a(x)->ia, &i); + HArr_p r = reuse? harr_parts(REUSE(x)) : m_harr0c(x); AS2B xget = reuse? TI(x,getU) : TI(x,get); Arr* xa = a(x); BBB2B fc2 = c2fn(f); @@ -202,7 +202,8 @@ B scan_c2(Md1D* d, B w, B x) { B f = d->f; B pr = r.a[0] = fc2(f, w, xget(xa,0)); i++; for (; i < ia; i++) r.a[i] = pr = fc2(f, inc(pr), xget(xa,i)); } - return withFill(reuse? x : harr_fcd(r, x), wf); + if (!reuse) dec(x); + return withFill(r.b, wf); } B fold_c1(Md1D* d, B x) { B f = d->f; @@ -353,18 +354,17 @@ B cell_c1(Md1D* d, B x) { B f = d->f; csh = m_shArr(cr); memcpy(csh->a, a(x)->sh+1, sizeof(usz)*cr); } - usz i = 0; BSS2A slice = TI(x,slice); - HArr_p r = m_harrs(cam, &i); + M_HARR(r, cam); usz p = 0; - for (; i < cam; i++) { + for (usz i = 0; i < cam; i++) { Arr* s = slice(inc(x), p, csz); arr_shSetI(s, cr, csh); - r.a[i] = c1(f, taga(s)); + HARR_ADD(r, i, c1(f, taga(s))); p+= csz; } if (cr>1) ptr_dec(csh); dec(x); - return bqn_merge(harr_fv(r)); + return bqn_merge(HARR_FV(r)); } B cell_c2(Md1D* d, B w, B x) { B f = d->f; if ((isAtm(x) || rnk(x)==0) && (isAtm(w) || rnk(w)==0)) { diff --git a/src/builtins/md2.c b/src/builtins/md2.c index ad2410a0..96c5ffad 100644 --- a/src/builtins/md2.c +++ b/src/builtins/md2.c @@ -55,9 +55,9 @@ B repeat_replace(B g, B* q) { // doesn't consume if (isArr(g)) { SGetU(g) usz ia = a(g)->ia; - HArr_p r = m_harrUc(g); - for (usz i = 0; i < ia; i++) r.a[i] = repeat_replace(GetU(g,i), q); - return r.b; + M_HARR(r, ia); + for (usz i = 0; i < ia; i++) HARR_ADD(r, i, repeat_replace(GetU(g,i), q)); + return HARR_FC(r, g); } else { return inc(q[o2i64u(g)]); } diff --git a/src/builtins/sfns.c b/src/builtins/sfns.c index 8aff9d83..59de1b0d 100644 --- a/src/builtins/sfns.c +++ b/src/builtins/sfns.c @@ -192,7 +192,7 @@ B shape_c2(B t, B w, B x) { if (xia<=1) { if (RARE(xia==0)) { thrM("β₯Š: Empty 𝕩 and non-empty result"); - // if (xf.u == bi_noFill.u) thrM("β₯Š: No fill for empty array"); + // if (noFill(xf)) thrM("β₯Š: No fill for empty array"); // dec(x); // x = inc(xf); } else { @@ -284,14 +284,13 @@ static B recPick(B w, B x) { // doesn't consume } return IGet(x,c); } else { - usz i = 0; - HArr_p r = m_harrs(ia, &i); - for(; itype==t_harr || v(x)->type==t_hslice) { \ B* xp = hany_ptr(x); \ - for (; i < wia; i++) r.a[i] = inc(xp[WRAP(wp[i], xia, thrF("⊏: Indexing out-of-bounds (%iβˆŠπ•¨, %s≑≠𝕩)", wp[i], xia))]); \ - dec(x); return harr_fcd(r, w); \ + for (usz i=0; i < wia; i++) HARR_ADD(r, i, inc(xp[WRAP(wp[i], xia, thrF("⊏: Indexing out-of-bounds (%iβˆŠπ•¨, %s≑≠𝕩)", wp[i], xia))])); \ + dec(x); return HARR_FCD(r, w); \ } SLOW2("π•¨βŠπ•©", w, x); \ - for (; i < wia; i++) r.a[i] = Get(x, WRAP(wp[i], xia, thrF("⊏: Indexing out-of-bounds (%iβˆŠπ•¨, %s≑≠𝕩)", wp[i], xia))); \ - dec(x); return withFill(harr_fcd(r,w),xf); \ + for (usz i=0; i < wia; i++) HARR_ADD(r, i, Get(x, WRAP(wp[i], xia, thrF("⊏: Indexing out-of-bounds (%iβˆŠπ•¨, %s≑≠𝕩)", wp[i], xia)))); \ + dec(x); return withFill(HARR_FCD(r,w),xf); \ } if (TI(w,elType)==el_bit && xia>=2) { SGetU(x) @@ -390,16 +389,16 @@ B select_c2(B t, B w, B x) { else if (TI(w,elType)==el_i32) TYPE(i32) else { SLOW2("π•¨βŠπ•©", w, x); - usz i=0; HArr_p r = m_harrs(wia, &i); + M_HARR(r, wia) SGetU(w) - for (; i < wia; i++) { + for (usz i = 0; i < wia; i++) { B cw = GetU(w, i); - if (!isNum(cw)) { harr_abandon(r); goto base; } + if (!isNum(cw)) { HARR_ABANDON(r); goto base; } usz c = WRAP(o2i64(cw), xia, thrF("⊏: Indexing out-of-bounds (%RβˆŠπ•¨, %s≑≠𝕩)", cw, xia)); - r.a[i] = Get(x, c); + HARR_ADD(r, i, Get(x, c)); } dec(x); - return withFill(harr_fcd(r,w),xf); + return withFill(HARR_FCD(r,w),xf); } #undef CASE } else { @@ -554,9 +553,9 @@ B slash_c2(B t, B w, B x) { r = withFill(rp.b, xf); } else { SLOW2("𝕨/𝕩", w, x); - HArr_p rp = m_harrs(wsum, &ri); SGet(x) - for (usz i = 0; i < wia; i++) if (bitp_get(wp,i)) rp.a[ri++] = Get(x,i); - r = withFill(harr_fv(rp), xf); + M_HARR(rp, wsum) SGet(x) + for (usz i = 0; i < wia; i++) if (bitp_get(wp,i)) HARR_ADDA(rp, Get(x,i)); + r = withFill(HARR_FV(rp), xf); } break; } @@ -598,15 +597,15 @@ B slash_c2(B t, B w, B x) { dec(w); dec(x); return r; \ } \ CASE(WT,i8) CASE(WT,i16) CASE(WT,i32) CASE(WT,f64) \ - SLOW2("𝕨/𝕩", w, x); \ - HArr_p r = m_harrs(wsum, &ri); SGetU(x) \ - for (usz i = 0; i < wia; i++) { \ - i32 cw = wp[i]; if (cw==0) continue; \ - B cx = incBy(GetU(x, i), cw); \ - for (i64 j = 0; j < cw; j++) r.a[ri++] = cx; \ - } \ - dec(w); dec(x); \ - return withFill(harr_fv(r), xf); \ + SLOW2("𝕨/𝕩", w, x); \ + M_HARR(r, wsum) SGetU(x) \ + for (usz i = 0; i < wia; i++) { \ + i32 cw = wp[i]; if (cw==0) continue; \ + B cx = incBy(GetU(x, i), cw); \ + for (i64 j = 0; j < cw; j++) HARR_ADDA(r, cx);\ + } \ + dec(w); dec(x); \ + return withFill(HARR_FV(r), xf); \ } if (TI(w,elType)==el_i8 ) TYPED(i8,7); if (TI(w,elType)==el_i32) TYPED(i32,31); @@ -615,18 +614,16 @@ B slash_c2(B t, B w, B x) { SLOW2("𝕨/𝕩", w, x); u64 ria = usum(w); if (ria>=USZ_MAX) thrOOM(); - HArr_p r = m_harrs(ria, &ri); - SGetU(w) - SGetU(x) + M_HARR(r, ria) SGetU(w) SGetU(x) for (usz i = 0; i < wia; i++) { usz c = o2s(GetU(w, i)); if (c) { B cx = incBy(GetU(x, i), c); - for (usz j = 0; RARE(j < c); j++) r.a[ri++] = cx; + for (usz j = 0; RARE(j < c); j++) HARR_ADDA(r, cx); } } dec(w); dec(x); - return withFill(harr_fv(r), xf); + return withFill(HARR_FV(r), xf); } if (isArr(x) && rnk(x)==1 && q_i32(w)) { usz xia = a(x)->ia; diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 488b1774..fd16575a 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -801,21 +801,20 @@ B getPrimitives(void); static Body* file_nsGen; B sys_c1(B t, B x) { assert(isArr(x)); - usz i = 0; - HArr_p r = m_harrs(a(x)->ia, &i); - SGetU(x) + M_HARR(r, a(x)->ia) SGetU(x) B fileNS = m_f64(0); B path = m_f64(0); B name = m_f64(0); B wdpath = m_f64(0); #define REQ_PATH ({ if(!path.u) path = q_N(comp_currPath)? bi_N : path_abs(path_dir(inc(comp_currPath))); path; }) #define REQ_NAME ({ if(!name.u) name = path_name(inc(comp_currPath)); name; }) - for (; i < a(x)->ia; i++) { + for (usz i = 0; i < a(x)->ia; i++) { B c = GetU(x,i); - if (eqStr(c, U"out")) r.a[i] = incG(bi_out); - else if (eqStr(c, U"show")) r.a[i] = incG(bi_show); - else if (eqStr(c, U"exit")) r.a[i] = incG(bi_exit); - else if (eqStr(c, U"getline")) r.a[i] = incG(bi_getLine); + B cr; + if (eqStr(c, U"out")) cr = incG(bi_out); + else if (eqStr(c, U"show")) cr = incG(bi_show); + else if (eqStr(c, U"exit")) cr = incG(bi_exit); + else if (eqStr(c, U"getline")) cr = incG(bi_getLine); else if (eqStr(c, U"file")) { if(!fileNS.u) { REQ_PATH; @@ -823,54 +822,55 @@ B sys_c1(B t, B x) { fileNS = m_nns(file_nsGen, q_N(path)? m_c32(0) : inc(path), F(fileAt), F(list), F(fBytes), F(fChars), F(fLines)); #undef F } - r.a[i] = inc(fileNS); + cr = inc(fileNS); } else if (eqStr(c, U"wdpath")) { if (!wdpath.u) wdpath = path_abs(inc(cdPath)); - r.a[i] = inc(wdpath); + cr = inc(wdpath); } - else if (eqStr(c, U"internal")) r.a[i] = getInternalNS(); - else if (eqStr(c, U"math")) r.a[i] = getMathNS(); - else if (eqStr(c, U"type")) r.a[i] = incG(bi_type); - else if (eqStr(c, U"sh")) r.a[i] = incG(bi_sh); - else if (eqStr(c, U"decompose")) r.a[i] = incG(bi_decp); - else if (eqStr(c, U"while")) r.a[i] = incG(bi_while); - else if (eqStr(c, U"primind")) r.a[i] = incG(bi_primInd); - else if (eqStr(c, U"bqn")) r.a[i] = incG(bi_bqn); - else if (eqStr(c, U"cmp")) r.a[i] = incG(bi_cmp); - else if (eqStr(c, U"unixtime")) r.a[i] = incG(bi_unixTime); - else if (eqStr(c, U"monotime")) r.a[i] = incG(bi_monoTime); - else if (eqStr(c, U"timed")) r.a[i] = incG(bi_timed); - else if (eqStr(c, U"delay")) r.a[i] = incG(bi_delay); - else if (eqStr(c, U"hash")) r.a[i] = incG(bi_hash); - else if (eqStr(c, U"repr")) r.a[i] = incG(bi_repr); - else if (eqStr(c, U"fmt")) r.a[i] = incG(bi_fmt); - else if (eqStr(c, U"glyph")) r.a[i] = incG(bi_glyph); - else if (eqStr(c, U"makerand")) r.a[i] = incG(bi_makeRand); - else if (eqStr(c, U"rand")) r.a[i] = getRandNS(); - else if (eqStr(c, U"rebqn")) r.a[i] = incG(bi_reBQN); - else if (eqStr(c, U"primitives")) r.a[i] = getPrimitives(); - else if (eqStr(c, U"fromutf8")) r.a[i] = incG(bi_fromUtf8); - else if (eqStr(c, U"path")) r.a[i] = inc(REQ_PATH); - else if (eqStr(c, U"name")) r.a[i] = inc(REQ_NAME); - else if (eqStr(c, U"fchars")) r.a[i] = m_nfn(fCharsDesc, inc(REQ_PATH)); - else if (eqStr(c, U"fbytes")) r.a[i] = m_nfn(fBytesDesc, inc(REQ_PATH)); - else if (eqStr(c, U"flines")) r.a[i] = m_nfn(fLinesDesc, inc(REQ_PATH)); - else if (eqStr(c, U"import")) r.a[i] = m_nfn(importDesc, inc(REQ_PATH)); + else if (eqStr(c, U"internal")) cr = getInternalNS(); + else if (eqStr(c, U"math")) cr = getMathNS(); + else if (eqStr(c, U"type")) cr = incG(bi_type); + else if (eqStr(c, U"sh")) cr = incG(bi_sh); + else if (eqStr(c, U"decompose")) cr = incG(bi_decp); + else if (eqStr(c, U"while")) cr = incG(bi_while); + else if (eqStr(c, U"primind")) cr = incG(bi_primInd); + else if (eqStr(c, U"bqn")) cr = incG(bi_bqn); + else if (eqStr(c, U"cmp")) cr = incG(bi_cmp); + else if (eqStr(c, U"unixtime")) cr = incG(bi_unixTime); + else if (eqStr(c, U"monotime")) cr = incG(bi_monoTime); + else if (eqStr(c, U"timed")) cr = incG(bi_timed); + else if (eqStr(c, U"delay")) cr = incG(bi_delay); + else if (eqStr(c, U"hash")) cr = incG(bi_hash); + else if (eqStr(c, U"repr")) cr = incG(bi_repr); + else if (eqStr(c, U"fmt")) cr = incG(bi_fmt); + else if (eqStr(c, U"glyph")) cr = incG(bi_glyph); + else if (eqStr(c, U"makerand")) cr = incG(bi_makeRand); + else if (eqStr(c, U"rand")) cr = getRandNS(); + else if (eqStr(c, U"rebqn")) cr = incG(bi_reBQN); + else if (eqStr(c, U"primitives")) cr = getPrimitives(); + else if (eqStr(c, U"fromutf8")) cr = incG(bi_fromUtf8); + else if (eqStr(c, U"path")) cr = inc(REQ_PATH); + else if (eqStr(c, U"name")) cr = inc(REQ_NAME); + else if (eqStr(c, U"fchars")) cr = m_nfn(fCharsDesc, inc(REQ_PATH)); + else if (eqStr(c, U"fbytes")) cr = m_nfn(fBytesDesc, inc(REQ_PATH)); + else if (eqStr(c, U"flines")) cr = m_nfn(fLinesDesc, inc(REQ_PATH)); + else if (eqStr(c, U"import")) cr = m_nfn(importDesc, inc(REQ_PATH)); else if (eqStr(c, U"state")) { if (q_N(comp_currArgs)) thrM("No arguments present for β€’state"); - r.a[i] = m_hVec3(inc(REQ_PATH), inc(REQ_NAME), inc(comp_currArgs)); + cr = m_hVec3(inc(REQ_PATH), inc(REQ_NAME), inc(comp_currArgs)); } else if (eqStr(c, U"args")) { if (q_N(comp_currArgs)) thrM("No arguments present for β€’args"); - r.a[i] = inc(comp_currArgs); + cr = inc(comp_currArgs); } else { dec(x); thrF("Unknown system function β€’%R", c); } + HARR_ADD(r, i, cr); } #undef REQ_PATH dec(fileNS); dec(path); dec(name); dec(wdpath); - return harr_fcd(r, x); + return HARR_FCD(r, x); } B cdPath; diff --git a/src/core/fillarr.c b/src/core/fillarr.c index 981b08dc..d7ea2695 100644 --- a/src/core/fillarr.c +++ b/src/core/fillarr.c @@ -16,14 +16,13 @@ B asFill(B x) { // consumes dec(x); return r; } - HArr_p r = m_harrUc(x); + M_HARR(r, a(x)->ia) SGet(x) - bool noFill = false; - for (usz i = 0; i < ia; i++) if ((r.a[i]=asFill(Get(x,i))).u == bi_noFill.u) noFill = true; + for (usz i = 0; i < ia; i++) { + if (noFill(HARR_ADD(r, i, asFill(Get(x,i))))) { HARR_ABANDON(r); dec(x); return bi_noFill; } + } B xf = getFillQ(x); - dec(x); - if (noFill) { ptr_dec(r.c); return bi_noFill; } - return withFill(r.b, xf); + return withFill(HARR_FCD(r, x), xf); } if (isF64(x)) return m_i32(0); if (isC32(x)) return m_c32(' '); diff --git a/src/core/harr.c b/src/core/harr.c index 017184c0..db6bf2f3 100644 --- a/src/core/harr.c +++ b/src/core/harr.c @@ -7,14 +7,13 @@ B toCells(B x) { assert(isArr(x) && rnk(x)>1); usz cam = a(x)->sh[0]; usz csz = arr_csz(x); - usz i = 0; BSS2A slice = TI(x,slice); usz p = 0; - HArr_p r = m_harrs(cam, &i); + M_HARR(r, cam) if (rnk(x)==2) { - for (; i < cam; i++) { + for (usz i = 0; i < cam; i++) { Arr* s = slice(inc(x), p, csz); arr_shVec(s); - r.a[i] = taga(s); + HARR_ADD(r, i, taga(s)); p+= csz; } } else { @@ -22,15 +21,15 @@ B toCells(B x) { ShArr* csh = m_shArr(cr); usz* xsh = a(x)->sh; for (u64 i = 0; i < cr; i++) csh->a[i] = xsh[i+1]; - for (; i < cam; i++) { + for (usz i = 0; i < cam; i++) { Arr* s = slice(inc(x), p, csz); arr_shSetI(s, cr, csh); - r.a[i] = taga(s); + HARR_ADD(r, i, taga(s)); p+= csz; } ptr_dec(csh); } dec(x); - return harr_fv(r); + return HARR_FV(r); } B toKCells(B x, ur k) { assert(isArr(x) && k<=rnk(x) && k>=0); @@ -45,20 +44,19 @@ B toKCells(B x, ur k) { for (i32 i = 0; i < cr; i++) csh->a[i] = xsh[i+k]; } - usz i = 0; usz p = 0; - HArr_p r = m_harrs(cam, &i); + M_HARR(r, cam) BSS2A slice = TI(x,slice); - for (; i < cam; i++) { + for (usz i = 0; i < cam; i++) { Arr* s = slice(inc(x), p, csz); arr_shSetI(s, cr, csh); - r.a[i] = taga(s); + HARR_ADD(r, i, taga(s)); p+= csz; } if (cr>1) ptr_dec(csh); - usz* rsh = harr_fa(r, k); + usz* rsh = HARR_FA(r, k); if (rsh) for (i32 i = 0; i < k; i++) rsh[i] = xsh[i]; dec(x); - return r.b; + return HARR_O(r).b; } HArr* cpyHArr(B x) { @@ -153,13 +151,13 @@ static void harrP_print(B x) { } void harr_init() { - TIi(t_harr,get) = harr_get; TIi(t_hslice,get) = hslice_get; TIi(t_harrPartial,get) = harrP_get; - TIi(t_harr,getU) = harr_getU; TIi(t_hslice,getU) = hslice_getU; TIi(t_harrPartial,getU) = harrP_get; + TIi(t_harr,get) = harr_get; TIi(t_hslice,get) = hslice_get; + TIi(t_harr,getU) = harr_getU; TIi(t_hslice,getU) = hslice_getU; TIi(t_harr,slice) = harr_slice; TIi(t_hslice,slice) = hslice_slice; - TIi(t_harr,freeO) = harr_freeO; TIi(t_hslice,freeO) = slice_freeO; TIi(t_harrPartial,freeO) = harrP_freeO; - TIi(t_harr,freeF) = harr_freeF; TIi(t_hslice,freeF) = slice_freeF; TIi(t_harrPartial,freeF) = harrP_freeF; - TIi(t_harr,visit) = harr_visit; TIi(t_hslice,visit) = slice_visit; TIi(t_harrPartial,visit) = harrP_visit; - TIi(t_harr,print) = arr_print; TIi(t_hslice,print) = arr_print; TIi(t_harrPartial,print) = harrP_print; + TIi(t_harr,freeO) = harr_freeO; TIi(t_hslice,freeO) = slice_freeO; + TIi(t_harr,freeF) = harr_freeF; TIi(t_hslice,freeF) = slice_freeF; + TIi(t_harr,visit) = harr_visit; TIi(t_hslice,visit) = slice_visit; + TIi(t_harr,print) = arr_print; TIi(t_hslice,print) = arr_print; TIi(t_harr,isArr) = true; TIi(t_hslice,isArr) = true; TIi(t_harr,canStore) = harr_canStore; bi_emptyHVec = m_harrUv(0).b; gc_add(bi_emptyHVec); diff --git a/src/core/harr.h b/src/core/harr.h index 5f0cd824..0a98a708 100644 --- a/src/core/harr.h +++ b/src/core/harr.h @@ -23,47 +23,62 @@ static inline HArr_p harrP_parts(HArr* p) { NOINLINE void harr_pfree(B x, usz am); // am - item after last written -static HArr_p m_harrs(usz ia, usz* ctr) { +#define M_HARR(N, IA) usz N##_len = (IA); HArr_p N##_v = m_harr_impl(N##_len); usz* N##_ia = &N##_v.c->ia; usz N##_i = 0; +#define HARR_ADD(N, I, V) ({ B v_ = (V); usz i_ = (I); assert(N##_i==i_); N##_v.a[i_] = v_; *N##_ia = i_+1; N##_i++; v_; }) +#define HARR_ADDA(N, V) ({ B v_ = (V); N##_v.a[N##_i] = v_; *N##_ia = ++N##_i; v_; }) +#define HARR_O(N) N##_v +#define HARR_I(N) N##_i +static HArr_p m_harr_impl(usz ia) { HArr* r = m_arr(fsizeof(HArr,a,B,ia), t_harrPartial, ia); - r->sh = ctr; + r->ia = 0; + // don't need to initialize r->sh or rank at all i guess HArr_p rp = harrP_parts(r); gsAdd(rp.b); return rp; } -static B harr_fv(HArr_p p) { VTY(p.b, t_harrPartial); - assert(p.c->ia == *p.c->sh); + +#define HARR_FV(N) ({ assert(N##_v.c->ia == N##_len); harr_fv_impl(N##_v); }) +#define HARR_FC(N, X) ({ assert(N##_v.c->ia == N##_len); harr_fc_impl(N##_v, X); }) +#define HARR_FCD(N, X) ({ assert(N##_v.c->ia == N##_len); harr_fcd_impl(N##_v, X); }) +#define HARR_FA(N, R) ({ assert(N##_v.c->ia == N##_len); harr_fa_impl(N##_v, R); }) +#define HARR_ABANDON(N) harr_abandon_impl(N##_v) +static B harr_fv_impl(HArr_p p) { VTY(p.b, t_harrPartial); p.c->type = t_harr; p.c->sh = &p.c->ia; srnk(p.b, 1); gsPop(); return p.b; } -static B harr_fc(HArr_p p, B x) { VTY(p.b, t_harrPartial); - assert(p.c->ia == *p.c->sh); +static B harr_fc_impl(HArr_p p, B x) { VTY(p.b, t_harrPartial); p.c->type = t_harr; arr_shCopy((Arr*)p.c, x); gsPop(); return p.b; } -static B harr_fcd(HArr_p p, B x) { VTY(p.b, t_harrPartial); - assert(p.c->ia == *p.c->sh); +static B harr_fcd_impl(HArr_p p, B x) { VTY(p.b, t_harrPartial); p.c->type = t_harr; arr_shCopy((Arr*)p.c, x); dec(x); gsPop(); return p.b; } -static usz* harr_fa(HArr_p p, ur r) { VTY(p.b, t_harrPartial); +static usz* harr_fa_impl(HArr_p p, ur r) { VTY(p.b, t_harrPartial); p.c->type = t_harr; gsPop(); return arr_shAlloc((Arr*)p.c, r); } -static void harr_abandon(HArr_p p) { VTY(p.b, t_harrPartial); +static void harr_abandon_impl(HArr_p p) { VTY(p.b, t_harrPartial); gsPop(); + p.c->sh = &p.c->ia; // TODO more direct freeing + sprnk(p.c, 1); value_free((Value*)p.c); } // unsafe-ish things - don't allocate/GC anything before having written to all items + +#define m_harr0v(N) ({ usz n_ = (N); HArr_p r_ = m_harrUv(n_); for(usz i=0;iia; HArr_p r_ = m_harrUc(x_); for(usz i=0;iia; - usz i=0; HArr_p r = m_harrs(ia,&i); + M_HARR(r, ia) B* xp = arr_bptr(x); B xf = getFillQ(x); if (xp!=NULL) { - while (i < ia) { r.a[i] = squeeze_deep(inc(xp[i])); i++; } + for (usz i=0; iia!=2) thrM("β€’ReBQN: 𝕩.primitives must consist of glyph-primitive pairs"); if (!isC32(IGet(p, 0))) thrM("β€’ReBQN 𝕩.primitives: Glyphs must be characters"); B v = IGetU(p, 1); - i32 t = isFun(v)? 0 : isMd1(v) ? 1 : isMd2(v) ? 2 : 3; + i32 t = isFun(v)? 0 : isMd1(v)? 1 : isMd2(v)? 2 : 3; if (t==3) thrM("β€’ReBQN 𝕩.primitives: Primitives must be operations"); np[t]+= 1; } - usz i = 0; - HArr_p r = m_harrs(3, &i); + M_HARR(r, 3) u32* gl[3]; usz sum = 0; - for (; i < 3; i++) { + for (usz i = 0; i < 3; i++) { usz l = np[i]; - r.a[i] = m_c32arrv(gl+i, l); + HARR_ADD(r, i, m_c32arrv(gl+i, l)); np[i] = sum; sum+= l; } - harr_fv(r); + B rb = HARR_FV(r); - i = 0; - HArr_p prh = m_harrs(pia, &i); - B* rt = prh.a; - for (; i < pia; i++) { + HArr_p prh = m_harr0v(pia); + for (usz i = 0; i < pia; i++) { B gv = GetU(prim, i); B v = IGet(gv, 1); - i32 t = isFun(v) ? 0 : isMd1(v) ? 1 : isMd2(v) ? 2 : 3; + i32 t = isFun(v)? 0 : isMd1(v)? 1 : isMd2(v)? 2 : 3; *(gl[t]++) = o2cu(IGet(gv, 0)); - rt[np[t]++] = v; + prh.a[np[t]++] = v; } - set[1] = harr_fv(prh); - set[2] = inc(r.b); - set[0] = c1(load_compgen, r.b); + set[1] = prh.b; + set[2] = inc(rb); + set[0] = c1(load_compgen, rb); } } B getPrimitives() { @@ -274,18 +271,16 @@ B getPrimitives() { } B* pr = harr_ptr(r); B* gg = harr_ptr(g); - usz pi = 0; - HArr_p ph = m_harrs(a(r)->ia, &pi); + M_HARR(ph, a(r)->ia); for (usz gi = 0; gi < 3; gi++) { usz l = a(gg[gi])->ia; u32* gp = c32arr_ptr(gg[gi]); for (usz i = 0; i < l; i++) { - ph.a[pi] = m_hVec2(m_c32(gp[i]), inc(pr[i])); - pi++; + HARR_ADDA(ph, m_hVec2(m_c32(gp[i]), inc(pr[i]))); } pr+= l; } - return harr_fv(ph); + return HARR_FV(ph); } B rebqn_exec(B str, B path, B args, B o) { diff --git a/src/main.c b/src/main.c index 6daf7bc4..6fde601e 100644 --- a/src/main.c +++ b/src/main.c @@ -129,9 +129,9 @@ int main(int argc, char* argv[]) { if (i==argc) { args = emptySVec(); } else { - HArr_p ap = m_harrUv(argc-i); // eh whatever, erroring will exit anyways - for (i64 j = 0; j < argc-i; j++) ap.a[j] = fromUTF8l(argv[i+j]); - args = ap.b; + M_HARR(ap, argc-i) + for (usz j = 0; j < argc-i; j++) HARR_ADD(ap, j, fromUTF8l(argv[i+j])); + args = HARR_FV(ap); } B execRes; diff --git a/src/ns.c b/src/ns.c index 39fd5838..9bf75f95 100644 --- a/src/ns.c +++ b/src/ns.c @@ -94,9 +94,8 @@ Body* m_nnsDescF(i32 n, char** names) { if (emptyi32ptr==NULL) gc_add(emptyi32obj = m_i32arrv(&emptyi32ptr, 0)); incBy(emptyi32obj, 3); - usz i = 0; - HArr_p nl = m_harrs(n, &i); - for (; i < n; i++) nl.a[i] = m_str8l(names[i]); + M_HARR(nl, n) + for (usz i = 0; i < n; i++) HARR_ADD(nl, i, m_str8l(names[i])); Comp* comp = mm_alloc(sizeof(Comp), t_comp); comp->bc = emptyi32obj; @@ -105,7 +104,7 @@ Body* m_nnsDescF(i32 n, char** names) { comp->path = bi_N; comp->objs = c(HArr, emptyHVec()); comp->blockAm = 0; - comp->nameList = harr_fv(nl); + comp->nameList = HARR_FV(nl); Block* bl = mm_alloc(fsizeof(Block,bodies,Body*,0), t_block); bl->ty = 0; bl->imm = true; @@ -117,13 +116,13 @@ Body* m_nnsDescF(i32 n, char** names) { NSDesc* nd = mm_alloc(fsizeof(NSDesc, expGIDs, i32, n<2?2:n), t_nsDesc); nd->varAm = n; - for (i = 0; i < n; i++) nd->expGIDs[i] = str2gid(nl.a[i]); + for (usz i = 0; i < n; i++) nd->expGIDs[i] = str2gid(HARR_O(nl).a[i]); Body* body = m_body(n, 0, 0, 0); body->nsDesc = nd; body->bc = (u32*) emptyi32ptr; body->bl = bl; - for (i = 0; i < n; i++) { + for (usz i = 0; i < n; i++) { body->varData[i] = nd->expGIDs[i]; body->varData[i+n] = i; } diff --git a/src/utils/each.c b/src/utils/each.c index 2da68461..b8fa66bf 100644 --- a/src/utils/each.c +++ b/src/utils/each.c @@ -36,23 +36,22 @@ B eachd_fn(BBB2B f, B fo, B w, B x) { B bo = wg? w : x; usz ria = a(bo)->ia; - usz ri = 0; - HArr_p r = m_harrs(ria, &ri); - if (wr==xr) for(; ri < ria; ri++) r.a[ri] = f(fo, Get(w,ri), Get(x,ri)); - else if (wr==0) { B c=Get(w, 0); for(; ri < ria; ri++) r.a[ri] = f(fo, inc(c) , Get(x,ri)); dec(c); } - else if (xr==0) { B c=Get(x, 0); for(; ri < ria; ri++) r.a[ri] = f(fo, Get(w,ri), inc(c) ); dec(c); } + M_HARR(r, ria) + if (wr==xr) for(usz ri=0; ri0) { usz min = wg? a(x)->ia : a(w)->ia; usz ext = ria / min; - if (wg) for (usz i = 0; i < min; i++) { B c=Get(x,i); for (usz j = 0; j < ext; j++,ri++) r.a[ri] = f(fo, Get(w,ri), inc(c)); } - else for (usz i = 0; i < min; i++) { B c=Get(w,i); for (usz j = 0; j < ext; j++,ri++) r.a[ri] = f(fo, inc(c), Get(x,ri)); } + if (wg) for (usz i = 0; i < min; i++) { B c=Get(x,i); for (usz j = 0; j < ext; j++) HARR_ADDA(r, f(fo, Get(w,HARR_I(r)), inc(c))); } + else for (usz i = 0; i < min; i++) { B c=Get(w,i); for (usz j = 0; j < ext; j++) HARR_ADDA(r, f(fo, inc(c), Get(x,HARR_I(r)))); } } - B rb = harr_fc(r, bo); + B rb = HARR_FC(r, bo); dec(w); dec(x); return rb; } -B eachm_fn(BB2B f, B fo, B x) { +B eachm_fn(BB2B f, B fo, B x) { // TODO definitely rewrite this. Probably still has refcounting errors usz ia = a(x)->ia; if (ia==0) return x; SGet(x); @@ -68,10 +67,10 @@ B eachm_fn(BB2B f, B fo, B x) { for (; i < ia; i++) xp[i] = f(fo, mv(xp,i)); return REUSE(x); } else { - rH = m_harrs(ia, &i); - rH.a[i++] = cr; - for (; i < ia; i++) rH.a[i] = f(fo, inc(xp[i])); - return harr_fcd(rH, x); + M_HARR(rHc, ia) + HARR_ADD(rHc, i, cr); + for (usz i = 1; i < ia; i++) HARR_ADD(rHc, i, f(fo, inc(xp[i]))); + return HARR_FCD(rHc, x); } } else if (TI(x,elType)==el_i32) { i32* xp = i32any_ptr(x); @@ -82,7 +81,7 @@ B eachm_fn(BB2B f, B fo, B x) { for (; i < ia; i++) { cr = f(fo, m_i32(xp[i])); if (!q_i32(cr)) { - rH = m_harrs(ia, &i); + rH = m_harr0c(x); for (usz j = 0; j < i; j++) rH.a[j] = m_i32(rp[j]); dec(r); goto fallback; @@ -100,7 +99,7 @@ B eachm_fn(BB2B f, B fo, B x) { for (; i < ia; i++) { cr = f(fo, m_f64(xp[i])); if (!q_f64(cr)) { - rH = m_harrs(ia, &i); + rH = m_harr0c(x); for (usz j = 0; j < i; j++) rH.a[j] = m_f64(rp[j]); dec(r); goto fallback; @@ -118,17 +117,18 @@ B eachm_fn(BB2B f, B fo, B x) { for (; i < ia; i++) xp[i] = f(fo, mv(xp,i)); return REUSE(x); } else { - HArr_p rp = m_harrs(ia, &i); - rp.a[i++] = cr; - for (; i < ia; i++) rp.a[i] = f(fo, inc(xp[i])); - return harr_fcd(rp, x); + M_HARR(rHc, ia) + HARR_ADD(rHc, i, cr); + for (usz i = 1; i < ia; i++) HARR_ADD(rHc, i, f(fo, inc(xp[i]))); + return HARR_FCD(rHc, x); } } else - rH = m_harrs(ia, &i); + rH = m_harr0c(x); } else - rH = m_harrs(ia, &i); + rH = m_harr0c(x); fallback: rH.a[i++] = cr; for (; i < ia; i++) rH.a[i] = f(fo, Get(x,i)); - return harr_fcd(rH, x); + dec(x); + return rH.b; } \ No newline at end of file diff --git a/src/utils/file.c b/src/utils/file.c index 607dcd6a..f3d6e6da 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -72,18 +72,17 @@ B file_lines(B path) { // consumes; TODO rewrite this, it's horrible } } if (ia && (p[ia-1]!='\n' && p[ia-1]!='\r')) lineCount++; - usz i = 0; - HArr_p r = m_harrs(lineCount, &i); + M_HARR(r, lineCount) usz pos = 0; - while (i < lineCount) { + for (usz i = 0; i < lineCount; i++) { usz spos = pos; while(pos