Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ElementOrder.cs
1using System;
2using System.Collections.Generic;
7
8namespace Waher.Script.Order
9{
13 public class ElementOrder : IComparer<IElement>
14 {
15 private readonly ScriptNode node;
16
22 {
23 this.node = Node;
24 }
25
32 public int Compare(IElement x, IElement y)
33 {
34 return Compare(x, y, this.node);
35 }
36
44 public static int Compare(IElement x, IElement y, ScriptNode Node)
45 {
46 if (x.AssociatedSet is IOrderedSet OrderedSet1)
47 return OrderedSet1.Compare(x, y);
48 else if (y.AssociatedSet is IOrderedSet OrderedSet2)
49 return OrderedSet2.Compare(x, y);
50 else if (x is IVector v1 && y is IVector v2)
51 {
52 IElement e1, e2;
53 int c1 = v1.Dimension;
54 int c2 = v2.Dimension;
55 int c = Math.Min(c1, c2);
56 int i, j;
57
58 for (i = 0; i < c; i++)
59 {
60 e1 = v1.GetElement(i);
61 e2 = v2.GetElement(i);
62 j = Compare(e1, e2, Node);
63 if (j != 0)
64 return j;
65 }
66
67 return c1 - c2;
68 }
69 else
70 throw new ScriptRuntimeException("Elements not ordered", Node);
71 }
72
73 }
74}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Orders elements based on their values.
Definition: ElementOrder.cs:14
static int Compare(IElement x, IElement y, ScriptNode Node)
Compares two elements using the order operator defined by their associated sets.
Definition: ElementOrder.cs:44
ElementOrder(ScriptNode Node)
Orders elements based on their values.
Definition: ElementOrder.cs:21
int Compare(IElement x, IElement y)
Compares two elements.
Definition: ElementOrder.cs:32
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11