@@ -30,7 +30,7 @@ static partial class MoreEnumerable
30
30
/// Type of elements in <paramref name="source"/> sequence.</typeparam>
31
31
/// <typeparam name="TResult">Type of result elements returned.</typeparam>
32
32
/// <param name="source">The source sequence.</param>
33
- /// <param name="indicies ">The sequence of indicies .</param>
33
+ /// <param name="indices ">The sequence of indices .</param>
34
34
/// <param name="missingSelector">
35
35
/// TODO Complete documentation
36
36
/// </param>
@@ -42,9 +42,9 @@ static partial class MoreEnumerable
42
42
/// </returns>
43
43
44
44
public static IEnumerable < TResult >
45
- BindByIndex < T , TResult > ( this IEnumerable < T > source , IEnumerable < int > indicies ,
45
+ BindByIndex < T , TResult > ( this IEnumerable < T > source , IEnumerable < int > indices ,
46
46
Func < int , TResult > missingSelector , Func < T , int , TResult > resultSelector ) =>
47
- BindByIndex ( source , indicies , null , missingSelector , resultSelector ) ;
47
+ BindByIndex ( source , indices , null , missingSelector , resultSelector ) ;
48
48
49
49
/// <summary>
50
50
/// TODO Complete documentation
@@ -53,7 +53,7 @@ public static IEnumerable<TResult>
53
53
/// Type of elements in <paramref name="source"/> sequence.</typeparam>
54
54
/// <typeparam name="TResult">Type of result elements returned.</typeparam>
55
55
/// <param name="source">The source sequence.</param>
56
- /// <param name="indicies ">The sequence of indicies .</param>
56
+ /// <param name="indices ">The sequence of indices .</param>
57
57
/// <param name="lookBackSize">Size of look-back buffer.</param>
58
58
/// <param name="missingSelector">
59
59
/// TODO Complete documentation
@@ -66,77 +66,79 @@ public static IEnumerable<TResult>
66
66
/// </returns>
67
67
68
68
public static IEnumerable < TResult >
69
- BindByIndex < T , TResult > ( this IEnumerable < T > source , IEnumerable < int > indicies ,
70
- int lookBackSize ,
71
- Func < int , TResult > missingSelector ,
72
- Func < T , int , TResult > resultSelector ) =>
73
- BindByIndex ( source , indicies , ( int ? ) lookBackSize , missingSelector , resultSelector ) ;
69
+ BindByIndex < T , TResult > ( this IEnumerable < T > source , IEnumerable < int > indices ,
70
+ int lookBackSize ,
71
+ Func < int , TResult > missingSelector ,
72
+ Func < T , int , TResult > resultSelector ) =>
73
+ BindByIndex ( source , indices , ( int ? ) lookBackSize , missingSelector , resultSelector ) ;
74
74
75
75
static IEnumerable < TResult >
76
- BindByIndex < T , TResult > ( IEnumerable < T > source , IEnumerable < int > indicies ,
77
- int ? lookBackSize ,
78
- Func < int , TResult > missingSelector ,
79
- Func < T , int , TResult > resultSelector )
76
+ BindByIndex < T , TResult > ( IEnumerable < T > source , IEnumerable < int > indices ,
77
+ int ? lookBackSize ,
78
+ Func < int , TResult > missingSelector ,
79
+ Func < T , int , TResult > resultSelector )
80
80
{
81
81
if ( source == null ) throw new ArgumentNullException ( nameof ( source ) ) ;
82
- if ( indicies == null ) throw new ArgumentNullException ( nameof ( indicies ) ) ;
82
+ if ( indices == null ) throw new ArgumentNullException ( nameof ( indices ) ) ;
83
83
if ( lookBackSize < 0 ) throw new ArgumentOutOfRangeException ( nameof ( lookBackSize ) ) ;
84
84
if ( missingSelector == null ) throw new ArgumentNullException ( nameof ( missingSelector ) ) ;
85
85
if ( resultSelector == null ) throw new ArgumentNullException ( nameof ( resultSelector ) ) ;
86
86
87
87
// TODO A version optimized for lists
88
88
89
- return _ ( lookBackSize is int lbs ? lbs > 0 ? new Queue < T > ( lbs , lbs ) : null
90
- : new Queue < T > ( ) ) ;
89
+ return _ ( lookBackSize switch
90
+ {
91
+ { } lbs and > 0 => new Queue < T > ( lbs , lbs ) ,
92
+ 0 => null ,
93
+ _ => new Queue < T > ( )
94
+ } ) ;
91
95
92
- IEnumerable < TResult > _ ( Queue < T > queue )
96
+ IEnumerable < TResult > _ ( Queue < T > ? queue )
93
97
{
94
- using ( var rie = indicies . GetEnumerator ( ) )
98
+ using var rie = indices . GetEnumerator ( ) ;
99
+ if ( ! rie . MoveNext ( ) )
100
+ yield break ;
101
+
102
+ while ( rie . Current < 0 )
95
103
{
104
+ yield return missingSelector ( rie . Current ) ;
96
105
if ( ! rie . MoveNext ( ) )
97
106
yield break ;
107
+ }
98
108
99
- while ( rie . Current < 0 )
100
- {
101
- yield return missingSelector ( rie . Current ) ;
102
- if ( ! rie . MoveNext ( ) )
103
- yield break ;
104
- }
105
-
106
- var ri = rie . Current ;
107
- var si = 0 ;
109
+ var ri = rie . Current ;
110
+ var si = 0 ;
108
111
109
- foreach ( var item in source )
112
+ foreach ( var item in source )
113
+ {
114
+ while ( si == ri )
110
115
{
111
- while ( si == ri )
116
+ yield return resultSelector ( item , si ) ;
117
+ do
112
118
{
113
- yield return resultSelector ( item , si ) ;
114
- do
119
+ if ( ! rie . MoveNext ( ) )
120
+ yield break ;
121
+ ri = rie . Current ;
122
+ if ( ri < si )
115
123
{
116
- if ( ! rie . MoveNext ( ) )
117
- yield break ;
118
- ri = rie . Current ;
119
- if ( ri < si )
120
- {
121
- if ( si - queue ? . Count is int qi && ri >= qi )
122
- yield return resultSelector ( queue [ ri - qi ] , ri ) ;
123
- else
124
- yield return missingSelector ( ri ) ;
125
- }
124
+ if ( queue is { } q && si - q . Count is var qi && ri >= qi )
125
+ yield return resultSelector ( q [ ri - qi ] , ri ) ;
126
+ else
127
+ yield return missingSelector ( ri ) ;
126
128
}
127
- while ( ri < si ) ;
128
129
}
129
-
130
- queue ? . Enqueue ( item ) ;
131
- si ++ ;
130
+ while ( ri < si ) ;
132
131
}
133
132
134
- if ( ri != si )
135
- {
136
- yield return missingSelector ( ri ) ;
137
- while ( rie . MoveNext ( ) )
138
- yield return missingSelector ( rie . Current ) ;
139
- }
133
+ queue ? . Enqueue ( item ) ;
134
+ si ++ ;
135
+ }
136
+
137
+ if ( ri != si )
138
+ {
139
+ yield return missingSelector ( ri ) ;
140
+ while ( rie . MoveNext ( ) )
141
+ yield return missingSelector ( rie . Current ) ;
140
142
}
141
143
}
142
144
}
@@ -149,23 +151,13 @@ IEnumerable<TResult> _(Queue<T> queue)
149
151
/// directly indexing into the queue to retrieve any one item.
150
152
/// </summary>
151
153
152
- sealed class Queue < T > : IReadOnlyList < T >
154
+ sealed class Queue < T > ( int maxCount = 0 , int capacity = 0 ) : IReadOnlyList < T >
153
155
{
154
- T [ ] _items ;
155
- int _firstIndex ;
156
- readonly int _maxCount ;
157
-
158
- static readonly T [ ] ZeroItems = new T [ 0 ] ;
156
+ T [ ] items = capacity > 0 ? new T [ capacity ] : [ ] ;
157
+ int firstIndex ;
158
+ readonly int maxCount = maxCount ;
159
159
160
- public Queue ( int maxCount = 0 , int capacity = 0 )
161
- {
162
- _items = capacity > 0 ? new T [ capacity ] : ZeroItems ;
163
- _firstIndex = 0 ;
164
- _maxCount = maxCount ;
165
- Count = 0 ;
166
- }
167
-
168
- int Capacity => _items . Length ;
160
+ int Capacity => this . items . Length ;
169
161
public int Count { get ; private set ; }
170
162
171
163
T IReadOnlyList < T > . this [ int index ] => this [ index ] ;
@@ -175,25 +167,30 @@ public T this[int index]
175
167
get
176
168
{
177
169
if ( index < 0 || index >= Count )
178
- throw new IndexOutOfRangeException ( ) ;
170
+ {
171
+ #pragma warning disable CA2201 // Do not raise reserved exception types
172
+ throw new IndexOutOfRangeException ( ) ;
173
+ #pragma warning restore CA2201
174
+ }
175
+
179
176
return Cell ( index ) ;
180
177
}
181
178
}
182
179
183
- ref T Cell ( int index ) => ref _items [ ( _firstIndex + index ) % Capacity ] ;
180
+ ref T Cell ( int index ) => ref this . items [ ( this . firstIndex + index ) % Capacity ] ;
184
181
185
182
public void Enqueue ( T item )
186
183
{
187
- if ( _maxCount > 0 && Count == _maxCount )
188
- Dequeue ( ) ;
184
+ if ( this . maxCount > 0 && Count == this . maxCount )
185
+ _ = Dequeue ( ) ;
189
186
190
187
if ( Count == Capacity )
191
188
{
192
189
var array = new T [ Math . Max ( 4 , Capacity * 2 ) ] ;
193
190
for ( var i = 0 ; i < Count ; i ++ )
194
191
array [ i ] = this [ i ] ;
195
- _firstIndex = 0 ;
196
- _items = array ;
192
+ this . firstIndex = 0 ;
193
+ this . items = array ;
197
194
}
198
195
199
196
Cell ( Count ++ ) = item;
@@ -204,7 +201,7 @@ public T Dequeue()
204
201
if ( Count == 0 )
205
202
throw new InvalidOperationException ( ) ;
206
203
var result = this [ 0 ] ;
207
- _firstIndex ++ ;
204
+ this . firstIndex ++ ;
208
205
-- Count ;
209
206
return result ;
210
207
}
0 commit comments