Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a756bc

Browse files
committed
Implement overloads for EnumerableExtensions API
1 parent 0d0d6c2 commit 0a756bc

File tree

3 files changed

+161
-4
lines changed

3 files changed

+161
-4
lines changed

‎src/AlgorithmForce.Searching/Deferred/EnumerableExtensions.cs‎

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,94 @@ namespace AlgorithmForce.Searching.Deferred
77
{
88
/// <summary>
99
/// 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>
1117
public static class EnumerableExtensions
1218
{
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)
1498
where T : IEquatable<T>
1599
{
16100
Validate(s, t);
@@ -36,6 +120,10 @@ internal static int IndexOf<T>(this IEnumerable<T> s, IReadOnlyList<T> t, int st
36120
}
37121
else
38122
{
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}}).
39127
if (table[i] > -1)
40128
{
41129
startIndex += i;
@@ -54,6 +142,10 @@ internal static int IndexOf<T>(this IEnumerable<T> s, IReadOnlyList<T> t, int st
54142
return -1;
55143
}
56144

145+
#endregion
146+
147+
#region Others
148+
57149
internal static int IndexOf<T>(this IEnumerable<T> s, T t, int startIndex, IEqualityComparer<T> comparer)
58150
where T : IEquatable<T>
59151
{
@@ -85,5 +177,7 @@ internal static void Validate<T>(IEnumerable<T> s, IReadOnlyList<T> t)
85177
if (s == null) throw new ArgumentNullException(nameof(s));
86178
if (t == null) throw new ArgumentNullException(nameof(t));
87179
}
180+
181+
#endregion
88182
}
89183
}

‎src/AlgorithmForce.Searching/Deferred/ReaderExtensions.cs‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,113 @@
44

55
namespace AlgorithmForce.Searching.Deferred
66
{
7+
/// <summary>
8+
/// Provides a set of extension methods that wrap <see cref="TextReader" /> or <see cref="BinaryReader" />
9+
/// as <see cref="IEnumerable{T}" /> instance.
10+
/// </summary>
711
public static class ReaderExtensions
812
{
13+
/// <summary>
14+
/// Wraps <see cref="TextReader" /> as <see cref="IEnumerable{T}" /> instance.
15+
/// </summary>
16+
/// <param name="reader">The instance of <see cref="TextReader" />.</param>
17+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="char"/> from the source.</returns>
918
public static IEnumerable<char> AsCharEnumerable(this TextReader reader)
1019
{
1120
return EnumerateChars(reader.Read);
1221
}
1322

23+
/// <summary>
24+
/// Wraps <see cref="TextReader" /> as <see cref="IEnumerable{T}" /> instance.
25+
/// </summary>
26+
/// <param name="reader">The instance of <see cref="TextReader" />.</param>
27+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="string" /> from the source.</returns>
28+
public static IEnumerable<string> AsStringEnumerable(this TextReader reader)
29+
{
30+
return Enumerate(reader.ReadLine);
31+
}
32+
33+
/// <summary>
34+
/// Wraps <see cref="BinaryReader" /> as <see cref="IEnumerable{T}" /> instance.
35+
/// </summary>
36+
/// <param name="reader">The instance of <see cref="BinaryReader" />.</param>
37+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="char"/> from the source.</returns>
1438
public static IEnumerable<char> AsCharEnumerable(this BinaryReader reader)
1539
{
1640
return Enumerate(reader.ReadChar);
1741
}
1842

43+
/// <summary>
44+
/// Wraps <see cref="BinaryReader" /> as <see cref="IEnumerable{T}" /> instance.
45+
/// </summary>
46+
/// <param name="reader">The instance of <see cref="BinaryReader" />.</param>
47+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="byte"/> from the source.</returns>
1948
public static IEnumerable<byte> AsByteEnumerable(this BinaryReader reader)
2049
{
2150
return Enumerate(reader.ReadByte);
2251
}
2352

53+
/// <summary>
54+
/// Wraps <see cref="BinaryReader" /> as <see cref="IEnumerable{T}" /> instance.
55+
/// </summary>
56+
/// <param name="reader">The instance of <see cref="BinaryReader" />.</param>
57+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="short"/> from the source.</returns>
2458
public static IEnumerable<short> AsInt16Enumerable(this BinaryReader reader)
2559
{
2660
return Enumerate(reader.ReadInt16);
2761
}
2862

63+
/// <summary>
64+
/// Wraps <see cref="BinaryReader" /> as <see cref="IEnumerable{T}" /> instance.
65+
/// </summary>
66+
/// <param name="reader">The instance of <see cref="BinaryReader" />.</param>
67+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="int"/> from the source.</returns>
2968
public static IEnumerable<int> AsInt32Enumerable(this BinaryReader reader)
3069
{
3170
return Enumerate(reader.ReadInt32);
3271
}
3372

73+
/// <summary>
74+
/// Wraps <see cref="BinaryReader" /> as <see cref="IEnumerable{T}" /> instance.
75+
/// </summary>
76+
/// <param name="reader">The instance of <see cref="BinaryReader" />.</param>
77+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="long"/> from the source.</returns>
3478
public static IEnumerable<long> AsInt64Enumerable(this BinaryReader reader)
3579
{
3680
return Enumerate(reader.ReadInt64);
3781
}
3882

83+
/// <summary>
84+
/// Wraps <see cref="BinaryReader" /> as <see cref="IEnumerable{T}" /> instance.
85+
/// </summary>
86+
/// <param name="reader">The instance of <see cref="BinaryReader" />.</param>
87+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="float"/> from the source.</returns>
3988
public static IEnumerable<float> AsSingleEnumerable(this BinaryReader reader)
4089
{
4190
return Enumerate(reader.ReadSingle);
4291
}
4392

93+
/// <summary>
94+
/// Wraps <see cref="BinaryReader" /> as <see cref="IEnumerable{T}" /> instance.
95+
/// </summary>
96+
/// <param name="reader">The instance of <see cref="BinaryReader" />.</param>
97+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="double"/> from the source.</returns>
4498
public static IEnumerable<double> AsDoubleEnumerable(this BinaryReader reader)
4599
{
46100
return Enumerate(reader.ReadDouble);
47101
}
48102

103+
/// <summary>
104+
/// Wraps <see cref="BinaryReader" /> as <see cref="IEnumerable{T}" /> instance.
105+
/// </summary>
106+
/// <param name="reader">The instance of <see cref="BinaryReader" />.</param>
107+
/// <returns>An <see cref="IEnumerable{T}" /> instance that enumerates <see cref="decimal"/> from the source.</returns>
49108
public static IEnumerable<decimal> AsDecimalEnumerable(this BinaryReader reader)
50109
{
51110
return Enumerate(reader.ReadDecimal);
52111
}
53112

54-
internal static IEnumerable<T> Enumerate<T>(Func<T> method)
113+
internal static IEnumerable<T> Enumerate<T>(Func<T> method)// We may need to customize this for different cases.
55114
{
56115
var c = default(T);
57116

‎src/AlgorithmForce.Searching/StringWrapper.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34

45
namespace AlgorithmForce.Searching
56
{
7+
/// <summary>
8+
/// If one day string implements IReadOnlyList{T} we may dig out this class
9+
/// </summary>
610
class StringWrapper : IReadOnlyList<char>
711
{
812
#region Fields

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /