ElementCountMethod

Applies to:Fast Math Parser
Class:uCalc.Item
Returns the number of elements in a uCalc.Item
Syntax
ElementCount()
Remarks
Depending on the kind of object, this may represent the number of elements in an array, the number of parameters in a function, the number of operands in an operator, the number of parameters in a syntax construct, the number of parts in a transform, the number of parts in a compound token, or the value explicitlely set in the Count property when calling Define.

When determining the number of elements, optional parameters in a function count as an element. If the function has a variable number of arguments, then count is -1.

The value returned for a syntax construct may not seem obvious, especially if it has optional and/or alternative parts or some other complexity.

+++ the number of symbols in a uCalc instance,
Example 1: Disambiguates between binary (Infix) version of - (minus) operator and unary (Prefix) version

Console.WriteLine(uc.GetItemOf("-", uc.Properties(ItemIsEnum.Infix)).ElementCount()) ' Returns 2
Console.WriteLine(uc.GetItemOf("-", uc.Properties(ItemIsEnum.Prefix)).ElementCount()) ' Returns 1

          

Console.WriteLine(uc.GetItemOf("-", uc.Properties(ItemIsEnum.Infix)).ElementCount()); // Returns 2;
Console.WriteLine(uc.GetItemOf("-", uc.Properties(ItemIsEnum.Prefix)).ElementCount()); // Returns 1;

          

      WriteLn(uc.GetItemOf('-', uc.Properties(ItemIsEnum.Infix)).ElementCount()); // Returns 2;
      WriteLn(uc.GetItemOf('-', uc.Properties(ItemIsEnum.Prefix)).ElementCount()); // Returns 1;

          

cout << uc.GetItemOf("-", uc.Properties(ItemIsEnum::Infix)).ElementCount() << endl; // Returns 2;
cout << uc.GetItemOf("-", uc.Properties(ItemIsEnum::Prefix)).ElementCount() << endl; // Returns 1;

          

Console::WriteLine(uc.GetItemOf("-", uc.Properties(ItemIsEnum::Infix)).ElementCount()); // Returns 2;
Console::WriteLine(uc.GetItemOf("-", uc.Properties(ItemIsEnum::Prefix)).ElementCount()); // Returns 1;

          
Example 2: Dispaying the number of elements in an array

Dim MyArrayA = uc.DefineVariable("MyArrayA[] = {10, 20, 30, 40, 50}")
Dim MyArrayB = uc.DefineVariable("MyArrayB[15]")
Dim FunctionA = uc.DefineFunction("FuncA(x, y, z) = x + y + z")
Dim FunctionB = uc.DefineFunction("FuncB(x, y, a = 12, b = 34) = x+y+a+b")
Dim FunctionC = uc.DefineFunction("FuncC(x, y ...)", uc.PinAddr(AddressOf MyAverageCB))
Dim FunctionD = uc.DefineFunction("FuncD() = 1+1")
Dim SyntaxA = uc.DefineSyntax("This ::= That")
Dim SyntaxB = uc.DefineSyntax("This {etc} and {more} ::= Whatever({etc})")

Console.WriteLine("Elements in MyArrayA: " + MyArrayA.ElementCount()) ' Returns "Elements in MyArrayA: 5"
Console.WriteLine("Elements in MyArrayB: " + MyArrayB.ElementCount()) ' Returns "Elements in MyArrayB: 15"
Console.WriteLine("Params in FuncA(): " + FunctionA.ElementCount()) ' Returns "Params in FuncA(): 3"
Console.WriteLine("Params in FuncB(): " + FunctionB.ElementCount()) ' Returns "Params in FuncB(): 4"
Console.WriteLine("Params in FuncC(): " + FunctionC.ElementCount()) ' Returns "Params in FuncC(): -1"
Console.WriteLine("Params in FuncD(): " + FunctionD.ElementCount()) ' Returns "Params in FuncD(): 0"
Console.WriteLine("Operands in ! operator: " + uc.GetItemOf("!").ElementCount()) ' Returns "Operands in ! operator: 1"
Console.WriteLine("Operands in > operator: " + uc.GetItemOf(">").ElementCount()) ' Returns "Operands in > operator: 2"
Console.WriteLine("Params in SyntaxA: " + SyntaxA.ElementCount()) ' Returns "Params in SyntaxA: 0"
Console.WriteLine("Params in SyntaxB: " + SyntaxB.ElementCount()) ' Returns "Params in SyntaxB: 2"

          

