explicit switches in mut.h & update comments

This commit is contained in:
dzaima 2022-06-08 00:54:33 +03:00
parent b48b184f14
commit f74f355360

View File

@ -4,12 +4,12 @@
Start with MAKE_MUT(name, itemAmount);
MAKE_MUT allocates the object on the stack, so everything must happen within the scope of it.
Optionally, call mut_init(name, el_something) with an appropriate ElType
Optionally, call mut_init(name, el_something) with an appropriate ElType.
End with mut_f(v|c|cd|p);
There must be no allocations while a mut object is being built so GC doesn't do bad things.
mut_pfree must be used to free a partially finished `mut` instance safely (e.g. before throwing an error)
methods ending with G expect that mut_init has been called with a type that can fit the elements that it'll set
mut_pfree must be used to free a partially finished `mut` instance safely (e.g. before throwing an error).
Methods ending with G expect that mut_init has been called with a type that can fit the elements that it'll set, and MUTG_INIT afterwards in the same scope.
*/
typedef struct Mut Mut;
@ -168,36 +168,39 @@ FORCE_INLINE B vec_join_inline(B w, B x) {
u64 wsz = mm_size(v(w));
u8 wt = v(w)->type;
// TODO f64∾i32, i32∾i8, c32∾c8 etc
if (wt==t_bitarr && BITARR_SZ( ria)<wsz && TI(x,elType)==el_bit) { a(w)->ia=ria; bit_cpy(bitarr_ptr(w),wia,bitarr_ptr(x),0,xia); decG(x); return FL_KEEP(w,fl_squoze); }
if (wt==t_i8arr && TYARR_SZ(I8, ria)<wsz && TI(x,elType)==el_i8 ) { a(w)->ia=ria; memcpy(i8arr_ptr (w)+wia, i8any_ptr (x), xia*1); decG(x); return FL_KEEP(w,fl_squoze); }
if (wt==t_i16arr && TYARR_SZ(I16,ria)<wsz && TI(x,elType)==el_i16) { a(w)->ia=ria; memcpy(i16arr_ptr(w)+wia, i16any_ptr(x), xia*2); decG(x); return FL_KEEP(w,fl_squoze); }
if (wt==t_i32arr && TYARR_SZ(I32,ria)<wsz && TI(x,elType)==el_i32) { a(w)->ia=ria; memcpy(i32arr_ptr(w)+wia, i32any_ptr(x), xia*4); decG(x); return FL_KEEP(w,fl_squoze); }
if (wt==t_c8arr && TYARR_SZ(C8, ria)<wsz && TI(x,elType)==el_c8 ) { a(w)->ia=ria; memcpy(c8arr_ptr (w)+wia, c8any_ptr (x), xia*1); decG(x); return FL_KEEP(w,fl_squoze); }
if (wt==t_c16arr && TYARR_SZ(C16,ria)<wsz && TI(x,elType)==el_c16) { a(w)->ia=ria; memcpy(c16arr_ptr(w)+wia, c16any_ptr(x), xia*2); decG(x); return FL_KEEP(w,fl_squoze); }
if (wt==t_c32arr && TYARR_SZ(C32,ria)<wsz && TI(x,elType)==el_c32) { a(w)->ia=ria; memcpy(c32arr_ptr(w)+wia, c32any_ptr(x), xia*4); decG(x); return FL_KEEP(w,fl_squoze); }
if (wt==t_f64arr && TYARR_SZ(F64,ria)<wsz && TI(x,elType)==el_f64) { a(w)->ia=ria; memcpy(f64arr_ptr(w)+wia, f64any_ptr(x), xia*8); decG(x); return FL_KEEP(w,fl_squoze); }
if (wt==t_harr && fsizeof(HArr,a,B,ria)<wsz) {
a(w)->ia = ria;
B* rp = harr_ptr(w)+wia;
u8 xe = TI(x,elType);
B* xpB = arr_bptr(x);
if (xpB!=NULL) {
memcpy(rp, xpB, xia*sizeof(B));
for (usz i = 0; i < xia; i++) inc(rp[i]);
}
else if (xe==el_i8 ) { i8* xp=i8any_ptr (x); for (usz i=0; i<xia; i++) rp[i] = m_i32(xp[i]); }
else if (xe==el_i16) { i16* xp=i16any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_i32(xp[i]); }
else if (xe==el_i32) { i32* xp=i32any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_i32(xp[i]); }
else if (xe==el_c8 ) { u8* xp=c8any_ptr (x); for (usz i=0; i<xia; i++) rp[i] = m_c32(xp[i]); }
else if (xe==el_c16) { u16* xp=c16any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_c32(xp[i]); }
else if (xe==el_c32) { u32* xp=c32any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_c32(xp[i]); }
else if (xe==el_f64) { f64* xp=f64any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_f64(xp[i]); }
else {
SGet(x)
for (usz i = 0; i < xia; i++) rp[i] = Get(x, i);
}
decG(x);
return FL_KEEP(w,fl_squoze); // keeping fl_squoze as appending items can't make the smallest item smaller
switch (wt) {
case t_bitarr: if (BITARR_SZ( ria)<wsz && TI(x,elType)==el_bit) { a(w)->ia=ria; bit_cpy(bitarr_ptr(w),wia,bitarr_ptr(x),0,xia); decG(x); return FL_KEEP(w,fl_squoze); } break;
case t_i8arr: if (TYARR_SZ(I8, ria)<wsz && TI(x,elType)==el_i8 ) { a(w)->ia=ria; memcpy(i8arr_ptr (w)+wia, i8any_ptr (x), xia*1); decG(x); return FL_KEEP(w,fl_squoze); } break;
case t_i16arr: if (TYARR_SZ(I16,ria)<wsz && TI(x,elType)==el_i16) { a(w)->ia=ria; memcpy(i16arr_ptr(w)+wia, i16any_ptr(x), xia*2); decG(x); return FL_KEEP(w,fl_squoze); } break;
case t_i32arr: if (TYARR_SZ(I32,ria)<wsz && TI(x,elType)==el_i32) { a(w)->ia=ria; memcpy(i32arr_ptr(w)+wia, i32any_ptr(x), xia*4); decG(x); return FL_KEEP(w,fl_squoze); } break;
case t_c8arr: if (TYARR_SZ(C8, ria)<wsz && TI(x,elType)==el_c8 ) { a(w)->ia=ria; memcpy(c8arr_ptr (w)+wia, c8any_ptr (x), xia*1); decG(x); return FL_KEEP(w,fl_squoze); } break;
case t_c16arr: if (TYARR_SZ(C16,ria)<wsz && TI(x,elType)==el_c16) { a(w)->ia=ria; memcpy(c16arr_ptr(w)+wia, c16any_ptr(x), xia*2); decG(x); return FL_KEEP(w,fl_squoze); } break;
case t_c32arr: if (TYARR_SZ(C32,ria)<wsz && TI(x,elType)==el_c32) { a(w)->ia=ria; memcpy(c32arr_ptr(w)+wia, c32any_ptr(x), xia*4); decG(x); return FL_KEEP(w,fl_squoze); } break;
case t_f64arr: if (TYARR_SZ(F64,ria)<wsz && TI(x,elType)==el_f64) { a(w)->ia=ria; memcpy(f64arr_ptr(w)+wia, f64any_ptr(x), xia*8); decG(x); return FL_KEEP(w,fl_squoze); } break;
case t_harr: if (fsizeof(HArr,a,B,ria)<wsz) {
a(w)->ia = ria;
B* rp = harr_ptr(w)+wia;
B* xpB = arr_bptr(x);
if (xpB!=NULL) {
memcpy(rp, xpB, xia*sizeof(B));
for (usz i = 0; i < xia; i++) inc(rp[i]);
} else {
switch(TI(x,elType)) {
case el_i8: { i8* xp=i8any_ptr (x); for (usz i=0; i<xia; i++) rp[i] = m_i32(xp[i]); } break;
case el_i16: { i16* xp=i16any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_i32(xp[i]); } break;
case el_i32: { i32* xp=i32any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_i32(xp[i]); } break;
case el_c8: { u8* xp=c8any_ptr (x); for (usz i=0; i<xia; i++) rp[i] = m_c32(xp[i]); } break;
case el_c16: { u16* xp=c16any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_c32(xp[i]); } break;
case el_c32: { u32* xp=c32any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_c32(xp[i]); } break;
case el_f64: { f64* xp=f64any_ptr(x); for (usz i=0; i<xia; i++) rp[i] = m_f64(xp[i]); } break;
default:; SGet(x)
for (usz i = 0; i < xia; i++) rp[i] = Get(x, i);
break;
}
}
decG(x);
return FL_KEEP(w,fl_squoze); // keeping fl_squoze as appending items can't make the smallest item smaller
} break;
}
}
MAKE_MUT(r, ria); mut_init(r, el_or(TI(w,elType), TI(x,elType)));
@ -213,18 +216,20 @@ static inline bool inplace_add(B w, B x) { // consumes x if returns true; fails
if (reusable(w)) {
u64 wsz = mm_size(v(w));
u8 wt = v(w)->type;
if (wt==t_bitarr && BITARR_SZ( ria)<wsz && q_bit(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; bitp_set(bitarr_ptr(w),wia,o2bu(x)); return true; }
if (wt==t_i8arr && TYARR_SZ(I8, ria)<wsz && q_i8 (x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; i8arr_ptr (w)[wia]=o2iu(x); return true; }
if (wt==t_i16arr && TYARR_SZ(I16,ria)<wsz && q_i16(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; i16arr_ptr(w)[wia]=o2iu(x); return true; }
if (wt==t_i32arr && TYARR_SZ(I32,ria)<wsz && q_i32(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; i32arr_ptr(w)[wia]=o2iu(x); return true; }
if (wt==t_c8arr && TYARR_SZ(C8, ria)<wsz && q_c8 (x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; c8arr_ptr (w)[wia]=o2cu(x); return true; }
if (wt==t_c16arr && TYARR_SZ(C16,ria)<wsz && q_c16(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; c16arr_ptr(w)[wia]=o2cu(x); return true; }
if (wt==t_c32arr && TYARR_SZ(C32,ria)<wsz && q_c32(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; c32arr_ptr(w)[wia]=o2cu(x); return true; }
if (wt==t_f64arr && TYARR_SZ(F64,ria)<wsz && q_f64(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; f64arr_ptr(w)[wia]=o2fu(x); return true; }
if (wt==t_harr && fsizeof(HArr,a,B,ria)<wsz) {
a(FL_KEEP(w,fl_squoze))->ia = ria;
harr_ptr(w)[wia] = x;
return true;
switch (wt) {
case t_bitarr: if (BITARR_SZ( ria)<wsz && q_bit(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; bitp_set(bitarr_ptr(w),wia,o2bu(x)); return true; } break;
case t_i8arr: if (TYARR_SZ(I8, ria)<wsz && q_i8 (x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; i8arr_ptr (w)[wia]=o2iu(x); return true; } break;
case t_i16arr: if (TYARR_SZ(I16,ria)<wsz && q_i16(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; i16arr_ptr(w)[wia]=o2iu(x); return true; } break;
case t_i32arr: if (TYARR_SZ(I32,ria)<wsz && q_i32(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; i32arr_ptr(w)[wia]=o2iu(x); return true; } break;
case t_c8arr: if (TYARR_SZ(C8, ria)<wsz && q_c8 (x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; c8arr_ptr (w)[wia]=o2cu(x); return true; } break;
case t_c16arr: if (TYARR_SZ(C16,ria)<wsz && q_c16(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; c16arr_ptr(w)[wia]=o2cu(x); return true; } break;
case t_c32arr: if (TYARR_SZ(C32,ria)<wsz && q_c32(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; c32arr_ptr(w)[wia]=o2cu(x); return true; } break;
case t_f64arr: if (TYARR_SZ(F64,ria)<wsz && q_f64(x)) { a(FL_KEEP(w,fl_squoze))->ia=ria; f64arr_ptr(w)[wia]=o2fu(x); return true; } break;
case t_harr: if (fsizeof(HArr,a,B,ria)<wsz) {
a(FL_KEEP(w,fl_squoze))->ia = ria;
harr_ptr(w)[wia] = x;
return true;
} break;
}
}
return false;