Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LastIndexOf.cs
1using System;
2using System.Collections.Generic;
8
10{
15 {
25 : base(new ScriptNode[] { Vector, Item }, new ArgumentType[] { ArgumentType.Normal, ArgumentType.Scalar },
27 {
28 }
29
40 : base(new ScriptNode[] { Vector, Item, From }, new ArgumentType[] { ArgumentType.Normal, ArgumentType.Scalar, ArgumentType.Scalar },
42 {
43 }
44
48 public override string FunctionName => nameof(LastIndexOf);
49
53 public override string[] DefaultArgumentNames
54 {
55 get { return new string[] { "Vector", "Item" }; }
56 }
57
65 {
66 IElement Vector = Arguments[0];
67 IElement Item = Arguments[1];
68 IElement From = Arguments.Length > 2 ? Arguments[2] : null;
69 int FromIndex;
70
71 if (From is null)
72 FromIndex = int.MaxValue;
73 else
74 FromIndex = (int)Expression.ToDouble(From.AssociatedObjectValue);
75
76 if (Vector.AssociatedObjectValue is string s1 && Item.AssociatedObjectValue is string s2)
77 {
78 if (From is null)
79 return new DoubleNumber(s1.LastIndexOf(s2));
80
81 return new DoubleNumber(s1.LastIndexOf(s2, FromIndex));
82 }
83 else
84 {
85 ICollection<IElement> ChildElements;
86
87 if (Vector is IVector V)
88 ChildElements = V.VectorElements;
89 else if (Vector is ISet S)
90 ChildElements = S.ChildElements;
91 else
92 throw new ScriptRuntimeException("Expected string arguments, or the first argument to be a vector or a set.", this);
93
94 if (ChildElements is IElement[] V2)
95 return new DoubleNumber(Array.LastIndexOf(V2, Item));
96
97 int i = 0;
98 int Result = -1;
99
100 foreach (IElement Element in ChildElements)
101 {
102 if (Item.Equals(Element))
103 Result = i;
104
105 if (++i > FromIndex)
106 break;
107 }
108
109 return new DoubleNumber(Result);
110 }
111 }
112
113 }
114}
Base class for all types of elements.
Definition: Element.cs:13
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
LastIndexOf(Vector,Item[,From])
Definition: LastIndexOf.cs:15
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function on a vector argument.
Definition: LastIndexOf.cs:64
LastIndexOf(ScriptNode Vector, ScriptNode Item, ScriptNode From, int Start, int Length, Expression Expression)
LastIndexOf(Vector,Item,From)
Definition: LastIndexOf.cs:39
override string[] DefaultArgumentNames
Default Argument names
Definition: LastIndexOf.cs:54
override string FunctionName
Name of the function
Definition: LastIndexOf.cs:48
LastIndexOf(ScriptNode Vector, ScriptNode Item, int Start, int Length, Expression Expression)
LastIndexOf(Vector,Item)
Definition: LastIndexOf.cs:24
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for all types of sets.
Definition: ISet.cs:10
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9