@@ -97,7 +97,7 @@ public static int IndexOf<T>(this IEnumerable<T> s, IReadOnlyList<T> t, int star
97
97
public static int IndexOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t , int startIndex , IEqualityComparer < T > comparer )
98
98
where T : IEquatable < T >
99
99
{
100
- Validate ( s , t ) ;
100
+ Validate ( s , t , startIndex ) ;
101
101
102
102
// Follow the behavior of string.IndexOf(string) method.
103
103
if ( t . Count == 0 ) return 0 ;
@@ -142,6 +142,102 @@ public static int IndexOf<T>(this IEnumerable<T> s, IReadOnlyList<T> t, int star
142
142
return - 1 ;
143
143
}
144
144
145
+
146
+ #region IReadOnlyList(T) (IndexesOf)
147
+
148
+ /// <summary>
149
+ /// Enumerates each zero-based index of all occurrences of the specified collection in this instance.
150
+ /// </summary>
151
+ /// <param name="s">The current collection.</param>
152
+ /// <param name="t">The collection to seek.</param>
153
+ /// <typeparam name="T">The type of element in the collection.</typeparam>
154
+ /// <returns>
155
+ /// The zero-based index positions of value if one or more <paramref name="t"/> are found.
156
+ /// If <paramref name="t"/> is empty, no indexes will be enumerated.
157
+ /// </returns>
158
+ /// <exception cref="ArgumentNullException"><paramref name="s"/> or <paramref name="t"/> is null.</exception>
159
+ public static IEnumerable < int > IndexesOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t )
160
+ where T : IEquatable < T >
161
+ {
162
+ return s . IndexesOf ( t , 0 , EqualityComparer < T > . Default ) ;
163
+ }
164
+
165
+ /// <summary>
166
+ /// Enumerates each zero-based index of all occurrences of the specified collection in this instance
167
+ /// and uses the specified <see cref="IEqualityComparer{T}"/>.
168
+ /// </summary>
169
+ /// <param name="s">The current collection.</param>
170
+ /// <param name="t">The collection to seek.</param>
171
+ /// <param name="comparer">The specified <see cref="IEqualityComparer{T}"/> instance.</param>
172
+ /// <typeparam name="T">The type of element in the collection.</typeparam>
173
+ /// <returns>
174
+ /// The zero-based index positions of value if one or more <paramref name="t"/> are found.
175
+ /// If <paramref name="t"/> is empty, no indexes will be enumerated.
176
+ /// </returns>
177
+ /// <exception cref="ArgumentNullException"><paramref name="s"/> or <paramref name="t"/> is null.</exception>
178
+ public static IEnumerable < int > IndexesOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t , IEqualityComparer < T > comparer )
179
+ where T : IEquatable < T >
180
+ {
181
+ return s . IndexesOf ( t , 0 , comparer ) ;
182
+ }
183
+
184
+ /// <summary>
185
+ /// Enumerates each zero-based index of all occurrences of the specified collection in this instance.
186
+ /// The search starts at a specified position.
187
+ /// </summary>
188
+ /// <param name="s">The current collection.</param>
189
+ /// <param name="t">The collection to seek.</param>
190
+ /// <param name="startIndex">The search starting position.</param>
191
+ /// <typeparam name="T">The type of element in the collection.</typeparam>
192
+ /// <returns>
193
+ /// The zero-based index positions of value if one or more <paramref name="t"/> are found.
194
+ /// If <paramref name="t"/> is empty, no indexes will be enumerated.
195
+ /// </returns>
196
+ /// <exception cref="ArgumentNullException"><paramref name="s"/> or <paramref name="t"/> is null.</exception>
197
+ /// <exception cref="ArgumentOutOfRangeException">
198
+ /// <paramref name="startIndex"/> is less than zero.
199
+ /// </exception>
200
+ public static IEnumerable < int > IndexesOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t , int startIndex )
201
+ where T : IEquatable < T >
202
+ {
203
+ return s . IndexesOf ( t , startIndex , EqualityComparer < T > . Default ) ;
204
+ }
205
+
206
+ /// <summary>
207
+ /// Enumerates each zero-based index of all occurrences of the specified collection in this instance
208
+ /// and uses the specified <see cref="IEqualityComparer{T}"/>.
209
+ /// The search starts at a specified position.
210
+ /// </summary>
211
+ /// <param name="s">The current collection.</param>
212
+ /// <param name="t">The collection to seek.</param>
213
+ /// <param name="startIndex">The search starting position.</param>
214
+ /// <param name="comparer">The specified <see cref="IEqualityComparer{T}"/> instance.</param>
215
+ /// <typeparam name="T">The type of element in the collection.</typeparam>
216
+ /// <returns>
217
+ /// The zero-based index positions of value if one or more <paramref name="t"/> are found.
218
+ /// If <paramref name="t"/> is empty, no indexes will be enumerated.
219
+ /// </returns>
220
+ /// <exception cref="ArgumentNullException"><paramref name="s"/> or <paramref name="t"/> is null.</exception>
221
+ /// <exception cref="ArgumentOutOfRangeException">
222
+ /// <paramref name="startIndex"/> is less than zero.
223
+ /// </exception>
224
+ public static IEnumerable < int > IndexesOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t , int startIndex , IEqualityComparer < T > comparer )
225
+ where T : IEquatable < T >
226
+ {
227
+ Validate ( s , t , startIndex ) ;
228
+
229
+ if ( comparer == null ) comparer = EqualityComparer < T > . Default ;
230
+ if ( t . Count == 1 )
231
+ return EnumerateIndexes ( s , t [ 0 ] , startIndex , comparer ) ;
232
+ else
233
+ return EnumerateIndexes ( s , t , startIndex , comparer ) ;
234
+ }
235
+
236
+
237
+ #endregion
238
+
239
+ #region Others
240
+
145
241
internal static IEnumerable < int > EnumerateIndexes < T > ( this IEnumerable < T > s , IReadOnlyList < T > t , int startIndex , IEqualityComparer < T > comparer )
146
242
where T : IEquatable < T >
147
243
{
@@ -193,8 +289,6 @@ internal static IEnumerable<int> EnumerateIndexes<T>(this IEnumerable<T> s, IRea
193
289
194
290
#endregion
195
291
196
- #region Others
197
-
198
292
internal static int IndexOf < T > ( this IEnumerable < T > s , T t , int startIndex , IEqualityComparer < T > comparer )
199
293
where T : IEquatable < T >
200
294
{
@@ -245,10 +339,13 @@ internal static IEnumerator<T> Skip<T>(IEnumerator<T> enumerator, int count)
245
339
return enumerator ;
246
340
}
247
341
248
- internal static void Validate < T > ( IEnumerable < T > s , IReadOnlyList < T > t )
342
+ internal static void Validate < T > ( IEnumerable < T > s , IReadOnlyList < T > t , int startIndex )
249
343
{
250
344
if ( s == null ) throw new ArgumentNullException ( nameof ( s ) ) ;
251
345
if ( t == null ) throw new ArgumentNullException ( nameof ( t ) ) ;
346
+
347
+ if ( startIndex < 0 )
348
+ throw new ArgumentOutOfRangeException ( nameof ( startIndex ) , "Value is less than zero." ) ;
252
349
}
253
350
254
351
#endregion
0 commit comments