var MyArrayA = uc.DefineVariable("MyArrayA[] = {10, 20, 30, 40, 50}");
var MyArrayB = uc.DefineVariable("MyArrayB[15]");
var FunctionA = uc.DefineFunction("FuncA(x, y, z) = x + y + z");
var FunctionB = uc.DefineFunction("FuncB(x, y, a = 12, b = 34) = x+y+a+b");
var FunctionC = uc.DefineFunction("FuncC(x, y ...)", uc.PinAddr(MyAverageCB));
var FunctionD = uc.DefineFunction("FuncD() = 1+1");
var SyntaxA = uc.DefineSyntax("This ::= That");
var SyntaxB = uc.DefineSyntax("This {etc} and {more} ::= Whatever({etc})");

Console.WriteLine("Elements in MyArrayA: " + MyArrayA.ElementCount()); // Returns "Elements in MyArrayA: 5";
Console.WriteLine("Elements in MyArrayB: " + MyArrayB.ElementCount()); // Returns "Elements in MyArrayB: 15";
Console.WriteLine("Params in FuncA(): " + FunctionA.ElementCount()); // Returns "Params in FuncA(): 3";
Console.WriteLine("Params in FuncB(): " + FunctionB.ElementCount()); // Returns "Params in FuncB(): 4";
Console.WriteLine("Params in FuncC(): " + FunctionC.ElementCount()); // Returns "Params in FuncC(): -1";
Console.WriteLine("Params in FuncD(): " + FunctionD.ElementCount()); // Returns "Params in FuncD(): 0";
Console.WriteLine("Operands in ! operator: " + uc.GetItemOf("!").ElementCount()); // Returns "Operands in ! operator: 1";
Console.WriteLine("Operands in > operator: " + uc.GetItemOf(">").ElementCount()); // Returns "Operands in > operator: 2";
Console.WriteLine("Params in SyntaxA: " + SyntaxA.ElementCount()); // Returns "Params in SyntaxA: 0";
Console.WriteLine("Params in SyntaxB: " + SyntaxB.ElementCount()); // Returns "Params in SyntaxB: 2";

          

//var MyArrayA = uc.DefineVariable('MyArrayA[] = {10, 20, 30, 40, 50}');
//var MyArrayB = uc.DefineVariable('MyArrayB[15]');
//var FunctionA = uc.DefineFunction('FuncA(x, y, z) = x + y + z');
//var FunctionB = uc.DefineFunction('FuncB(x, y, a = 12, b = 34) = x+y+a+b');
//var FunctionC = uc.DefineFunction('FuncC(x, y ...)', MyAverageCB);
//var FunctionD = uc.DefineFunction('FuncD() = 1+1');
//var SyntaxA = uc.DefineSyntax('This ::= That');
//var SyntaxB = uc.DefineSyntax('This {etc} and {more} ::= Whatever({etc})');

         WriteLn('Elements in MyArrayA: ' + MyArrayA.ElementCount()); // Returns 'Elements in MyArrayA: 5';
         WriteLn('Elements in MyArrayB: ' + MyArrayB.ElementCount()); // Returns 'Elements in MyArrayB: 15';
         WriteLn('Params in FuncA(): ' + FunctionA.ElementCount()); // Returns 'Params in FuncA(): 3';
         WriteLn('Params in FuncB(): ' + FunctionB.ElementCount()); // Returns 'Params in FuncB(): 4';
         WriteLn('Params in FuncC(): ' + FunctionC.ElementCount()); // Returns 'Params in FuncC(): -1';
         WriteLn('Params in FuncD(): ' + FunctionD.ElementCount()); // Returns 'Params in FuncD(): 0';
         WriteLn('Operands in ! operator: ' + uc.GetItemOf('!').ElementCount()); // Returns 'Operands in ! operator: 1';
         WriteLn('Operands in > operator: ' + uc.GetItemOf('>').ElementCount()); // Returns 'Operands in > operator: 2';
         WriteLn('Params in SyntaxA: ' + SyntaxA.ElementCount()); // Returns 'Params in SyntaxA: 0';
         WriteLn('Params in SyntaxB: ' + SyntaxB.ElementCount()); // Returns 'Params in SyntaxB: 2';

          

