forked from leofun01/Transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiniteSet.cs
More file actions
67 lines (65 loc) · 1.85 KB
/
FiniteSet.cs
File metadata and controls
67 lines (65 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections;
using System.Collections.Generic;
using DotNetTransformer.Extensions;
namespace DotNetTransformer.Math.Set {
public abstract partial class FiniteSet<T> : IFiniteSet<T>
, ISubSet<T, ISet<T>>
where T : IEquatable<T>
{
public abstract long Count { get; }
public virtual bool Contains(T item) {
return this.Exist<T>(e => e.Equals(item));
}
public abstract IEnumerator<T> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public virtual bool Equals(IFiniteSet<T> other) {
return ReferenceEquals(this, other) || (
!ReferenceEquals(other, null)
&& Count == other.Count
&& !this.Exist<T>(e => !other.Contains(e))
&& !other.Exist<T>(e => !Contains(e))
);
}
public override sealed bool Equals(object obj) {
return Equals(obj as IFiniteSet<T>);
}
public override int GetHashCode() {
int hash = (int)Count;
foreach(T item in this)
hash ^= item.GetHashCode();
return hash;
}
public virtual bool IsSubsetOf(ISet<T> other) {
return ReferenceEquals(this, other) || (
!ReferenceEquals(other, null)
&& !this.Exist<T>(e => !other.Contains(e))
);
}
public virtual bool IsSubsetOf(IFiniteSet<T> other) {
return ReferenceEquals(this, other) || (
!ReferenceEquals(other, null)
&& Count <= other.Count
&& !this.Exist<T>(e => !other.Contains(e))
);
}
public virtual bool IsSupersetOf(IFiniteSet<T> other) {
return ReferenceEquals(this, other) || (
!ReferenceEquals(other, null)
&& Count >= other.Count
&& !other.Exist<T>(e => !Contains(e))
);
}
public static bool operator ==(FiniteSet<T> l, IFiniteSet<T> r) {
return ReferenceEquals(l, r) || (
!ReferenceEquals(l, null) &&
l.Equals(r)
);
}
public static bool operator !=(FiniteSet<T> l, IFiniteSet<T> r) {
return !(l == r);
}
}
}