Three types of set parameters are supported: INTEGER, REAL, and STRINGn.
If you want to sum up all the elements in a set of integers with an external function, to declare the function in the SERVICE structure:
SetFuncLib := SERVICE INTEGER SumInt(SET OF INTEGER ss) : holertl,library='dab',entrypoint='rtlSumInt'; END; x:= 3+4.5; SetFuncLib.SumInt([x, 11.79]); //passed two REAL numbers - it works
To define the external function, in the header (.h) file:
__int64 rtlSumInt(unsigned len, __int64 * a);
In the source code (.cpp) file:
__int64 rtlSumInt(unsigned len, __int64 * a) { __int64 sum = 0; for(unsigned i = 0; i < len; i++) { sum += a[i]; } return sum; }
The first parameter contains the length of the set, and the second parameter is an int array that holds the elements of the set. Note: In declaring the function in ECL, you can also have sets of INTEGER4, INTEGER2 and INTEGER1, but you need to change the type of the C function parameter, too. The relationship is:
INTEGER8 -- __int64 INTEGER4 -- int INTEGER2 -- short INTEGER1 -- char
If you want to sum up all the elements in a set of real numbers:
To declare the function in the SERVICE structure:
SetFuncLib := SERVICE REAL8 SumReal(SET OF REAL8 ss) : holertl,library='dab',entrypoint='rtlSumReal'; END; INTEGER r1 := 10; r2 := 20.345; SetFuncLib.SumReal([r1, r2]); // intentionally passed an integer to the real set, it works too.
To define the external function, in the header (.h) file:
double rtlSumReal(unsigned len, double * a);
In the source code (.cpp) file:
double rtlSumReal(unsigned len, double * a) { double sum = 0; for(unsigned i = 0; i < len; i++) { sum += a[i]; } return sum; }
The first parameter contains the length of the set, and the second parameter is an array that holds the elements of the set.
Note: You can also declare the function in ECL as set of REAL4, but you need to change the parameter of the C function to float.
If you want to calculate the sum of the lengths of all the strings in a set, with the trailing blanks trimmed off:
To declare the function in the SERVICE structure:
SetFuncLib := SERVICE INTEGER SumCharLen(SET OF STRING20 ss) : holertl,library='dab',entrypoint='rtlSumCharLen'; END; str1 := '1234567890'+'xxxx '; str2 := 'abc'; SetFuncLib.SumCharLen([str1, str2]);
To define the external function, in the header (.h) file:
__int64 rtlSumCharLen(unsigned len, char a[ ][20]);
In the source code (.cpp) file:
__int64 rtlSumCharLen(unsigned len, char a[][20]) { __int64 sumtrimedlen = 0; for(unsigned i = 0; i < len; i++) { for(int j = 20-1; j >= 0; j--) { if(a[i][j] != ' ') { break; } a[i][j] = 0; } sumtrimedlen += j + 1; } return sumtrimedlen; }
Note: In declaring the C function, we have two parameters for the set. The first parameter is the length of the set, the second parameter is char[][n] where n is the SAME as that in stringn. Eg., if the service is declared as "integer SumCharLen(set of string20)", then in the C function the parameter type must be char a[][20].