auto MyArrayA = uc.DefineVariable("MyArrayA[] = {10, 20, 30, 40, 50}");
auto MyArrayB = uc.DefineVariable("MyArrayB[15]");
auto FunctionA = uc.DefineFunction("FuncA(x, y, z) = x + y + z");
auto FunctionB = uc.DefineFunction("FuncB(x, y, a = 12, b = 34) = x+y+a+b");
auto FunctionC = uc.DefineFunction("FuncC(x, y ...)", MyAverageCB);
auto FunctionD = uc.DefineFunction("FuncD() = 1+1");
auto SyntaxA = uc.DefineSyntax("This ::= That");
auto SyntaxB = uc.DefineSyntax("This {etc} and {more} ::= Whatever({etc})");

cout << "Elements in MyArrayA: " << MyArrayA.ElementCount() << endl; // Returns "Elements in MyArrayA: 5";
cout << "Elements in MyArrayB: " << MyArrayB.ElementCount() << endl; // Returns "Elements in MyArrayB: 15";
cout << "Params in FuncA(): " << FunctionA.ElementCount() << endl; // Returns "Params in FuncA(): 3";
cout << "Params in FuncB(): " << FunctionB.ElementCount() << endl; // Returns "Params in FuncB(): 4";
cout << "Params in FuncC(): " << FunctionC.ElementCount() << endl; // Returns "Params in FuncC(): -1";
cout << "Params in FuncD(): " << FunctionD.ElementCount() << endl; // Returns "Params in FuncD(): 0";
cout << "Operands in ! operator: " << uc.GetItemOf("!").ElementCount() << endl; // Returns "Operands in ! operator: 1";
cout << "Operands in > operator: " << uc.GetItemOf(">").ElementCount() << endl; // Returns "Operands in > operator: 2";
cout << "Params in SyntaxA: " << SyntaxA.ElementCount() << endl; // Returns "Params in SyntaxA: 0";
cout << "Params in SyntaxB: " << SyntaxB.ElementCount() << endl; // Returns "Params in SyntaxB: 2";

          

auto MyArrayA = uc.DefineVariable("MyArrayA[] = {10, 20, 30, 40, 50}");
auto MyArrayB = uc.DefineVariable("MyArrayB[15]");
auto FunctionA = uc.DefineFunction("FuncA(x, y, z) = x + y + z");
auto FunctionB = uc.DefineFunction("FuncB(x, y, a = 12, b = 34) = x+y+a+b");
auto FunctionC = uc.DefineFunction("FuncC(x, y ...)", ucPinAddr(MyAverageCB));
auto FunctionD = uc.DefineFunction("FuncD() = 1+1");
auto SyntaxA = uc.DefineSyntax("This ::= That");
auto SyntaxB = uc.DefineSyntax("This {etc} and {more} ::= Whatever({etc})");

Console::WriteLine("Elements in MyArrayA: " + MyArrayA.ElementCount()); // Returns "Elements in MyArrayA: 5";
Console::WriteLine("Elements in MyArrayB: " + MyArrayB.ElementCount()); // Returns "Elements in MyArrayB: 15";
Console::WriteLine("Params in FuncA(): " + FunctionA.ElementCount()); // Returns "Params in FuncA(): 3";
Console::WriteLine("Params in FuncB(): " + FunctionB.ElementCount()); // Returns "Params in FuncB(): 4";
Console::WriteLine("Params in FuncC(): " + FunctionC.ElementCount()); // Returns "Params in FuncC(): -1";
Console::WriteLine("Params in FuncD(): " + FunctionD.ElementCount()); // Returns "Params in FuncD(): 0";
Console::WriteLine("Operands in ! operator: " + uc.GetItemOf("!").ElementCount()); // Returns "Operands in ! operator: 1";
Console::WriteLine("Operands in > operator: " + uc.GetItemOf(">").ElementCount()); // Returns "Operands in > operator: 2";
Console::WriteLine("Params in SyntaxA: " + SyntaxA.ElementCount()); // Returns "Params in SyntaxA: 0";
Console::WriteLine("Params in SyntaxB: " + SyntaxB.ElementCount()); // Returns "Params in SyntaxB: 2";

          
Example 3: Determining properties of an expression part

