⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calc.mx

📁 一个内存数据库的源代码这是服务器端还有客户端
💻 MX
📖 第 1 页 / 共 3 页
字号:
@' The contents of this file are subject to the MonetDB Public License@' Version 1.1 (the "License"); you may not use this file except in@' compliance with the License. You may obtain a copy of the License at@' http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html@'@' Software distributed under the License is distributed on an "AS IS"@' basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the@' License for the specific language governing rights and limitations@' under the License.@'@' The Original Code is the MonetDB Database System.@'@' The Initial Developer of the Original Code is CWI.@' Portions created by CWI are Copyright (C) 1997-2007 CWI.@' All Rights Reserved.@f calc@a N.J. Nes, P. Boncz, M. Kersten, A. de Vries@v 2.0@+ Basic arithmetic This module is an extended version of the V4 arithmetic module.It implements the arithmetic operations on the built-in types,@emph{chr}, @emph{sht}, @emph{int}, @emph{flt}, @emph{dbl} and @emph{lng}.All combinations are implemented. Limited combinations are implementedfor @emph{bit}, @emph{oid} and @emph{str}. @table @code@item [binary operators]The implemented operators are first of all all comparison that return a TRUE/FALSE value (@emph{bit} values), i.e. @emph{<=}, @emph{<}, @emph{==}, @emph{!=}, @emph{>=}, and @emph{>=}.The module also implements the operators @emph{+}, @emph{-}, @emph{*} and @emph{/}. The rules for the return types operators is as follows.If one of the input types is a floating point the result will be afloating point.  The largest type of the input types is taken. The @emph{max} and @emph{min} functions return the maximum and minimum of the two input parameters.@item [unary operators]This module also implements the unary @emph{abs}() function, which calculates the absolute value of the given input parameter, as well as the @emph{-} unaryoperator. The @emph{inv} unary operation calculates the inverse of the input value. An error message is given when the input value is zero.@item [bitwise operators]For integers there are some additional operations. The @emph{\%} operatorimplements the congruent modulo operation. The @emph{<<} and @emph{>>} are the left and right bit shift. The @emph{or}, @emph{and}, @emph{xor} and @emph{not} for integers are implemented as bitwise boolean operations. @item [boolean operators]The @emph{or}, @emph{and}, @emph{xor} and @emph{not} for the bit atomic type in MIL (this corresponds to what is normally called boolean)are implemented as the logic operations.@item [random numbers]This module also contains the rand and srand functions. The @emph{srand}() function initializes the random number generator using a seed value. The subsequent calls to @emph{rand}() are pseudo random numbers (with the same seed the sequence can be repeated).@end tableThe general interpretation for the NIL value is "unknown".This semantics mean that any operation that receives at least one NIL value, will produce a NIL value in the output for sure.The only exception to this rule are the "==" and "!=" equality test routines (it would otherwise become rather difficult to test whether a value is nil). @{The collection of type conversion routines are included here as well.The definitions shown are limited to the Mx macros.This should be sufficient to understand the functionalityat the cost of precision.In most situations the macros are expanded using thebuilt-in type set (int,lng,sht,bit,oid,flt,...) @= mal_isnil	command isnil(v:@1) :bit 	address CALCisnil_@1	comment "is a value nil?";	command isnotnil(v:@1) :bit 	address CALCisnotnil_@1	comment "is a value not equal to nil?";@-[Mx bug, space required here]@malmodule calc;	@:mal_isnil(chr)@	@:mal_isnil(bit)@	@:mal_isnil(sht)@	@:mal_isnil(int)@	@:mal_isnil(oid)@	@:mal_isnil(flt)@	@:mal_isnil(lng)@	@:mal_isnil(dbl)@	@:mal_isnil(str)@	@:mal_isnil(bat)@command isnil(v:void) :bit address CALCisnil_voidcomment "is a value nil?";command isnotnil(v:void) :bit address CALCisnotnil_voidcomment "is a value not equal to nil?";@+ Comparison operationsThe @emph{eq_ops} and @emph{cmp_ops} Mx macro implements the interface to thearithmetic comparisons. Note that comparison operators with differentoperand types are already supported in the kernel, but are not nearly as fast,(because they have to convert values on the fly).  The code expansion is organized such thatthe least interesting one is pushed onto the symbol table stack first.@= eq_ops		command ==(left:@1, right:@2) :bit 		address CALCcompEQ@1@2;		command !=(left:@1, right:@2) :bit 		address CALCcompNEQ@1@2;@= cmp_ops		@:eq_ops(@1,@2)@		command <(left:@1, right:@2) :bit 		address CALCcompLT@1@2;		command <=(left:@1, right:@2) :bit 		address CALCcompLE@1@2;		command >=(left:@1, right:@2) :bit 		address CALCcompGE@1@2;		command >(left:@1, right:@2) :bit 		address CALCcompGT@1@2;		command between(val:@1, low:@1, high:@1) :bit 		address CALCcompBetween@1;@mal		@:cmp_ops(oid,oid)@		@:cmp_ops(flt,flt)@		@:cmp_ops(flt,dbl)@		@:cmp_ops(dbl,dbl)@		@:cmp_ops(chr,chr)@		@:cmp_ops(chr,sht)@		@:cmp_ops(chr,int)@		@:cmp_ops(chr,lng)@		@:eq_ops(bit,bit)@		@:eq_ops(bit,chr)@		@:eq_ops(bit,sht)@		@:eq_ops(bit,int)@		@:eq_ops(bit,lng)@		@:cmp_ops(sht,chr)@		@:cmp_ops(sht,sht)@		@:cmp_ops(sht,int)@		@:cmp_ops(sht,lng)@		@:cmp_ops(int,chr)@		@:cmp_ops(int,sht)@		@:cmp_ops(int,int)@		@:cmp_ops(int,lng)@		@:cmp_ops(lng,chr)@		@:cmp_ops(lng,sht)@		@:cmp_ops(lng,int)@		@:cmp_ops(lng,lng)@		@:cmp_ops(str,str)@@+ Arithmetic computation The operators @{ +, -, *, /, % @} are handled here.The macro expects three parameters, two input atomic types and a result type. @malcommand %(left:chr, right:int) :int address CALCbinarycheckMODchrint;command %(left:sht, right:int) :int address CALCbinarycheckMODshtint;command %(left:int, right:int) :int address CALCbinarycheckMODintint;command %(left:lng, right:int) :int address CALCbinarycheckMODlngint;command %(left:lng, right:lng) :lng address CALCbinarycheckMODlnglng;command %(left:int, right:chr) :chr address CALCbinarycheckMODintchr;command %(left:int, right:sht) :sht address CALCbinarycheckMODintsht;command +(l:str,r:str):straddress CALCstrConcatcomment "Concatenate two strings";command +(l:str,r:int):straddress CALCstrConcatIntcomment "Concatenate two strings";@= mal_calc_ops		command +(left:@1, right:@2) :@3 		address CALCbinaryADD@1@2; 		command -(left:@1, right:@2) :@3 		address CALCbinarySUB@1@2;		command *(left:@1, right:@2) :@3 		address CALCbinaryMUL@1@2;		command /(left:@1, right:@2) :@3 		address CALCbinarycheckDIV@1@2;@-The coercions described below ensure that there is never any information loss.@mal	@:mal_calc_ops(flt,chr,flt)@	@:mal_calc_ops(flt,sht,flt)@	@:mal_calc_ops(flt,int,flt)@	@:mal_calc_ops(flt,lng,flt)@	@:mal_calc_ops(flt,flt,flt)@	@:mal_calc_ops(flt,dbl,dbl)@	@:mal_calc_ops(dbl,chr,dbl)@	@:mal_calc_ops(dbl,sht,dbl)@	@:mal_calc_ops(dbl,int,dbl)@	@:mal_calc_ops(dbl,lng,dbl)@	@:mal_calc_ops(dbl,flt,dbl)@	@:mal_calc_ops(dbl,dbl,dbl)@	@:mal_calc_ops(oid,oid,oid)@	@:mal_calc_ops(chr,chr,chr)@	@:mal_calc_ops(chr,sht,sht)@	@:mal_calc_ops(chr,int,int)@	@:mal_calc_ops(chr,lng,lng)@	@:mal_calc_ops(chr,flt,flt)@	@:mal_calc_ops(chr,dbl,dbl)@	@:mal_calc_ops(sht,chr,sht)@	@:mal_calc_ops(sht,sht,sht)@	@:mal_calc_ops(sht,int,int)@	@:mal_calc_ops(sht,lng,lng)@	@:mal_calc_ops(sht,flt,flt)@	@:mal_calc_ops(sht,dbl,dbl)@	@:mal_calc_ops(int,chr,int)@	@:mal_calc_ops(int,sht,int)@	@:mal_calc_ops(int,int,int)@	@:mal_calc_ops(int,lng,lng)@	@:mal_calc_ops(int,flt,flt)@	@:mal_calc_ops(int,dbl,dbl)@	@:mal_calc_ops(lng,chr,lng)@	@:mal_calc_ops(lng,sht,lng)@	@:mal_calc_ops(lng,int,lng)@	@:mal_calc_ops(lng,lng,lng)@	@:mal_calc_ops(lng,flt,flt)@	@:mal_calc_ops(lng,dbl,dbl)@@+ Binary  operations { max, min, }@= mal_binary_ops		command max(l:@1, r:@1) :@1 		address CALCbinaryMAX@2; 		command min(l:@1, r:@1) :@1 		address CALCbinaryMIN@2;@-@mal	@:mal_binary_ops(chr,chr)@	@:mal_binary_ops(sht,sht)@	@:mal_binary_ops(int,int)@	@:mal_binary_ops(oid,oid)@	@:mal_binary_ops(flt,flt)@	@:mal_binary_ops(lng,lng)@	@:mal_binary_ops(dbl,dbl)@@+ Unary operations { abs, inv }The unary operators include coercion routines for built-in types@= mal_unary_ops		command abs(x:@1) :@1 		address CALCunary@1ABS 		comment "absolute value";		command inv(x:@1) :@1 		address CALCunarycheck@1INV 			comment "inverse value (1/x)";		command -(x:@1) :@1 		address CALCunary@1NEG 		comment "negative value";		command length(x:@1):int		address CALClength@1;@-@mal	@:mal_unary_ops(dbl)@	@:mal_unary_ops(flt)@	@:mal_unary_ops(chr)@	@:mal_unary_ops(sht)@	@:mal_unary_ops(int)@	@:mal_unary_ops(lng)@	command length(x:str):int	address CALClengthstr;@-Coercion functions are typical used in multiplex calls.@= mal_coercion		command @1(x:@2):@1 		address CALC@22@1 		comment "coercion @2 to @1";@= scalar_coercion	@:mal_coercion(@1,oid)@	@:mal_coercion(@1,bit)@	@:mal_coercion(@1,chr)@	@:mal_coercion(@1,sht)@	@:mal_coercion(@1,int)@	@:mal_coercion(@1,lng)@	@:mal_coercion(@1,flt)@	@:mal_coercion(@1,dbl)@@mal	@:scalar_coercion(bit)@	@:scalar_coercion(chr)@	@:scalar_coercion(lng)@	@:scalar_coercion(int)@	@:scalar_coercion(sht)@	@:mal_coercion(oid,oid)@	@:mal_coercion(oid,lng)@	@:mal_coercion(oid,sht)@	@:mal_coercion(oid,int)@	@:mal_coercion(oid,flt)@	@:mal_coercion(oid,dbl)@	@:mal_coercion(flt,flt)@	@:mal_coercion(flt,dbl)@	@:mal_coercion(flt,sht)@	@:mal_coercion(flt,int)@	@:mal_coercion(flt,lng)@	@:mal_coercion(dbl,dbl)@	@:mal_coercion(dbl,flt)@	@:mal_coercion(dbl,sht)@	@:mal_coercion(dbl,int)@	@:mal_coercion(dbl,lng)@	@:mal_coercion(str,sht)@	@:mal_coercion(str,int)@	@:mal_coercion(str,lng)@	@:mal_coercion(str,flt)@	@:mal_coercion(str,dbl)@	@:mal_coercion(chr,chr)@	@:mal_coercion(chr,sht)@	@:mal_coercion(chr,int)@	@:mal_coercion(chr,lng)@@+ Boolean operations { or, xor, and, not }@malcommand or(left:bit, right:bit) :bit address CALCbinaryORbit;command and(left:bit, right:bit) :bit address CALCbinaryANDbit;command xor(left:bit, right:bit) :bit address CALCbinaryXORbit;command not(left:bit) :bit address CALCunarybitNOT ;pattern ifthenelse(b:bit,t:any_1,f:any_1):any_1 address CALCswitchbit;@+ Bitwise operationsCardinal numerical types (inclusing @emph{chr} can be regarded as a bitarray. Specific operations work with this interpretation.@= mal_bitwise_ops		command or(left:@1, right:@1) :@1 		address CALCbinaryOR@1@1;		command and(left:@1, right:@1) :@1 		address CALCbinaryAND@1@1;		command xor(left:@1, right:@1) :@1 		address CALCbinaryXOR@1@1;		command not(left:@1) :@1 		address CALCunary@1NOT;		command <<(left:@1,right:int) :@1 		address CALCbinaryLSH@1int;		command >>(left:@1, right:int) :@1 		address CALCbinaryRSH@1int;@mal	@:mal_bitwise_ops(chr)@	@:mal_bitwise_ops(sht)@	@:mal_bitwise_ops(int)@	@:mal_bitwise_ops(lng)@@+ Type coercionsThe Monet kernel contains a few built-in atomic types togetherwith useful functions. Those accessible at the MAL command level are introduced below.@= convertCmd		command @1(v:str):@1 		address CALCstr2@1;		command str(v:@1):str 		address CALC@12str;		command @1(v:void) :@1 		address CALCnil2@1;@mal@:convertCmd(oid)@@:convertCmd(sht)@@:convertCmd(int)@@:convertCmd(bat)@@:convertCmd(lng)@@:convertCmd(flt)@@:convertCmd(dbl)@@:convertCmd(bit)@@:convertCmd(ptr)@@:convertCmd(chr)@command bat(v:str):bat[:any_1,:any_2] address CALCstr2bat;command str(v:bat[:any_1,:any_2]):str address CALCbat2str;command bat(v:void) :bat[:any_1,:any_2] address CALCnil2bat;#command bat(b:bat):bat[:any_1,:any_2]#address CALCBAT2bat;#command bat(b:bat[:any_1,:any_2]):bat#address CALCbat2BAT;command str(v:void) :str address CALCnil2str;command str(v:str) :str address CALCstr2str;command void(v:void) :void address CALCnil2void;command void(v:int) :void address CALCint2void;command void(v:sht) :void address CALCsht2void;command void(v:lng) :void address CALClng2void;command setoid(v:int)address intSetoidImpl;command setoid(v:lng)address lngSetoidImpl;command setoid(v:oid)address oidSetoidImpl;command getBATidentifier(b:bat[:any_1,:any_2]):bataddress CALCbat2batidcomment "Coerce bat to BAT identifier";command getBAT(b:bat):bat[:any_1,:any_2]address CALCbatid2batcomment "Coerce bat to BAT identifier";@-We should also deal with superflous operations, such as int(v:int).This is a noop operation that should ideally be filtered out withthe code squeezer. [TODO]@+ OID utility functions@malcommand newoid() :oid address CALCnewoidBasecomment "Generate a new oid. Equivalent to newoid(0,1)";command newoid(incr:lng) :oid address CALCnewoidInclng;command newoid(incr:int) :oid address CALCnewoidInccomment "Reserves a range of consecutive unique OIDs; returns the lowest 		in range.  equivalent to newoid(0,incr)";command setoid(base:oid) :oid address CALCsetoidInccomment "Sets the oid range of consecutive unique OIDs; returns the 		lowest in range.";command setoid() :oid address CALCsetoidBasecomment "Equivalent to setoid(1:oid).";@+ Example script The following example MIL script will do each operation in thecalc module. It can be used for testing and the M2m compiler.@milsetoid(oid(20000000));chr1 := 'a';chr2 := 'c';sht1 := sht(2);sht2 := sht(5);int1 := 5;int2 := 2;flt1 := 2.5;flt2 := 5.4;dbl1 := dbl(-2.500001);	       # may also need string since float is defaultdbl2 := dbl(54.00456789);      # real and is less significant	lng1 := lng("-9000000000000"); # string needed because lng doesn't fit in anlng2 := lng("5400456789");     # intoid1 := oid(int1);oid2 := oid(int2);@:mil_comp_ops('a','z')@@:mil_comp_ops(sht1,sht2)@@:mil_comp_ops(int1,int2)@@:mil_comp_ops(flt1,flt2)@@:mil_comp_ops(dbl1,dbl2)@@:mil_comp_ops("abcde","abcdf")@@:mil_calc_ops(chr1,chr2)@@:mil_calc_ops(chr1,sht2)@@:mil_calc_ops(chr1,int2)@@:mil_calc_ops(chr1,lng2)@@:mil_calc_ops(chr1,flt2)@@:mil_calc_ops(chr1,dbl2)@@:mil_calc_ops(sht1,chr2)@@:mil_calc_ops(sht1,sht2)@@:mil_calc_ops(sht1,int2)@@:mil_calc_ops(sht1,lng2)@@:mil_calc_ops(sht1,flt2)@@:mil_calc_ops(sht1,dbl2)@@:mil_calc_ops(int1,chr2)@@:mil_calc_ops(int1,sht2)@@:mil_calc_ops(int1,int2)@@:mil_calc_ops(int1,lng2)@@:mil_calc_ops(int1,flt2)@@:mil_calc_ops(int1,dbl2)@@:mil_calc_ops(lng1,chr2)@@:mil_calc_ops(lng1,sht2)@@:mil_calc_ops(lng1,int2)@@:mil_calc_ops(lng1,lng2)@@:mil_calc_ops(lng1,flt2)@@:mil_calc_ops(lng1,dbl2)@@:mil_calc_ops(flt1,chr2)@@:mil_calc_ops(flt1,sht2)@@:mil_calc_ops(flt1,int2)@@:mil_calc_ops(flt1,lng2)@@:mil_calc_ops(flt1,flt2)@@:mil_calc_ops(flt1,dbl2)@@:mil_calc_ops(dbl1,chr2)@@:mil_calc_ops(dbl1,sht2)@@:mil_calc_ops(dbl1,int2)@@:mil_calc_ops(dbl1,lng2)@@:mil_calc_ops(dbl1,flt2)@@:mil_calc_ops(dbl1,dbl2)@@:mil_calc_ops(oid1,oid2)@max(sht1,sht2).print;max(int1,int2).print;max(lng1,lng2).print;max(flt1,flt2).print;max(dbl1,dbl2).print;min(sht1,sht2).print;min(int1,int2).print;min(flt1,flt2).print;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -