Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IndexOf.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(IndexOf);
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 = 0;
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.IndexOf(s2));
80
81 return new DoubleNumber(s1.IndexOf(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.IndexOf(V2, Item));
96
97 int i = 0;
98
99 foreach (IElement Element in ChildElements)
100 {
101 if (i >= FromIndex)
102 {
103 if (Item.Equals(Element))
104 return new DoubleNumber(i);
105 }
106
107 i++;
108 }
109
110 return new DoubleNumber(-1);
111 }
112 }
113
114 }
115}
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
IndexOf(Vector,Item[,From])
Definition: IndexOf.cs:15
override string FunctionName
Name of the function
Definition: IndexOf.cs:48
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function on a vector argument.
Definition: IndexOf.cs:64
IndexOf(ScriptNode Vector, ScriptNode Item, int Start, int Length, Expression Expression)
IndexOf(Vector,Item)
Definition: IndexOf.cs:24
override string[] DefaultArgumentNames
Default Argument names
Definition: IndexOf.cs:54
IndexOf(ScriptNode Vector, ScriptNode Item, ScriptNode From, int Start, int Length, Expression Expression)
IndexOf(Vector,Item,From)
Definition: IndexOf.cs:39
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