forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrays.cs
More file actions
596 lines (517 loc) · 19.7 KB
/
Arrays.cs
File metadata and controls
596 lines (517 loc) · 19.7 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NumSharp.Backends;
namespace NumSharp.Utilities
{
public static class Arrays
{
/// <summary>
/// Inserts item into a specific index.
/// </summary>
/// <param name="source">The array to insert the value to.</param>
/// <param name="index">The index to insert to.</param>
/// <param name="value"></param>
public static void Insert<T>(ref T[] source, int index, T value)
{
if (index < 0 || index > source.Length)
throw new ArgumentOutOfRangeException(nameof(index));
Array.Resize(ref source, source.Length + 1);
Array.Copy(source, index, source, index + 1, source.Length - index - 1);
source[index] = value;
}
/// <summary>
/// Inserts item into a specific index.
/// </summary>
/// <param name="source">The array to insert the value to.</param>
/// <param name="index">The index to insert to.</param>
/// <param name="value"></param>
public static T[] Insert<T>(T[] source, int index, T value) where T : unmanaged
{
if (index < 0 || index > source.Length)
throw new ArgumentOutOfRangeException(nameof(index));
unsafe
{
var ret = new T[source.Length + 1];
fixed (T* src = source)
{
fixed (T* dst = ret)
{
new Span<T>(src, index).CopyTo(new Span<T>(dst, index));
*(dst+index) = value;
var left = source.Length - index;
new Span<T>(src + index, left).CopyTo(new Span<T>(dst + index + 1, left));
}
}
return ret;
}
}
/// <summary>
/// Inserts item into a specific index.
/// </summary>
/// <param name="source">The array to insert copy and insert value to.</param>
/// <param name="index">The index to insert to.</param>
/// <returns>a copy of <see cref="source"/> with the appended value.</returns>
public static T[] AppendAt<T>(T[] source, int index, T value)
{
var ret = (T[])source.Clone();
Insert(ref source, index, value);
return ret;
}
/// <summary>
/// Removes a specific index from given array.
/// </summary>
/// <param name="source">The array to remove <paramref name="index"/> from.</param>
/// <param name="index">The index to remove.</param>
/// <returns></returns>
public static T[] RemoveAt<T>(this T[] source, int index)
{
T[] dest = new T[source.Length - 1];
if (index > 0)
Array.Copy(source, 0, dest, 0, index);
if (index < source.Length - 1)
Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
return dest;
}
/// <summary>
/// Flattens any type of <see cref="Array"/>.
/// </summary>
/// <remarks>Supports both jagged array and multi-dim arrays.</remarks>
public static Array Flatten(Array array)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
IEnumerable _flat(IEnumerable @this)
{
foreach (var item in @this)
{
if (item is IEnumerable enumerable)
{
foreach (var subitem in _flat(enumerable))
{
yield return subitem;
}
}
else yield return item;
}
}
return Arrays.Create(array.ResolveElementType(), _flat(array));
}
/// <summary>
/// Performs fast concatenation of multiple arrays
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="arrays"></param>
/// <returns></returns>
[MethodImpl((MethodImplOptions)512)]
public static T[] Concat<T>(params T[][] arrays)
{
int sum = 0;
foreach (var array in arrays)
{
sum += array.Length;
}
T[] ret = new T[sum];
int offset = 0;
for (int i = 0; i < arrays.Length; i++)
{
var arr = arrays[i];
var arrlen = arr.Length;
Array.Copy(arr, 0, ret, offset, arrlen);
offset += arrlen;
}
return ret;
}
/// <summary>
/// Performs fast concatenation of multiple arrays
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="arrays"></param>
/// <returns></returns>
[MethodImpl((MethodImplOptions)768)]
public static T[] Concat<T>(T[] left, T[] right)
{
T[] ret = new T[left.Length + right.Length];
Array.Copy(left, 0, ret, 0, left.Length);
Array.Copy(right, 0, ret, left.Length, right.Length);
return ret;
}
/// <summary>
/// Resolves <see cref="Array"/> element type recusivly.
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
[MethodImpl((MethodImplOptions)768)]
public static Type ResolveElementType(this Array arr)
{
if (arr == null)
throw new ArgumentNullException(nameof(arr));
var t = arr.GetType().GetElementType();
// ReSharper disable once PossibleNullReferenceException
while (t.IsArray)
t = t.GetElementType();
return t;
}
/// <summary>
/// Resolves <see cref="Array"/>'s rank, supports both jagged array and multidim array.
/// </summary>
/// <returns>The number of ranks <paramref name="arr"/> has</returns>
[MethodImpl((MethodImplOptions)768)]
public static int ResolveRank(this Array arr)
{
if (arr == null)
throw new ArgumentNullException(nameof(arr));
var nestedArraysRenk = 1;
var t = arr.GetType().GetElementType();
// ReSharper disable once PossibleNullReferenceException
while (t.IsArray)
{
t = t.GetElementType();
nestedArraysRenk++;
}
return Math.Max(arr.Rank, nestedArraysRenk);
}
/// <summary>
/// Resolves the shape of this given array.
/// </summary>
/// <param name="array"></param>
/// <remarks>Supports multi-dim and jagged arrays.</remarks>
[MethodImpl((MethodImplOptions)768)]
public static (Shape Shape, Type Type) ResolveShapeAndType(this Array array)
{
//get lengths incase it is multi-dimensional
if (array.Rank > 1)
{
//is multidim
int[] dim = new int[array.Rank];
for (int idx = 0; idx < dim.Length; idx++)
dim[idx] = array.GetLength(idx);
return (new Shape(dim), array.GetType().GetElementType());
}
else
{
if (array.GetType().GetElementType()?.IsArray == true)
{
//is jagged
Array curr = array;
var dimList = new List<int>(16);
do
{
var child = (Array)curr.GetValue(0);
dimList.Add(child.Length);
curr = child;
} while (curr.GetType().GetElementType()?.IsArray == true);
return (new Shape(dimList.ToArray()), curr.GetType().GetElementType());
}
//is 1d
return (new Shape(array.Length), array.GetType().GetElementType());
}
}
/// <summary>
/// Resolves the shape of this given array.
/// </summary>
/// <param name="array"></param>
/// <remarks>Supports multi-dim and jagged arrays.</remarks>
[MethodImpl((MethodImplOptions)768)]
public static Shape ResolveShape(this Array array)
{
Shape shape;
//get lengths incase it is multi-dimensional
if (array.Rank > 1)
{
int[] dim = new int[array.Rank];
for (int idx = 0; idx < dim.Length; idx++)
dim[idx] = array.GetLength(idx);
shape = new Shape(dim);
}
else
{
if (array.GetType().GetElementType()?.IsArray == true)
{
//is jagged
Array curr = array;
var dimList = new List<int>(16);
do
{
var child = (Array)curr.GetValue(0);
dimList.Add(child.Length);
curr = child;
} while (curr.GetType().GetElementType()?.IsArray == true);
return new Shape(dimList.ToArray());
}
shape = new Shape(array.Length);
}
return shape;
}
/// <summary>
/// Creates an array of 1D of type <paramref name="type"/>.
/// </summary>
/// <typeparam name="T">The type of the array</typeparam>
/// <param name="type">The type to create this array.</param>
/// <param name="length">The length of the array</param>
/// <remarks>Do not use this if you are trying to create jagged or multidimensional array.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Array Create(Type type, IEnumerable enumerable)
{
// ReSharper disable once PossibleNullReferenceException
while (type.IsArray)
type = type.GetElementType();
var l = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
foreach (var v in enumerable)
l.Add(v);
return (Array)l.GetType().GetMethod("ToArray", BindingFlags.Public | BindingFlags.Instance).Invoke(l, null);
}
/// <summary>
/// Creates an array of 1D of type <paramref name="type"/>.
/// </summary>
/// <typeparam name="T">The type of the array</typeparam>
/// <param name="type">The type to create this array.</param>
/// <param name="length">The length of the array</param>
/// <remarks>Do not use this if you are trying to create jagged or multidimensional array.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Array Create(Type type, int length)
{
// ReSharper disable once PossibleNullReferenceException
while (type.IsArray)
type = type.GetElementType();
return Array.CreateInstance(type, length);
}
/// <summary>
/// Creates an array of specific <paramref name="length"/> of type <paramref name="type"/>.
/// </summary>
/// <param name="type">The type to create this array.</param>
/// <param name="length">The length of the array</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Array Create(Type type, int[] length)
{
// ReSharper disable once PossibleNullReferenceException
while (type.IsArray)
type = type.GetElementType();
return Array.CreateInstance(type, length);
}
/// <summary>
/// Creates an array 1D of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the array</typeparam>
/// <param name="length">The length of the array</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] Create<T>(int length)
{
return new T[length];
}
/// <summary>
/// Creates an array of 1D of type <paramref name="typeCode"/>.
/// </summary>
/// <param name="typeCode">The type to create this array.</param>
/// <param name="length">The length of the array</param>
/// <remarks>Do not use this if you are trying to create jagged or multidimensional array.</remarks>
public static Array Create(NPTypeCode typeCode, int length)
{
switch (typeCode)
{
#if _REGEN
%foreach all_dtypes,all_dtypes_lowercase%
case NPTypeCode.#1:
{
return new #2[length];
}
%
default:
throw new NotImplementedException();
#else
case NPTypeCode.NDArray:
{
return new NDArray[length];
}
case NPTypeCode.Complex:
{
return new Complex[length];
}
case NPTypeCode.Boolean:
{
return new bool[length];
}
case NPTypeCode.Byte:
{
return new byte[length];
}
case NPTypeCode.Int16:
{
return new short[length];
}
case NPTypeCode.UInt16:
{
return new ushort[length];
}
case NPTypeCode.Int32:
{
return new int[length];
}
case NPTypeCode.UInt32:
{
return new uint[length];
}
case NPTypeCode.Int64:
{
return new long[length];
}
case NPTypeCode.UInt64:
{
return new ulong[length];
}
case NPTypeCode.Char:
{
return new char[length];
}
case NPTypeCode.Double:
{
return new double[length];
}
case NPTypeCode.Single:
{
return new float[length];
}
case NPTypeCode.Decimal:
{
return new decimal[length];
}
case NPTypeCode.String:
{
return new string[length];
}
default:
throw new NotImplementedException();
#endif
}
}
/// <summary>
/// Creates an array of 1D of type <paramref name="typeCode"/> with length of 1 and a single <paramref name="value"/> inside.
/// </summary>
/// <param name="typeCode">The type to create this array.</param>
/// <param name="value">The value to insert</param>
/// <remarks>Do not use this if you are trying to create jagged or multidimensional array.</remarks>
public static Array Wrap(NPTypeCode typeCode, object value)
{
switch (typeCode)
{
#if _REGEN
%foreach all_dtypes,all_dtypes_lowercase%
case NPTypeCode.#1:
{
return new #2[1] {(#1)value};
}
%
default:
throw new NotImplementedException();
#else
case NPTypeCode.NDArray:
{
return new NDArray[1] {(NDArray)value};
}
case NPTypeCode.Complex:
{
return new Complex[1] {(Complex)value};
}
case NPTypeCode.Boolean:
{
return new bool[1] {(Boolean)value};
}
case NPTypeCode.Byte:
{
return new byte[1] {(Byte)value};
}
case NPTypeCode.Int16:
{
return new short[1] {(Int16)value};
}
case NPTypeCode.UInt16:
{
return new ushort[1] {(UInt16)value};
}
case NPTypeCode.Int32:
{
return new int[1] {(Int32)value};
}
case NPTypeCode.UInt32:
{
return new uint[1] {(UInt32)value};
}
case NPTypeCode.Int64:
{
return new long[1] {(Int64)value};
}
case NPTypeCode.UInt64:
{
return new ulong[1] {(UInt64)value};
}
case NPTypeCode.Char:
{
return new char[1] {(Char)value};
}
case NPTypeCode.Double:
{
return new double[1] {(Double)value};
}
case NPTypeCode.Single:
{
return new float[1] {(Single)value};
}
case NPTypeCode.Decimal:
{
return new decimal[1] {(Decimal)value};
}
case NPTypeCode.String:
{
return new string[1] {(String)value};
}
default:
throw new NotImplementedException();
#endif
}
}
/// <summary>
/// Extracts shape and type from given <paramref name="array"/>.
/// </summary>
/// <param name="array">The array to extract D<see cref="Type"/> and <see cref="Shape"/> from.</param>
public static (Shape Shape, Type DType) ExtractStructure(Array array)
{
if (array == null)
throw new ArgumentNullException(nameof(array));
//get lengths incase it is multi-dimensional
if (array.Rank > 1)
{
int[] dim = new int[array.Rank];
for (int idx = 0; idx < dim.Length; idx++)
dim[idx] = array.GetLength(idx);
var shape = new Shape(dim);
Type elementType = array.GetType();
// ReSharper disable once PossibleNullReferenceException
while (elementType.IsArray)
elementType = elementType.GetElementType();
return (shape, elementType);
}
// single dimension.
return (new Shape(array.Length), array.GetType().GetElementType());
}
/// <summary>
/// Extracts shape and type from given <paramref name="array"/>.
/// </summary>
/// <param name="array">The array to extract D<see cref="Type"/> and <see cref="Shape"/> from.</param>
public static (Shape Shape, Type DType) ExtractStructure<T>(T[] array)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
//this is single dimensional array.
var shape = new Shape(array.Length);
Type elementType = array.GetType().GetElementType();
return (shape, elementType);
}
}
}