📄 aggr.mx
字号:
/* arithprod3 @6: sht / int / lng / flt / dbl b/bn-tail-type# not used any more# @7: "loc" for fixsized b/bn-tail-type, b/bn-tail-access# "var" for varsized b/bn-tail-type (only loc used currently) @9: result type*/@= arithprod3static intaggrX3_prod3_@1_@2_EarrayBvoidGvoid (BAT *bn, BAT *b, BAT *g, BAT *e, size_t range, oid min, oid max){ @2 zero = (@2) 1, *prods; BUN p, q, r; int xx; oid bhsb; size_t base, end, w; ssize_t offset; ALGODEBUG THRprintf(GDKout, "#aggrX3_prod3_@1_@2_EarrayBvoidGvoid(bn=%s,b=%s,g=%s,e=%s,range=" SZFMT ",min=" SZFMT ",max=" SZFMT ");\n", BATgetId(bn),BATgetId(b),BATgetId(g),BATgetId(e), range,(size_t) min,(size_t) max); @:aggrX3_prod3(1,0,CHKrange,loc,&bhsb;bhsb++,@1,&prods[(*(oid*)h)-min],,@2)@ return GDK_SUCCEED;}intCMDaggrX3_prod3_@1_@2(BAT **ret, BAT *b, BAT *g, BAT *e){ BAT *bn = BATnew(e->htype, TYPE_@2, BATcount(e)); @2 zero = (@2) 1, *prods; BUN p, q, r; int xx; size_t range; oid min, max; oid bhsb; size_t base, end, w; ssize_t offset; if( bn == NULL) return GDK_FAIL; ALGODEBUG THRprintf(GDKout, "#CMDaggrX3_prod3_@1_@2(b=%s,g=%s,e=%s);\n", BATgetId(b),BATgetId(g),BATgetId(e));/* ERRORcheck((BATcount(b)!=BATcount(g)), "CMDaggrX3_prod3_@1: BATs b & g must be of equal size.\n"); */ ERRORcheck((g->htype == TYPE_void) && (g->hseqbase == oid_nil) && (BATcount(g) > 0), "CMDaggrX3_prod3_@1: head of BAT g must not be nil.\n"); if((g->hseqbase == oid_nil) || (!BAThdense(g)) || (!(BAThordered(b)&1)) || (!g->hkey)) { return CMDaggrX3_prod_@1_@2(ret, BATjoin(BATmirror(g),b,oid_nil), e); } *ret = bn; /* init: set all prods to zero and calculate min/max oid */ @:init_result(@2)@ range = max - min + 1; /* scan b & g, and calculate prods */ if (e->htype == TYPE_void) { /* void lookup */ if (b->htype == TYPE_void) { if ((g->htype == TYPE_void)&&(BATcount(b)==BATcount(g))&&(b->hseqbase==g->hseqbase)) { @:aggrX3_prod3(0,0,BUNfndVOID,var,&bhsb;bhsb++,@1,BUNtloc(bn,r),,@2)@ } else { @:aggrX3_prod3(0,0,BUNfndVOID,var,&bhsb;bhsb++,@1,BUNtloc(bn,r),if (w >= base && w < end),@2)@ } } else { @:aggrX3_prod3(0,0,BUNfndVOID,var,BUNhloc(b,p),@1,BUNtloc(bn,r),if (w >= base && w < end),@2)@ } /* e->htype == TYPE_oid */ } else if (range > SMALL_AGGR_MAX) { /* hash lookup */ if (b->htype == TYPE_void) { if ((g->htype == TYPE_void)&&(BATcount(b)==BATcount(g))&&(b->hseqbase==g->hseqbase)) { @:aggrX3_prod3(0,1,HASHfnd_oid,loc,&bhsb;bhsb++,@1,BUNtloc(bn,r),,@2)@ } else { @:aggrX3_prod3(0,1,HASHfnd_oid,loc,&bhsb;bhsb++,@1,BUNtloc(bn,r),if (w >= base && w < end),@2)@ } } else { @:aggrX3_prod3(0,1,HASHfnd_oid,loc,BUNhloc(b,p),@1,BUNtloc(bn,r),if (w >= base && w < end),@2)@ } } else { /* array lookup */ if (b->htype == TYPE_void) { if ((g->htype == TYPE_void)&&(BATcount(b)==BATcount(g))&&(b->hseqbase==g->hseqbase)) { return aggrX3_prod3_@1_@2_EarrayBvoidGvoid (bn, b, g, e, range, min, max); } else { @:aggrX3_prod3(1,0,CHKrange,loc,&bhsb;bhsb++,@1,&prods[(*(oid*)h)-min],if (w >= base && w < end),@2)@ } } else { @:aggrX3_prod3(1,0,CHKrange,loc,BUNhloc(b,p),@1,&prods[(*(oid*)h)-min],if (w >= base && w < end),@2)@ } } return GDK_SUCCEED;}@c/* arithsumprod3 @6: sht / int / lng / flt / dbl b/bn-tail-type# not used any more# @7: "loc" for fixsized b/bn-tail-type, b/bn-tail-access# "var" for varsized b/bn-tail-type (only loc used currently) @9: result type*/@= arithsumprod3@:arithsum3(@1,@2)@@:arithprod3(@1,@2)@@c@:arithsumprod3(sht,sht)@@:arithsumprod3(sht,int)@@:arithsumprod3(sht,lng)@@:arithsumprod3(int,int)@@:arithsumprod3(oid,int)@@:arithsumprod3(int,lng)@@:arithsumprod3(lng,lng)@@:arithsumprod3(flt,flt)@@:arithsumprod3(flt,dbl)@@:arithsumprod3(dbl,dbl)@/* arithavg3 @6: sht / int / lng / flt / dbl b/bn-tail-type @7: "loc" for fixsized b/bn-tail-type, b/bn-tail-access "var" for varsized b/bn-tail-type (only loc used currently) @9: result type*/@= arithavg3intCMDaggrX3_avg3_@1(BAT **ret, BAT *b, BAT *g, BAT *e){ BAT *bn = BATnew(e->htype, TYPE_dbl, BATcount(e)); int xx; size_t off; size_t *cnt; size_t range; oid min, max; dbl zero = (dbl) 0, *sums; size_t slots; BUN p, q, r; oid bhsb; size_t base, end, w; ssize_t offset; if( bn == NULL) return GDK_FAIL; off = BUNindex(bn,BUNfirst(bn)); ALGODEBUG THRprintf(GDKout, "#CMDaggrX3_avg3_@1[@2](b=%s,g=%s,e=%s);\n", BATgetId(b),BATgetId(g),BATgetId(e));/* ERRORcheck((BATcount(b)!=BATcount(g)), "CMDaggrX3_avg3_@1: BATs b & g must be of equal size.\n"); */ ERRORcheck((g->htype == TYPE_void) && (g->hseqbase == oid_nil) && (BATcount(g) > 0), "CMDaggrX3_avg3_@1: head of BAT g must not be nil.\n"); if((g->hseqbase == oid_nil) || (!BAThdense(g)) || (!(BAThordered(b)&1)) || (!g->hkey)) { return CMDaggrX3_avg_@1(ret, BATjoin(BATmirror(g),b,oid_nil), e); } /* init: set all sums & counts to zero and calculate min/max oid */ @:init_result(dbl)@ range = max - min + 1; /* scan b & g, calculate sums & counts, derive averages */ if (e->htype == TYPE_void) { /* void lookup */ slots = BATcount(e); if (b->htype == TYPE_void) { @:aggrX3_avg3(0,0,BUNfndVOID,var,&bhsb;bhsb++,@1,@2,BUNt@2(bn,r),BUNindex(bn,r)-off)@ } else { @:aggrX3_avg3(0,0,BUNfndVOID,var,BUNhloc(b,p),@1,@2,BUNt@2(bn,r),BUNindex(bn,r)-off)@ } /* e->htype == TYPE_oid */ } else if (range > SMALL_AGGR_MAX) { /* hash lookup */ slots = BATcount(e); if (b->htype == TYPE_void) { @:aggrX3_avg3(0,1,HASHfnd_oid,loc,&bhsb;bhsb++,@1,@2,BUNt@2(bn,r),BUNindex(bn,r)-off)@ } else { @:aggrX3_avg3(0,1,HASHfnd_oid,loc,BUNhloc(b,p),@1,@2,BUNt@2(bn,r),BUNindex(bn,r)-off)@ } } else { /* array lookup */ slots = range; if (b->htype == TYPE_void) { @:aggrX3_avg3(1,0,CHKrange,loc,&bhsb;bhsb++,@1,@2,&sums[(*(oid*)h)-min],(*(oid*)h)-min)@ } else { @:aggrX3_avg3(1,0,CHKrange,loc,BUNhloc(b,p),@1,@2,&sums[(*(oid*)h)-min],(*(oid*)h)-min)@ } } *ret = bn; return GDK_SUCCEED;}@c@:arithavg3(sht,loc)@@:arithavg3(int,loc)@@:arithavg3(lng,loc)@@:arithavg3(flt,loc)@@:arithavg3(dbl,loc)@@- "Extremes" (Min & Max)@c/* aggrX3_minmax3_ptr e-void-head e-oid-head e-oid-head void-lookup hash-lookup array-lookup @1: </> </> </> compare operator: <(min) or >(max) @2: 0 1 0 do BATprepareHash? @3: BUNfndVOID HASHfnd_oid CHKrange lookup @4: var loc loc e/bn-head-access @5: "BUNhloc(b,p)" for (oid) b-head-type, b-head access "&bhsb; bhsb++" for (void) b-head-type @6: (u)chr / sht / int / lng / any b/bn-tail-storage-type @7: "loc" for fixsized b/bn-tail-type, b/bn-tail-access "var" for varsized b/bn-tail-type @8: BUNindex(bn,r)-off (*(oid*)h)-min index in extremes array @9: max/min initial result value*/@= aggrX3_minmax3_ptr ALGODEBUG THRprintf(GDKout, "#aggrX3_minmax3_ptr(@1,@2,@3,@4,@5,@6,@7,@8,@9);\n"); if (@2 && BATprepareHash(e)) { BBPreclaim(e); return GDK_FAIL; } /* init: create extremes and set all to the zero pointer */ extremes = (ptr*) GDKmalloc(slots*sizeof(ptr)); memset(extremes, 0, slots*sizeof(ptr)); /* scan b & g and replace totals by the extreme value (just pointers to vals in b) */ base = BUNindex(g, BUNfirst(g)); offset = (ssize_t) (base - g->hseqbase); end = base + BATcount(g); bhsb = b->hseqbase; BATloopFast(b, p, q, xx) { ptr t = BUNt@7(b,p); oid v = *(oid*) @5; w = offset + v; if (w >= base && w < end) { oid *h = (oid*) BUNtloc(g, BUNptr(g, w)); @3(r, bn, (oid*)h); if (r) { ptr *val = &extremes[@8]; if (*val != nil) { if ((*cmp)(t,nil) == 0) { *val = nil; } else if (*val == NULL || (*cmp)(t,*val) @1 0) { *val = t; } } } } } /* insert the extreme values into the result */ BATloopFast(bn, r, q, xx) { oid *h = (oid*)BUNh@4(bn,r); ptr val = extremes[@8]; (void) h; /* silence compiler about unused variable */ ATOMput(bn->ttype, bn->theap, BUNtloc(bn,r), val?val:nil); }@c/* aggrX3_minmax3_oid e-void-head e-oid-head e-oid-head void-lookup hash-lookup array-lookup @1: </> </> </> compare operator: <(min) or >(max) @2: 0 1 0 do BATprepareHash? @3: BUNfndVOID HASHfnd_oid CHKrange lookup @4: var loc loc e/bn-head-access @5: "BUNhloc(b,p)" for (oid) b-head-type, b-head access "&bhsb; bhsb++" for (void) b-head-type @6: (u)chr / sht / int / lng / any b/bn-tail-storage-type @7: "loc" for fixsized b/bn-tail-type, b/bn-tail-access "var" for varsized b/bn-tail-type @8: BUNindex(bn,r)-off (*(oid*)h)-min index in extremes array @9: max/min initial result value*/@= aggrX3_minmax3_oid ALGODEBUG THRprintf(GDKout, "#aggrX3_minmax3_oid(@1,@2,@3,@4,@5,@6,@7,@8,@9);\n"); if (@2 && BATprepareHash(e)) { BBPreclaim(e); return GDK_FAIL; } /* init: create extremes and set all to the zero pointer */ extremes = (oid*) GDKmalloc(slots*sizeof(oid)); { size_t i = 0; for (; i < slots; i++ ) extremes[i] = GDK_oid_@9; } /* scan b & g and replace totals by the extreme value (just pointers to vals in b) */ base = BUNindex(g, BUNfirst(g)); offset = (ssize_t) (base - g->hseqbase); end = base + BATcount(g); bhsb = b->hseqbase; BATloopFast(b, p, q, xx) { ptr t = BUNt@7(b,p); oid v = *(oid*) @5; w = offset + v; if (w >= base && w < end) { oid *h = (oid*) BUNtloc(g, BUNptr(g, w)); @3(r, bn, (oid*)h); if (r) { oid *val = &extremes[@8]; if (*val != *(oid*)nil) { if ((*cmp)(t,nil) == 0) { *val = *(oid*)nil; } else if ((*cmp)(t,val) @1 0) { *val = *(oid*)t; } } } } } /* insert the extreme values into the result */ BATloopFast(bn, r, q, xx) { oid *h = (oid*)BUNh@4(bn,r); oid val = extremes[@8]; (void) h; /* silence compiler about unused variable */ ATOMput(bn->ttype, bn->theap, BUNtloc(bn,r), &val); }@c/* extreme_typed @1: min/max @2: < / > @3: b/bn-tail-storage-type (u) chr/sht/int/lng/any @4: "loc" for fixsized b/bn-tail-type, b/bn-tail-access "var" for varsized b/bn-tail-type @5: ptr/oid void/any @6: max/min initial result value*/@= extreme3_typedstatic intaggrX3_@13_@3_@4_@5(BAT **ret, BAT *b, BAT *g, BAT *e){ BAT *bn = BATnew(e->htype, BATttype(b), BATcount(e)); int (*cmp)(ptr,ptr); ptr nil; int xx; size_t off; @5 *extremes = NULL; BUN p, q, r; size_t slots, range; oid min, max; oid bhsb; size_t base, end, w; ssize_t offset; if(bn == NULL) return GDK_FAIL; cmp = BATatoms[bn->ttype].atomCmp; nil = ATOMnilptr(bn->ttype); off = BUNindex(bn,BUNfirst(bn)); *ret = NULL; ALGODEBUG THRprintf(GDKout, "#aggrX3_@1_@3_@4_@5[@2,@6](b=%s,g=%s,e=%s);\n", BATgetId(b),BATgetId(g),BATgetId(e));/* ERRORcheck((BATcount(b)!=BATcount(g)), "aggrX3_@13_@3_@4: BATs b & g must be of equal size.\n"); */ ERRORcheck((g->htype == TYPE_void) && (g->hseqbase == oid_nil) && (BATcount(g) > 0), "aggrX3_@13_@3_@4_@5: head of BAT g must not be nil.\n"); if((g->hseqbase == oid_nil) || (!BAThdense(g)) || (!(BAThordered(b)&1)) || (!g->hkey)) { return aggrX3_@1_@3_@4_@5(ret, BATjoin(BATmirror(g),b,oid_nil), e); } /* init: prepare the result bat; * trick: by using "tail-type" void, we only write the head column, here */ @:init_result(void)@ range = max - min + 1; /* scan b & g and replace totals by the extreme value (just pointers to vals in b) */ slots = BATcount(e); if (e->htype == TYPE_void) { /* void lookup */ if (b->htype == TYPE_void) { @:aggrX3_minmax3_@5(@2,0,BUNfndVOID,var,&bhsb; bhsb++,@3,@4,BUNindex(bn,r)-off,@6)@ } else { @:aggrX3_minmax3_@5(@2,0,BUNfndVOID,var,BUNhloc(b,p),@3,@4,BUNindex(bn,r)-off,@6)@ } /* e->htype == TYPE_oid */ } else if (range > SMALL_AGGR_MAX) { /* hash lookup */ if (b->htype == TYPE_void) { @:aggrX3_minmax3_@5(@2,1,HASHfnd_oid,loc,&bhsb; bhsb++,@3,@4,BUNindex(bn,r)-off,@6)@ } else { @:aggrX3_minmax3_@5(@2,1,HASHfnd_oid,loc,BUNhloc(b,p),@3,@4,BUNindex(bn,r)-off,@6)@ } } else { /* array lookup */ slots = range; if (b->htype == TYPE_void) { @:aggrX3_minmax3_@5(@2,0,CHKrange,loc,&bhsb; bhsb++,@3,@4,(*(oid*)h)-min,@6)@ } else { @:aggrX3_minmax3_@5(@2,0,CHKrange,loc,BUNhloc(b,p),@3,@4,(*(oid*)h)-min,@6)@ } } *ret = bn;bunins_failed: if (extremes) GDKfree(extremes); return *ret?GDK_SUCCEED:GDK_FAIL;}@c/* extreme @1: min/max @2: < / >*/@= extreme3@:extreme3_typed(@1,@2,chr,loc,ptr,@3)@@:extreme3_typed(@1,@2,sht,loc,ptr,@3)@@:extreme3_typed(@1,@2,int,loc,ptr,@3)@@:extreme3_typed(@1,@2,lng,loc,ptr,@3)@@:extreme3_typed(@1,@2,any,loc,ptr,@3)@@:extreme3_typed(@1,@2,any,var,ptr,@3)@@:extreme3_typed(@1,@2,any,var,oid,@3)@intCMDaggrX3_@13(BAT **ret, BAT *b, BAT *g, BAT *e){ int rtrn, tt = ATOMstorage(b->ttype); ALGODEBUG THRprintf(GDKout, "#CMDaggrX3_@1[@2](b=%s,g=%s,e=%s);\n", BATgetId(b),BATgetId(g),BATgetId(e));/* ERRORcheck((BATcount(b)!=BATcount(g)), "aggrX3_@13: BATs b & g must be of equal size.\n"); */ ERRORcheck((g->htype == TYPE_void) && (g->hseqbase == oid_nil) && (BATcount(g) > 0), "aggrX3_@13: head of BAT g must not be nil.\n"); if((g->hseqbase == oid_nil) || (!BAThdense(g)) || (!(BAThordered(b)&1)) || (!g->hkey)) { return CMDaggrX3_@1(ret, BATjoin(BATmirror(g),b,oid_nil), e); } if (b->ttype == TYPE_void) { rtrn = aggrX3_@13_any_var_oid(ret, b, g, e); } else if (tt == TYPE_chr) { rtrn = aggrX3_@13_chr_loc_ptr(ret, b, g, e); } else if (tt == TYPE_sht) { rtrn = aggrX3_@13_sht_loc_ptr(ret, b, g, e); } else if (b->ttype != TYPE_bat && (tt == TYPE_int || tt == TYPE_flt)) { /* ensure use of ATOMput for TYPE_bat */ rtrn = aggrX3_@13_int_loc_ptr(ret, b, g, e); } else if (tt == TYPE_dbl || tt == TYPE_lng) { rtrn = aggrX3_@13_lng_loc_ptr(ret, b, g, e); } else if (b->tvarsized) { rtrn = aggrX3_@13_any_var_ptr(ret, b, g, e); } else { rtrn = aggrX3_@13_any_loc_ptr(ret, b, g, e); } return rtrn;}@c@:extreme3(min,<,max)@@:extreme3(max,>,min)@@- Count@c/* aggrX3_count3 e-void-head e-oid-head e-oid-head void-lookup hash-lookup array-lookup @1: 0 0 1 use cnt-array? @2: 0 1 0 do BATprepareHash? @3: BUNfndVOID HASHfnd_oid CHKrange lookup @4: var loc loc e/bn-head-access
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -