-
Notifications
You must be signed in to change notification settings - Fork 1
/
SetExtension.cs
97 lines (91 loc) · 2.85 KB
/
SetExtension.cs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
namespace DotNetTransformer.Math.Set {
public static class SetExtension {
private sealed class InternalSet<T> : ISet<T>
where T : IEquatable<T>
{
private readonly Predicate<T> _contains;
public InternalSet(Predicate<T> contains) { _contains = contains; }
public bool Contains(T item) { return _contains(item); }
}
public static ISet<T> Inverse<T>(this ISet<T> _this)
where T : IEquatable<T>
{
return new InternalSet<T>(e => !_this.Contains(e));
}
public static ISet<T> Union<T>(this ISet<T> _this, ISet<T> other)
where T : IEquatable<T>
{
return ReferenceEquals(_this, other) ? _this :
new InternalSet<T>(e => _this.Contains(e) || other.Contains(e));
}
public static ISet<T> Intersect<T>(this ISet<T> _this, ISet<T> other)
where T : IEquatable<T>
{
return ReferenceEquals(_this, other) ? _this :
new InternalSet<T>(e => _this.Contains(e) && other.Contains(e));
}
public static ISet<T> Except<T>(this ISet<T> _this, ISet<T> other)
where T : IEquatable<T>
{
return ReferenceEquals(_this, other) ? (ISet<T>)FiniteSet<T>.Empty :
new InternalSet<T>(e => _this.Contains(e) && !other.Contains(e));
}
public static ISet<T> SymmetricExcept<T>(this ISet<T> _this, ISet<T> other)
where T : IEquatable<T>
{
return ReferenceEquals(_this, other) ? (ISet<T>)FiniteSet<T>.Empty :
new InternalSet<T>(e => _this.Contains(e) ^ other.Contains(e));
}
public static bool IsSubsetOf<T, S>(this S _this, ISuperSet<T, S> other)
where T : IEquatable<T>
where S : ISet<T>
{
return !ReferenceEquals(other, null) && other.IsSupersetOf(_this);
}
public static bool IsSupersetOf<T, S>(this S _this, ISubSet<T, S> other)
where T : IEquatable<T>
where S : ISet<T>
{
return !ReferenceEquals(other, null) && other.IsSubsetOf(_this);
}
public static bool EqualsSubsets<T, S>(this S _this, S other)
where T : IEquatable<T>
where S : ISubSet<T, S>
{
return ReferenceEquals(_this, other) || (
other.IsSupersetOf<T, S>(_this) &&
_this.IsSupersetOf<T, S>(other)
);
}
public static bool EqualsSupersets<T, S>(this S _this, S other)
where T : IEquatable<T>
where S : ISuperSet<T, S>
{
return ReferenceEquals(_this, other) || (
other.IsSubsetOf<T, S>(_this) &&
_this.IsSubsetOf<T, S>(other)
);
}
public static bool Equals<T, U, S>(this U _this, S other)
where T : IEquatable<T>
where U : ISubSet<T, S>, ISuperSet<T, S>
where S : ISet<T>
{
return ReferenceEquals(_this, other) || (
other.IsSupersetOf<T, S>(_this) &&
_this.IsSupersetOf(other)
);
}
public static bool Equals<T, U, S>(this S _this, U other)
where T : IEquatable<T>
where U : ISubSet<T, S>, ISuperSet<T, S>
where S : ISet<T>
{
return ReferenceEquals(_this, other) || (
_this.IsSupersetOf<T, S>(other) &&
other.IsSupersetOf(_this)
);
}
}
}