@@ -30,7 +30,7 @@ static partial class MoreEnumerable
3030 /// Type of elements in <paramref name="source"/> sequence.</typeparam>
3131 /// <typeparam name="TResult">Type of result elements returned.</typeparam>
3232 /// <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>
3434 /// <param name="missingSelector">
3535 /// TODO Complete documentation
3636 /// </param>
@@ -42,9 +42,9 @@ static partial class MoreEnumerable
4242 /// </returns>
4343
4444 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 ,
4646 Func < int , TResult > missingSelector , Func < T , int , TResult > resultSelector ) =>
47- BindByIndex ( source , indicies , null , missingSelector , resultSelector ) ;
47+ BindByIndex ( source , indices , null , missingSelector , resultSelector ) ;
4848
4949 /// <summary>
5050 /// TODO Complete documentation
@@ -53,7 +53,7 @@ public static IEnumerable<TResult>
5353 /// Type of elements in <paramref name="source"/> sequence.</typeparam>
5454 /// <typeparam name="TResult">Type of result elements returned.</typeparam>
5555 /// <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>
5757 /// <param name="lookBackSize">Size of look-back buffer.</param>
5858 /// <param name="missingSelector">
5959 /// TODO Complete documentation
@@ -66,77 +66,79 @@ public static IEnumerable<TResult>
6666 /// </returns>
6767
6868 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 ) ;
7474
7575 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 )
8080 {
8181 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 ) ) ;
8383 if ( lookBackSize < 0 ) throw new ArgumentOutOfRangeException ( nameof ( lookBackSize ) ) ;
8484 if ( missingSelector == null ) throw new ArgumentNullException ( nameof ( missingSelector ) ) ;
8585 if ( resultSelector == null ) throw new ArgumentNullException ( nameof ( resultSelector ) ) ;
8686
8787 // TODO A version optimized for lists
8888
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+ } ) ;
9195
92- IEnumerable < TResult > _ ( Queue < T > queue )
96+ IEnumerable < TResult > _ ( Queue < T > ? queue )
9397 {
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 )
95103 {
104+ yield return missingSelector ( rie . Current ) ;
96105 if ( ! rie . MoveNext ( ) )
97106 yield break ;
107+ }
98108
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 ;
108111
109- foreach ( var item in source )
112+ foreach ( var item in source )
113+ {
114+ while ( si == ri )
110115 {
111- while ( si == ri )
116+ yield return resultSelector ( item , si ) ;
117+ do
112118 {
113- yield return resultSelector ( item , si ) ;
114- do
119+ if ( ! rie . MoveNext ( ) )
120+ yield break ;
121+ ri = rie . Current ;
122+ if ( ri < si )
115123 {
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 ) ;
126128 }
127- while ( ri < si ) ;
128129 }
129-
130- queue ? . Enqueue ( item ) ;
131- si ++ ;
130+ while ( ri < si ) ;
132131 }
133132
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 ) ;
140142 }
141143 }
142144 }
@@ -149,23 +151,13 @@ IEnumerable<TResult> _(Queue<T> queue)
149151 /// directly indexing into the queue to retrieve any one item.
150152 /// </summary>
151153
152- sealed class Queue < T > : IReadOnlyList < T >
154+ sealed class Queue < T > ( int maxCount = 0 , int capacity = 0 ) : IReadOnlyList < T >
153155 {
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 ;
159159
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 ;
169161 public int Count { get ; private set ; }
170162
171163 T IReadOnlyList < T > . this [ int index ] => this [ index ] ;
@@ -175,25 +167,30 @@ public T this[int index]
175167 get
176168 {
177169 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+
179176 return Cell ( index ) ;
180177 }
181178 }
182179
183- ref T Cell ( int index ) => ref _items [ ( _firstIndex + index ) % Capacity ] ;
180+ ref T Cell ( int index ) => ref this . items [ ( this . firstIndex + index ) % Capacity ] ;
184181
185182 public void Enqueue ( T item )
186183 {
187- if ( _maxCount > 0 && Count == _maxCount )
188- Dequeue ( ) ;
184+ if ( this . maxCount > 0 && Count == this . maxCount )
185+ _ = Dequeue ( ) ;
189186
190187 if ( Count == Capacity )
191188 {
192189 var array = new T [ Math . Max ( 4 , Capacity * 2 ) ] ;
193190 for ( var i = 0 ; i < Count ; i ++ )
194191 array [ i ] = this [ i ] ;
195- _firstIndex = 0 ;
196- _items = array ;
192+ this . firstIndex = 0 ;
193+ this . items = array ;
197194 }
198195
199196 Cell ( Count ++ ) = item;
@@ -204,7 +201,7 @@ public T Dequeue()
204201 if ( Count == 0 )
205202 throw new InvalidOperationException ( ) ;
206203 var result = this [ 0 ] ;
207- _firstIndex ++ ;
204+ this . firstIndex ++ ;
208205 -- Count ;
209206 return result ;
210207 }
0 commit comments