Move search functions to search.c

This commit is contained in:
Marshall Lochbaum 2022-10-19 20:17:47 -04:00
parent cc467ce8ca
commit 236d8dedd6
5 changed files with 128 additions and 115 deletions

View File

@ -212,7 +212,7 @@ ${bd}/%.o: src/jit/%.c
@echo $< | cut -c 5-
@$(CC_INC) $@.d -o $@ -c $<
builtins: ${addprefix ${bd}/, arithm.o arithd.o cmp.o sfns.o squeeze.o select.o slash.o group.o sort.o selfsearch.o md1.o md2.o fns.o sysfn.o internal.o inverse.o}
builtins: ${addprefix ${bd}/, arithm.o arithd.o cmp.o sfns.o squeeze.o select.o slash.o group.o sort.o search.o selfsearch.o md1.o md2.o fns.o sysfn.o internal.o inverse.o}
${bd}/%.o: src/builtins/%.c
@echo $< | cut -c 5-
@$(CC_INC) $@.d -o $@ -c $<

View File

@ -198,120 +198,11 @@ B fne_c2(B t, B w, B x) {
}
extern B rt_indexOf;
B indexOf_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) || RNK(x)==0) {
usz wia = IA(w);
B el = isArr(x)? IGetU(x,0) : x;
i32 res = wia;
if (TI(w,elType)==el_i32) {
if (q_i32(el)) {
i32* wp = i32any_ptr(w);
i32 v = o2iG(el);
for (usz i = 0; i < wia; i++) {
if (wp[i] == v) { res = i; break; }
}
}
} else {
SGetU(w)
for (usz i = 0; i < wia; i++) {
if (equal(GetU(w,i), el)) { res = i; break; }
}
}
decG(w); dec(x);
i32* rp; Arr* r = m_i32arrp(&rp, 1);
arr_shAlloc(r, 0);
rp[0] = res;
return taga(r);
} else {
usz wia = IA(w);
usz xia = IA(x);
// TODO O(wia×xia) for small wia or xia
i32* rp; B r = m_i32arrc(&rp, x);
H_b2i* map = m_b2i(64);
SGetU(x)
SGetU(w)
for (usz i = 0; i < wia; i++) {
bool had; u64 p = mk_b2i(&map, GetU(w,i), &had);
if (!had) map->a[p].val = i;
}
for (usz i = 0; i < xia; i++) rp[i] = getD_b2i(map, GetU(x,i), wia);
free_b2i(map); decG(w); decG(x);
return wia<=I8_MAX? taga(cpyI8Arr(r)) : wia<=I16_MAX? taga(cpyI16Arr(r)) : r;
}
}
return c2(rt_indexOf, w, x);
}
B enclosed_0;
B enclosed_1;
extern B rt_memberOf;
extern B eq_c2(B,B,B);
extern B or_c2(B,B,B);
B memberOf_c2(B t, B w, B x) {
if (isAtm(x) || RNK(x)!=1) goto bad;
if (isAtm(w)) goto single;
ur wr = RNK(w);
if (wr==0) {
B w0 = IGet(w, 0);
dec(w);
w = w0;
goto single;
}
if (wr==1) goto many;
goto bad;
bad: return c2(rt_memberOf, w, x);
B r;
single: {
usz xia = IA(x);
SGetU(x)
for (usz i = 0; i < xia; i++) if (equal(GetU(x, i), w)) { r = inc(enclosed_1); goto dec_wx; }
r = inc(enclosed_0);
dec_wx:; dec(w);
goto dec_x;
}
many: {
u8 we = TI(w,elType); usz wia = IA(w);
u8 xe = TI(x,elType); usz xia = IA(x);
if (xia<=16 && wia>16 && we<el_B && xe<el_B) {
SGetU(x);
Arr* ba=allZeroes(wia); arr_shVec(ba); r=taga(ba);
for (usz i=0; i<xia; i++) r = or_c2(m_f64(0), r, eq_c2(m_f64(0), inc(w), GetU(x,i)));
decG(w);
goto dec_x;
}
// TODO O(wia×xia) for small wia or xia
H_Sb* set = m_Sb(64);
SGetU(x) SGetU(w)
bool had;
for (usz i = 0; i < xia; i++) mk_Sb(&set, GetU(x,i), &had);
u64* rp; r = m_bitarrv(&rp, wia);
for (usz i = 0; i < wia; i++) bitp_set(rp, i, has_Sb(set, GetU(w,i)));
free_Sb(set); decG(w);
goto dec_x;
}
dec_x:;
decG(x);
return r;
}
extern B rt_find;
B find_c2(B t, B w, B x) {
return c2(rt_find, w, x);
}
extern B rt_count;
B count_c2(B t, B w, B x) {
return c2(rt_count, w, x);
}
static H_b2i* prevImports;
i32 getPrevImport(B path) { // -1 for unset, -2 for unfinished
if (prevImports==NULL) prevImports = m_b2i(16);
@ -380,8 +271,6 @@ void profiler_freeMap(void* mapRaw) {
void fun_gcFn() {
if (prevImports!=NULL) mm_visitP(prevImports);
if (globalNames!=NULL) mm_visitP(globalNames);
mm_visit(enclosed_0);
mm_visit(enclosed_1);
mm_visit(globalNameList);
for (i32 i = 0; i < 3; i++) mm_visitP(bitUD[i]);
}
@ -400,8 +289,6 @@ void fns_init() {
TIi(t_funBI,fn_uc1) = funBI_uc1;
TIi(t_funBI,fn_ucw) = funBI_ucw;
TIi(t_funBI,fn_im) = funBI_im;
{ u64* p; Arr* a=m_bitarrp(&p, 1); arr_shAlloc(a,0); *p= 0; enclosed_0=taga(a); }
{ u64* p; Arr* a=m_bitarrp(&p, 1); arr_shAlloc(a,0); *p=~0ULL; enclosed_1=taga(a); }
bitUD[0] = a(emptyIVec());
{ u64* p; B a=m_bitarrv(&p, 1); *p=0; bitUD[1] = a(a); }
{ u64* p; B a=m_bitarrv(&p, 2); *p=0; bitp_set(p,1,1); bitUD[2] = a(a); }

125
src/builtins/search.c Normal file
View File

@ -0,0 +1,125 @@
#include "../core.h"
#include "../utils/hash.h"
#include "../utils/mut.h"
#include "../utils/talloc.h"
extern B rt_indexOf;
B indexOf_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) || RNK(x)==0) {
usz wia = IA(w);
B el = isArr(x)? IGetU(x,0) : x;
i32 res = wia;
if (TI(w,elType)==el_i32) {
if (q_i32(el)) {
i32* wp = i32any_ptr(w);
i32 v = o2iG(el);
for (usz i = 0; i < wia; i++) {
if (wp[i] == v) { res = i; break; }
}
}
} else {
SGetU(w)
for (usz i = 0; i < wia; i++) {
if (equal(GetU(w,i), el)) { res = i; break; }
}
}
decG(w); dec(x);
i32* rp; Arr* r = m_i32arrp(&rp, 1);
arr_shAlloc(r, 0);
rp[0] = res;
return taga(r);
} else {
usz wia = IA(w);
usz xia = IA(x);
// TODO O(wia×xia) for small wia or xia
i32* rp; B r = m_i32arrc(&rp, x);
H_b2i* map = m_b2i(64);
SGetU(x)
SGetU(w)
for (usz i = 0; i < wia; i++) {
bool had; u64 p = mk_b2i(&map, GetU(w,i), &had);
if (!had) map->a[p].val = i;
}
for (usz i = 0; i < xia; i++) rp[i] = getD_b2i(map, GetU(x,i), wia);
free_b2i(map); decG(w); decG(x);
return wia<=I8_MAX? taga(cpyI8Arr(r)) : wia<=I16_MAX? taga(cpyI16Arr(r)) : r;
}
}
return c2(rt_indexOf, w, x);
}
B enclosed_0;
B enclosed_1;
extern B rt_memberOf;
extern B eq_c2(B,B,B);
extern B or_c2(B,B,B);
B memberOf_c2(B t, B w, B x) {
if (isAtm(x) || RNK(x)!=1) goto bad;
if (isAtm(w)) goto single;
ur wr = RNK(w);
if (wr==0) {
B w0 = IGet(w, 0);
dec(w);
w = w0;
goto single;
}
if (wr==1) goto many;
goto bad;
bad: return c2(rt_memberOf, w, x);
B r;
single: {
usz xia = IA(x);
SGetU(x)
for (usz i = 0; i < xia; i++) if (equal(GetU(x, i), w)) { r = inc(enclosed_1); goto dec_wx; }
r = inc(enclosed_0);
dec_wx:; dec(w);
goto dec_x;
}
many: {
u8 we = TI(w,elType); usz wia = IA(w);
u8 xe = TI(x,elType); usz xia = IA(x);
if (xia<=16 && wia>16 && we<el_B && xe<el_B) {
SGetU(x);
Arr* ba=allZeroes(wia); arr_shVec(ba); r=taga(ba);
for (usz i=0; i<xia; i++) r = or_c2(m_f64(0), r, eq_c2(m_f64(0), inc(w), GetU(x,i)));
decG(w);
goto dec_x;
}
// TODO O(wia×xia) for small wia or xia
H_Sb* set = m_Sb(64);
SGetU(x) SGetU(w)
bool had;
for (usz i = 0; i < xia; i++) mk_Sb(&set, GetU(x,i), &had);
u64* rp; r = m_bitarrv(&rp, wia);
for (usz i = 0; i < wia; i++) bitp_set(rp, i, has_Sb(set, GetU(w,i)));
free_Sb(set); decG(w);
goto dec_x;
}
dec_x:;
decG(x);
return r;
}
extern B rt_count;
B count_c2(B t, B w, B x) {
return c2(rt_count, w, x);
}
void search_gcFn() {
mm_visit(enclosed_0);
mm_visit(enclosed_1);
}
void search_init() {
gc_addFn(search_gcFn);
{ u64* p; Arr* a=m_bitarrp(&p, 1); arr_shAlloc(a,0); *p= 0; enclosed_0=taga(a); }
{ u64* p; Arr* a=m_bitarrp(&p, 1); arr_shAlloc(a,0); *p=~0ULL; enclosed_1=taga(a); }
}

