@@ -12,6 +12,106 @@ namespace Lachee.Utilities
12
12
///</summary>
13
13
public static class Linq
14
14
{
15
+ #region Find
16
+ /// <summary>Searches for the specified object and returns the zero-based index of the first occurrence within the entire enumeration</summary>
17
+ /// <param name="item">The item to search for</param>
18
+ /// <remarks>
19
+ /// The enumerable is searched forward starting at the first item.
20
+ /// <para>The <see cref="object.Equals(object)"/> is used to validate equality</para>
21
+ /// </remarks>
22
+ /// <returns>The index. If none is found then -1.</returns>
23
+ public static int IndexOf < T > ( this IEnumerable < T > arr , T item )
24
+ => IndexOf < T > ( arr , item , 0 , int . MaxValue ) ;
25
+ /// <summary>Searches for the specified object and returns the zero-based index of the first occurrence within the entire enumeration</summary>
26
+ /// <param name="item">The item to search for</param>
27
+ /// <param name="index">The zero-based starting index of the search. 0 (zero) is valid in an empty list.</param>
28
+ /// <remarks>
29
+ /// The enumerable is searched forward starting at the first item.
30
+ /// <para>The <see cref="object.Equals(object)"/> is used to validate equality</para>
31
+ /// </remarks>
32
+ /// <returns>The index. If none is found then -1.</returns>
33
+ public static int IndexOf < T > ( this IEnumerable < T > arr , T item , int index )
34
+ => IndexOf < T > ( arr , item , index , int . MaxValue ) ;
35
+ /// <summary>Searches for the specified object and returns the zero-based index of the first occurrence within the entire enumeration</summary>
36
+ /// <param name="item">The item to search for</param>
37
+ /// <param name="index">The zero-based starting index of the search. 0 (zero) is valid in an empty list.</param>
38
+ /// <param name="count">The number of elements in the section to search.</param>
39
+ /// <remarks>
40
+ /// The enumerable is searched forward starting at the first item.
41
+ /// <para>The <see cref="object.Equals(object)"/> is used to validate equality</para>
42
+ /// </remarks>
43
+ /// <returns>The index. If none is found then -1.</returns>
44
+ public static int IndexOf < T > ( this IEnumerable < T > arr , T item , int index , int count )
45
+ {
46
+ int cnt = 0 ;
47
+ foreach ( var a in arr . Skip ( index ) )
48
+ {
49
+ if ( ( a == null && item == null ) || a . Equals ( item ) )
50
+ return cnt + index ;
51
+
52
+ if ( ++ cnt >= count )
53
+ break ;
54
+ }
55
+ return - 1 ;
56
+
57
+ }
58
+
59
+ /// <summary>Searches untill the predicate is true and returns the zero-based index of the first occurrence within the entire enumeration</summary>
60
+ /// <param name="predicate">Method invoked for each item to check for equality</param>
61
+ /// <remarks>
62
+ /// The enumerable is searched forward starting at the first item.
63
+ /// </remarks>
64
+ /// <returns>The index. If none is found then -1.</returns>
65
+ public static int IndexOf < T > ( this IEnumerable < T > arr , System . Predicate < T > predicate )
66
+ => IndexOf ( arr , predicate , 0 , int . MaxValue , out var _ ) ;
67
+ /// <summary>Searches untill the predicate is true and returns the zero-based index of the first occurrence within the entire enumeration</summary>
68
+ /// <param name="predicate">Method invoked for each item to check for equality</param>
69
+ /// <param name="index">The zero-based starting index of the search. 0 (zero) is valid in an empty list.</param>
70
+ /// <remarks>
71
+ /// The enumerable is searched forward starting at the first item.
72
+ /// </remarks>
73
+ /// <returns>The index. If none is found then -1.</returns>
74
+ public static int IndexOf < T > ( this IEnumerable < T > arr , System . Predicate < T > predicate , int index )
75
+ => IndexOf ( arr , predicate , index , int . MaxValue , out var _ ) ;
76
+ /// <summary>Searches untill the predicate is true and returns the zero-based index of the first occurrence within the entire enumeration</summary>
77
+ /// <param name="predicate">Method invoked for each item to check for equality</param>
78
+ /// <param name="index">The zero-based starting index of the search. 0 (zero) is valid in an empty list.</param>
79
+ /// <param name="count">The number of elements in the section to search.</param>
80
+ /// <remarks>
81
+ /// The enumerable is searched forward starting at the first item.
82
+ /// </remarks>
83
+ /// <returns>The index. If none is found then -1.</returns>
84
+ public static int IndexOf < T > ( this IEnumerable < T > arr , System . Predicate < T > predicate , int index , int count )
85
+ => IndexOf ( arr , predicate , index , count , out var _ ) ;
86
+ /// <summary>Searches untill the predicate is true and returns the zero-based index of the first occurrence within the entire enumeration</summary>
87
+ /// <param name="predicate">Method invoked for each item to check for equality</param>
88
+ /// <param name="index">The zero-based starting index of the search. 0 (zero) is valid in an empty list.</param>
89
+ /// <param name="count">The number of elements in the section to search.</param>
90
+ /// <param name="item">The item that is at the given index</param>
91
+ /// <remarks>
92
+ /// The enumerable is searched forward starting at the first item.
93
+ /// </remarks>
94
+ /// <returns>The index. If none is found then -1.</returns>
95
+ public static int IndexOf < T > ( this IEnumerable < T > arr , System . Predicate < T > predicate , int index , int count , out T item )
96
+ {
97
+ item = default ;
98
+ int cnt = 0 ;
99
+ foreach ( var a in arr . Skip ( index ) )
100
+ {
101
+ if ( predicate . Invoke ( a ) )
102
+ {
103
+ item = a ;
104
+ return cnt + index ;
105
+ }
106
+
107
+ if ( ++ cnt >= count )
108
+ break ;
109
+ }
110
+ return - 1 ;
111
+ }
112
+ #endregion
113
+
114
+ #region Random
15
115
/// <summary>
16
116
/// Picks a random item from the enumerator by enumerating over a random amount.
17
117
/// Do not use this on fixed length arrays or lists, as it is less efficient than a direct lookup.
@@ -56,6 +156,36 @@ public static TSource Random<TSource>(this IEnumerable<TSource> source, int uppe
56
156
return enumerator . Current ;
57
157
}
58
158
159
+ ///<summary>Shuffles the list in place</summary>
160
+ ///<remarks>The list will be updated</remarks>
161
+ public static List < T > Shuffle < T > ( this List < T > src )
162
+ {
163
+ // Knuth shuffle algorithm :: courtesy of Wikipedia :)
164
+ for ( int t = 0 ; t < src . Count ; t ++ )
165
+ {
166
+ T tmp = src [ t ] ;
167
+ int r = UnityEngine . Random . Range ( t , src . Count ) ;
168
+ src [ t ] = src [ r ] ;
169
+ src [ r ] = tmp ;
170
+ }
171
+ return src ;
172
+ }
173
+ ///<summary>Shuffles the array in place</summary>
174
+ ///<remarks>The list will be updated</remarks>
175
+ public static T [ ] Shuffle < T > ( this T [ ] src )
176
+ {
177
+ // Knuth shuffle algorithm :: courtesy of Wikipedia :)
178
+ for ( int t = 0 ; t < src . Length ; t ++ )
179
+ {
180
+ T tmp = src [ t ] ;
181
+ int r = UnityEngine . Random . Range ( t , src . Length ) ;
182
+ src [ t ] = src [ r ] ;
183
+ src [ r ] = tmp ;
184
+ }
185
+ return src ;
186
+ }
187
+ #endregion
188
+
59
189
#region HashSet
60
190
61
191
/// <summary>
0 commit comments