-
Notifications
You must be signed in to change notification settings - Fork 1
/
FiniteSetExtension.cs
89 lines (82 loc) · 2.7 KB
/
FiniteSetExtension.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
using System;
using System.Collections.Generic;
namespace DotNetTransformer.Math.Set {
public static class FiniteSetExtension
{
private abstract class BaseAdapter<T, E> : FiniteSet<T>
where T : IEquatable<T>
where E : IEnumerable<T>
{
protected readonly E _collection;
protected internal BaseAdapter(E collection) {
_collection = collection;
}
public override abstract bool Contains(T item);
public override sealed IEnumerator<T> GetEnumerator() {
return _collection.GetEnumerator();
}
public override sealed bool Equals(IFiniteSet<T> other) {
return IsMatch<IFiniteSet<T>>(other, base.Equals);
}
public override sealed int GetHashCode() {
return _collection.GetHashCode();
}
public override sealed bool IsSubsetOf(ISet<T> other) {
return IsMatch<ISet<T>>(other, base.IsSubsetOf);
}
public override sealed bool IsSubsetOf(IFiniteSet<T> other) {
return IsMatch<IFiniteSet<T>>(other, base.IsSubsetOf);
}
public override sealed bool IsSupersetOf(IFiniteSet<T> other) {
return IsMatch<IFiniteSet<T>>(other, base.IsSupersetOf);
}
private bool IsMatch<S>(S other, Predicate<S> match)
where S : ISet<T>
{
BaseAdapter<T, E> o = other as BaseAdapter<T, E>;
return ReferenceEquals(_collection, other)
|| !ReferenceEquals(o, null) && ReferenceEquals(_collection, o._collection)
|| match(other);
}
}
private sealed class CollectionAdapter<T> : BaseAdapter<T, ICollection<T>>
where T : IEquatable<T>
{
internal CollectionAdapter(ICollection<T> collection) : base(collection) { }
public override sealed long Count { get { return _collection.Count; } }
public override sealed bool Contains(T item) {
return _collection.Contains(item);
}
}
private sealed class EnumerableAdapter<T> : BaseAdapter<T, IEnumerable<T>>
where T : IEquatable<T>
{
private readonly long _count;
private readonly Predicate<T> _contains;
internal EnumerableAdapter(IEnumerable<T> collection,
long count, Predicate<T> contains
) : base(collection) {
_count = count;
_contains = contains;
}
public override sealed long Count { get { return _count; } }
public override sealed bool Contains(T item) {
return _contains(item);
}
}
internal static IFiniteSet<T> ToFiniteSet<T>(this ICollection<T> collection)
where T : IEquatable<T>
{
return ReferenceEquals(collection, null) ?
null : new CollectionAdapter<T>(collection);
}
internal static IFiniteSet<T> ToFiniteSet<T>(this IEnumerable<T> collection,
long count, Predicate<T> contains
)
where T : IEquatable<T>
{
return ReferenceEquals(collection, null) ?
null : new EnumerableAdapter<T>(collection, count, contains);
}
}
}