Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CustomOrderEnumerator.cs
1using System;
4using System.Threading.Tasks;
9
11{
16 {
17 private readonly Dictionary<Type, ObjectProperties> propertiesX = new Dictionary<Type, ObjectProperties>();
18 private readonly Dictionary<Type, ObjectProperties> propertiesY = new Dictionary<Type, ObjectProperties>();
19 private readonly KeyValuePair<ScriptNode, bool>[] order;
20 private readonly IResultSetEnumerator items;
21 private readonly Variables variables;
22 private IEnumerator<object> e = null;
23
30 public CustomOrderEnumerator(IResultSetEnumerator ItemEnumerator, Variables Variables, KeyValuePair<ScriptNode, bool>[] Order)
31 {
32 this.order = Order;
33 this.items = ItemEnumerator;
34 this.variables = Variables;
35 }
36
40 public object Current => this.e.Current;
41
45 public void Dispose()
46 {
47 this.e?.Dispose();
48 this.e = null;
49 }
50
54 public bool MoveNext()
55 {
56 return this.MoveNextAsync().Result;
57 }
58
62 public async Task<bool> MoveNextAsync()
63 {
64 if (this.e is null)
65 {
66 List<object> Items = new List<object>();
67
68 while (await this.items.MoveNextAsync())
69 Items.Add(this.items.Current);
70
71 Items.Sort((x, y) =>
72 {
73 if (x is null)
74 {
75 if (y is null)
76 return 0;
77 else
78 return -1;
79 }
80 else if (y is null)
81 return 1;
82
83 Type Tx = x.GetType();
84 Type Ty = y.GetType();
85
86 if (this.propertiesX.TryGetValue(Tx, out ObjectProperties Vx))
87 Vx.Object = x;
88 else
89 {
90 Vx = new ObjectProperties(x, this.variables);
91 this.propertiesX[Tx] = Vx;
92 }
93
94 if (this.propertiesY.TryGetValue(Ty, out ObjectProperties Vy))
95 Vy.Object = y;
96 else
97 {
98 Vy = new ObjectProperties(y, this.variables);
99 this.propertiesY[Ty] = Vy;
100 }
101
102 int i, j, c = this.order.Length;
103 IElement Ex, Ey;
104 ScriptNode Node;
105
106 for (i = 0; i < c; i++)
107 {
108 Node = this.order[i].Key;
109 Ex = Node.Evaluate(Vx); // TODO: Async
110 Ey = Node.Evaluate(Vy); // TODO: Async
111
112 if (!(Ex.AssociatedSet is IOrderedSet S))
113 throw new ScriptRuntimeException("Result not member of an ordered set.", Node);
114
115 j = S.Compare(Ex, Ey);
116 if (j != 0)
117 {
118 if (this.order[i].Value)
119 return j;
120 else
121 return -j;
122 }
123 }
124
125 return 0;
126 });
127
128 this.e = Items.GetEnumerator();
129 }
130
131 return this.e.MoveNext();
132 }
133
137 public void Reset()
138 {
139 this.items.Reset();
140 this.e = null;
141 }
142 }
143}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
CustomOrderEnumerator(IResultSetEnumerator ItemEnumerator, Variables Variables, KeyValuePair< ScriptNode, bool >[] Order)
Enumerator that reorders a sequence of items.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11