Sub OpItemTestCB(ByVal ExprPartPtr As IntPtr)
   Dim ExprPart As New uCalc.Callback(ExprPartPtr)
   Console.WriteLine("Name: " + ExprPart.OpItem().Name())
   Console.WriteLine("Data type: " + ExprPart.OpItem().GetDataType().Name())
   Console.WriteLine("Param count: " + ExprPart.OpItem().ElementCount())
   Console.WriteLine("Is operator? " + ExprPart.OpItem().IsProperty(ItemIsEnum.Operator_))
End Sub

Sub HandleTest()
   uc.DefineFunction("AAA() As Double", uc.PinAddr(AddressOf OpItemTestCB))
   uc.DefineFunction("BBB(x, y, z) As String", uc.PinAddr(AddressOf OpItemTestCB))
   uc.DefineOperator("{x} CCC {y} As Int32", 0, GroupingEnum.GroupLeftToRight, uc.PinAddr(AddressOf OpItemTestCB)): ' 0 should work
   uc.EvalStr("AAA()")
   ' Output
   ' Name: aaa
   ' Data type: double
   ' Param count: 0
   ' Is Operator_? 0

   uc.EvalStr("BBB(9, 8, 7)")
   ' Output
   ' Name: bbb
   ' Data type: string
   ' Param count: 3
   ' Is Operator_? 0

   uc.EvalStr("5 CCC 4")
   ' Output
   ' Name: ccc
   ' Data type: Int32
   ' Param count: 2
   ' Is Operator_? 1
End Sub

          

static void OpItemTestCB(IntPtr ExprPartPtr) {
   var ExprPart = New uCalc.Callback(ExprPartPtr);
   Console.WriteLine("Name: " + ExprPart.OpItem().Name());
   Console.WriteLine("Data type: " + ExprPart.OpItem().GetDataType().Name());
   Console.WriteLine("Param count: " + ExprPart.OpItem().ElementCount());
   Console.WriteLine("Is operator? " + ExprPart.OpItem().IsProperty(ItemIsEnum.Operator));
}

static void HandleTest() {
   uc.DefineFunction("AAA() As Double", uc.PinAddr(OpItemTestCB));
   uc.DefineFunction("BBB(x, y, z) As String", uc.PinAddr(OpItemTestCB));
   uc.DefineOperator("{x} CCC {y} As Int32", 0, GroupingEnum.GroupLeftToRight, uc.PinAddr(OpItemTestCB)); // 0 should work
   uc.EvalStr("AAA()");
   // Output
   // Name: aaa
   // Data type: double
   // Param count: 0
   // Is operator? 0
   
   uc.EvalStr("BBB(9, 8, 7)");
   // Output
   // Name: bbb
   // Data type: string
   // Param count: 3
   // Is operator? 0
   
   uc.EvalStr("5 CCC 4");
   // Output
   // Name: ccc
   // Data type: Int32
   // Param count: 2
   // Is operator? 1
   
}

          

procedure OpItemTestCB(ExprPartPtr: System.Pointer);
begin
   
//var ExprPart: uCalc.CreateCallback(ExprPartPtr);
   WriteLn('Name: ' + ExprPart.OpItem().Name());
   WriteLn('Data type: ' + ExprPart.OpItem().GetDataType().Name());
   WriteLn('Param count: ' + ExprPart.OpItem().ElementCount());
   WriteLn('Is operator? ' + ExprPart.OpItem().IsProperty(ItemIsEnum.Operator_));
End;

procedure HandleTest();
begin
   
   uc.DefineFunction('AAA() As Double', OpItemTestCB);
   uc.DefineFunction('BBB(x, y, z) As String', OpItemTestCB);
   uc.DefineOperator('{x} CCC {y} As Int32', 0, GroupingEnum.GroupLeftToRight, OpItemTestCB); // 0 should work
   uc.EvalStr('AAA()');
   // Output
