📄 calc.mx
字号:
@:c_isnil(chr)@@:c_isnil(sht)@@:c_isnil(int)@@:c_isnil(oid)@@:c_isnil(flt)@@:c_isnil(lng)@@:c_isnil(dbl)@@:c_isnil(ptr)@@:c_isnil(bat)@calc_export str CALCisnil_str(bit *retval, str *val);strCALCisnil_str(bit *retval, str *val){ *retval = strcmp(*val, ATOMnilptr(TYPE_str)) == 0; return MAL_SUCCEED;}calc_export str CALCisnotnil_str(bit *retval, str *val);strCALCisnotnil_str(bit *retval, str *val){ *retval = strcmp(*val, ATOMnilptr(TYPE_str)); return MAL_SUCCEED;}calc_export str CALCstr2nil(str *retval, str *val);strCALCstr2nil(str *retval, str *val){ (void) val; /* fool compiler */ *retval = GDKstrdup(ATOMnilptr(TYPE_str)); return MAL_SUCCEED;}calc_export str CALCisnil_any(bit *retval, ptr *val, int tpe);strCALCisnil_any(bit *retval, ptr *val, int tpe){ ptr nilval = ATOMnilptr(tpe); *retval = (ATOMcmp(tpe, nilval, *val) == 0); return MAL_SUCCEED;}calc_export str CALCisnil_void(bit *retval, oid *val);strCALCisnil_void(bit *retval, oid *val){ *retval = (*val == oid_nil); return MAL_SUCCEED;}calc_export str CALCisnotnil_void(bit *retval, oid *val);strCALCisnotnil_void(bit *retval, oid *val){ *retval = (*val != oid_nil); return MAL_SUCCEED;}@= c_between_opcalc_export str CALCcompBetween@1(bit *retval, @1 *v, @1 *low, @1 *high);str CALCcompBetween@1(bit *retval, @1 *v, @1 *low, @1 *high){ if (*v == @1_nil || (*low == @1_nil && *high == @1_nil)) { *retval = bit_nil; } else if (*low == @1_nil) { *retval = (*v <= *high); } else if (*high == @1_nil) { *retval = (*low <= *v); } else { *retval = (*low <= *v) && (*v <= *high); } return MAL_SUCCEED;}@-The @emph{c_calc_ops} implement the arithmetic operations on the given type.The @emph{c_calc_ops} macro gets three arguments, two input types and a result type. @= c_calc_ops@:calc_binop(ADD,+,@1,@2,@3)@@:calc_binop(SUB,-,@1,@2,@3)@@:calc_binop(MUL,*,@1,@2,@3)@@:check_binop(DIV,/,@1,@2,@3,@3,"Division by zero")@@@= calc_unopcalc_export str CALCunary@3@1(@3 *res , @3 *a );str CALCunary@3@1(@3 *res , @3 *a ) {#ifdef DEBUG printf( "CALCunary@3@1\n");#endif if (*a == @3_nil) { *res = @3_nil; } else { *res = @2 (*a); } return(MAL_SUCCEED);}@@= check_unopcalc_export str CALCunarycheck@3@1(@3 *res , @3 *a );str CALCunarycheck@3@1(@3 *res , @3 *a ) {#ifdef DEBUG printf( "CALCunary@3@1\n");#endif if (*a == 0 ){ throw(MAL, "calc.@3", "Illegal reference"); } else if ( *a == @3_nil) { *res = @3_nil; } else { *res = @2 (*a); } return(MAL_SUCCEED);}@@= calc_binopcalc_export str CALCbinary@1@3@4(@5 *res, @3 *a, @4 *b );str CALCbinary@1@3@4(@5 *res, @3 *a, @4 *b ) {#ifdef DEBUG printf( "CALCbinary@1@3@4\n");#endif if (*a == @3_nil || *b == @4_nil) { *res = @5_nil; } else { *res = ((@5)(*a)) @2 ((@5)(*b)); } return(MAL_SUCCEED);}@/* 1: function name suffix (_MOD,_DIV etc) 2: operator (%,/ etc) 3: type argument 1 4: type argument 2 5: result type 6: intermediate type (sometimes the result is a smaller type (%)) 7: Error message when something goes wrong.*/@= check_binopcalc_export str CALCbinarycheck@1@3@4(@5 *res, @3 *a, @4 *b );str CALCbinarycheck@1@3@4(@5 *res, @3 *a, @4 *b ) {#ifdef DEBUG printf( "CALCbinarycheck@1@3@4\n");#endif if (*b == 0) { throw(MAL, "calc.@2", @7); } else if (*a == @3_nil || *b == @4_nil) { *res = @5_nil; } else { *res = (@5) OP((@6)*a,@2,(@6)*b); } return(MAL_SUCCEED);}@@= c_bitwise_ops@:calc_binop(OR,|,@1,@1,@1)@@:calc_binop(AND,&,@1,@1,@1)@@:calc_binop(XOR,^,@1,@1,@1)@@:calc_unop(NOT,~,@1)@@= c_shift_ops@:calc_binop(LSH,<<,@1,int,@1)@@:calc_binop(RSH,>>,@1,int,@1)@@+ The Coercion implementationCoercions generally do not check on information loss@-@= mal_coercion_implcalc_export str CALC@22@1(@1 *res, @2 *a);str CALC@22@1(@1 *res, @2 *a){ if (*a == @2_nil @3) *res= @1_nil; else *res= @4; return MAL_SUCCEED;}@-@c@:mal_coercion_impl(bit,bit, , *a != 0)@@:mal_coercion_impl(bit,oid, , *a != 0)@@:mal_coercion_impl(bit,chr, , *a != 0)@@:mal_coercion_impl(bit,sht, , *a != 0)@@:mal_coercion_impl(bit,int, , *a != 0)@@:mal_coercion_impl(bit,lng, , *a != 0)@@:mal_coercion_impl(bit,flt, , *a != 0)@@:mal_coercion_impl(bit,dbl, , *a != 0)@@:mal_coercion_impl(lng,oid,, (lng) *a)@@:mal_coercion_impl(lng,chr,, (lng) *a)@@:mal_coercion_impl(lng,bit,, (lng) *a)@@:mal_coercion_impl(lng,sht,, (lng) *a)@@:mal_coercion_impl(lng,int,, (lng) *a)@@:mal_coercion_impl(lng,lng,, (lng) *a)@@:mal_coercion_impl(lng,flt,, (lng) *a)@@:mal_coercion_impl(lng,dbl,, (lng) *a)@@:mal_coercion_impl(sht,oid, , (sht) *a)@@:mal_coercion_impl(sht,bit, , (sht) *a)@@:mal_coercion_impl(sht,chr, , (sht) *a)@@:mal_coercion_impl(sht,sht, , (sht) *a)@@:mal_coercion_impl(sht,int, , (sht) *a)@@:mal_coercion_impl(sht,lng, , (sht) *a)@@:mal_coercion_impl(sht,flt, , (sht) *a)@@:mal_coercion_impl(sht,dbl, , (sht) *a)@@:mal_coercion_impl(int,oid, , (int) *a)@@:mal_coercion_impl(int,bit, , (int) *a)@@:mal_coercion_impl(int,chr, , (int) *a)@@:mal_coercion_impl(int,sht, , (int) *a)@@:mal_coercion_impl(int,int, , (int) *a)@@:mal_coercion_impl(int,lng, , (int) *a)@@:mal_coercion_impl(int,flt, , (int) *a)@@:mal_coercion_impl(int,dbl, , (int) *a)@@:mal_coercion_impl(oid,bit, || *a < 0 , (oid) *a)@@:mal_coercion_impl(oid,chr, || *a < 0 , (oid) *a)@@:mal_coercion_impl(oid,oid, , (oid) *a)@@:mal_coercion_impl(oid,lng, || *a < 0 , (oid) *a)@@:mal_coercion_impl(oid,sht, || *a < 0 , (oid) *a)@@:mal_coercion_impl(oid,int, || *a < 0 , (oid) *a)@@:mal_coercion_impl(oid,flt, || *a < 0 , (oid) *a)@@:mal_coercion_impl(oid,dbl, || *a < 0 , (oid) *a)@@:mal_coercion_impl(flt,flt, , (flt) *a)@@:mal_coercion_impl(flt,dbl, , (flt) *a)@@:mal_coercion_impl(flt,sht, , (flt) *a)@@:mal_coercion_impl(flt,chr, , (flt) *a)@@:mal_coercion_impl(flt,int, , (flt) *a)@@:mal_coercion_impl(flt,lng, , (flt) *a)@@:mal_coercion_impl(dbl,dbl, , (dbl) *a)@@:mal_coercion_impl(dbl,flt, , (dbl) *a)@@:mal_coercion_impl(dbl,sht, , (dbl) *a)@@:mal_coercion_impl(dbl,int, , (dbl) *a)@@:mal_coercion_impl(dbl,chr, , (dbl) *a)@@:mal_coercion_impl(dbl,lng, , (dbl) *a)@@:mal_coercion_impl(chr,bit, , (chr) *a)@@:mal_coercion_impl(chr,chr, , (chr) *a)@@:mal_coercion_impl(chr,oid, , (chr) *a)@@:mal_coercion_impl(chr,flt, , (chr) *a)@@:mal_coercion_impl(chr,dbl, , (chr) *a)@@:mal_coercion_impl(chr,sht, , (chr) *a)@@:mal_coercion_impl(chr,int, , (chr) *a)@@:mal_coercion_impl(chr,lng, , (chr) *a)@calc_export str CALCbat2BAT(int *res, bat *bid);str CALCbat2BAT(int *res, bat *bid){ *res= *bid; BBPincref(*res, TRUE); return MAL_SUCCEED;}calc_export str CALCBAT2bat(bat *res, int *bid);str CALCBAT2bat(int *res, bat *bid){ *res= *bid; return MAL_SUCCEED;}@-The conversion routines are relatively easy to define.@= convertImplcalc_export str CALCstr2@1(@1 *ret, str *val);str CALCstr2@1(@1 *ret, str *val){ int l = sizeof(@1); (void) @1FromStr(*val, &l, &ret); return MAL_SUCCEED;}calc_export str CALC@12str(str *ret, @1 *val);str CALC@12str(str *ret, @1 *val){ int l=0; if( *val == @1_nil) *ret= GDKstrdup(str_nil); else { if(*ret) l=strlen(*ret); (void) @1ToStr(ret,&l,val); } return MAL_SUCCEED;}str @1FromvoidImpl(@1 *ret, void *val){ (void) val; /* fool compiler */ memcpy(ret, ATOMnilptr(TYPE_@1), ATOMsize(TYPE_@1)); return MAL_SUCCEED;}@-Strings have to be dealt with differently, becausewe recieve a pointer to the string directly.To make it work properly, we need a call be referencein those places where we leave a result behind.@cstrvoidFromStrImpl(void *ret, str val){ (void) val; /* fool compiler */ memcpy(ret, ATOMnilptr(TYPE_void), ATOMsize(TYPE_void)); return MAL_SUCCEED;}calc_export str CALCnil2str(str *ret, void *val);strCALCnil2str(str *ret, void *val){ (void) val; /* fool compiler */ *ret = GDKstrdup(str_nil); return MAL_SUCCEED;}calc_export str CALCstr2str(str *ret, str *val);strCALCstr2str(str *ret, str *val){ if(*val) *ret = GDKstrdup(*val); else *ret = 0; return MAL_SUCCEED;}calc_export str CALCstr2oid(oid *ret, str *val);strCALCstr2oid(oid *ret, str *val){ int l = strlen(*val); (void) OIDfromStr(*val, &l, (ptr) ret); return MAL_SUCCEED;}calc_export str CALCoid2str(str *ret, oid *val);strCALCoid2str(str *ret, oid *val){ int l = sizeof(oid); (void) OIDtoStr(ret, &l, val); return MAL_SUCCEED;}calc_export str CALCnil2void(oid *ret, ptr *val);strCALCnil2void(oid *ret, ptr *val){ (void) val; /* fool compiler */ *ret = oid_nil; return MAL_SUCCEED;}calc_export str CALClng2void(oid *ret, lng *val);strCALClng2void(oid *ret, lng *val){ *ret = (oid) *val; return MAL_SUCCEED;}calc_export str CALCsht2void(oid *ret, sht *val);strCALCsht2void(oid *ret, sht *val){ *ret = *val; return MAL_SUCCEED;}calc_export str CALCint2void(oid *ret, int *val);strCALCint2void(oid *ret, int *val){ *ret = *val; return MAL_SUCCEED;}calc_export str oidFromoidImpl(oid *ret, oid *val);stroidFromoidImpl(oid *ret, oid *val){ *ret = *val; return MAL_SUCCEED;}calc_export str oidFromvoidImpl(oid *ret, void *val);stroidFromvoidImpl(oid *ret, void *val){ (void) val; /* fool compiler */ memcpy(ret, ATOMnilptr(TYPE_oid), ATOMsize(TYPE_oid)); return MAL_SUCCEED;}calc_export str CALCbat2batid(int *ret, int *bid);strCALCbat2batid(bat *ret, int *bid){ BAT *b; if( *bid == bat_nil){ *ret= bat_nil; return MAL_SUCCEED; } b = BATdescriptor(*bid); if (b == 0) throw(MAL, "calc.getBAT", "Bat does not exist"); *ret = b->batCacheid; BBPkeepref(b->batCacheid); return MAL_SUCCEED;}calc_export str CALCbatid2bat(int *bid, int *ret);strCALCbatid2bat(int *bid, int *ret){ BAT *b; if( *ret == bat_nil){ *bid= bat_nil; return MAL_SUCCEED; } b = BATdescriptor(*ret); if (b == 0) throw(MAL, "calc.:bat", "Bat does not exist"); *bid = b->batCacheid; BBPkeepref(*bid); return MAL_SUCCEED;}@:convertImpl(sht)@@:convertImpl(int)@@:convertImpl(lng)@@:convertImpl(flt)@@:convertImpl(dbl)@@:convertImpl(bit)@@:convertImpl(ptr)@@:convertImpl(bat)@@:convertImpl(chr)@@-@= setoidCodecalc_export str @1SetoidImpl(str *ret, @1 *v);str @1SetoidImpl(str *ret, @1 *v){ (void) ret; /* fool compiler */ OIDbase((oid) *v); return MAL_SUCCEED;}@c@:setoidCode(oid)@@:setoidCode(lng)@@:setoidCode(int)@@-Type conversion template. Only allowed then no information is lost.@= coercionImplcalc_export str @1From@2Impl(@1 *res, @2 *val);str @1From@2Impl(@1 *res, @2 *val){ *res = (@1) *val; return MAL_SUCCEED;}@c@:coercionImpl(lng,oid)@@:coercionImpl(lng,int)@@:coercionImpl(lng,sht)@@:coercionImpl(lng,bit)@@:coercionImpl(int,sht)@@:coercionImpl(int,bit)@@:coercionImpl(dbl,flt)@@:coercionImpl(oid,lng)@@:coercionImpl(oid,int)@@:coercionImpl(oid,sht)@calc_export str shtFromlngImpl(sht *res, lng *val);strshtFromlngImpl(sht *res, lng *val){ /* perform a range check !! */ if (*val < 0 || *val > 255) throw(MAL, "calc.coercion", "error lng->sht"); *res = (sht) *val; return MAL_SUCCEED;}calc_export str shtFromintImpl(sht *res, int *val);strshtFromintImpl(sht *res, int *val){ /* perform a range check !! */ if (*val < 0 || *val > 255) throw(MAL, "calc.coercion", "error int->sht"); *res = *val; return MAL_SUCCEED;}@+ Value, Types and VariablesThe routines below implement the OID utility functions.@ccalc_export str CALCnewoidBase(oid *res);strCALCnewoidBase(oid *res){ *res = OIDnew(1); return MAL_SUCCEED;}calc_export str CALCnewoidInc(oid *res, int *inc);strCALCnewoidInc(oid *res, int *inc){ if (*inc <= 0) *res = OIDnew(1); else { *res = OIDnew(*inc); } return MAL_SUCCEED;}calc_export str CALCnewoidInclng(oid *res, lng *inc);strCALCnewoidInclng(oid *res, lng *inc){ if (*inc <= 0) *res = OIDnew(1); else { *res = OIDnew((size_t) *inc); } return MAL_SUCCEED;}calc_export str CALCsetoidBase(oid *res);strCALCsetoidBase(oid *res){ *res = OIDbase(1); return MAL_SUCCEED;}calc_export str CALCsetoidInc(oid *res, oid *inc);strCALCsetoidInc(oid *res, oid *inc){ *res = OIDbase(*inc); return MAL_SUCCEED;}@}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -