-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiDateOnlyRange.cs
323 lines (285 loc) · 11.8 KB
/
MultiDateOnlyRange.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// "Copyright (c) Cyrille NDOUMBE.
// Licenced under GNU General Public Licence, version 3.0"
#if NET6_0_OR_GREATER
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
#if NET8_0_OR_GREATER
using System.Numerics;
#endif
using System.Text;
namespace Candoumbe.Types.Calendar;
/// <summary>
/// A type that optimize the storage of several <see cref="DateOnlyRange"/>s.
/// </summary>
public class MultiDateOnlyRange : IEquatable<MultiDateOnlyRange>, IEnumerable<DateOnlyRange>
#if NET7_0_OR_GREATER
, IAdditionOperators<MultiDateOnlyRange, MultiDateOnlyRange, MultiDateOnlyRange>
, IAdditionOperators<MultiDateOnlyRange, DateOnlyRange, MultiDateOnlyRange>
, IUnaryNegationOperators<MultiDateOnlyRange, MultiDateOnlyRange>
, IEqualityOperators<MultiDateOnlyRange, MultiDateOnlyRange, bool>
#endif
{
private readonly HashSet<DateOnlyRange> _ranges;
/// <summary>
/// A <see cref="MultiDateOnlyRange"/> that contains no <see cref="DateOnlyRange"/>.
/// </summary>
public static MultiDateOnlyRange Empty => [];
/// <summary>
/// A <see cref="MultiDateOnlyRange"/> that overlaps any other <see cref="MultiDateOnlyRange"/>.
/// </summary>
public static MultiDateOnlyRange Infinite => new(DateOnlyRange.Infinite);
/// <summary>
/// Builds a new <see cref="MultiDateOnlyRange"/> instance
/// </summary>
/// <param name="ranges"></param>
public MultiDateOnlyRange(params DateOnlyRange[] ranges)
{
_ranges = new HashSet<DateOnlyRange>(ranges.Length);
foreach (DateOnlyRange range in ranges.OrderBy(x => x.Start))
{
Add(range);
}
}
/// <inheritdoc />
public IEnumerator<DateOnlyRange> GetEnumerator() => _ranges.OrderBy(x => x.Start).GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Adds <paramref name="range"/>.
/// </summary>
/// <remarks>
/// The algorithm will first try to find if any other <see cref="DateOnlyRange"/> overlaps or abuts with <paramref name="range"/> and if so,
/// will swap that element with the result of <c>range.Union(element)</c>
/// </remarks>
/// <param name="range">a range to add to the current instance</param>
/// <exception cref="ArgumentNullException">if <paramref name="range"/> is <see langword="null"/>.</exception>
public void Add(DateOnlyRange range)
{
ArgumentNullException.ThrowIfNull(range);
if (range.IsEmpty())
{
return;
}
if (range.IsInfinite())
{
_ranges.Clear();
_ranges.Add(range);
}
else
{
DateOnlyRange[] previous = _ranges.Where(item => item.IsContiguousWith(range) || item.Overlaps(range))
.OrderBy(x => x.Start)
.ToArray();
if (previous.Length != 0)
{
previous.ForEach(item => _ranges.Remove(item));
DateOnlyRange union = previous.Aggregate(range, (a, b) => a.Merge(b));
_ranges.Add(union);
}
else
{
_ranges.Add(range);
}
}
}
///<inheritdoc/>
public static MultiDateOnlyRange operator +(MultiDateOnlyRange left, DateOnlyRange range)
{
left.Add(range);
return left;
}
/// <summary>
/// Computes a <see cref="MultiDateOnlyRange"/> that represents the union of the current instance with <paramref name="other"/>.
/// </summary>
/// <param name="other">The other instance to add</param>
/// <exception cref="ArgumentNullException">if <paramref name="other"/> is <see langword="null"/></exception>
/// <returns>a <see cref="MultiDateOnlyRange"/> that represents the union of the current instance with <paramref name="other"/>.</returns>
public MultiDateOnlyRange Merge(MultiDateOnlyRange other)
{
ArgumentNullException.ThrowIfNull(other);
return new MultiDateOnlyRange([.. _ranges.Union(other._ranges)]);
}
#if !NET7_0_OR_GREATER
/// <summary>
/// Performs a "union" operation between <paramref name="left"/> and <paramref name="right"/> elements.
/// </summary>
/// <param name="left">The left element of the operator</param>
/// <param name="right">The right element of the operator</param>
/// <returns>a <see cref="MultiDateOnlyRange"/> that represents <paramref name="left"/> and <paramref name="right"/> values.</returns>
#else
///<inheritdoc/>
#endif
public static MultiDateOnlyRange operator +(MultiDateOnlyRange left, MultiDateOnlyRange right)
{
ArgumentNullException.ThrowIfNull(left);
ArgumentNullException.ThrowIfNull(right);
return left.Merge(right);
}
/// <summary>
/// Checks if the current instance contains one or more <see cref="DateOnlyRange"/>s which, once combined, overlap the specified <paramref name="range"/>.
/// </summary>
/// <param name="range">The range to test</param>
/// <returns><see langword="true"/> if the current instance contains <see cref="DateOnlyRange"/>s which, once combined, overlap <paramref name="range"/> and <see langword="false"/> otherwise.</returns>
public bool Overlaps(DateOnlyRange range)
{
return _ranges.Count != 0
&& _ranges.AsParallel()
.Any(item => item == range || (item.Overlaps(range) && item.Start <= range.Start && range.End <= item.End));
}
/// <summary>
/// Checks if the current instance contains one or more <see cref="TimeOnlyRange"/>s which, once combined, cover the specified <paramref name="other"/>.
/// </summary>
/// <param name="other">The range to test</param>
/// <returns><see langword="true"/> if the current instance contains <see cref="TimeOnlyRange"/>s which, once combined, cover <paramref name="other"/> and <see langword="false"/> otherwise.</returns>
public bool Overlaps(MultiDateOnlyRange other) => other is not null
&& _ranges.AsParallel().All(other.Overlaps);
///<inheritdoc/>
public override string ToString()
{
string representation = "{infinite}";
if (IsEmpty())
{
representation = "{empty}";
}
else if (!IsInfinite())
{
StringBuilder sb = new();
foreach (DateOnlyRange item in _ranges)
{
if (sb.Length > 0)
{
sb.Append(',');
}
sb.Append(item);
}
representation = sb.Insert(0, "{").Append('}').ToString();
}
return representation;
}
/// <summary>
/// Computes the complement of the current instance.
/// </summary>
public MultiDateOnlyRange Complement()
{
MultiDateOnlyRange complement;
if (IsInfinite())
{
complement = Empty;
}
else if (IsEmpty())
{
complement = Infinite;
}
else
{
complement = new();
switch (_ranges)
{
case { Count: 1 }:
{
DateOnlyRange current = _ranges.ElementAt(0);
complement.Add(DateOnlyRange.UpTo(current.Start));
complement.Add(DateOnlyRange.DownTo(current.End));
}
break;
default:
{
DateOnlyRange[] ranges = _ranges.OrderBy(range => range.Start)
.ToArray();
for (int i = 0; i < ranges.Length; i++)
{
DateOnlyRange current = ranges[i];
switch (i)
{
case 0:
complement.Add(DateOnlyRange.UpTo(current.Start));
break;
case int _ when i <= ranges.Length - 2:
{
DateOnlyRange previous = ranges[i- 1];
DateOnlyRange next = ranges[i + 1];
complement.Add(new DateOnlyRange(previous.End, current.Start));
complement.Add(new DateOnlyRange(current.End, next.Start));
}
break;
default:
{
DateOnlyRange previous = ranges[i - 1];
complement.Add(new DateOnlyRange(previous.End, current.Start));
complement.Add(DateOnlyRange.DownTo(current.End));
}
break;
}
}
}
break;
}
}
return complement;
}
///<inheritdoc/>
public static MultiDateOnlyRange operator -(MultiDateOnlyRange range) => range.Complement();
/// <summary>
/// Checks if the current instance is empty.
/// </summary>
/// <returns><see langword="true"/> when the current instance cannot cover any other </returns>
public bool IsEmpty() => _ranges.Count == 0 || _ranges.All(range => range.IsEmpty());
/// <summary>
/// Checks if the current instance is "infinite" which, if <see langword="true" />, means that it overlaps any other <see cref="DateOnlyRange"/>.
/// </summary>
/// <returns></returns>
public bool IsInfinite() => _ranges.AtLeastOnce() && _ranges.All(range => range.IsInfinite());
///<inheritdoc/>
public bool Equals(MultiDateOnlyRange other)
{
bool equals = false;
if (other is not null)
{
equals = ReferenceEquals(this, other) || (IsEmpty() && other.IsEmpty()) || (Overlaps(other) && other.Overlaps(this));
}
return equals;
}
///<inheritdoc/>
public override bool Equals(object obj) => Equals(obj as MultiDateOnlyRange);
///<inheritdoc/>
public override int GetHashCode()
{
HashCode hashCode = new();
foreach (DateOnlyRange range in _ranges)
{
hashCode.Add(range);
}
return hashCode.ToHashCode();
}
#if !NET8_0_OR_GREATER
///<summary>
/// Compares two values to determine equality
/// </summary>
/// <param name="left">The value to compare to <paramref name="right"/></param>
/// <param name="right">The value to compare to <paramref name="left"/></param>
/// <returns><see langword="true"/> if <paramref name="left"/> is equal to <paramref name="right"/>; otherwise, <see langword="false"/></returns>
#else
/// <inheritdoc />
#endif
public static bool operator ==(MultiDateOnlyRange left, MultiDateOnlyRange right)
=> ( left, right ) switch
{
(null, null) => true,
(null, not null) => false,
_ => left.Equals(right),
};
#if !NET8_0_OR_GREATER
///<summary>
/// Compares two values to determine inequality
/// </summary>
/// <param name="left">The value to compare to <paramref name="right"/></param>
/// <param name="right">The value to compare to <paramref name="left"/></param>
/// <returns><see langword="true"/> if <paramref name="left"/> is not equal to <paramref name="right"/>; otherwise, <see langword="false"/></returns>
#else
/// <inheritdoc />
#endif
public static bool operator !=(MultiDateOnlyRange left, MultiDateOnlyRange right) => !(left == right);
}
#endif