Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ElementList.cs
1using System.Collections.Generic;
2using System.Numerics;
3using System.Threading.Tasks;
8
10{
14 public class ElementList : ScriptNode
15 {
16 private readonly ScriptNode[] elements;
17 private readonly int nrElements;
18
22 protected bool isAsync;
23
32 : base(Start, Length, Expression)
33 {
34 this.elements = Elements;
35 this.elements?.SetParent(this);
36
37 this.nrElements = Elements.Length;
38
39 this.CalcIsAsync();
40 }
41
42 private void CalcIsAsync()
43 {
44 this.isAsync = false;
45
46 for (int i = 0; i < this.nrElements; i++)
47 {
48 if (this.elements[i]?.IsAsynchronous ?? false)
49 {
50 this.isAsync = true;
51 break;
52 }
53 }
54 }
55
59 public ScriptNode[] Elements => this.elements;
60
65 public override bool IsAsynchronous => this.isAsync;
66
73 {
74 LinkedList<IElement> List = new LinkedList<IElement>();
75 int c = 0;
76
77 foreach (ScriptNode E in this.elements)
78 {
79 if (E is null)
80 List.AddLast((IElement)null);
81 else
82 List.AddLast(E.Evaluate(Variables));
83
84 c++;
85 }
86
87 switch (c)
88 {
89 case 0:
90 return ObjectValue.Null;
91
92 case 1:
93 return List.First.Value;
94
95 case 2:
96 if (List.First.Value.AssociatedObjectValue is double Re &&
97 List.First.Next.Value.AssociatedObjectValue is double Im)
98 {
99 return new ComplexNumber(new Complex(Re, Im));
100 }
101 break;
102 }
103
104 return VectorDefinition.Encapsulate(List, true, this);
105 }
106
112 public override async Task<IElement> EvaluateAsync(Variables Variables)
113 {
114 if (!this.isAsync)
115 return this.Evaluate(Variables);
116
117 LinkedList<IElement> List = new LinkedList<IElement>();
118 int c = 0;
119
120 foreach (ScriptNode E in this.elements)
121 {
122 if (E is null)
123 List.AddLast((IElement)null);
124 else
125 List.AddLast(await E.EvaluateAsync(Variables));
126
127 c++;
128 }
129
130 switch (c)
131 {
132 case 0:
133 return ObjectValue.Null;
134
135 case 1:
136 return List.First.Value;
137
138 case 2:
139 if (List.First.Value.AssociatedObjectValue is double Re &&
140 List.First.Next.Value.AssociatedObjectValue is double Im)
141 {
142 return new ComplexNumber(new Complex(Re, Im));
143 }
144 break;
145 }
146
147 return VectorDefinition.Encapsulate(List, true, this);
148 }
149
157 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
158 {
159 if (Order == SearchMethod.DepthFirst)
160 {
161 if (!this.elements.ForAllChildNodes(Callback, State, Order))
162 return false;
163 }
164
165 ScriptNode Node;
166 bool RecalcIsAsync = false;
167 int i;
168
169 for (i = 0; i < this.nrElements; i++)
170 {
171 Node = this.elements[i];
172 if (!(Node is null))
173 {
174 bool b = !Callback(Node, out ScriptNode NewNode, State);
175 if (!(NewNode is null))
176 {
177 this.elements[i] = Node = NewNode;
178 NewNode.SetParent(this);
179
180 RecalcIsAsync = true;
181 }
182
183 if (b || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
184 {
185 if (RecalcIsAsync)
186 this.CalcIsAsync();
187
188 return false;
189 }
190 }
191 }
192
193 if (RecalcIsAsync)
194 this.CalcIsAsync();
195
196 if (Order == SearchMethod.BreadthFirst)
197 {
198 for (i = 0; i < this.nrElements; i++)
199 {
200 if (!this.elements.ForAllChildNodes(Callback, State, Order))
201 return false;
202 }
203 }
204
205 return true;
206 }
207
209 public override bool Equals(object obj)
210 {
211 return obj is ElementList O &&
212 AreEqual(this.elements, O.elements) &&
213 base.Equals(obj);
214 }
215
217 public override int GetHashCode()
218 {
219 int Result = base.GetHashCode();
220 Result ^= Result << 5 ^ GetHashCode(this.elements);
221 return Result;
222 }
223
224 }
225}
Class managing a script expression.
Definition: Expression.cs:39
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, bool DepthFirst)
Calls the callback method for all child nodes.
Definition: ScriptNode.cs:243
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
static bool AreEqual(ScriptNode S1, ScriptNode S2)
Compares if two script nodes are equal.
Definition: ScriptNode.cs:275
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
abstract IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
object Value
Object value.
Definition: ObjectValue.cs:30
Represents a list of elements.
Definition: ElementList.cs:15
override bool Equals(object obj)
Definition: ElementList.cs:209
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: ElementList.cs:65
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Definition: ElementList.cs:157
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: ElementList.cs:72
bool isAsync
If any of the elements are asynchronous
Definition: ElementList.cs:22
ElementList(ScriptNode[] Elements, int Start, int Length, Expression Expression)
Represents a list of elements.
Definition: ElementList.cs:31
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: ElementList.cs:112
ScriptNode[] Elements
Elements.
Definition: ElementList.cs:59
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
delegate bool ScriptNodeEventHandler(ScriptNode Node, out ScriptNode NewNode, object State)
Delegate for ScriptNode callback methods.
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38