@@ -13,10 +13,23 @@ public static ValueEnumerable<FromSequence<T>, T> Sequence<T>(T start, T endIncl
13
13
where T : INumber < T >
14
14
{
15
15
if ( start is null ) Throws . Null ( nameof ( start ) ) ;
16
+ if ( T . IsNaN ( start ) ) Throws . ArgumentOutOfRange ( nameof ( start ) ) ;
16
17
if ( endInclusive is null ) Throws . Null ( nameof ( endInclusive ) ) ;
18
+ if ( T . IsNaN ( endInclusive ) ) Throws . ArgumentOutOfRange ( nameof ( endInclusive ) ) ;
17
19
if ( step is null ) Throws . Null ( nameof ( step ) ) ;
20
+ if ( T . IsNaN ( step ) ) Throws . ArgumentOutOfRange ( nameof ( step ) ) ;
18
21
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 ) )
20
33
{
21
34
// Enumerable.Sequence has known primitive + 1 step has use Range(FillIncremental) optimization but currently we don't do it.
22
35
// 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
26
39
Throws . ArgumentOutOfRange ( nameof ( endInclusive ) ) ;
27
40
}
28
41
29
-
30
42
// increment pattern
31
43
return new ( new ( start , endInclusive , step , isIncrement : true ) ) ;
32
44
}
33
- else if ( step < T . Zero )
45
+ else
34
46
{
35
47
if ( endInclusive > start )
36
48
{
@@ -40,18 +52,6 @@ public static ValueEnumerable<FromSequence<T>, T> Sequence<T>(T start, T endIncl
40
52
// decrement pattern
41
53
return new ( new ( start , endInclusive , step , isIncrement : false ) ) ;
42
54
}
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
- }
55
55
}
56
56
}
57
57
}
@@ -60,7 +60,7 @@ namespace ZLinq.Linq
60
60
{
61
61
[ StructLayout ( LayoutKind . Auto ) ]
62
62
[ 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 >
64
64
where T : INumber < T >
65
65
{
66
66
bool calledGetNext ;
@@ -87,46 +87,46 @@ public bool TryGetNext(out T current)
87
87
if ( ! calledGetNext )
88
88
{
89
89
calledGetNext = true ;
90
- current = start ;
90
+ current = currentValue ;
91
91
return true ;
92
92
}
93
93
94
94
if ( isIncrement )
95
95
{
96
- var next = start + step ;
96
+ var next = currentValue + step ;
97
97
98
- if ( next >= endInclusive || next <= start )
98
+ if ( next >= endInclusive || next <= currentValue )
99
99
{
100
- if ( next == endInclusive && start != next )
100
+ if ( next == endInclusive && currentValue != next )
101
101
{
102
- current = start = next ;
102
+ current = currentValue = next ;
103
103
return true ;
104
104
}
105
105
106
106
current = default ! ;
107
107
return false ;
108
108
}
109
109
110
- current = start = next ;
110
+ current = currentValue = next ;
111
111
return true ;
112
112
}
113
113
else
114
114
{
115
- var next = start + step ;
115
+ var next = currentValue + step ;
116
116
117
- if ( next <= endInclusive || next >= start )
117
+ if ( next <= endInclusive || next >= currentValue )
118
118
{
119
- if ( next == endInclusive && start != next )
119
+ if ( next == endInclusive && currentValue != next )
120
120
{
121
- current = start = next ;
121
+ current = currentValue = next ;
122
122
return true ;
123
123
}
124
124
125
125
current = default ! ;
126
126
return false ;
127
127
}
128
128
129
- current = start = next ;
129
+ current = currentValue = next ;
130
130
return true ;
131
131
}
132
132
}
0 commit comments