-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReadRange{T}.cs
199 lines (162 loc) · 6.71 KB
/
ReadRange{T}.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace System
{
[Serializable]
public readonly struct ReadRange<T> : IRange<T>, IEquatableReadOnlyStruct<ReadRange<T>>, ISerializable
where T : struct
{
public T Start { get; }
public T End { get; }
public bool IsFromEnd { get; }
private readonly IRangeEnumerator<T> enumerator;
public ReadRange(T start, T end)
{
this.Start = start;
this.End = end;
this.IsFromEnd = false;
this.enumerator = null;
}
public ReadRange(T start, T end, IRangeEnumerator<T> enumerator)
{
this.Start = start;
this.End = end;
this.IsFromEnd = false;
this.enumerator = enumerator;
}
public ReadRange(T start, T end, bool fromEnd)
{
this.Start = start;
this.End = end;
this.IsFromEnd = fromEnd;
this.enumerator = null;
}
public ReadRange(T start, T end, bool fromEnd, IRangeEnumerator<T> enumerator)
{
this.Start = start;
this.End = end;
this.IsFromEnd = fromEnd;
this.enumerator = enumerator;
}
private ReadRange(SerializationInfo info, StreamingContext context)
{
this.Start = info.GetValueOrDefault<T>(nameof(this.Start));
this.End = info.GetValueOrDefault<T>(nameof(this.End));
this.IsFromEnd = info.GetBooleanOrDefault(nameof(this.IsFromEnd));
this.enumerator = null;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.Start), this.Start);
info.AddValue(nameof(this.End), this.End);
info.AddValue(nameof(this.IsFromEnd), this.IsFromEnd);
}
public void Deconstruct(out T start, out T end)
{
start = this.Start;
end = this.End;
}
public void Deconstruct(out T start, out T end, out bool fromEnd)
{
start = this.Start;
end = this.End;
fromEnd = this.IsFromEnd;
}
public ReadRange<T> With(T? Start = null, T? End = null, bool? IsFromEnd = null, IRangeEnumerator<T> Enumerator = null)
=> new ReadRange<T>(
Start ?? this.Start,
End ?? this.End,
IsFromEnd ?? this.IsFromEnd,
Enumerator ?? this.enumerator
);
public ReadRange<T> FromStart()
=> new ReadRange<T>(this.Start, this.End, false, this.enumerator);
public ReadRange<T> FromEnd()
=> new ReadRange<T>(this.Start, this.End, true, this.enumerator);
IRange<T> IRange<T>.FromStart()
=> FromStart();
IRange<T> IRange<T>.FromEnd()
=> FromEnd();
public override bool Equals(object obj)
=> obj is ReadRange<T> other &&
this.Start.Equals(other.Start) &&
this.End.Equals(other.End) &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(in ReadRange<T> other)
=> this.Start.Equals(other.Start) &&
this.End.Equals(other.End) &&
this.IsFromEnd == other.IsFromEnd;
public bool Equals(ReadRange<T> other)
=> this.Start.Equals(other.Start) &&
this.End.Equals(other.End) &&
this.IsFromEnd == other.IsFromEnd;
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.Start, this.End, this.IsFromEnd);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = -1418356749;
hashCode = hashCode * -1521134295 + this.Start.GetHashCode();
hashCode = hashCode * -1521134295 + this.End.GetHashCode();
hashCode = hashCode * -1521134295 + this.IsFromEnd.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override string ToString()
=> $"{{ {nameof(this.Start)}={this.Start}, {nameof(this.End)}={this.End}, {nameof(this.IsFromEnd)}={this.IsFromEnd} }}";
public IEnumerator<T> GetEnumerator()
=> (this.enumerator ?? new Enumerator()).Enumerate(this.Start, this.End, this.IsFromEnd);
public IEnumerator<T> Range()
=> GetEnumerator();
public ReadRange<T> Normalize(IComparer<T> comparer)
=> Normal(this.Start, this.End, this.enumerator, comparer);
/// <summary>
/// Create a normal range from (a, b) where <see cref="Start"/> is lesser than <see cref="End"/>.
/// </summary>
public static ReadRange<T> Normal(T a, T b, IComparer<T> comparer)
{
if (comparer == null)
throw new ArgumentNullException(nameof(comparer));
return comparer.Compare(a, b) > 0 ? new ReadRange<T>(b, a) : new ReadRange<T>(a, b);
}
public static ReadRange<T> Normal(T a, T b, IRangeEnumerator<T> enumerator, IComparer<T> comparer)
{
if (comparer == null)
throw new ArgumentNullException(nameof(comparer));
return comparer.Compare(a, b) > 0 ? new ReadRange<T>(b, a, enumerator) : new ReadRange<T>(a, b, enumerator);
}
public static ReadRange<T> FromStart(in T start, in T end)
=> new ReadRange<T>(start, end, false);
public static ReadRange<T> FromEnd(in T start, in T end)
=> new ReadRange<T>(start, end, true);
public static implicit operator ReadRange<T>(in (T start, T end) value)
=> new ReadRange<T>(value.start, value.end);
public static implicit operator ReadRange<T>(in (T start, T end, bool fromEnd) value)
=> new ReadRange<T>(value.start, value.end, value.fromEnd);
public static bool operator ==(in ReadRange<T> lhs, in ReadRange<T> rhs)
=> lhs.Equals(in rhs);
public static bool operator !=(in ReadRange<T> lhs, in ReadRange<T> rhs)
=> !lhs.Equals(in rhs);
private struct Enumerator : IRangeEnumerator<T>
{
public IEnumerator<T> Enumerate(T start, T end, bool fromEnd)
{
if (start.Equals(end))
{
yield return start;
}
else if (fromEnd)
{
yield return end;
yield return start;
}
else
{
yield return start;
yield return end;
}
}
}
}
}