Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FiniteSet.cs
3using System.Text;
6
8{
12 public sealed class FiniteSet : Set
13 {
14 private readonly Dictionary<IElement, bool> elements;
15
20 public FiniteSet(IEnumerable<IElement> Elements)
21 {
22 this.elements = new Dictionary<IElement, bool>();
23 foreach (IElement E in Elements)
24 this.elements[E] = true;
25 }
26
31 public FiniteSet(IEnumerable Elements)
32 {
33 this.elements = new Dictionary<IElement, bool>();
34
35 foreach (object Item in Elements)
36 {
38 this.elements[E] = true;
39 }
40 }
41
47 public override bool Contains(IElement Element)
48 {
49 return this.elements.ContainsKey(Element);
50 }
51
57 public override bool Equals(object obj)
58 {
59 if (!(obj is FiniteSet S))
60 return false;
61
62 if (this.elements.Count != S.elements.Count)
63 return false;
64
65 foreach (IElement E in this.elements.Keys)
66 {
67 if (!S.elements.ContainsKey(E))
68 return false;
69 }
70
71 return true;
72 }
73
78 public override int GetHashCode()
79 {
80 int i = 0;
81
82 foreach (IElement E in this.elements.Keys)
83 i ^= i << 5 ^ E.GetHashCode();
84
85 return i;
86 }
87
91 public override ICollection<IElement> ChildElements => this.elements.Keys;
92
94 public override string ToString()
95 {
96 StringBuilder sb = null;
97
98 foreach (IElement Element in this.elements.Keys)
99 {
100 if (sb is null)
101 sb = new StringBuilder("{");
102 else
103 sb.Append(", ");
104
105 sb.Append(Element.ToString());
106 }
107
108 if (sb is null)
109 return "{}";
110 else
111 {
112 sb.Append('}');
113 return sb.ToString();
114 }
115 }
116
120 public override object AssociatedObjectValue
121 {
122 get
123 {
124 object[] Elements = new object[this.elements.Count];
125 int i = 0;
126
127 foreach (IElement E in this.elements.Keys)
128 Elements[i++] = E.AssociatedObjectValue;
129
130 return Elements;
131 }
132 }
133
137 public override int? Size
138 {
139 get { return this.elements.Count; }
140 }
141
142 }
143}
Base class for all types of elements.
Definition: Element.cs:14
Base class for all types of sets.
Definition: Set.cs:14
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
Represents a finite set.
Definition: FiniteSet.cs:13
FiniteSet(IEnumerable< IElement > Elements)
Represents a finite set.
Definition: FiniteSet.cs:20
FiniteSet(IEnumerable Elements)
Represents a finite set.
Definition: FiniteSet.cs:31
override object AssociatedObjectValue
Associated object value.
Definition: FiniteSet.cs:121
override bool Contains(IElement Element)
Checks if the set contains an element.
Definition: FiniteSet.cs:47
override? int Size
Size of set, if finite and known, otherwise null is returned.
Definition: FiniteSet.cs:138
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: FiniteSet.cs:91
override int GetHashCode()
Calculates a hash code of the element.
Definition: FiniteSet.cs:78
override bool Equals(object obj)
Compares the element to another.
Definition: FiniteSet.cs:57
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34