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;
9
11{
17 {
27 : base(new ScriptNode[] { Vector, Item }, new ArgumentType[] { ArgumentType.Normal, ArgumentType.Scalar },
29 {
30 }
31
42 : base(new ScriptNode[] { Vector, Item, From }, new ArgumentType[] { ArgumentType.Normal, ArgumentType.Scalar, ArgumentType.Scalar },
44 {
45 }
46
57 public LastIndexOf(ScriptNode Matrix, ScriptNode Item, ScriptNode FromColumn, ScriptNode FromRow,
59 : base(new ScriptNode[] { Matrix, Item, FromColumn, FromRow }, new ArgumentType[] { ArgumentType.Matrix, ArgumentType.Scalar, ArgumentType.Scalar, ArgumentType.Scalar },
61 {
62 }
63
67 public override string FunctionName => nameof(LastIndexOf);
68
72 public override string[] DefaultArgumentNames
73 {
74 get { return new string[] { "Vector", "Item" }; }
75 }
76
84 {
85 IElement Vector = Arguments[0];
86 IElement Item = Arguments[1];
87 IElement From = Arguments.Length > 2 ? Arguments[2] : null;
88 IElement From2 = Arguments.Length > 3 ? Arguments[3] : null;
89 int FromIndex, FromIndex2;
90
91 if (From is null)
92 FromIndex = int.MaxValue;
93 else
94 FromIndex = (int)Expression.ToDouble(From.AssociatedObjectValue);
95
96 if (Vector.AssociatedObjectValue is string s1 && Item.AssociatedObjectValue is string s2)
97 {
98 if (From is null)
99 return new DoubleNumber(s1.LastIndexOf(s2));
100
101 return new DoubleNumber(s1.LastIndexOf(s2, FromIndex));
102 }
103 else if (Vector is IMatrix M)
104 {
105 if (From is null || From2 is null)
106 {
107 if (M.TryFindLast(Item, out int Column, out int Row))
108 return new DoubleVector(Column, Row);
109 else
110 return new DoubleVector(-1, -1);
111 }
112 else
113 {
114 FromIndex2 = (int)Expression.ToDouble(From2.AssociatedObjectValue);
115
116 if (M.TryFindLast(Item, FromIndex, FromIndex2, out int Column, out int Row))
117 return new DoubleVector(Column, Row);
118 else
119 return new DoubleVector(-1, -1);
120 }
121 }
122 else
123 {
124 ICollection<IElement> ChildElements;
125
126 if (Vector is IVector V)
127 ChildElements = V.VectorElements;
128 else if (Vector is ISet S)
129 ChildElements = S.ChildElements;
130 else
131 throw new ScriptRuntimeException("Expected string arguments, or the first argument to be a vector or a set.", this);
132
133 if (ChildElements is IElement[] V2)
134 return new DoubleNumber(Array.LastIndexOf(V2, Item));
135
136 int i = 0;
137 int Result = -1;
138
139 foreach (IElement Element in ChildElements)
140 {
141 if (Item.Equals(Element))
142 Result = i;
143
144 if (++i > FromIndex)
145 break;
146 }
147
148 return new DoubleNumber(Result);
149 }
150 }
151
152 }
153}
Base class for all types of elements.
Definition: Element.cs:14
Class managing a script expression.
Definition: Expression.cs:41
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:5110
LastIndexOf(Vector,Item[,From]) LastIndexOf(Matrix,Item[,FromColumn,FromRow])
Definition: LastIndexOf.cs:17
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function on a vector argument.
Definition: LastIndexOf.cs:83
LastIndexOf(ScriptNode Vector, ScriptNode Item, ScriptNode From, int Start, int Length, Expression Expression)
LastIndexOf(Vector,Item,From)
Definition: LastIndexOf.cs:41
override string[] DefaultArgumentNames
Default Argument names
Definition: LastIndexOf.cs:73
LastIndexOf(ScriptNode Matrix, ScriptNode Item, ScriptNode FromColumn, ScriptNode FromRow, int Start, int Length, Expression Expression)
LastIndexOf(Matrix,Item,FromColumn,FromRow)
Definition: LastIndexOf.cs:57
override string FunctionName
Name of the function
Definition: LastIndexOf.cs:67
LastIndexOf(ScriptNode Vector, ScriptNode Item, int Start, int Length, Expression Expression)
LastIndexOf(Vector,Item)
Definition: LastIndexOf.cs:26
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:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
Basic interface for matrices.
Definition: IMatrix.cs:7
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