From 979c31fc3e970ae50e5c968575d0f8bd1c2495fd Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sat, 22 Oct 2022 08:36:11 -0400 Subject: [PATCH] =?UTF-8?q?Table-based=20dyadic=20=E2=8A=92=20for=20intege?= =?UTF-8?q?rs=20up=20to=202=20bytes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/search.c | 52 ++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/builtins/search.c b/src/builtins/search.c index a2ba7fb7..b27f19b8 100644 --- a/src/builtins/search.c +++ b/src/builtins/search.c @@ -11,10 +11,12 @@ extern B or_c2(B,B,B); extern B sub_c2(B,B,B); extern B mul_c2(B,B,B); +static u64 elRange(u8 eltype) { return 1ull<<(1<ft? it : ft; /* Table allocation width */ \ + usz it = elRange(IN##e); /* Range of writes */ \ + usz ft = elRange(FOR##e); /* Range of lookups */ \ + usz t = it>ft? it : ft; /* Table allocation width */ \ TALLOC(TY, tab0, t); TY* tab = tab0 + t/2; \ usz m=IN##ia, n=FOR##ia; \ void* ip = tyany_ptr(IN); \ @@ -169,16 +171,40 @@ B memberOf_c2(B t, B w, B x) { extern B rt_count; B count_c2(B t, B w, B x) { if (!isArr(w) || RNK(w)==0) thrM("⊒: 𝕨 must have rank at least 1"); - if (RNK(w)==1) { - if (!isArr(x) || IA(x)<=1) return indexOf_c2(m_f64(0), w, x); - usz wia = IA(w); - usz xia = IA(x); - i32* rp; B r = m_i32arrc(&rp, x); - TALLOC(usz, wnext, wia+1); + if (RNK(w)!=1) return c2(rt_count, w, x); + if (!isArr(x) || IA(x)<=1) return indexOf_c2(m_f64(0), w, x); + u8 we = TI(w,elType); usz wia = IA(w); + u8 xe = TI(x,elType); usz xia = IA(x); + i32* rp; B r = m_i32arrc(&rp, x); + TALLOC(usz, wnext, wia+1); + wnext[wia] = wia; + if (we<=el_i16 && xe<=el_i16) { + if (we==el_bit) { w = toI8Any(w); we = TI(w,elType); } + if (xe==el_bit) { x = toI8Any(x); xe = TI(x,elType); } + usz it = elRange(we); // Range of writes + usz ft = elRange(xe); // Range of lookups + usz t = it>ft? it : ft; // Table allocation width + TALLOC(i32, tab0, t); i32* tab = tab0 + t/2; + usz m=wia, n=xia; + void* ip = tyany_ptr(w); + void* fp = tyany_ptr(x); + // Initialize + if (xe==el_i16 && na[p].val : wia; @@ -190,10 +216,10 @@ B count_c2(B t, B w, B x) { if (had) { j = map->a[p].val; map->a[p].val = wnext[j]; } rp[i] = j; } - TFREE(wnext); free_b2i(map); decG(w); decG(x); - return wia<=I8_MAX? taga(cpyI8Arr(r)) : wia<=I16_MAX? taga(cpyI16Arr(r)) : r; + free_b2i(map); } - return c2(rt_count, w, x); + TFREE(wnext); decG(w); decG(x); + return wia<=I8_MAX? taga(cpyI8Arr(r)) : wia<=I16_MAX? taga(cpyI16Arr(r)) : r; }