Skip to content

Commit 864ea2a

Browse files
committed
more mod Sequence
1 parent 451a954 commit 864ea2a

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

src/ZLinq/Linq/Sequence.cs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,23 @@ public static ValueEnumerable<FromSequence<T>, T> Sequence<T>(T start, T endIncl
1313
where T : INumber<T>
1414
{
1515
if (start is null) Throws.Null(nameof(start));
16+
if (T.IsNaN(start)) Throws.ArgumentOutOfRange(nameof(start));
1617
if (endInclusive is null) Throws.Null(nameof(endInclusive));
18+
if (T.IsNaN(endInclusive)) Throws.ArgumentOutOfRange(nameof(endInclusive));
1719
if (step is null) Throws.Null(nameof(step));
20+
if (T.IsNaN(step)) Throws.ArgumentOutOfRange(nameof(step));
1821

19-
if (step > T.Zero)
22+
if (T.IsZero(step))
23+
{
24+
if (start != endInclusive)
25+
{
26+
Throws.ArgumentOutOfRange(nameof(step));
27+
}
28+
29+
// repeat one
30+
return new(new(start, endInclusive, step, isIncrement: true));
31+
}
32+
else if (T.IsPositive(step))
2033
{
2134
// Enumerable.Sequence has known primitive + 1 step has use Range(FillIncremental) optimization but currently we don't do it.
2235
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Sequence.cs
@@ -26,11 +39,10 @@ public static ValueEnumerable<FromSequence<T>, T> Sequence<T>(T start, T endIncl
2639
Throws.ArgumentOutOfRange(nameof(endInclusive));
2740
}
2841

29-
3042
// increment pattern
3143
return new(new(start, endInclusive, step, isIncrement: true));
3244
}
33-
else if (step < T.Zero)
45+
else
3446
{
3547
if (endInclusive > start)
3648
{
@@ -40,18 +52,6 @@ public static ValueEnumerable<FromSequence<T>, T> Sequence<T>(T start, T endIncl
4052
// decrement pattern
4153
return new(new(start, endInclusive, step, isIncrement: false));
4254
}
43-
else
44-
{
45-
// step == 0
46-
47-
if (start != endInclusive)
48-
{
49-
Throws.ArgumentOutOfRange(nameof(step));
50-
}
51-
52-
// repeat one?
53-
return new(new(start, endInclusive, step, isIncrement: true));
54-
}
5555
}
5656
}
5757
}
@@ -60,7 +60,7 @@ namespace ZLinq.Linq
6060
{
6161
[StructLayout(LayoutKind.Auto)]
6262
[EditorBrowsable(EditorBrowsableState.Never)]
63-
public struct FromSequence<T>(T start, T endInclusive, T step, bool isIncrement) : IValueEnumerator<T>
63+
public struct FromSequence<T>(T currentValue, T endInclusive, T step, bool isIncrement) : IValueEnumerator<T>
6464
where T : INumber<T>
6565
{
6666
bool calledGetNext;
@@ -87,46 +87,46 @@ public bool TryGetNext(out T current)
8787
if (!calledGetNext)
8888
{
8989
calledGetNext = true;
90-
current = start;
90+
current = currentValue;
9191
return true;
9292
}
9393

9494
if (isIncrement)
9595
{
96-
var next = start + step;
96+
var next = currentValue + step;
9797

98-
if (next >= endInclusive || next <= start)
98+
if (next >= endInclusive || next <= currentValue)
9999
{
100-
if (next == endInclusive && start != next)
100+
if (next == endInclusive && currentValue != next)
101101
{
102-
current = start = next;
102+
current = currentValue = next;
103103
return true;
104104
}
105105

106106
current = default!;
107107
return false;
108108
}
109109

110-
current = start = next;
110+
current = currentValue = next;
111111
return true;
112112
}
113113
else
114114
{
115-
var next = start + step;
115+
var next = currentValue + step;
116116

117-
if (next <= endInclusive || next >= start)
117+
if (next <= endInclusive || next >= currentValue)
118118
{
119-
if (next == endInclusive && start != next)
119+
if (next == endInclusive && currentValue != next)
120120
{
121-
current = start = next;
121+
current = currentValue = next;
122122
return true;
123123
}
124124

125125
current = default!;
126126
return false;
127127
}
128128

129-
current = start = next;
129+
current = currentValue = next;
130130
return true;
131131
}
132132
}

tests/ZLinq.Tests/Linq/SequenceTest.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ namespace ZLinq.Tests.Linq;
1414
public class SequenceTests
1515
{
1616
[Fact]
17-
public void NullArguments_Throws()
17+
public void InvalidArguments_Throws()
1818
{
1919
Assert.Throws<ArgumentNullException>("start", () => ValueEnumerable.Sequence((ReferenceAddable)null!, new(1), new(2)));
2020
Assert.Throws<ArgumentNullException>("endInclusive", () => ValueEnumerable.Sequence(new(1), (ReferenceAddable)null!, new(2)));
2121
Assert.Throws<ArgumentNullException>("step", () => ValueEnumerable.Sequence(new(1), new(2), (ReferenceAddable)null!));
22+
23+
Assert.Throws<ArgumentOutOfRangeException>("start", () => ValueEnumerable.Sequence(float.NaN, 1.0f, 1.0f));
24+
Assert.Throws<ArgumentOutOfRangeException>("endInclusive", () => ValueEnumerable.Sequence(1.0f, float.NaN, 1.0f));
25+
Assert.Throws<ArgumentOutOfRangeException>("step", () => ValueEnumerable.Sequence(1.0f, 1.0f, float.NaN));
2226
}
2327

2428
[Fact]
@@ -242,16 +246,16 @@ private sealed class ReferenceAddable(int value) : INumber<ReferenceAddable>
242246
public static bool IsImaginaryNumber(ReferenceAddable value) => throw new NotImplementedException();
243247
public static bool IsInfinity(ReferenceAddable value) => throw new NotImplementedException();
244248
public static bool IsInteger(ReferenceAddable value) => throw new NotImplementedException();
245-
public static bool IsNaN(ReferenceAddable value) => throw new NotImplementedException();
246-
public static bool IsNegative(ReferenceAddable value) => throw new NotImplementedException();
249+
public static bool IsNaN(ReferenceAddable value) => false;
250+
public static bool IsNegative(ReferenceAddable value) => false;
247251
public static bool IsNegativeInfinity(ReferenceAddable value) => throw new NotImplementedException();
248252
public static bool IsNormal(ReferenceAddable value) => throw new NotImplementedException();
249253
public static bool IsOddInteger(ReferenceAddable value) => throw new NotImplementedException();
250-
public static bool IsPositive(ReferenceAddable value) => throw new NotImplementedException();
254+
public static bool IsPositive(ReferenceAddable value) => false;
251255
public static bool IsPositiveInfinity(ReferenceAddable value) => throw new NotImplementedException();
252256
public static bool IsRealNumber(ReferenceAddable value) => throw new NotImplementedException();
253257
public static bool IsSubnormal(ReferenceAddable value) => throw new NotImplementedException();
254-
public static bool IsZero(ReferenceAddable value) => throw new NotImplementedException();
258+
public static bool IsZero(ReferenceAddable value) => false;
255259
public static ReferenceAddable MaxMagnitude(ReferenceAddable x, ReferenceAddable y) => throw new NotImplementedException();
256260
public static ReferenceAddable MaxMagnitudeNumber(ReferenceAddable x, ReferenceAddable y) => throw new NotImplementedException();
257261
public static ReferenceAddable MinMagnitude(ReferenceAddable x, ReferenceAddable y) => throw new NotImplementedException();

0 commit comments

Comments
 (0)