Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
And.cs
1using System;
7
9{
14 {
24 {
25 }
26
30 public override string FunctionName => nameof(And);
31
39 {
40 throw new ScriptRuntimeException("Expected a double or boolean vector.", this);
41 }
42
50 {
51 return new DoubleNumber(CalcAnd(Argument.Values, this));
52 }
53
60 public static double CalcAnd(double[] Values, ScriptNode Node)
61 {
62 double d;
63 ulong Result;
64 int i, c = Values.Length;
65 bool Signed = false;
66
67 if (c == 0)
68 throw new ScriptRuntimeException("Empty set of values.", Node);
69
70 d = Values[0];
71 if (d < long.MinValue || d > ulong.MaxValue || d != Math.Truncate(d))
72 throw new ScriptRuntimeException("Values must be integers.", Node);
73
74 if (d < 0)
75 {
76 Result = (ulong)((long)d);
77 Signed = true;
78 }
79 else
80 Result = (ulong)d;
81
82 if (Result == 0)
83 return 0;
84
85 for (i = 1; i < c; i++)
86 {
87 d = Values[i];
88 if (d < long.MinValue || d > ulong.MaxValue || d != Math.Truncate(d))
89 throw new ScriptRuntimeException("Values must be integers.", Node);
90
91 if (d < 0)
92 {
93 Result = (ulong)(((long)Result) & ((long)Result));
94 Signed = true;
95 }
96 else
97 Result &= (ulong)d;
98
99 if (Result == 0)
100 return 0;
101 }
102
103 if (Signed)
104 {
105 if (Result > long.MaxValue)
106 throw new ScriptRuntimeException("Overflow.", Node);
107 else
108 return (long)Result;
109 }
110 else
111 return Result;
112 }
113
121 {
122 return new BooleanValue(CalcAnd(Argument.Values, this));
123 }
124
131 public static bool CalcAnd(bool[] Values, ScriptNode Node)
132 {
133 int i, c = Values.Length;
134
135 if (c == 0)
136 throw new ScriptRuntimeException("Empty set of values.", Node);
137
138 for (i = 0; i < c; i++)
139 {
140 if (!Values[i])
141 return false;
142 }
143
144 return true;
145 }
146
147 }
148}
Class managing a script expression.
Definition: Expression.cs:39
override string FunctionName
Name of the function
Definition: And.cs:30
override IElement EvaluateVector(BooleanVector Argument, Variables Variables)
Evaluates the function on a vector argument.
Definition: And.cs:120
override IElement EvaluateVector(DoubleVector Argument, Variables Variables)
Evaluates the function on a vector argument.
Definition: And.cs:49
override IElement EvaluateVector(IVector Argument, Variables Variables)
Evaluates the function on a vector argument.
Definition: And.cs:38
static double CalcAnd(double[] Values, ScriptNode Node)
Calculates the binary AND of all double-valued elements. Values must be integers.
Definition: And.cs:60
static bool CalcAnd(bool[] Values, ScriptNode Node)
Calculates the logical AND of all boolean-valued elements.
Definition: And.cs:131
And(ScriptNode Argument, int Start, int Length, Expression Expression)
And(v)
Definition: And.cs:22
ScriptNode Argument
Function argument.
Base class for funcions of one vector variable.
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
Boolean-valued number.
Definition: BooleanValue.cs:12
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for vectors.
Definition: IVector.cs:9