View File

@ -5,7 +5,7 @@
#include "ns.h"
#include "builtins.h"
#define FOR_INIT(F) F(base) F(harr) F(mutF) F(fillarr) F(tyarr) F(hash) F(sfns) F(fns) F(arith) F(md1) F(md2) F(derv) F(comp) F(rtWrap) F(ns) F(nfn) F(sysfn) F(inverse) F(slash) F(load) F(sysfnPost) F(dervPost) F(ffi) F(mmap) F(typesFinished)
#define FOR_INIT(F) F(base) F(harr) F(mutF) F(fillarr) F(tyarr) F(hash) F(sfns) F(fns) F(arith) F(md1) F(md2) F(derv) F(comp) F(rtWrap) F(ns) F(nfn) F(sysfn) F(inverse) F(slash) F(search) F(load) F(sysfnPost) F(dervPost) F(ffi) F(mmap) F(typesFinished)
#define F(X) void X##_init(void);
FOR_INIT(F)
#undef F

View File

@ -20,6 +20,7 @@
#include "../builtins/group.c"
#include "../builtins/sysfn.c"
#include "../builtins/sort.c"
#include "../builtins/search.c"
#include "../builtins/selfsearch.c"
#include "../builtins/arithm.c"
#include "../builtins/arithd.c"