Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DateTimeVector.cs
1using System;
3using System.Reflection;
4using System.Text;
11
13{
17 public sealed class DateTimeVector : VectorSpaceElement
18 {
19 private DateTime[] values;
20 private ICollection<IElement> elements;
21 private readonly int dimension;
22
27 public DateTimeVector(params DateTime[] Values)
28 {
29 this.values = Values;
30 this.elements = null;
31 this.dimension = Values.Length;
32 }
33
38 public DateTimeVector(ICollection<IElement> Elements)
39 {
40 this.values = null;
41 this.elements = Elements;
42 this.dimension = Elements.Count;
43 }
44
48 public DateTime[] Values
49 {
50 get
51 {
52 if (this.values is null)
53 {
54 DateTime[] v = new DateTime[this.dimension];
55 int i = 0;
56
57 if (this.elements is ChunkedList<IElement> Values)
58 {
59 ChunkNode<IElement> Loop = Values.FirstChunk;
60 int j, c;
61
62 while (!(Loop is null))
63 {
64 for (j = Loop.Start, c = Loop.Pos; j < c; j++)
65 {
66 if (!(Loop[j].AssociatedObjectValue is DateTime TP))
67 TP = DateTime.MinValue;
68
69 v[i++] = TP;
70 }
71
72 Loop = Loop.Next;
73 }
74 }
75 else
76 {
77 foreach (IElement Element in this.elements)
78 {
79 if (!(Element.AssociatedObjectValue is DateTime TP))
80 TP = DateTime.MinValue;
81
82 v[i++] = TP;
83 }
84 }
85
86 this.values = v;
87 }
88
89 return this.values;
90 }
91 }
92
96 public ICollection<IElement> Elements
97 {
98 get
99 {
100 if (this.elements is null)
101 {
102 int i;
103 IElement[] v = new IElement[this.dimension];
104
105 for (i = 0; i < this.dimension; i++)
106 v[i] = new DateTimeValue(this.values[i]);
107
108 this.elements = v;
109 }
110
111 return this.elements;
112 }
113 }
114
118 public override int Dimension => this.dimension;
119
121 public override string ToString()
122 {
123 StringBuilder sb = null;
124
125 foreach (DateTime d in this.Values)
126 {
127 if (sb is null)
128 sb = new StringBuilder("[");
129 else
130 sb.Append(", ");
131
132 sb.Append(Expression.ToString(d));
133 }
134
135 if (sb is null)
136 return "[]";
137 else
138 {
139 sb.Append(']');
140 return sb.ToString();
141 }
142 }
143
148 {
149 get
150 {
151 if (this.associatedVectorSpace is null)
152 this.associatedVectorSpace = new DateTimeVectors(this.dimension);
153
154 return this.associatedVectorSpace;
155 }
156 }
157
158 private DateTimeVectors associatedVectorSpace = null;
159
163 public override object AssociatedObjectValue => this.Values;
164
171 {
172 return null;
173 }
174
181 {
182 return null;
183 }
184
189 public override IGroupElement Negate()
190 {
191 return null;
192 }
193
199 public override bool Equals(object obj)
200 {
202 if (DateTimeVector is null)
203 return false;
204
205 int i;
206 if (DateTimeVector.dimension != this.dimension)
207 return false;
208
209 DateTime[] Values = this.Values;
210 DateTime[] Values2 = DateTimeVector.Values;
211 for (i = 0; i < this.dimension; i++)
212 {
213 if (Values[i] != Values2[i])
214 return false;
215 }
216
217 return true;
218 }
219
224 public override int GetHashCode()
225 {
226 DateTime[] Values = this.Values;
227 int Result = 0;
228 int i;
229
230 for (i = 0; i < this.dimension; i++)
231 Result ^= Values[i].GetHashCode();
232
233 return Result;
234 }
235
239 public override bool IsScalar
240 {
241 get { return false; }
242 }
243
247 public override ICollection<IElement> ChildElements => this.Elements;
248
256 {
257 return VectorDefinition.Encapsulate(Elements, true, Node);
258 }
259
266 public override IElement Encapsulate(ICollection<IElement> Elements, ScriptNode Node)
267 {
268 return VectorDefinition.Encapsulate(Elements, true, Node);
269 }
270
275 {
276 get
277 {
278 if (this.zero is null)
279 this.zero = new DateTimeVector(new DateTime[this.dimension]);
280
281 return this.zero;
282 }
283 }
284
285 private DateTimeVector zero = null;
286
292 public override IElement GetElement(int Index)
293 {
294 if (Index < 0 || Index >= this.dimension)
295 throw new ScriptException("Index out of bounds.");
296
297 DateTime[] V = this.Values;
298
299 return new DateTimeValue(V[Index]);
300 }
301
307 public override void SetElement(int Index, IElement Value)
308 {
309 if (Index < 0 || Index >= this.dimension)
310 throw new ScriptException("Index out of bounds.");
311
312 if (!(Value.AssociatedObjectValue is DateTime V))
313 throw new ScriptException("Elements in a boolean vector are required to be boolean values.");
314
315 DateTime[] Values = this.Values;
316 this.elements = null;
317
318 Values[Index] = V;
319 }
320
327 public override bool TryConvertTo(Type DesiredType, out object Value)
328 {
329 if (DesiredType == typeof(DateTime[]))
330 {
331 Value = this.Values;
332 return true;
333 }
334 else if (DesiredType.IsAssignableFrom(typeof(DateTimeVector).GetTypeInfo()))
335 {
336 Value = this;
337 return true;
338 }
339 else
340 return Expression.TryConvert(this.Values, DesiredType, true, out Value);
341 }
342
343 }
344}
Node referencing a chunk in a ChunkedList<T>
Definition: ChunkNode.cs:11
ChunkNode< T > Next
Next chunk
Definition: ChunkNode.cs:26
int Pos
Index after the last element in chunk.
Definition: ChunkNode.cs:51
int Start
Index of first element in chunk.
Definition: ChunkNode.cs:46
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Base class for all types of elements.
Definition: Element.cs:14
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:43
Base class for all types of vector space elements (vectors).
Base class for script exceptions.
Class managing a script expression.
Definition: Expression.cs:41
static bool TryConvert(object Value, Type DesiredType, bool AcceptInformationLoss, out object Result)
Tries to convert an object Value to an object of type DesiredType .
Definition: Expression.cs:5530
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4760
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
override int GetHashCode()
Calculates a hash code of the element.
override IVectorSpaceElement MultiplyScalar(IFieldElement Scalar)
Tries to multiply a scalar to the current element.
override bool TryConvertTo(Type DesiredType, out object Value)
Converts the value to a .NET type.
override IAbelianGroupElement Add(IAbelianGroupElement Element)
Tries to add an element to the current element.
override IGroupElement Negate()
Negates the element.
DateTimeVector(ICollection< IElement > Elements)
DateTime-valued vector.
DateTime[] Values
Vector element values.
override bool IsScalar
If the element represents a scalar value.
override IAbelianGroupElement Zero
Returns the zero element of the group.
override bool Equals(object obj)
Compares the element to another.
override IVectorSpace AssociatedVectorSpace
Associated Right-VectorSpace.
override IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
override void SetElement(int Index, IElement Value)
Sets an element in the vector.
override int Dimension
Dimension of vector.
override object AssociatedObjectValue
Associated object value.
override IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
override IElement GetElement(int Index)
Gets an element of the vector.
ICollection< IElement > Elements
Vector elements.
DateTimeVector(params DateTime[] Values)
DateTime-valued vector.
Pseudo-vector space of DateTime-valued vectors.
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Basic interface for all types of abelian group elements.
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
Basic interface for all types of field elements.
Basic interface for all types of group elements.
Definition: IGroupElement.cs:9
Basic interface for all types of module elements.
Basic interface for all types of modules.
Definition: IVectorSpace.cs:10