Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FiniteSet.cs
1using System;
2using System.Collections.Generic;
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
32 public override bool Contains(IElement Element)
33 {
34 return this.elements.ContainsKey(Element);
35 }
36
42 public override bool Equals(object obj)
43 {
44 if (!(obj is FiniteSet S))
45 return false;
46
47 if (this.elements.Count != S.elements.Count)
48 return false;
49
50 foreach (IElement E in this.elements.Keys)
51 {
52 if (!S.elements.ContainsKey(E))
53 return false;
54 }
55
56 return true;
57 }
58
63 public override int GetHashCode()
64 {
65 int i = 0;
66
67 foreach (IElement E in this.elements.Keys)
68 i ^= i << 5 ^ E.GetHashCode();
69
70 return i;
71 }
72
76 public override ICollection<IElement> ChildElements
77 {
78 get
79 {
80 return this.elements.Keys;
81 }
82 }
83
85 public override string ToString()
86 {
87 StringBuilder sb = null;
88
89 foreach (IElement Element in this.elements.Keys)
90 {
91 if (sb is null)
92 sb = new StringBuilder("{");
93 else
94 sb.Append(", ");
95
96 sb.Append(Element.ToString());
97 }
98
99 if (sb is null)
100 return "{}";
101 else
102 {
103 sb.Append('}');
104 return sb.ToString();
105 }
106 }
107
111 public override object AssociatedObjectValue
112 {
113 get
114 {
115 object[] Elements = new object[this.elements.Count];
116 int i = 0;
117
118 foreach (IElement E in this.elements.Keys)
119 Elements[i++] = E.AssociatedObjectValue;
120
121 return Elements;
122 }
123 }
124
128 public override int? Size
129 {
130 get { return this.elements.Count; }
131 }
132
133 }
134}
Base class for all types of elements.
Definition: Element.cs:13
Base class for all types of sets.
Definition: Set.cs:14
Represents a finite set.
Definition: FiniteSet.cs:13
FiniteSet(IEnumerable< IElement > Elements)
Represents a finite set.
Definition: FiniteSet.cs:20
override object AssociatedObjectValue
Associated object value.
Definition: FiniteSet.cs:112
override bool Contains(IElement Element)
Checks if the set contains an element.
Definition: FiniteSet.cs:32
override? int Size
Size of set, if finite and known, otherwise null is returned.
Definition: FiniteSet.cs:129
override ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: FiniteSet.cs:77
override int GetHashCode()
Calculates a hash code of the element.
Definition: FiniteSet.cs:63
override bool Equals(object obj)
Compares the element to another.
Definition: FiniteSet.cs:42
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33