📄 aggr.mx
字号:
} else { @:aggrX3_sum(1,0,CHKrange,loc,BUNhloc(b,p),@1,@2,&sums[(*(oid*)h)-min],@3)@ } } *ret = bn; return GDK_SUCCEED;}@c/* arithprod @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*/@= arithprodintCMDaggrX3_prod_@1_@3(BAT **ret, BAT *b, BAT *e){ BAT *bn = BATnew(e->htype, TYPE_@3, BATcount(e)); @3 zero = (@3) 1, *prods; BUN p, q, r; int xx; size_t range; oid min, max; oid bhsb; if( bn == NULL) return GDK_FAIL; ALGODEBUG THRprintf(GDKout, "#CMDaggrX3_prod_@1_@3[@2](b=%s,e=%s);\n", BATgetId(b), BATgetId(e)); /* init: set all prods to zero and calculate min/max oid */ @:init_result(@3)@ range = max - min + 1; /* scan b, and calculate prods */ if (e->htype == TYPE_void) { /* void lookup */ if (b->htype == TYPE_void) { @:aggrX3_prod(0,0,BUNfndVOID,var,&bhsb;bhsb++,@1,@2,BUNt@2(bn,r),@3)@ } else { @:aggrX3_prod(0,0,BUNfndVOID,var,BUNhloc(b,p),@1,@2,BUNt@2(bn,r),@3)@ } /* e->htype == TYPE_oid */ } else if (range > SMALL_AGGR_MAX) { /* hash lookup */ if (b->htype == TYPE_void) { @:aggrX3_prod(0,1,HASHfnd_oid,loc,&bhsb;bhsb++,@1,@2,BUNt@2(bn,r),@3)@ } else { @:aggrX3_prod(0,1,HASHfnd_oid,loc,BUNhloc(b,p),@1,@2,BUNt@2(bn,r),@3)@ } } else { /* array lookup */ if (b->htype == TYPE_void) { @:aggrX3_prod(1,0,CHKrange,loc,&bhsb;bhsb++,@1,@2,&prods[(*(oid*)h)-min],@3)@ } else { @:aggrX3_prod(1,0,CHKrange,loc,BUNhloc(b,p),@1,@2,&prods[(*(oid*)h)-min],@3)@ } } *ret = bn; return GDK_SUCCEED;}@c/* arithsumprod @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*/@= arithsumprod@:arithsum(@1,@2,@3)@@:arithprod(@1,@2,@3)@@c@:arithsumprod(sht,loc,sht)@@:arithsumprod(sht,loc,int)@@:arithsumprod(sht,loc,lng)@@:arithsumprod(int,loc,int)@@:arithsumprod(oid,loc,int)@@:arithsumprod(int,loc,lng)@@:arithsumprod(lng,loc,lng)@@:arithsumprod(flt,loc,flt)@@:arithsumprod(flt,loc,dbl)@@:arithsumprod(dbl,loc,dbl)@/* arithavg @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)*/@= arithavgintCMDaggrX3_avg_@1(BAT **ret, BAT *b, BAT *e){ BAT *bn = BATnew(e->htype, TYPE_dbl, BATcount(e)); int xx; size_t off; size_t *cnt; size_t slots, range; oid min, max; dbl zero = (dbl) 0, *sums; BUN p, q, r; oid bhsb; if( bn == NULL) return GDK_FAIL; off = BUNindex(bn,BUNfirst(bn)); ALGODEBUG THRprintf(GDKout, "#CMDaggrX3_avg_@1[@2](b=%s,e=%s);\n", BATgetId(b),BATgetId(e)); /* init: set all sums & counts to zero and calculate min/max oid */ @:init_result(dbl)@ range = max - min + 1; /* scan b, calculate sums & counts, derive averages */ if (e->htype == TYPE_void) { /* void lookup */ slots = BATcount(e); if (b->htype == TYPE_void) { @:aggrX3_avg(0,0,BUNfndVOID,var,&bhsb;bhsb++,@1,@2,BUNt@2(bn,r),BUNindex(bn,r)-off)@ } else { @:aggrX3_avg(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_avg(0,1,HASHfnd_oid,loc,&bhsb;bhsb++,@1,@2,BUNt@2(bn,r),BUNindex(bn,r)-off)@ } else { @:aggrX3_avg(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_avg(1,0,CHKrange,loc,&bhsb;bhsb++,@1,@2,&sums[(*(oid*)h)-min],(*(oid*)h)-min)@ } else { @:aggrX3_avg(1,0,CHKrange,loc,BUNhloc(b,p),@1,@2,&sums[(*(oid*)h)-min],(*(oid*)h)-min)@ } } *ret = bn; return GDK_SUCCEED;}@c@:arithavg(sht,loc)@@:arithavg(int,loc)@@:arithavg(lng,loc)@@:arithavg(flt,loc)@@:arithavg(dbl,loc)@@- "Extremes" (Min & Max)@c/* aggrX3_minmax_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: 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_minmax_ptr ALGODEBUG THRprintf(GDKout, "#aggrX3_minmax_ptr(@1,@2,@3,@4,@5,@6,@7,@8,@9);\n"); if (@2 && BATprepareHash(e)) { GDKfree(extremes); 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 and replace totals by the extreme value (just pointers to vals in b) */ bhsb = b->hseqbase - 1; BATloopFast(b, p, q, xx) { ptr t = BUNt@7(b,p); oid *h = (oid*) @5; @3(r, bn, 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 */ /* trick: using a void head-type, only the tail are (over)written */ ATOMput(bn->ttype, bn->theap, BUNtloc(bn,r), val?val:nil); }@c/* aggrX3_minmax_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: 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_minmax_oid ALGODEBUG THRprintf(GDKout, "#aggrX3_minmax_oid(@1,@2,@3,@4,@5,@6,@7,@8,@9);\n"); if (@2 && BATprepareHash(e)) { GDKfree(extremes); 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 and replace totals by the extreme value (just pointers to vals in b) */ bhsb = b->hseqbase - 1; BATloopFast(b, p, q, xx) { ptr t = BUNt@7(b,p); oid *h = (oid*) @5; @3(r, bn, 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 */ /* trick: using a void head-type, only the tail are (over)written */ ATOMput(bn->ttype, bn->theap, BUNtloc(bn,r), &val); }@c/* extreme_typed @1: min/max @2: < / > @3: b/bn-tail-storage-type 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*/@= extreme_typedstatic intaggrX3_@1_@3_@4_@5(BAT **ret, BAT *b, 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; 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,e=%s);\n", BATgetId(b),BATgetId(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 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_minmax_@5(@2,0,BUNfndVOID,var,&bhsb; bhsb++,@3,@4,BUNindex(bn,r)-off,@6)@ } else { @:aggrX3_minmax_@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_minmax_@5(@2,1,HASHfnd_oid,loc,&bhsb; bhsb++,@3,@4,BUNindex(bn,r)-off,@6)@ } else { @:aggrX3_minmax_@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_minmax_@5(@2,0,CHKrange,loc,&bhsb; bhsb++,@3,@4,(*(oid*)h)-min,@6)@ } else { @:aggrX3_minmax_@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: < / > @3: max/min*/@= extreme@:extreme_typed(@1,@2,chr,loc,ptr,@3)@@:extreme_typed(@1,@2,sht,loc,ptr,@3)@@:extreme_typed(@1,@2,int,loc,ptr,@3)@@:extreme_typed(@1,@2,lng,loc,ptr,@3)@@:extreme_typed(@1,@2,any,loc,ptr,@3)@@:extreme_typed(@1,@2,any,var,ptr,@3)@@:extreme_typed(@1,@2,any,var,oid,@3)@intCMDaggrX3_@1(BAT **ret, BAT *b, BAT *e){ int rtrn, tt = ATOMstorage(b->ttype); ALGODEBUG THRprintf(GDKout, "#CMDaggrX3_@1[@2,@3](b=%s,e=%s);\n", BATgetId(b),BATgetId(e)); if (b->ttype == TYPE_void) { rtrn = aggrX3_@1_any_var_oid(ret, b, e); } else if (tt == TYPE_chr) { rtrn = aggrX3_@1_chr_loc_ptr(ret, b, e); } else if (tt == TYPE_sht) { rtrn = aggrX3_@1_sht_loc_ptr(ret, b, e); } else if (b->ttype != TYPE_bat && (tt == TYPE_int || tt == TYPE_flt)) { /* ensure use of ATOMput for TYPE_bat */ rtrn = aggrX3_@1_int_loc_ptr(ret, b, e); } else if (tt == TYPE_dbl || tt == TYPE_lng) { rtrn = aggrX3_@1_lng_loc_ptr(ret, b, e); } else if (b->tvarsized) { rtrn = aggrX3_@1_any_var_ptr(ret, b, e); } else { rtrn = aggrX3_@1_any_loc_ptr(ret, b, e); } return rtrn;}@c@:extreme(min,<,max)@@:extreme(max,>,min)@@- Count@c/* aggrX3_count 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 @5: "BUNhloc(b,p)" for (oid) b-head-type, b-head access "&bhsb; bhsb++" for (void) b-head-type @6: BUNtloc(bn,r) BUNtloc(bn,r) &cnt[(*(oid*)h)-min] *dst: count in-place or in cnt-array ?*/@= aggrX3_count ALGODEBUG THRprintf(GDKout, "#aggrX3_count(@1,@2,@3,@4,@5,@6);\n"); if (@1) { /* create tmp. cnt array */ cnt = (int*) GDKmalloc(range*sizeof(int)); memset(cnt, 0, range*sizeof(int)); } if (@2 && BATprepareHash(bn)) { if (@1) GDKfree(cnt); BBPreclaim(bn); return GDK_FAIL; } /* scan b, and increment counts in-place or in cnt-array */ bhsb = b->hseqbase-1; btt = b->ttype; if (*ignore_nils) { switch(ATOMstorage(btt)) { case TYPE_chr: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,if (!simple_EQ(BUNtloc(b,p),bt_nil,chr)))@ case TYPE_sht: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,if (!simple_EQ(BUNtloc(b,p),bt_nil,sht)))@ case TYPE_int: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,if (!simple_EQ(BUNtloc(b,p),bt_nil,int)))@ case TYPE_lng: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,if (!simple_EQ(BUNtloc(b,p),bt_nil,lng)))@ case TYPE_flt: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,if (!simple_EQ(BUNtloc(b,p),bt_nil,flt)))@ case TYPE_dbl: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,if (!simple_EQ(BUNtloc(b,p),bt_nil,dbl)))@ default: if (b->tvarsized) { @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,if (!atom_EQ(BUNtvar(b,p),bt_nil,btt)))@ } else { @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,if (!atom_EQ(BUNtloc(b,p),bt_nil,btt)))@ } } } else { switch(ATOMstorage(btt)) { case TYPE_chr: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,)@ case TYPE_sht: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,)@ case TYPE_int: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,)@ case TYPE_lng: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,)@ case TYPE_flt: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,)@ case TYPE_dbl: @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,)@ default: if (b->tvarsized) { @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,)@ } else { @:aggrX3_count_loop(@1,@2,@3,@4,@5,@6,)@ } } } if (@1) { /* copy cnt array to final result */ BATloopFast(bn, p, q, xx) { oid h = (*(oid*) BUNh@4(bn,p)) - min; *(int*)BUNtloc(bn, p) = cnt[h]; } GDKfree(cnt); }@c/* aggrX3_count_loop @7: if (!atom_EQ(BUNt{loc,var}(b,p),bt_nil,btt)) b-tail nil check
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -