-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathValueInterval`1.cs
276 lines (220 loc) · 12.1 KB
/
ValueInterval`1.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Gapotchenko.FX
// Copyright © Gapotchenko and Contributors
//
// File introduced by: Oleksiy Gapotchenko
// Year of introduction: 2022
using System.Diagnostics;
namespace Gapotchenko.FX.Math.Intervals;
/// <summary>
/// Represents a continuous value interval.
/// </summary>
/// <typeparam name="T">The type of interval value.</typeparam>
[DebuggerDisplay("{ToString(),nq}")]
public readonly struct ValueInterval<T> : IInterval<T>, IEquatable<ValueInterval<T>>, IEmptiable<ValueInterval<T>>
where T : IEquatable<T>?, IComparable<T>?
{
/// <summary>
/// Initializes a new <see cref="ValueInterval{T}"/> instance with the specified inclusive left and exclusive right bounds:
/// <c>[from,to)</c>.
/// </summary>
/// <param name="from">
/// The left bound of the interval.
/// Represents a value the interval starts with.
/// The corresponding limit point is included in the interval.
/// </param>
/// <param name="to">
/// The right bound of the interval.
/// Represents a value the interval ends with.
/// The corresponding limit point is not included in the interval.
/// </param>
public ValueInterval(T from, T to) :
this(IntervalBoundary.Inclusive(from), IntervalBoundary.Exclusive(to))
{
}
/// <summary>
/// Initializes a new <see cref="ValueInterval{T}"/> instance with the specified boundaries.
/// </summary>
/// <param name="from">
/// The left boundary of the interval.
/// Represents a boundary the interval starts with.
/// </param>
/// <param name="to">
/// The right boundary of the interval.
/// Represents a boundary the interval ends with.
/// </param>
/// <exception cref="ArgumentException">If one interval boundary is empty, another should be empty too.</exception>
public ValueInterval(IntervalBoundary<T> from, IntervalBoundary<T> to)
{
IntervalEngine.ValidateBoundaries(from, to);
From = from;
To = to;
}
#pragma warning disable CA1000 // Do not declare static members on generic types
/// <inheritdoc cref="ValueInterval.Empty{T}"/>
[Obsolete(
"Use ValueInterval.Empty<T>() method instead because this method is a part of Gapotchenko.FX infrastructure and should not be used directly."
#if NET5_0_OR_GREATER
, DiagnosticId = "GPFX0001"
#endif
)]
[EditorBrowsable(EditorBrowsableState.Never)]
public static ValueInterval<T> Empty { get; } = new(IntervalBoundary<T>.Empty, IntervalBoundary<T>.Empty);
#pragma warning restore CA1000 // Do not declare static members on generic types
internal static ValueInterval<T> Infinite { get; } = new(IntervalBoundary<T>.NegativeInfinity, IntervalBoundary<T>.PositiveInfinity);
/// <inheritdoc/>
public IntervalBoundary<T> From { get; init; }
/// <inheritdoc/>
public IntervalBoundary<T> To { get; init; }
IComparer<T> IIntervalOperations<T>.Comparer => Comparer<T>.Default;
/// <inheritdoc/>
public bool IsBounded => IntervalEngine.IsBounded<ValueInterval<T>, T>(this);
/// <inheritdoc/>
public bool IsHalfBounded => IntervalEngine.IsHalfBounded<ValueInterval<T>, T>(this);
/// <inheritdoc/>
public bool IsOpen => IntervalEngine.IsOpen<ValueInterval<T>, T>(this);
/// <inheritdoc/>
public bool IsClosed => IntervalEngine.IsClosed<ValueInterval<T>, T>(this);
/// <inheritdoc/>
public bool IsHalfOpen => IntervalEngine.IsHalfOpen<ValueInterval<T>, T>(this);
/// <inheritdoc/>
public bool IsEmpty => IntervalEngine.IsEmpty(this, Comparer<T>.Default);
/// <inheritdoc/>
public bool IsInfinite => IntervalEngine.IsInfinite<ValueInterval<T>, T>(this);
/// <inheritdoc/>
public bool IsDegenerate => IntervalEngine.IsDegenerate(this, Comparer<T>.Default);
/// <inheritdoc/>
public bool Contains(T value) => IntervalEngine.Contains(this, value, Comparer<T>.Default);
/// <inheritdoc/>
public int Zone(T value) => IntervalEngine.Zone(this, value, Comparer<T>.Default);
/// <inheritdoc cref="IIntervalOperations{T}.Interior"/>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public ValueInterval<T> Interior => IntervalEngine.Interior<ValueInterval<T>, T>(this, Construct);
/// <inheritdoc/>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
IInterval<T> IIntervalOperations<T>.Interior => Interior;
/// <inheritdoc cref="IIntervalOperations{T}.Enclosure"/>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public ValueInterval<T> Enclosure => IntervalEngine.Enclosure<ValueInterval<T>, T>(this, Construct);
/// <inheritdoc/>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
IInterval<T> IIntervalOperations<T>.Enclosure => Enclosure;
/// <inheritdoc cref="IIntervalOperations{T}.Intersect(IInterval{T})"/>
public ValueInterval<T> Intersect(IInterval<T> other) => Intersect<IIntervalOperations<T>>(other);
/// <inheritdoc cref="Intersect(IInterval{T})"/>
/// <typeparam name="TOther">Type of the other interval to produce the intersection with.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public ValueInterval<T> Intersect<TOther>(in TOther other) where TOther : IIntervalOperations<T> =>
IntervalEngine.Intersect(
this,
other ?? throw new ArgumentNullException(nameof(other)),
Comparer<T>.Default,
Construct);
IInterval<T> IIntervalOperations<T>.Intersect(IInterval<T> other) => Intersect<IIntervalOperations<T>>(other);
/// <inheritdoc cref="IIntervalOperations{T}.Union(IInterval{T})"/>
public ValueInterval<T> Union(IInterval<T> other) => Union<IIntervalOperations<T>>(other);
/// <inheritdoc cref="Union(IInterval{T})"/>
/// <typeparam name="TOther">Type of the other interval to produce the union with.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public ValueInterval<T> Union<TOther>(in TOther other) where TOther : IIntervalOperations<T> =>
IntervalEngine.Union(
this,
other ?? throw new ArgumentNullException(nameof(other)),
Comparer<T>.Default,
Construct);
IInterval<T> IIntervalOperations<T>.Union(IInterval<T> other) => Union<IIntervalOperations<T>>(other);
static ValueInterval<T> Construct(in IntervalBoundary<T> from, in IntervalBoundary<T> to) => new(from, to);
/// <inheritdoc/>
public bool Overlaps(IInterval<T> other) => Overlaps<IInterval<T>>(other);
/// <inheritdoc cref="Overlaps(IInterval{T})"/>
/// <typeparam name="TOther">Type of the interval to check for overlapping.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool Overlaps<TOther>(in TOther other) where TOther : IIntervalOperations<T> =>
IntervalEngine.Overlaps(this, other, Comparer<T>.Default);
/// <inheritdoc/>
public bool IsSubintervalOf(IInterval<T> other) => IsSubintervalOf<IInterval<T>>(other);
/// <inheritdoc cref="IsSubintervalOf(IInterval{T})"/>
/// <typeparam name="TOther">Type of the other interval.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsSubintervalOf<TOther>(in TOther other) where TOther : IIntervalOperations<T> =>
IntervalEngine.IsSubintervalOf(this, other, Comparer<T>.Default);
/// <inheritdoc/>
public bool IsSuperintervalOf(IInterval<T> other) => IsSuperintervalOf<IInterval<T>>(other);
/// <inheritdoc cref="IsSuperintervalOf(IInterval{T})"/>
/// <typeparam name="TOther">Type of the other interval.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsSuperintervalOf<TOther>(in TOther other) where TOther : IIntervalOperations<T> =>
IntervalEngine.IsSuperintervalOf(this, other, Comparer<T>.Default);
/// <inheritdoc/>
public bool IsProperSubintervalOf(IInterval<T> other) => IsProperSubintervalOf<IInterval<T>>(other);
/// <inheritdoc cref="IsProperSubintervalOf(IInterval{T})"/>
/// <typeparam name="TOther">Type of the other interval.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsProperSubintervalOf<TOther>(in TOther other) where TOther : IIntervalOperations<T> =>
IntervalEngine.IsProperSubintervalOf(this, other, Comparer<T>.Default);
/// <inheritdoc/>
public bool IsProperSuperintervalOf(IInterval<T> other) => IsProperSuperintervalOf<IInterval<T>>(other);
/// <inheritdoc cref="IsProperSuperintervalOf(IInterval{T})"/>
/// <typeparam name="TOther">Type of the other interval.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IsProperSuperintervalOf<TOther>(in TOther other) where TOther : IIntervalOperations<T> =>
IntervalEngine.IsProperSuperintervalOf(this, other, Comparer<T>.Default);
/// <inheritdoc/>
public bool IntervalEquals(IInterval<T> other) => IntervalEquals<IIntervalOperations<T>>(other);
/// <inheritdoc cref="IntervalEquals(IInterval{T})"/>
/// <typeparam name="TOther">Type of the interval to compare.</typeparam>
[EditorBrowsable(EditorBrowsableState.Never)]
public bool IntervalEquals<TOther>(in TOther other) where TOther : IIntervalOperations<T> =>
IntervalEngine.IntervalsEqual(this, other, Comparer<T>.Default);
/// <summary>
/// Determines whether the specified intervals are equal.
/// </summary>
/// <param name="x">The first interval.</param>
/// <param name="y">The second interval.</param>
/// <returns><see langword="true"/> if the specified intervals are equal; otherwise, <see langword="false"/>.</returns>
public static bool operator ==(in ValueInterval<T> x, IInterval<T>? y) => EqualityOperator(x, y);
/// <summary>
/// Determines whether the specified intervals are not equal.
/// </summary>
/// <param name="x">The first interval.</param>
/// <param name="y">The second interval.</param>
/// <returns><see langword="true"/> if the specified intervals are not equal; otherwise, <see langword="false"/>.</returns>
public static bool operator !=(in ValueInterval<T> x, IInterval<T>? y) => !EqualityOperator(x, y);
/// <inheritdoc cref="operator ==(in ValueInterval{T}, IInterval{T}?)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool operator ==(in ValueInterval<T> x, Interval<T>? y) => EqualityOperator(x, y);
/// <inheritdoc cref="operator !=(in ValueInterval{T}, IInterval{T}?)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool operator !=(in ValueInterval<T> x, Interval<T>? y) => !EqualityOperator(x, y);
static bool EqualityOperator<TOther>(in ValueInterval<T> x, TOther? y)
where TOther : IIntervalOperations<T> =>
y is not null &&
x.IntervalEquals(y);
/// <inheritdoc/>
public override bool Equals([NotNullWhen(true)] object? obj) =>
obj is ValueInterval<T> other &&
Equals(other);
/// <inheritdoc/>
public bool Equals(ValueInterval<T> other) => IntervalEngine.IntervalsEqual(this, other, Comparer<T>.Default);
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(From.GetHashCode(), To.GetHashCode());
/// <inheritdoc/>
public override string ToString() => IntervalEngine.ToString<ValueInterval<T>, T>(this);
/// <inheritdoc cref="ToString(string?, IFormatProvider?)"/>
public string ToString(string? format) => ToString(format, null);
/// <inheritdoc/>
public string ToString(string? format, IFormatProvider? formatProvider) =>
IntervalEngine.ToString<ValueInterval<T>, T>(this, format, formatProvider);
/// <summary>
/// Converts a specified <see cref="Interval{T}"/> instance to <see cref="ValueInterval{T}"/>.
/// </summary>
/// <param name="interval">The <see cref="Interval{T}"/> instance to convert.</param>
public static implicit operator ValueInterval<T>(Interval<T> interval) =>
interval is null ?
default :
new(interval.From, interval.To);
/// <summary>
/// Converts a specified <see cref="ValueInterval{T}"/> instance to <see cref="Interval{T}"/>.
/// </summary>
/// <param name="interval">The <see cref="ValueInterval{T}"/> instance to convert.</param>
public static implicit operator Interval<T>(ValueInterval<T> interval) => new(interval.From, interval.To);
}