// Name: aaa
// Data type: double
// Param count: 0
// Is Operator_? 0

   uc.EvalStr('BBB(9, 8, 7)');
   // Output
// Name: bbb
// Data type: string
// Param count: 3
// Is Operator_? 0

   uc.EvalStr('5 CCC 4');
   // Output
// Name: ccc
// Data type: Int32
// Param count: 2
// Is Operator_? 1

End;

          

void _stdcall OpItemTestCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   cout << "Name: " << ExprPart.OpItem().Name() << endl;
   cout << "Data type: " << ExprPart.OpItem().GetDataType().Name() << endl;
   cout << "Param count: " << ExprPart.OpItem().ElementCount() << endl;
   cout << "Is operator? " << ExprPart.OpItem().IsProperty(ItemIsEnum::Operator) << endl;
}

void HandleTest() {
   uc.DefineFunction("AAA() As Double", OpItemTestCB);
   uc.DefineFunction("BBB(x, y, z) As String", OpItemTestCB);
   uc.DefineOperator("{x} CCC {y} As Int32", 0, GroupingEnum::GroupLeftToRight, OpItemTestCB); // 0 should work
   uc.EvalStr("AAA()");
   // Output
   // Name: aaa
   // Data type: double
   // Param count: 0
   // Is operator? 0
   
   uc.EvalStr("BBB(9, 8, 7)");
   // Output
   // Name: bbb
   // Data type: string
   // Param count: 3
   // Is operator? 0
   
   uc.EvalStr("5 CCC 4");
   // Output
   // Name: ccc
   // Data type: int
   // Param count: 2
   // Is operator? 1
   
}

          

static void OpItemTestCB(uCalcPtr ExprPartPtr) {
   auto ExprPart = uCalc::Callback(ExprPartPtr);
   Console::WriteLine("Name: " + ExprPart.OpItem().Name());
   Console::WriteLine("Data type: " + ExprPart.OpItem().GetDataType().Name());
   Console::WriteLine("Param count: " + ExprPart.OpItem().ElementCount());
   Console::WriteLine("Is operator? " + ExprPart.OpItem().IsProperty(ItemIsEnum::Operator));
}

static void HandleTest() {
   uc.DefineFunction("AAA() As Double", ucPinAddr(OpItemTestCB));
   uc.DefineFunction("BBB(x, y, z) As String", ucPinAddr(OpItemTestCB));
   uc.DefineOperator("{x} CCC {y} As Int32", 0, GroupingEnum::GroupLeftToRight, ucPinAddr(OpItemTestCB)); // 0 should work
   uc.EvalStr("AAA()");
   // Output
   // Name: aaa
   // Data type: double
   // Param count: 0
   // Is operator? 0
   
   uc.EvalStr("BBB(9, 8, 7)");
   // Output
   // Name: bbb
   // Data type: string
   // Param count: 3
   // Is operator? 0
   
   uc.EvalStr("5 CCC 4");
   // Output
   // Name: ccc
   // Data type: int
   // Param count: 2
   // Is operator? 1
   
}

          
DLL import code
<DllImport(uCalcDLL, CharSet:=CharSet.Ansi, CallingConvention:=CallingConvention.Cdecl, EntryPoint:="ElementCount")> _

Private Function ElementCount__(ByVal ItemHandle As IntPtr) As  Int32
End Function
            
[DllImport(uCalcDLL, CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl, EntryPoint="ElementCount")]

protected static extern  Int32 ElementCount_(IntPtr ItemHandle);
            
{DLLImport}function ElementCount__(ItemHandle: System.Pointer):  Int32; cdecl; external uCalcDLL name 'ElementCount';

            
typedef  int32_t (* __ElementCount)(void *ItemHandle); 

            
[DllImport(uCalcLib, CharSet=CharSet::Ansi, CallingConvention=CallingConvention::Cdecl, EntryPoint = "ElementCount")]

static Int32 ElementCount_(void *  ItemHandle);
            
Prev | Next