-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathArrayConvert.cs
More file actions
4108 lines (3631 loc) · 169 KB
/
ArrayConvert.cs
File metadata and controls
4108 lines (3631 loc) · 169 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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NumSharp.Backends;
namespace NumSharp.Utilities
{
//todo! write manually Complex[] ToComplex(String[] sourceArray)
//todo! that knows to parse input like pythons complex syntax: 10+5j readmore: https://stackoverflow.com/a/28873083/1481186
/// <summary>
/// Presents all possible combinations of array conversion of types supported by numpy.
/// </summary>
/// <remarks>Implementation is based on Array.ConvertAll from corefx source at https://github.com/dotnet/corefx/blob/b2097cbdcb26f7f317252334ddcce101a20b7f3d/src/Common/src/CoreLib/System/Array.cs#L586</remarks>
public static class ArrayConvert
{
#region Cloning
/// <summary>
/// Creates a clone of given <see cref="sourceArray"/>.
/// </summary>
/// <param name="sourceArray">The array to clone</param>
/// <remarks>If possible, for performance reasons use generic version of this method.</remarks>
public static Array Clone(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
//handle element type
var elementType = sourceArray.GetType().GetElementType();
while (elementType.IsArray)
elementType = elementType.GetElementType();
Array output;
//handle array length
var dims = sourceArray.Rank;
if (dims > 1)
{
int[] dimensions = new int[dims];
for (int idx = 0; idx < dims; idx++)
dimensions[idx] = sourceArray.GetLength(idx);
output = Arrays.Create(elementType, dimensions);
}
else
{
output = Arrays.Create(elementType, sourceArray.Length);
}
Array.Copy(sourceArray, 0, output, 0, sourceArray.Length);
return output;
}
/// <summary>
/// Creates a clone of given <see cref="sourceArray"/>.
/// </summary>
/// <param name="sourceArray">The array to clone</param>
public static T[] Clone<T>(T[] sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var output = new T[sourceArray.Length];
sourceArray.AsSpan().CopyTo(output);
return output;
}
/// <summary>
/// Creates a clone of given <see cref="sourceArray"/> using <see cref="Array.CopyTo(System.Array,int)"/>.
/// </summary>
/// <param name="sourceArray">The array to clone</param>
public static T[,] Clone<T>(T[,] sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var output = new T[sourceArray.GetLength(0), sourceArray.GetLength(1)];
Array.Copy(sourceArray, 0, output, 0, sourceArray.Length);
return output;
}
/// <summary>
/// Creates a clone of given <see cref="sourceArray"/> using <see cref="Array.CopyTo(System.Array,int)"/>.
/// </summary>
/// <param name="sourceArray">The array to clone</param>
public static T[,,] Clone<T>(T[,,] sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var output = new T[sourceArray.GetLength(0), sourceArray.GetLength(1), sourceArray.GetLength(2)];
Array.Copy(sourceArray, 0, output, 0, sourceArray.Length);
return output;
}
/// <summary>
/// Creates a clone of given <see cref="sourceArray"/> using <see cref="Array.CopyTo(System.Array,int)"/>.
/// </summary>
/// <param name="sourceArray">The array to clone</param>
public static T[,,,] Clone<T>(T[,,,] sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var output = new T[sourceArray.GetLength(0), sourceArray.GetLength(1), sourceArray.GetLength(2), sourceArray.GetLength(4)];
Array.Copy(sourceArray, 0, output, 0, sourceArray.Length);
return output;
}
#endregion
#region NonGeneric To Type
/// <summary>
/// Converts <see cref="sourceArray"/> to an array of type <see cref="returnType"/>.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <param name="returnType">The type to convert the data to</param>
/// <returns></returns>
/// <remarks>If <see cref="sourceArray"/>'s element type equals to <see cref="returnType"/> then a copy is returned</remarks>
public static Array To(Array sourceArray, Type returnType)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
if (returnType == null)
{
throw new ArgumentNullException(nameof(returnType));
}
if (returnType.IsArray)
{
returnType = returnType.GetElementType();
}
switch (returnType.GetTypeCode())
{
#if _REGEN
%foreach supported_dtypes %
case NPTypeCode.#1: return To#1(sourceArray);
%
#else
case NPTypeCode.Boolean: return ToBoolean(sourceArray);
case NPTypeCode.Byte: return ToByte(sourceArray);
case NPTypeCode.Int16: return ToInt16(sourceArray);
case NPTypeCode.UInt16: return ToUInt16(sourceArray);
case NPTypeCode.Int32: return ToInt32(sourceArray);
case NPTypeCode.UInt32: return ToUInt32(sourceArray);
case NPTypeCode.Int64: return ToInt64(sourceArray);
case NPTypeCode.UInt64: return ToUInt64(sourceArray);
case NPTypeCode.Char: return ToChar(sourceArray);
case NPTypeCode.Double: return ToDouble(sourceArray);
case NPTypeCode.Single: return ToSingle(sourceArray);
case NPTypeCode.Decimal: return ToDecimal(sourceArray);
#endif
default:
throw new NotSupportedException($"Unable to convert {sourceArray.GetType().GetElementType()?.Name} to {returnType?.Name}.");
}
}
/// <summary>
/// Converts <see cref="sourceArray"/> to an array of type <see cref="typeCode"/>.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <param name="typeCode">The type to convert the data to</param>
/// <returns></returns>
/// <remarks>If <see cref="sourceArray"/>'s element type equals to <see cref="typeCode"/> then a copy is returned</remarks>
public static Array To(Array sourceArray, NPTypeCode typeCode)
{
switch (typeCode)
{
#if _REGEN
%foreach supported_dtypes %
case NPTypeCode.#1: return To#1(sourceArray);
%
#else
case NPTypeCode.Boolean: return ToBoolean(sourceArray);
case NPTypeCode.Byte: return ToByte(sourceArray);
case NPTypeCode.Int16: return ToInt16(sourceArray);
case NPTypeCode.UInt16: return ToUInt16(sourceArray);
case NPTypeCode.Int32: return ToInt32(sourceArray);
case NPTypeCode.UInt32: return ToUInt32(sourceArray);
case NPTypeCode.Int64: return ToInt64(sourceArray);
case NPTypeCode.UInt64: return ToUInt64(sourceArray);
case NPTypeCode.Char: return ToChar(sourceArray);
case NPTypeCode.Double: return ToDouble(sourceArray);
case NPTypeCode.Single: return ToSingle(sourceArray);
case NPTypeCode.Decimal: return ToDecimal(sourceArray);
#endif
default:
throw new NotSupportedException($"Unable to convert {sourceArray.GetType().GetElementType()?.Name} to NPTypeCode.{typeCode}.");
}
}
/// <summary>
/// Converts <see cref="sourceArray"/> to an array of type <see cref="returnType"/>.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <param name="returnType">The type to convert the data to</param>
/// <returns></returns>
/// <remarks>If <see cref="sourceArray"/>'s element type equals to <see cref="returnType"/> then a copy is returned</remarks>
public static T[] To<T>(Array sourceArray)
{
return (T[])To(sourceArray, typeof(T)); // no need for direct cast.
}
#endregion
#region From NonGeneric
#if _REGEN
%foreach all_dtypes%
public static #1[] To#1(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return To#1((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return To#1((Byte[]) sourceArray);
case NPTypeCode.Int16:
return To#1((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return To#1((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return To#1((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return To#1((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return To#1((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return To#1((UInt64[]) sourceArray);
case NPTypeCode.Char:
return To#1((Char[]) sourceArray);
case NPTypeCode.Double:
return To#1((Double[]) sourceArray);
case NPTypeCode.Single:
return To#1((Single[]) sourceArray);
case NPTypeCode.Decimal:
return To#1((Decimal[]) sourceArray);
case NPTypeCode.String:
return To#1((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
%
#else
public static Boolean[] ToBoolean(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToBoolean((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToBoolean((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToBoolean((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToBoolean((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToBoolean((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToBoolean((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToBoolean((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToBoolean((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToBoolean((Char[]) sourceArray);
case NPTypeCode.Double:
return ToBoolean((Double[]) sourceArray);
case NPTypeCode.Single:
return ToBoolean((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToBoolean((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToBoolean((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static Byte[] ToByte(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToByte((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToByte((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToByte((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToByte((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToByte((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToByte((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToByte((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToByte((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToByte((Char[]) sourceArray);
case NPTypeCode.Double:
return ToByte((Double[]) sourceArray);
case NPTypeCode.Single:
return ToByte((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToByte((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToByte((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static Int16[] ToInt16(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToInt16((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToInt16((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToInt16((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToInt16((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToInt16((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToInt16((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToInt16((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToInt16((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToInt16((Char[]) sourceArray);
case NPTypeCode.Double:
return ToInt16((Double[]) sourceArray);
case NPTypeCode.Single:
return ToInt16((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToInt16((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToInt16((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static UInt16[] ToUInt16(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToUInt16((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToUInt16((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToUInt16((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToUInt16((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToUInt16((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToUInt16((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToUInt16((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToUInt16((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToUInt16((Char[]) sourceArray);
case NPTypeCode.Double:
return ToUInt16((Double[]) sourceArray);
case NPTypeCode.Single:
return ToUInt16((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToUInt16((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToUInt16((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static Int32[] ToInt32(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToInt32((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToInt32((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToInt32((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToInt32((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToInt32((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToInt32((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToInt32((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToInt32((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToInt32((Char[]) sourceArray);
case NPTypeCode.Double:
return ToInt32((Double[]) sourceArray);
case NPTypeCode.Single:
return ToInt32((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToInt32((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToInt32((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static UInt32[] ToUInt32(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToUInt32((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToUInt32((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToUInt32((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToUInt32((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToUInt32((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToUInt32((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToUInt32((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToUInt32((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToUInt32((Char[]) sourceArray);
case NPTypeCode.Double:
return ToUInt32((Double[]) sourceArray);
case NPTypeCode.Single:
return ToUInt32((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToUInt32((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToUInt32((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static Int64[] ToInt64(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToInt64((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToInt64((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToInt64((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToInt64((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToInt64((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToInt64((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToInt64((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToInt64((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToInt64((Char[]) sourceArray);
case NPTypeCode.Double:
return ToInt64((Double[]) sourceArray);
case NPTypeCode.Single:
return ToInt64((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToInt64((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToInt64((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static UInt64[] ToUInt64(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToUInt64((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToUInt64((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToUInt64((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToUInt64((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToUInt64((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToUInt64((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToUInt64((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToUInt64((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToUInt64((Char[]) sourceArray);
case NPTypeCode.Double:
return ToUInt64((Double[]) sourceArray);
case NPTypeCode.Single:
return ToUInt64((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToUInt64((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToUInt64((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static Char[] ToChar(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToChar((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToChar((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToChar((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToChar((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToChar((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToChar((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToChar((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToChar((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToChar((Char[]) sourceArray);
case NPTypeCode.Double:
return ToChar((Double[]) sourceArray);
case NPTypeCode.Single:
return ToChar((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToChar((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToChar((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static Double[] ToDouble(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToDouble((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToDouble((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToDouble((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToDouble((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToDouble((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToDouble((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToDouble((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToDouble((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToDouble((Char[]) sourceArray);
case NPTypeCode.Double:
return ToDouble((Double[]) sourceArray);
case NPTypeCode.Single:
return ToDouble((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToDouble((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToDouble((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static Single[] ToSingle(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToSingle((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToSingle((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToSingle((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToSingle((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToSingle((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToSingle((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToSingle((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToSingle((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToSingle((Char[]) sourceArray);
case NPTypeCode.Double:
return ToSingle((Double[]) sourceArray);
case NPTypeCode.Single:
return ToSingle((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToSingle((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToSingle((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static Decimal[] ToDecimal(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToDecimal((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToDecimal((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToDecimal((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToDecimal((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToDecimal((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToDecimal((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToDecimal((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToDecimal((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToDecimal((Char[]) sourceArray);
case NPTypeCode.Double:
return ToDecimal((Double[]) sourceArray);
case NPTypeCode.Single:
return ToDecimal((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToDecimal((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToDecimal((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static String[] ToString(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToString((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToString((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToString((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToString((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToString((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToString((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToString((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToString((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToString((Char[]) sourceArray);
case NPTypeCode.Double:
return ToString((Double[]) sourceArray);
case NPTypeCode.Single:
return ToString((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToString((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToString((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
public static Complex[] ToComplex(Array sourceArray)
{
if (sourceArray == null)
{
throw new ArgumentNullException(nameof(sourceArray));
}
var fromTypeCode = sourceArray.GetType().GetElementType().GetTypeCode();
switch (fromTypeCode)
{
case NPTypeCode.Boolean:
return ToComplex((Boolean[]) sourceArray);
case NPTypeCode.Byte:
return ToComplex((Byte[]) sourceArray);
case NPTypeCode.Int16:
return ToComplex((Int16[]) sourceArray);
case NPTypeCode.UInt16:
return ToComplex((UInt16[]) sourceArray);
case NPTypeCode.Int32:
return ToComplex((Int32[]) sourceArray);
case NPTypeCode.UInt32:
return ToComplex((UInt32[]) sourceArray);
case NPTypeCode.Int64:
return ToComplex((Int64[]) sourceArray);
case NPTypeCode.UInt64:
return ToComplex((UInt64[]) sourceArray);
case NPTypeCode.Char:
return ToComplex((Char[]) sourceArray);
case NPTypeCode.Double:
return ToComplex((Double[]) sourceArray);
case NPTypeCode.Single:
return ToComplex((Single[]) sourceArray);
case NPTypeCode.Decimal:
return ToComplex((Decimal[]) sourceArray);
case NPTypeCode.String:
return ToComplex((String[]) sourceArray);
default:
throw new ArgumentOutOfRangeException();
}
}
#endif
#endregion
#region Generic
#region To Same Type
#if _REGEN
%foreach all_dtypes%
/// <summary>
/// Converts <see cref="#1"/> array to a <see cref="#1"/> array.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <returns>Converted array of type #1</returns>
/// <remarks>Based on benchmark ArrayCopying</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static #1[] To#1(#1[] sourceArray)
{
if (sourceArray == null)
throw new ArgumentNullException(nameof(sourceArray));
var length = sourceArray.Length;
var output = new #1[length];
sourceArray.AsSpan().CopyTo(output);
return output;
}
%
#else
/// <summary>
/// Converts <see cref="Boolean"/> array to a <see cref="Boolean"/> array.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <returns>Converted array of type Boolean</returns>
/// <remarks>Based on benchmark ArrayCopying</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Boolean[] ToBoolean(Boolean[] sourceArray)
{
if (sourceArray == null)
throw new ArgumentNullException(nameof(sourceArray));
var length = sourceArray.Length;
var output = new Boolean[length];
sourceArray.AsSpan().CopyTo(output);
return output;
}
/// <summary>
/// Converts <see cref="Byte"/> array to a <see cref="Byte"/> array.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <returns>Converted array of type Byte</returns>
/// <remarks>Based on benchmark ArrayCopying</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Byte[] ToByte(Byte[] sourceArray)
{
if (sourceArray == null)
throw new ArgumentNullException(nameof(sourceArray));
var length = sourceArray.Length;
var output = new Byte[length];
sourceArray.AsSpan().CopyTo(output);
return output;
}
/// <summary>
/// Converts <see cref="Int16"/> array to a <see cref="Int16"/> array.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <returns>Converted array of type Int16</returns>
/// <remarks>Based on benchmark ArrayCopying</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int16[] ToInt16(Int16[] sourceArray)
{
if (sourceArray == null)
throw new ArgumentNullException(nameof(sourceArray));
var length = sourceArray.Length;
var output = new Int16[length];
sourceArray.AsSpan().CopyTo(output);
return output;
}
/// <summary>
/// Converts <see cref="UInt16"/> array to a <see cref="UInt16"/> array.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <returns>Converted array of type UInt16</returns>
/// <remarks>Based on benchmark ArrayCopying</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt16[] ToUInt16(UInt16[] sourceArray)
{
if (sourceArray == null)
throw new ArgumentNullException(nameof(sourceArray));
var length = sourceArray.Length;
var output = new UInt16[length];
sourceArray.AsSpan().CopyTo(output);
return output;
}
/// <summary>
/// Converts <see cref="Int32"/> array to a <see cref="Int32"/> array.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <returns>Converted array of type Int32</returns>
/// <remarks>Based on benchmark ArrayCopying</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32[] ToInt32(Int32[] sourceArray)
{
if (sourceArray == null)
throw new ArgumentNullException(nameof(sourceArray));
var length = sourceArray.Length;
var output = new Int32[length];
sourceArray.AsSpan().CopyTo(output);
return output;
}
/// <summary>
/// Converts <see cref="UInt32"/> array to a <see cref="UInt32"/> array.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <returns>Converted array of type UInt32</returns>
/// <remarks>Based on benchmark ArrayCopying</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt32[] ToUInt32(UInt32[] sourceArray)
{
if (sourceArray == null)
throw new ArgumentNullException(nameof(sourceArray));
var length = sourceArray.Length;
var output = new UInt32[length];
sourceArray.AsSpan().CopyTo(output);
return output;
}
/// <summary>
/// Converts <see cref="Int64"/> array to a <see cref="Int64"/> array.
/// </summary>
/// <param name="sourceArray">The array to convert</param>
/// <returns>Converted array of type Int64</returns>