-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
LastIndexOf.cs
138 lines (135 loc) · 5.79 KB
/
LastIndexOf.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Searches for the specified object and returns the zero-based index of the last occurrence within the entire
/// <see cref="IEnumerable{T}"/>.
/// </summary>
/// <typeparam name="TSource">
/// The type of elements of <paramref name="source"/>
/// </typeparam>
/// <param name="source">
/// The source sequence.
/// </param>
/// <param name="item">
/// The object to locate in the <see cref="IEnumerable{T}"/>. The value can be <see langword="null"/> for
/// reference types.
/// </param>
/// <returns>
/// The zero-based index of the last occurrence of <paramref name="item"/> within the entire <see
/// cref="IEnumerable{T}"/>, if found; otherwise, <c>-1</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// The <see cref="IEnumerable{T}"/> is searched forward starting at the first element and ending at the last
/// element, and the index of the last instance of <paramref name="item"/> is returned.
/// </para>
/// <para>
/// This method determines equality using the default equality comparer <see
/// cref="EqualityComparer{T}.Default"/> for <typeparamref name="TSource"/>, the type of values in the list.
/// </para>
/// <para>
/// This operator executes immediately.
/// </para>
/// </remarks>
public static int LastIndexOf<TSource>(this IEnumerable<TSource> source, TSource item)
{
return source.LastIndexOf(item, ^1, int.MaxValue);
}
/// <summary>
/// Searches for the specified object and returns the zero-based index of the last occurrence within the range
/// of elements in the <see cref="IEnumerable{T}"/> that extends backwards from the specified index to the first
/// element.
/// </summary>
/// <typeparam name="TSource">
/// The type of elements of <paramref name="source"/>
/// </typeparam>
/// <param name="source">
/// The source sequence.
/// </param>
/// <param name="item">
/// The object to locate in the <see cref="IEnumerable{T}"/>. The value can be <see langword="null"/> for
/// reference types.
/// </param>
/// <param name="index">
/// The <see cref="System.Index"/> of the ending element within the sequence.
/// </param>
/// <returns>
/// The zero-based index of the last occurrence of <paramref name="item"/> within the the range of elements in
/// the <see cref="IEnumerable{T}"/> that extends backwards from <paramref name="index"/> to the first element,
/// if found; otherwise, <c>-1</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// The <see cref="IEnumerable{T}"/> is searched forward starting at the first element and ending at <paramref
/// name="index"/>, and the index of the last instance of <paramref name="item"/> is returned.
/// </para>
/// <para>
/// This method determines equality using the default equality comparer <see
/// cref="EqualityComparer{T}.Default"/> for <typeparamref name="TSource"/>, the type of values in the list.
/// </para>
/// <para>
/// This operator executes immediately.
/// </para>
/// </remarks>
public static int LastIndexOf<TSource>(this IEnumerable<TSource> source, TSource item, Index index)
{
return source.LastIndexOf(item, index, int.MaxValue);
}
/// <summary>
/// Searches for the specified object and returns the zero-based index of the last occurrence within the range
/// of elements in the <see cref="IEnumerable{T}"/> that ends at the specified index to the last element and
/// contains the specified number of elements.
/// </summary>
/// <typeparam name="TSource">
/// The type of elements of <paramref name="source"/>
/// </typeparam>
/// <param name="source">
/// The source sequence.
/// </param>
/// <param name="item">
/// The object to locate in the <see cref="IEnumerable{T}"/>. The value can be <see langword="null"/> for
/// reference types.
/// </param>
/// <param name="index">
/// The <see cref="System.Index"/> of the ending element within the sequence.
/// </param>
/// <param name="count">
/// The number of elements in the section to search.
/// </param>
/// <returns>
/// The zero-based index of the last occurrence of <paramref name="item"/> within the the range of elements in
/// the <see cref="IEnumerable{T}"/> that that ends at <paramref name="index"/> and contains <paramref
/// name="count"/> number of elements, if found; otherwise, <c>-1</c>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="count"/> is less than <c>0</c>.
/// </exception>
/// <remarks>
/// <para>
/// The <see cref="IEnumerable{T}"/> is searched forward starting at the first element and ending at <paramref
/// name="index"/>, and the index of the last instance of <paramref name="item"/> no earlier in the sequence
/// than <paramref name="count"/> items before <paramref name="index"/> is returned.
/// </para>
/// <para>
/// This method determines equality using the default equality comparer <see
/// cref="EqualityComparer{T}.Default"/> for <typeparamref name="TSource"/>, the type of values in the list.
/// </para>
/// <para>
/// This operator executes immediately.
/// </para>
/// </remarks>
public static int LastIndexOf<TSource>(this IEnumerable<TSource> source, TSource item, Index index, int count)
{
return FindLastIndex(source, i => EqualityComparer<TSource>.Default.Equals(i, item), index, count);
}
}