Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Residue.cs
1using System;
3using System.Threading.Tasks;
10
12{
16 public class Residue : BinaryOperator
17 {
27 : base(Left, Right, Start, Length, Expression)
28 {
29 }
30
37 {
38 IElement Left = this.left.Evaluate(Variables);
39 IElement Right = this.right.Evaluate(Variables);
40
41 return EvaluateResidue(Left, Right, this);
42 }
43
49 public override async Task<IElement> EvaluateAsync(Variables Variables)
50 {
51 if (!this.isAsync)
52 return this.Evaluate(Variables);
53
54 IElement Left = await this.left.EvaluateAsync(Variables);
55 IElement Right = await this.right.EvaluateAsync(Variables);
56
57 return EvaluateResidue(Left, Right, this);
58 }
59
67 public static IElement EvaluateResidue(IElement Left, IElement Right, ScriptNode Node)
68 {
69 if (Left.AssociatedObjectValue is double dl && Right.AssociatedObjectValue is double dr)
70 {
71 if (dl < long.MinValue || dl > long.MaxValue || dl != Math.Truncate(dl))
72 throw new ScriptRuntimeException("Modulus operator does not work on decimal numbers.", Node);
73
74 if (dr < long.MinValue || dr > long.MaxValue || dr != Math.Truncate(dr))
75 throw new ScriptRuntimeException("Modulus operator does not work on decimal numbers.", Node);
76
77 long l = (long)dl;
78 long r = (long)dr;
79
80 return new DoubleNumber(l % r);
81 }
82
83 if (Left.IsScalar)
84 {
85 if (Right.IsScalar)
86 {
87 ISet LeftSet = Left.AssociatedSet;
88 ISet RightSet = Right.AssociatedSet;
89
90 if (!LeftSet.Equals(RightSet))
91 {
92 if (Expression.UpgradeField(ref Left, ref LeftSet, ref Right, ref RightSet))
93 {
94 if (Left is IEuclidianDomainElement LE && Right is IEuclidianDomainElement RE)
95 {
96 ((IEuclidianDomain)LeftSet).Divide(LE, RE, out IEuclidianDomainElement Result);
97 return Result;
98 }
99 }
100 }
101
102 IElement Result2 = EvaluateNamedOperator("op_Modulus", Left, Right, Node);
103 if (!(Result2 is null))
104 return Result2;
105
106 throw new ScriptRuntimeException("Residue could not be computed.", Node);
107 }
108 else
109 {
111
112 foreach (IElement RightChild in Right.ChildElements)
113 Elements.Add(EvaluateResidue(Left, RightChild, Node));
114
115 return Right.Encapsulate(Elements, Node);
116 }
117 }
118 else
119 {
120 if (Right.IsScalar)
121 {
123
124 foreach (IElement LeftChild in Left.ChildElements)
125 Elements.Add(EvaluateResidue(LeftChild, Right, Node));
126
127 return Left.Encapsulate(Elements, Node);
128 }
129 else
130 {
131 ICollection<IElement> LeftChildren = Left.ChildElements;
132 ICollection<IElement> RightChildren = Right.ChildElements;
133
134 if (LeftChildren.Count == RightChildren.Count)
135 {
137 IEnumerator<IElement> eLeft = LeftChildren.GetEnumerator();
138 IEnumerator<IElement> eRight = RightChildren.GetEnumerator();
139
140 try
141 {
142 while (eLeft.MoveNext() && eRight.MoveNext())
143 Elements.Add(EvaluateResidue(eLeft.Current, eRight.Current, Node));
144 }
145 finally
146 {
147 eLeft.Dispose();
148 eRight.Dispose();
149 }
150
151 return Left.Encapsulate(Elements, Node);
152 }
153 else
154 {
156
157 foreach (IElement LeftChild in LeftChildren)
158 {
160
161 foreach (IElement RightChild in RightChildren)
162 RightResult.Add(EvaluateResidue(LeftChild, RightChild, Node));
163
164 LeftResult.Add(Right.Encapsulate(RightResult, Node));
165 }
166
167 return Left.Encapsulate(LeftResult, Node);
168 }
169 }
170 }
171 }
172
173 }
174}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Class managing a script expression.
Definition: Expression.cs:41
static bool UpgradeField(ref IElement E1, ref ISet Set1, ref IElement E2, ref ISet Set2)
Upgrades elements if necessary, to a common field extension, trying to make them compatible....
Definition: Expression.cs:5327
Base class for all binary operators.
ScriptNode right
Right operand.
ScriptNode left
Left operand.
static IElement EvaluateNamedOperator(string Name, IElement Left, IElement Right, ScriptNode Node)
Evaluates a named operator available in code-behind.
bool isAsync
If subtree is asynchroneous.
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
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
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 IElement EvaluateResidue(IElement Left, IElement Right, ScriptNode Node)
Divides the right operand from the left one.
Definition: Residue.cs:67
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Residue.cs:36
Residue(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Residue operator.
Definition: Residue.cs:26
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Residue.cs:49
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
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:50
IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
bool IsScalar
If the element represents a scalar value.
Definition: IElement.cs:42
Basic interface for all types of Euclidian domain elements.
Basic interface for all types of euclidian domains.
Basic interface for all types of sets.
Definition: ISet.cs:10