Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UnionSet.cs
1using System;
2using System.Collections.Generic;
5
7{
11 public sealed class UnionSet : Set
12 {
13 private readonly ISet set1;
14 private readonly ISet set2;
15
21 public UnionSet(ISet Set1, ISet Set2)
22 {
23 this.set1 = Set1;
24 this.set2 = Set2;
25 }
26
32 public override bool Contains(IElement Element)
33 {
34 return this.set1.Contains(Element) || this.set2.Contains(Element);
35 }
36
42 public override bool Equals(object obj)
43 {
44 if (!(obj is UnionSet S))
45 return false;
46
47 return (this.set1.Equals(S.set1) && this.set2.Equals(S.set2));
48
49 // TODO: This is not mathematically correct. For the time being, it serves the purpose of allowing the object to be included in dictionaries, etc.
50 }
51
56 public override int GetHashCode()
57 {
58 return this.set1.GetHashCode() ^ this.set2.GetHashCode();
59 }
60
64 public override ICollection<IElement> ChildElements
65 {
66 get
67 {
68 if (!(this.elements is null))
69 return this.elements.Keys;
70
71 ICollection<IElement> E1 = this.set1.ChildElements;
72 if (E1 is null)
73 return null;
74
75 ICollection<IElement> E2 = this.set2.ChildElements;
76 if (E2 is null)
77 return null;
78
79 Dictionary<IElement, bool> Elements = new Dictionary<IElement, bool>();
80
81 foreach (IElement E in E1)
82 Elements[E] = true;
83
84 foreach (IElement E in E2)
85 Elements[E] = true;
86
87 this.elements = Elements;
88 return Elements.Keys;
89 }
90 }
91
92 private Dictionary<IElement, bool> elements = null;
93
95 public override string ToString()
96 {
97 return this.set1.ToString() + "∪" + this.set2.ToString();
98 }
99
103 public override object AssociatedObjectValue
104 {
105 get
106 {
107 ICollection<IElement> Elements = this.ChildElements;
108 if (Elements is null)
109 return this;
110
111 object[] Elements2 = new object[Elements.Count];
112 int i = 0;
113
114 foreach (IElement E in Elements)
115 Elements2[i++] = E.AssociatedObjectValue;
116
117 return Elements2;
118 }
119 }
120
124 public override int? Size
125 {
126 get
127 {
128 ICollection<IElement> ChildElements = this.ChildElements;
129 if (ChildElements is null)
130 return null;
131 else
132 return ChildElements.Count;
133 }
134 }
135 }
136}
Base class for all types of elements.
Definition: Element.cs:13
Base class for all types of sets.
Definition: Set.cs:14
Represents a union of two sets.
Definition: UnionSet.cs:12
override string ToString()
Definition: UnionSet.cs:95
UnionSet(ISet Set1, ISet Set2)
Represents a union of two sets.
Definition: UnionSet.cs:21
override int GetHashCode()
Calculates a hash code of the element.
Definition: UnionSet.cs:56
override bool Contains(IElement Element)
Checks if the set contains an element.
Definition: UnionSet.cs:32
override? int Size
Size of set, if finite and known, otherwise null is returned.
Definition: UnionSet.cs:125
override bool Equals(object obj)
Compares the element to another.
Definition: UnionSet.cs:42
override object AssociatedObjectValue
Associated object value.
Definition: UnionSet.cs:104
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: UnionSet.cs:65
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Basic interface for all types of sets.
Definition: ISet.cs:10