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;
2using System.Collections;
3using System.Collections.Generic;
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 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 bool MoveNext()
46 {
47 return this.MoveNextAsync().Result;
48 }
49
53 public async Task<bool> MoveNextAsync()
54 {
55 if (this.e is null)
56 {
57 List<object> Items = new List<object>();
58
59 while (await this.items.MoveNextAsync())
60 Items.Add(this.items.Current);
61
62 Items.Sort((x, y) =>
63 {
64 if (x is null)
65 {
66 if (y is null)
67 return 0;
68 else
69 return -1;
70 }
71 else if (y is null)
72 return 1;
73
74 Type Tx = x.GetType();
75 Type Ty = y.GetType();
76
77 if (this.propertiesX.TryGetValue(Tx, out ObjectProperties Vx))
78 Vx.Object = x;
79 else
80 {
81 Vx = new ObjectProperties(x, this.variables);
82 this.propertiesX[Tx] = Vx;
83 }
84
85 if (this.propertiesY.TryGetValue(Ty, out ObjectProperties Vy))
86 Vy.Object = y;
87 else
88 {
89 Vy = new ObjectProperties(y, this.variables);
90 this.propertiesY[Ty] = Vy;
91 }
92
93 int i, j, c = this.order.Length;
94 IElement Ex, Ey;
95 ScriptNode Node;
96
97 for (i = 0; i < c; i++)
98 {
99 Node = this.order[i].Key;
100 Ex = Node.Evaluate(Vx);
101 Ey = Node.Evaluate(Vy);
102
103 if (!(Ex.AssociatedSet is IOrderedSet S))
104 throw new ScriptRuntimeException("Result not member of an ordered set.", Node);
105
106 j = S.Compare(Ex, Ey);
107 if (j != 0)
108 {
109 if (this.order[i].Value)
110 return j;
111 else
112 return -j;
113 }
114 }
115
116 return 0;
117 });
118
119 this.e = Items.GetEnumerator();
120 }
121
122 return this.e.MoveNext();
123 }
124
128 public void Reset()
129 {
130 this.items.Reset();
131 this.e = null;
132 }
133 }
134}
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:20
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11