@@ -7,10 +7,94 @@ namespace AlgorithmForce.Searching.Deferred
7
7
{
8
8
/// <summary>
9
9
/// Provides a set of extensions for searching specified collection in an <see cref="IEnumerable{T}" /> instance.
10
- /// </summary>
10
+ /// </summary>
11
+ /// <remarks>
12
+ /// The APIs defined in the class are designed for instance that is produced in deferred execution (for example, yield return).
13
+ /// Although technically the APIs can be applied to all types that implement <see cref="IEnumerable{T}" />,
14
+ /// for those instances that are not produced in deferred execution,
15
+ /// <see cref="Extensions"/> is still recommended for best performance.
16
+ /// </remarks>
11
17
public static class EnumerableExtensions
12
18
{
13
- internal static int IndexOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t , int startIndex , IEqualityComparer < T > comparer )
19
+ #region IReadOnlyList(T) (IndexOf)
20
+
21
+ /// <summary>
22
+ /// Reports the zero-based index of the first occurrence of the specified collection in this instance.
23
+ /// </summary>
24
+ /// <param name="s">The current collection.</param>
25
+ /// <param name="t">The collection to seek.</param>
26
+ /// <typeparam name="T">The type of element in the collection.</typeparam>
27
+ /// <returns>
28
+ /// The zero-based index position of value if <paramref name="t"/> is found, or -1 if it is not.
29
+ /// If <paramref name="t"/> is empty, the return value is 0.
30
+ /// </returns>
31
+ /// <exception cref="ArgumentNullException"><paramref name="s"/> or <paramref name="t"/> is null.</exception>
32
+ public static int IndexOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t )
33
+ where T : IEquatable < T >
34
+ {
35
+ return s . IndexOf ( t , 0 , EqualityComparer < T > . Default ) ;
36
+ }
37
+
38
+ /// <summary>
39
+ /// Reports the zero-based index of the first occurrence of the specified collection in this instance
40
+ /// and uses the specified <see cref="IEqualityComparer{T}"/>.
41
+ /// </summary>
42
+ /// <param name="s">The current collection.</param>
43
+ /// <param name="t">The collection to seek.</param>
44
+ /// <param name="comparer">The specified <see cref="IEqualityComparer{T}"/> instance.</param>
45
+ /// <typeparam name="T">The type of element in the collection.</typeparam>
46
+ /// <returns>
47
+ /// The zero-based index position of value if <paramref name="t"/> is found, or -1 if it is not.
48
+ /// If <paramref name="t"/> is empty, the return value is 0.
49
+ /// </returns>
50
+ /// <exception cref="ArgumentNullException"><paramref name="s"/> or <paramref name="t"/> is null.</exception>
51
+ public static int IndexOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t , IEqualityComparer < T > comparer )
52
+ where T : IEquatable < T >
53
+ {
54
+ return s . IndexOf ( t , 0 , comparer ) ;
55
+ }
56
+
57
+ /// <summary>
58
+ /// Reports the zero-based index of the first occurrence of the specified collection in this instance.
59
+ /// The search starts at a specified position.
60
+ /// </summary>
61
+ /// <param name="s">The current collection.</param>
62
+ /// <param name="t">The collection to seek.</param>
63
+ /// <param name="startIndex">The search starting position.</param>
64
+ /// <typeparam name="T">The type of element in the collection.</typeparam>
65
+ /// <returns>
66
+ /// The zero-based index position of value if <paramref name="t"/> is found, or -1 if it is not.
67
+ /// If <paramref name="t"/> is empty, the return value is 0.
68
+ /// </returns>
69
+ /// <exception cref="ArgumentNullException"><paramref name="s"/> or <paramref name="t"/> is null.</exception>
70
+ /// <exception cref="ArgumentOutOfRangeException">
71
+ /// <paramref name="startIndex"/> is less than zero.
72
+ /// </exception>
73
+ public static int IndexOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t , int startIndex )
74
+ where T : IEquatable < T >
75
+ {
76
+ return s . IndexOf ( t , startIndex , EqualityComparer < T > . Default ) ;
77
+ }
78
+
79
+ /// <summary>
80
+ /// Reports the zero-based index of the first occurrence of the specified collection in this instance
81
+ /// and uses the specified <see cref="IEqualityComparer{T}"/>.
82
+ /// The search starts at a specified position.
83
+ /// </summary>
84
+ /// <param name="s">The current collection.</param>
85
+ /// <param name="t">The collection to seek.</param>
86
+ /// <param name="startIndex">The search starting position.</param>
87
+ /// <param name="comparer">The specified <see cref="IEqualityComparer{T}"/> instance.</param>
88
+ /// <typeparam name="T">The type of element in the collection.</typeparam>
89
+ /// <returns>
90
+ /// The zero-based index position of value if <paramref name="t"/> is found, or -1 if it is not.
91
+ /// If <paramref name="t"/> is empty, the return value is 0.
92
+ /// </returns>
93
+ /// <exception cref="ArgumentNullException"><paramref name="s"/> or <paramref name="t"/> is null.</exception>
94
+ /// <exception cref="ArgumentOutOfRangeException">
95
+ /// <paramref name="startIndex"/> is less than zero.
96
+ /// </exception>
97
+ public static int IndexOf < T > ( this IEnumerable < T > s , IReadOnlyList < T > t , int startIndex , IEqualityComparer < T > comparer )
14
98
where T : IEquatable < T >
15
99
{
16
100
Validate ( s , t ) ;
@@ -36,6 +120,10 @@ internal static int IndexOf<T>(this IEnumerable<T> s, IReadOnlyList<T> t, int st
36
120
}
37
121
else
38
122
{
123
+ // We will extract this as a method GetOffset() in future version
124
+ // for upcoming APIs:
125
+ // 1. IndexOfAny(IReadOnlyList{T}, IEnumerable{IReadOnlyList{T}})
126
+ // 2. IndexesOfAll(IReadOnlyList{T}, IEnumerable{IReadOnlyList{T}}).
39
127
if ( table [ i ] > - 1 )
40
128
{
41
129
startIndex += i ;
@@ -54,6 +142,10 @@ internal static int IndexOf<T>(this IEnumerable<T> s, IReadOnlyList<T> t, int st
54
142
return - 1 ;
55
143
}
56
144
145
+ #endregion
146
+
147
+ #region Others
148
+
57
149
internal static int IndexOf < T > ( this IEnumerable < T > s , T t , int startIndex , IEqualityComparer < T > comparer )
58
150
where T : IEquatable < T >
59
151
{
@@ -85,5 +177,7 @@ internal static void Validate<T>(IEnumerable<T> s, IReadOnlyList<T> t)
85
177
if ( s == null ) throw new ArgumentNullException ( nameof ( s ) ) ;
86
178
if ( t == null ) throw new ArgumentNullException ( nameof ( t ) ) ;
87
179
}
180
+
181
+ #endregion
88
182
}
89
183
}
0 commit comments