-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathArrayExtensions.cs
273 lines (228 loc) · 8.14 KB
/
ArrayExtensions.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
namespace Utilities;
public static class ArrayExtensions
{
//https://learn.microsoft.com/en-us/dotnet/api/system.array.indexof
public static int IndexOf<T>(this T[] array, T item)
{
return Array.IndexOf(array, item);
}
public static int IndexOf<T>(this T[] array, T item, int startIndex)
{
return Array.IndexOf(array, item, startIndex);
}
public static int IndexOf<T>(this T[] array, T item, int startIndex, int count)
{
return Array.IndexOf(array, item, startIndex, count);
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.lastindexof
public static int LastIndexOf<T>(this T[] array, T item)
{
return Array.LastIndexOf(array, item);
}
public static int LastIndexOf<T>(this T[] array, T item, int startIndex)
{
return Array.LastIndexOf(array, item, startIndex);
}
public static int LastIndexOf<T>(this T[] array, T item, int startIndex, int count)
{
return Array.LastIndexOf(array, item, startIndex, count);
}
public static bool Contains<T>(this T[] array, T item)
{
return Array.IndexOf(array, item) >= 0;
}
public static bool Contains<T>(this T[] array, T item, int startIndex)
{
return Array.IndexOf(array, item, startIndex) >= 0;
}
public static bool Contains<T>(this T[] array, T item, int startIndex, int count)
{
return Array.IndexOf(array, item, startIndex, count) >= 0;
}
public static bool ContainsBinarySearch<T>(this T[] array, T item)
{
return Array.BinarySearch(array, item) >= 0;
}
public static bool ContainsBinarySearch<T>(this T[] array, T item, int index, int length)
{
return Array.BinarySearch(array, index, length, item) >= 0;
}
public static bool ContainsBinarySearch<T>(this T[] array, T item, IComparer<T> comparer)
{
return Array.BinarySearch(array, item, comparer) >= 0;
}
public static bool ContainsBinarySearch<T>(this T[] array, T item, int index, int length, IComparer<T> comparer)
{
return Array.BinarySearch(array, index, length, item, comparer) >= 0;
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.binarysearch
public static int BinarySearch<T>(this T[] array, T item)
{
return Array.BinarySearch(array, item);
}
public static int BinarySearch<T>(this T[] array, T item, int index, int length)
{
return Array.BinarySearch(array, index, length, item);
}
public static int BinarySearch<T>(this T[] array, T item, IComparer<T> comparer)
{
return Array.BinarySearch(array, item, comparer);
}
public static int BinarySearch<T>(this T[] array, T item, int index, int length, IComparer<T> comparer)
{
return Array.BinarySearch(array, index, length, item, comparer);
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.exists
public static bool Exists<T>(this T[] array, Predicate<T> match)
{
return Array.Exists(array, match);
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.fill
public static T[] Fill<T>(this T[] array, T value)
{
Array.Fill(array, value);
return array;
}
public static T[] Fill<T>(this T[] array, T value, int startIndex, int count)
{
Array.Fill(array, value, startIndex, count);
return array;
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.find
public static T? Find<T>(this T[] array, Predicate<T> match)
{
return Array.Find(array, match);
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.findlast
public static T? FindLast<T>(this T[] array, Predicate<T> match)
{
return Array.FindLast(array, match);
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.findall
public static T[] FindAll<T>(this T[] array, Predicate<T> match)
{
return Array.FindAll(array, match);
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.findindex
public static int FindIndex<T>(this T[] array, Predicate<T> match)
{
return Array.FindIndex(array, match);
}
public static int FindIndex<T>(this T[] array, Predicate<T> match, int startIndex)
{
return Array.FindIndex(array, startIndex, match);
}
public static int FindIndex<T>(this T[] array, Predicate<T> match, int startIndex, int count)
{
return Array.FindIndex(array, startIndex, count, match);
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.findlastindex
public static int FindLastIndex<T>(this T[] array, Predicate<T> match)
{
return Array.FindLastIndex(array, match);
}
public static int FindLastIndex<T>(this T[] array, Predicate<T> match, int startIndex)
{
return Array.FindLastIndex(array, startIndex, match);
}
public static int FindLastIndex<T>(this T[] array, Predicate<T> match, int startIndex, int count)
{
return Array.FindLastIndex(array, startIndex, count, match);
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.foreach
public static T[] ForEach<T>(this T[] array, Action<T> action)
{
Array.ForEach(array, action);
return array;
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.reverse
public static T[] Reverse<T>(this T[] array)
{
Array.Reverse(array);
return array;
}
public static T[] Reverse<T>(this T[] array, int index, int length)
{
Array.Reverse(array, index, length);
return array;
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.sort
public static T[] Sort<T>(this T[] array)
{
Array.Sort(array);
return array;
}
public static T[] Sort<T>(this T[] array, int index, int length)
{
Array.Sort(array, index, length);
return array;
}
public static T[] Sort<T>(this T[] array, IComparer<T> comparer)
{
Array.Sort(array, comparer);
return array;
}
public static T[] Sort<T>(this T[] array, int index, int length, IComparer<T> comparer)
{
Array.Sort(array, index, length, comparer);
return array;
}
//https://learn.microsoft.com/en-us/dotnet/api/system.array.trueforall
public static bool TrueForAll<T>(this T[] array, Predicate<T> match)
{
return Array.TrueForAll(array, match);
}
public static bool TryGetValue<T>(this T[] collection, T value, out T? actualValue)
{
var index = collection.IndexOf(value);
if (index >= 0)
{
actualValue = collection[index];
return true;
}
actualValue = default;
return false;
}
public static bool TryGetValueBinarySearch<T>(this T[] collection, T value, out T? actualValue)
{
var index = collection.BinarySearch(value);
if (index >= 0)
{
actualValue = collection[index];
return true;
}
actualValue = default;
return false;
}
////This technique is used in Dictionary structures
//public static bool TryGetValueByRef<T>(this LinkedList<T> collection, T value, out T? actualValue)
//{
// var node = collection.Find(value);
// if (node is not null)
// {
// ref var valueRef = ref node.ValueRef;
// if (!Unsafe.IsNullRef(ref valueRef)) //for ref non-readonly
// {
// actualValue = valueRef;
// return true;
// }
// }
// actualValue = default;
// return false;
//}
//public static bool TryGetValueByRef<T>(this ImmutableArray<T> collection, T value, out T? actualValue)
//{
// var index = collection.IndexOf(value); //.BinarySearch(value);
// if (index >= 0)
// {
// ref readonly var valueRef = ref collection.ItemRef(index); //for ref readonly
// if (!Unsafe.IsNullRef(ref Unsafe.AsRef(in valueRef)))
// {
// actualValue = valueRef;
// return true;
// }
// }
// actualValue = default;
// return false;
//}
}