-
Notifications
You must be signed in to change notification settings - Fork 5
/
ArrayEx.cs
267 lines (204 loc) · 6.54 KB
/
ArrayEx.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
using System.Collections;
using System.Collections.ObjectModel;
namespace System
{
public static class ArrayEx
{
public static T[] Empty<T> ()
{
return EmptyArray<T>.Value;
}
internal static class EmptyArray<T>
{
public static readonly T[] Value = new T[0];
}
public static bool TrueForAll<T> (T[] array, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (match == null)
throw new ArgumentNullException ("match");
return array.All (t => match (t));
}
public static void ForEach<T> (T[] array, Action<T> action)
{
if (array == null)
throw new ArgumentNullException ("array");
if (action == null)
throw new ArgumentNullException ("action");
foreach (T t in array)
action (t);
}
public static TOutput[] ConvertAll<TInput, TOutput> (TInput[] array, Converter<TInput, TOutput> converter)
{
if (array == null)
throw new ArgumentNullException ("array");
if (converter == null)
throw new ArgumentNullException ("converter");
var output = new TOutput[array.Length];
for (int i = 0; i < array.Length; i++)
output[i] = converter (array[i]);
return output;
}
public static int FindLastIndex<T> (T[] array, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (match == null)
throw new ArgumentNullException ("match");
return GetLastIndex (array, 0, array.Length, match);
}
public static int FindLastIndex<T> (T[] array, int startIndex, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ();
if (startIndex < 0 || (uint)startIndex > (uint)array.Length)
throw new ArgumentOutOfRangeException ("startIndex");
if (match == null)
throw new ArgumentNullException ("match");
return GetLastIndex (array, 0, startIndex + 1, match);
}
public static int FindLastIndex<T> (T[] array, int startIndex, int count, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (match == null)
throw new ArgumentNullException ("match");
if (startIndex < 0 || (uint)startIndex > (uint)array.Length)
throw new ArgumentOutOfRangeException ("startIndex");
if (count < 0)
throw new ArgumentOutOfRangeException ("count");
if (startIndex - count + 1 < 0)
throw new ArgumentOutOfRangeException ("count must refer to a location within the array");
return GetLastIndex (array, startIndex - count + 1, count, match);
}
internal static int GetLastIndex<T> (T[] array, int startIndex, int count, Predicate<T> match)
{
// unlike FindLastIndex, takes regular params for search range
for (int i = startIndex + count; i != startIndex; )
if (match (array[--i]))
return i;
return -1;
}
public static int FindIndex<T> (T[] array, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (match == null)
throw new ArgumentNullException ("match");
return GetIndex (array, 0, array.Length, match);
}
public static int FindIndex<T> (T[] array, int startIndex, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (startIndex < 0 || (uint)startIndex > (uint)array.Length)
throw new ArgumentOutOfRangeException ("startIndex");
if (match == null)
throw new ArgumentNullException ("match");
return GetIndex (array, startIndex, array.Length - startIndex, match);
}
public static int FindIndex<T> (T[] array, int startIndex, int count, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (startIndex < 0)
throw new ArgumentOutOfRangeException ("startIndex");
if (count < 0)
throw new ArgumentOutOfRangeException ("count");
if ((uint)startIndex + (uint)count > (uint)array.Length)
throw new ArgumentOutOfRangeException ("index and count exceed length of list");
return GetIndex (array, startIndex, count, match);
}
internal static int GetIndex<T> (T[] array, int startIndex, int count, Predicate<T> match)
{
int end = startIndex + count;
for (int i = startIndex; i < end; i++)
if (match (array[i]))
return i;
return -1;
}
public static int IndexOf (Array array, object value)
{
if (array == null)
throw new ArgumentNullException ("array");
return Array.IndexOf (array, value, 0, array.Length);
}
public static int IndexOf (Array array, object value, int startIndex)
{
if (array == null)
throw new ArgumentNullException ("array");
return Array.IndexOf (array, value, startIndex, array.Length - startIndex);
}
public static void Sort (Array keys, Array items)
{
Array.Sort (keys, items, (IComparer)null);
}
public static void Sort (Array array, int index, int length)
{
Array.Sort (array, index, length, (IComparer)null);
}
public static void Sort (Array keys, Array items, int index, int length)
{
Array.Sort (keys, items, index, length, (IComparer)null);
}
public static T[] FindAll<T> (T[] array, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (match == null)
throw new ArgumentNullException ("match");
int pos = 0;
T[] d = new T[array.Length];
foreach (T t in array)
if (match (t))
d[pos++] = t;
Array.Resize<T> (ref d, pos);
return d;
}
public static bool Exists<T> (T[] array, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (match == null)
throw new ArgumentNullException ("match");
foreach (T t in array)
if (match (t))
return true;
return false;
}
public static ReadOnlyCollection<T> AsReadOnly<T> (T[] array)
{
if (array == null)
throw new ArgumentNullException ("array");
return new ReadOnlyCollection<T> (array);
}
public static T Find<T> (T[] array, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (match == null)
throw new ArgumentNullException ("match");
foreach (T t in array)
if (match (t))
return t;
return default (T);
}
public static T FindLast<T> (T[] array, Predicate<T> match)
{
if (array == null)
throw new ArgumentNullException ("array");
if (match == null)
throw new ArgumentNullException ("match");
for (int i = array.Length - 1; i >= 0; i--)
if (match (array[i]))
return array[i];
return default (T);
}
}
}