Skip to content

Commit a2c32fc

Browse files
committed
small refactoring
1 parent 442b6a7 commit a2c32fc

File tree

6 files changed

+174
-11
lines changed

6 files changed

+174
-11
lines changed

src/DateRecurrenceR/Collections/UnionEnumerator.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,84 @@ internal struct UnionEnumerator : IEnumerator<DateOnly>
77
private readonly EWrapper[] _enumerators;
88
private DateOnly? _current = null;
99

10+
public UnionEnumerator(IEnumerator<DateOnly> e1, IEnumerator<DateOnly> e2)
11+
{
12+
var hash = new HashSet<EWrapper>();
13+
14+
if (e1 is UnionEnumerator ue1)
15+
{
16+
for (var j = 0; j < ue1._enumerators.Length; j++)
17+
{
18+
hash.Add(ue1._enumerators[j]);
19+
}
20+
}
21+
else
22+
{
23+
hash.Add(new EWrapper(e1));
24+
}
25+
26+
if (e2 is UnionEnumerator ue2)
27+
{
28+
for (var j = 0; j < ue2._enumerators.Length; j++)
29+
{
30+
hash.Add(ue2._enumerators[j]);
31+
}
32+
}
33+
else
34+
{
35+
hash.Add(new EWrapper(e2));
36+
}
37+
38+
_enumerators = hash.ToArray();
39+
40+
Current = default;
41+
}
42+
43+
public UnionEnumerator(IEnumerator<DateOnly> e1, IEnumerator<DateOnly> e2, IEnumerator<DateOnly> e3)
44+
{
45+
var hash = new HashSet<EWrapper>();
46+
47+
if (e1 is UnionEnumerator ue1)
48+
{
49+
for (var j = 0; j < ue1._enumerators.Length; j++)
50+
{
51+
hash.Add(ue1._enumerators[j]);
52+
}
53+
}
54+
else
55+
{
56+
hash.Add(new EWrapper(e1));
57+
}
58+
59+
if (e2 is UnionEnumerator ue2)
60+
{
61+
for (var j = 0; j < ue2._enumerators.Length; j++)
62+
{
63+
hash.Add(ue2._enumerators[j]);
64+
}
65+
}
66+
else
67+
{
68+
hash.Add(new EWrapper(e2));
69+
}
70+
71+
if (e3 is UnionEnumerator ue3)
72+
{
73+
for (var j = 0; j < ue3._enumerators.Length; j++)
74+
{
75+
hash.Add(ue3._enumerators[j]);
76+
}
77+
}
78+
else
79+
{
80+
hash.Add(new EWrapper(e3));
81+
}
82+
83+
_enumerators = hash.ToArray();
84+
85+
Current = default;
86+
}
87+
1088
public UnionEnumerator(IReadOnlyList<IEnumerator<DateOnly>> enumerators)
1189
{
1290
var hash = new HashSet<EWrapper>();
@@ -31,6 +109,30 @@ public UnionEnumerator(IReadOnlyList<IEnumerator<DateOnly>> enumerators)
31109
Current = default;
32110
}
33111

112+
public UnionEnumerator(IEnumerable<IEnumerator<DateOnly>> enumerators)
113+
{
114+
var hash = new HashSet<EWrapper>();
115+
116+
foreach (var enumerator in enumerators)
117+
{
118+
if (enumerator is UnionEnumerator ue)
119+
{
120+
for (var j = 0; j < ue._enumerators.Length; j++)
121+
{
122+
hash.Add(ue._enumerators[j]);
123+
}
124+
}
125+
else
126+
{
127+
hash.Add(new EWrapper(enumerator));
128+
}
129+
}
130+
131+
_enumerators = hash.ToArray();
132+
133+
Current = default;
134+
}
135+
34136
public bool MoveNext()
35137
{
36138
var nextIndex = -1;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
namespace DateRecurrenceR;
22

3+
/// <summary>Specifies the week of the month.</summary>
34
public enum NumberOfWeek
45
{
6+
/// <summary>Indicates first week of the month.</summary>
57
First = 0,
8+
/// <summary>Indicates second week of the month.</summary>
69
Second = 1,
10+
/// <summary>Indicates third week of the month.</summary>
711
Third = 2,
12+
/// <summary>Indicates fourth week of the month.</summary>
813
Fourth = 3,
14+
/// <summary>Indicates last week of the month.</summary>
915
Last = 4
1016
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using DateRecurrenceR.Collections;
2+
3+
namespace DateRecurrenceR;
4+
5+
public partial struct Recurrence
6+
{
7+
/// <summary>
8+
/// Produces the union enumerator of two enumerators.
9+
/// </summary>
10+
/// <param name="e1"><see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />. Expects that parameter will provide values in ascending order</param>
11+
/// <param name="e2"><see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />. Expects that parameter will provide values in ascending order</param>
12+
/// <returns>
13+
/// <see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />
14+
/// </returns>
15+
public static IEnumerator<DateOnly> Union(IEnumerator<DateOnly> e1, IEnumerator<DateOnly> e2)
16+
{
17+
return new UnionEnumerator(e1, e2);
18+
}
19+
20+
/// <summary>
21+
/// Produces the union enumerator of three enumerators.
22+
/// </summary>
23+
/// <param name="e1"><see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />. Expects that parameter will provide values in ascending order</param>
24+
/// <param name="e2"><see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />. Expects that parameter will provide values in ascending order</param>
25+
/// <param name="e3"><see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />. Expects that parameter will provide values in ascending order</param>
26+
/// <returns>
27+
/// <see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />
28+
/// </returns>
29+
public static IEnumerator<DateOnly> Union(IEnumerator<DateOnly> e1, IEnumerator<DateOnly> e2,
30+
IEnumerator<DateOnly> e3)
31+
{
32+
return new UnionEnumerator(e1, e2, e3);
33+
}
34+
35+
/// <summary>
36+
/// Produces the union enumerator of enumerators.
37+
/// </summary>
38+
/// <param name="enumerators">Array of <see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />. Expects that parameters will provide values in ascending order</param>
39+
/// <returns>
40+
/// <see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />
41+
/// </returns>
42+
public static IEnumerator<DateOnly> Union(params IEnumerator<DateOnly>[] enumerators)
43+
{
44+
return new UnionEnumerator(enumerators);
45+
}
46+
47+
/// <summary>
48+
/// Produces the union enumerator of enumerators.
49+
/// </summary>
50+
/// <param name="enumerators">Enumerable of <see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />. Expects that parameters will provide values in ascending order</param>
51+
/// <returns>
52+
/// <see cref="IEnumerator{T}" /> type of <see cref="DateOnly" />
53+
/// </returns>
54+
#if NET9_0_OR_GREATER
55+
public static IEnumerator<DateOnly> Union(params IEnumerable<IEnumerator<DateOnly>> enumerators)
56+
#else
57+
public static IEnumerator<DateOnly> Union(IEnumerable<IEnumerator<DateOnly>> enumerators)
58+
#endif
59+
{
60+
return new UnionEnumerator(enumerators);
61+
}
62+
}

src/DateRecurrenceR/Recurrence.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ public readonly partial struct Recurrence
99
{
1010
private static readonly EmptyEnumerator EmptyEnumerator = new();
1111

12-
public static IEnumerator<DateOnly> Union(IEnumerator<DateOnly> d1, IEnumerator<DateOnly> d2)
13-
{
14-
return new UnionEnumerator(new[] {d1, d2});
15-
}
16-
17-
public static IEnumerator<DateOnly> Union(params IEnumerator<DateOnly>[] enumerators)
18-
{
19-
return new UnionEnumerator(enumerators);
20-
}
21-
2212
private static DateOnly DateOnlyMin(DateOnly val1, DateOnly val2)
2313
{
2414
return val1 <= val2 ? val1 : val2;

src/DateRecurrenceR/WeekDays.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace DateRecurrenceR;
33
public sealed class WeekDays
44
{
55
#if NET8_0_OR_GREATER
6-
private readonly WeekDaysArray _ds = new WeekDaysArray();
6+
private readonly WeekDaysArray _ds;
77
#else
88
private readonly bool[] _ds = new bool[DaysInWeek];
99
#endif

src/DateRecurrenceR/WeekDaysArray.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#if NET8_0_OR_GREATER
22
namespace DateRecurrenceR;
33

4+
/// <summary>
5+
/// Represents array of week days. The first day of week is Sunday.
6+
/// </summary>
47
[System.Runtime.CompilerServices.InlineArray(7)]
58
public struct WeekDaysArray
69
{

0 commit comments

Comments
 (0)