Repository navigation
Expand file tree
/
Copy pathUnmanagedStorage.Getters.cs
More file actions
770 lines (707 loc) · 40 KB
/
Copy pathUnmanagedStorage.Getters.cs
File metadata and controls
770 lines (707 loc) · 40 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
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using NumSharp.Backends.Unmanaged;
namespace NumSharp.Backends
{
public partial class UnmanagedStorage
{
#region Getters
/// <summary>
/// <see cref="IArraySlice.Slice(long,long)"/> for a sub-view, tolerating the EMPTY case.
/// </summary>
/// <remarks>
/// A zero-length sub-view has no element to address, so its start offset is meaningless
/// — and it is routinely out of range: an empty window over a live buffer
/// (<c>x[:, 1:1]</c>) narrows <see cref="InternalArray"/> to Count 0 while the shape's
/// strides still step across the original rows, so <c>[1]</c> asks to slice at offset 2
/// of a 0-element array. NumPy hands back the empty view; <see cref="IArraySlice"/>'s
/// range guard is right to reject the call, so the empty case starts at 0 instead.
/// Nothing can be read or written through a length-0 slice, so where it points is
/// unobservable.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private IArraySlice SliceForSubview(long offset, long count)
=> InternalArray.Slice(count == 0 ? 0 : offset, count);
/// <summary>
/// Retrieves value of unspecified type (will figure using <see cref="IStorage.DType"/>).
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="IStorage.DType"/> is not <see cref="object"/></exception>
public unsafe object GetValue(int[] indices)
{
switch (TypeCode)
{
// %foreach supported_dtypes,supported_dtypes_lowercase%
// case NPTypeCode.#1: return *((#2*)Address + _shape.GetOffset(indices));
// %
// default:
// throw new NotSupportedException();
case NPTypeCode.Boolean: return *((bool*)Address + _shape.GetOffset(indices));
case NPTypeCode.SByte: return *((sbyte*)Address + _shape.GetOffset(indices));
case NPTypeCode.Byte: return *((byte*)Address + _shape.GetOffset(indices));
case NPTypeCode.Int16: return *((short*)Address + _shape.GetOffset(indices));
case NPTypeCode.UInt16: return *((ushort*)Address + _shape.GetOffset(indices));
case NPTypeCode.Int32: return *((int*)Address + _shape.GetOffset(indices));
case NPTypeCode.UInt32: return *((uint*)Address + _shape.GetOffset(indices));
case NPTypeCode.Int64: return *((long*)Address + _shape.GetOffset(indices));
case NPTypeCode.UInt64: return *((ulong*)Address + _shape.GetOffset(indices));
case NPTypeCode.Char: return *((char*)Address + _shape.GetOffset(indices));
case NPTypeCode.Half: return *((Half*)Address + _shape.GetOffset(indices));
case NPTypeCode.Double: return *((double*)Address + _shape.GetOffset(indices));
case NPTypeCode.Single: return *((float*)Address + _shape.GetOffset(indices));
case NPTypeCode.Decimal: return *((decimal*)Address + _shape.GetOffset(indices));
case NPTypeCode.Complex: return *((System.Numerics.Complex*)Address + _shape.GetOffset(indices));
default:
throw new NotSupportedException();
}
}
/// <summary>
/// Retrieves value of unspecified type (will figure using <see cref="IStorage.DType"/>).
/// </summary>
/// <param name="indices">The shape's indices to get (long array version).</param>
/// <returns></returns>
public unsafe object GetValue(params long[] indices)
{
switch (TypeCode)
{
case NPTypeCode.Boolean: return *((bool*)Address + _shape.GetOffset(indices));
case NPTypeCode.SByte: return *((sbyte*)Address + _shape.GetOffset(indices));
case NPTypeCode.Byte: return *((byte*)Address + _shape.GetOffset(indices));
case NPTypeCode.Int16: return *((short*)Address + _shape.GetOffset(indices));
case NPTypeCode.UInt16: return *((ushort*)Address + _shape.GetOffset(indices));
case NPTypeCode.Int32: return *((int*)Address + _shape.GetOffset(indices));
case NPTypeCode.UInt32: return *((uint*)Address + _shape.GetOffset(indices));
case NPTypeCode.Int64: return *((long*)Address + _shape.GetOffset(indices));
case NPTypeCode.UInt64: return *((ulong*)Address + _shape.GetOffset(indices));
case NPTypeCode.Char: return *((char*)Address + _shape.GetOffset(indices));
case NPTypeCode.Half: return *((Half*)Address + _shape.GetOffset(indices));
case NPTypeCode.Double: return *((double*)Address + _shape.GetOffset(indices));
case NPTypeCode.Single: return *((float*)Address + _shape.GetOffset(indices));
case NPTypeCode.Decimal: return *((decimal*)Address + _shape.GetOffset(indices));
case NPTypeCode.Complex: return *((System.Numerics.Complex*)Address + _shape.GetOffset(indices));
default:
throw new NotSupportedException();
}
}
public unsafe object GetAtIndex(long index)
{
// switch (TypeCode)
// {
// %foreach supported_dtypes,supported_dtypes_lowercase%
// case NPTypeCode.#1: return *((#2*)Address + _shape.TransformOffset(index));
// %
// default:
// throw new NotSupportedException();
// }
switch (TypeCode)
{
case NPTypeCode.Boolean: return *((bool*)Address + _shape.TransformOffset(index));
case NPTypeCode.SByte: return *((sbyte*)Address + _shape.TransformOffset(index));
case NPTypeCode.Byte: return *((byte*)Address + _shape.TransformOffset(index));
case NPTypeCode.Int16: return *((short*)Address + _shape.TransformOffset(index));
case NPTypeCode.UInt16: return *((ushort*)Address + _shape.TransformOffset(index));
case NPTypeCode.Int32: return *((int*)Address + _shape.TransformOffset(index));
case NPTypeCode.UInt32: return *((uint*)Address + _shape.TransformOffset(index));
case NPTypeCode.Int64: return *((long*)Address + _shape.TransformOffset(index));
case NPTypeCode.UInt64: return *((ulong*)Address + _shape.TransformOffset(index));
case NPTypeCode.Char: return *((char*)Address + _shape.TransformOffset(index));
case NPTypeCode.Half: return *((Half*)Address + _shape.TransformOffset(index));
case NPTypeCode.Double: return *((double*)Address + _shape.TransformOffset(index));
case NPTypeCode.Single: return *((float*)Address + _shape.TransformOffset(index));
case NPTypeCode.Decimal: return *((decimal*)Address + _shape.TransformOffset(index));
case NPTypeCode.Complex: return *((System.Numerics.Complex*)Address + _shape.TransformOffset(index));
default:
throw new NotSupportedException();
}
}
[MethodImpl(OptimizeAndInline)]
public unsafe T GetAtIndex<T>(long index) where T : unmanaged
{
Debug.Assert(typeof(T) == _dtype, $"GetAtIndex<{typeof(T).Name}> called on {_dtype.Name} array.");
return *((T*)Address + _shape.TransformOffset(index));
}
/// <summary>
/// Gets a sub-array based on the given indices, returning a view that shares memory.
/// </summary>
/// <param name="indices">
/// The indices specifying which dimensions to select. Negative indices are supported
/// and are converted to positive indices relative to the dimension size.
/// </param>
/// <returns>
/// A new <see cref="UnmanagedStorage"/> representing the sub-array. This is a view
/// that shares memory with the original storage. The returned storage's
/// <see cref="_baseStorage"/> points to the ultimate owner.
/// </returns>
/// <remarks>
/// <para>
/// <b>Memory Sharing:</b> The returned storage shares memory with this storage.
/// Modifications through either storage affect the same underlying data.
/// </para>
/// <para>
/// <b>Base Tracking:</b> Sets <c>_baseStorage</c> to chain to the ultimate owner,
/// ensuring all views in a chain point to the original storage, not intermediate views.
/// </para>
/// <para>
/// <b>Code Paths:</b>
/// <list type="bullet">
/// <item><b>Broadcasted shapes:</b> Creates a broadcasted view with base tracking</item>
/// <item><b>Non-contiguous shapes:</b> Delegates to <see cref="GetView(Slice[])"/> which uses <see cref="Alias(Shape)"/></item>
/// <item><b>Contiguous shapes:</b> Creates a direct memory slice with base tracking</item>
/// </list>
/// </para>
/// </remarks>
/// <seealso cref="GetView(Slice[])"/>
/// <seealso cref="Alias()"/>
[MethodImpl(OptimizeAndInline)]
public UnmanagedStorage GetData(int[] indices)
{
var this_shape = Shape;
// ReSharper disable once ConvertIfStatementToReturnStatement
indices = Shape.InferNegativeCoordinates(Shape.dimensions, indices);
if (this_shape.IsBroadcasted)
{
var (shape, offset) = this_shape.GetSubshape(indices);
// For non-broadcasted contiguous subshapes, use size (the actual data extent).
// Only use BufferSize when the subshape itself is broadcasted.
long sliceSize = shape.IsBroadcasted
? (shape.BufferSize > 0 ? shape.BufferSize : shape.size)
: shape.size;
// Create shape with offset=0 since InternalArray.Slice already accounts for the offset
var adjustedShape = new Shape(shape.dimensions, shape.strides, 0, sliceSize);
var view = UnmanagedStorage.CreateBroadcastedUnsafe(SliceForSubview(offset, sliceSize), adjustedShape);
view._baseStorage = _baseStorage ?? this;
view.OnReshaped(); // a view never owns its buffer (NumPy: base != NULL ⟹ !OWNDATA)
return view;
}
else if (!this_shape.IsContiguous)
{
// Non-contiguous shapes (stepped slices, transposed, etc.) cannot use
// memory slicing. Create a view with indexed slices instead.
return GetView(indices.Select(Slice.Index).ToArray());
}
else
{
// Contiguous shape: can take a direct memory slice.
// GetSubshape computes the correct offset accounting for shape.offset.
var (shape, offset) = this_shape.GetSubshape(indices);
// A sub-array is a VIEW, so it inherits writeability: indexing a row/element out of a
// read-only array (broadcast, or a contiguous 'r' memmap) must stay read-only —
// GetSubshape rebuilds the shape with fresh (writeable) flags, so carry it through.
if (!this_shape.IsWriteable)
shape = shape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE);
var view = new UnmanagedStorage(SliceForSubview(offset, shape.Size), shape);
view._baseStorage = _baseStorage ?? this;
view.OnReshaped(); // a view never owns its buffer (NumPy: base != NULL ⟹ !OWNDATA)
return view;
}
}
/// <summary>
/// Gets a sub-array based on the given indices (long array version), returning a view that shares memory.
/// </summary>
/// <param name="indices">The dimension indices specifying the sub-array.</param>
/// <returns>
/// A new <see cref="UnmanagedStorage"/> representing the sub-array. This is a view
/// that shares memory with the original storage. The returned storage's
/// <see cref="_baseStorage"/> points to the ultimate owner.
/// </returns>
/// <seealso cref="GetData(int[])"/>
[MethodImpl(OptimizeAndInline)]
public UnmanagedStorage GetData(params long[] indices)
{
var this_shape = Shape;
// ReSharper disable once ConvertIfStatementToReturnStatement
indices = Shape.InferNegativeCoordinates(Shape.dimensions, indices);
if (this_shape.IsBroadcasted)
{
var (shape, offset) = this_shape.GetSubshape(indices);
// For non-broadcasted contiguous subshapes, use size (the actual data extent).
// Only use BufferSize when the subshape itself is broadcasted.
long sliceSize = shape.IsBroadcasted
? (shape.BufferSize > 0 ? shape.BufferSize : shape.size)
: shape.size;
// Create shape with offset=0 since InternalArray.Slice already accounts for the offset
var adjustedShape = new Shape(shape.dimensions, shape.strides, 0, sliceSize);
var view = UnmanagedStorage.CreateBroadcastedUnsafe(SliceForSubview(offset, sliceSize), adjustedShape);
view._baseStorage = _baseStorage ?? this;
view.OnReshaped(); // a view never owns its buffer (NumPy: base != NULL ⟹ !OWNDATA)
return view;
}
else if (!this_shape.IsContiguous)
{
// Non-contiguous shapes (stepped slices, transposed, etc.) cannot use
// memory slicing. Create a view with indexed slices instead.
return GetView(indices.Select(i => Slice.Index(i)).ToArray());
}
else
{
// Contiguous shape: can take a direct memory slice.
// GetSubshape computes the correct offset accounting for shape.offset.
var (shape, offset) = this_shape.GetSubshape(indices);
// A sub-array is a VIEW, so it inherits writeability: indexing a row/element out of a
// read-only array (broadcast, or a contiguous 'r' memmap) must stay read-only —
// GetSubshape rebuilds the shape with fresh (writeable) flags, so carry it through.
if (!this_shape.IsWriteable)
shape = shape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE);
var view = new UnmanagedStorage(SliceForSubview(offset, shape.Size), shape);
view._baseStorage = _baseStorage ?? this;
view.OnReshaped(); // a view never owns its buffer (NumPy: base != NULL ⟹ !OWNDATA)
return view;
}
}
/// <summary>
/// Gets a sub-array based on the given indices (pointer version), returning a view that shares memory.
/// </summary>
/// <param name="dims">Pointer to an array of dimension indices.</param>
/// <param name="ndims">The number of indices in the array.</param>
/// <returns>
/// A new <see cref="UnmanagedStorage"/> representing the sub-array. This is a view
/// that shares memory with the original storage. The returned storage's
/// <see cref="_baseStorage"/> points to the ultimate owner.
/// </returns>
/// <remarks>
/// <para>
/// <b>Memory Sharing:</b> The returned storage shares memory with this storage.
/// Modifications through either storage affect the same underlying data.
/// </para>
/// <para>
/// <b>Base Tracking:</b> Sets <c>_baseStorage</c> to chain to the ultimate owner,
/// ensuring all views in a chain point to the original storage, not intermediate views.
/// </para>
/// <para>
/// <b>Code Paths:</b>
/// <list type="bullet">
/// <item><b>Broadcasted shapes:</b> Creates a broadcasted view with base tracking</item>
/// <item><b>Non-contiguous shapes:</b> Delegates to <see cref="GetView(Slice[])"/> which uses <see cref="Alias(Shape)"/></item>
/// <item><b>Contiguous shapes:</b> Creates a direct memory slice with base tracking</item>
/// </list>
/// </para>
/// </remarks>
/// <seealso cref="GetData(int[])"/>
[MethodImpl(OptimizeAndInline)]
public unsafe UnmanagedStorage GetData(int* dims, int ndims)
{
var this_shape = Shape;
// ReSharper disable once ConvertIfStatementToReturnStatement
Shape.InferNegativeCoordinates(Shape.dimensions, dims, ndims);
// Convert int* to long* for GetSubshape
long* longDims = stackalloc long[ndims];
for (int i = 0; i < ndims; i++)
longDims[i] = dims[i];
if (this_shape.IsBroadcasted)
{
var (shape, offset) = this_shape.GetSubshape(longDims, ndims);
// For non-broadcasted contiguous subshapes, use size (the actual data extent).
// Only use BufferSize when the subshape itself is broadcasted.
long sliceSize = shape.IsBroadcasted
? (shape.BufferSize > 0 ? shape.BufferSize : shape.size)
: shape.size;
// Create shape with offset=0 since InternalArray.Slice already accounts for the offset
var adjustedShape = new Shape(shape.dimensions, shape.strides, 0, sliceSize);
var view = UnmanagedStorage.CreateBroadcastedUnsafe(SliceForSubview(offset, sliceSize), adjustedShape);
view._baseStorage = _baseStorage ?? this;
view.OnReshaped(); // a view never owns its buffer (NumPy: base != NULL ⟹ !OWNDATA)
return view;
}
else if (!this_shape.IsContiguous)
{
// Non-contiguous shapes (stepped slices, transposed, etc.) cannot use
// memory slicing. Create a view with indexed slices instead.
var slices = new Slice[ndims];
for (int i = 0; i < ndims; i++)
{
slices[i] = Slice.Index(*(dims + i));
}
return GetView(slices);
}
else
{
// Contiguous shape: can take a direct memory slice.
// GetSubshape computes the correct offset accounting for shape.offset.
var (shape, offset) = this_shape.GetSubshape(longDims, ndims);
// A sub-array view inherits writeability (see the int[] overload).
if (!this_shape.IsWriteable)
shape = shape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE);
var view = new UnmanagedStorage(SliceForSubview(offset, shape.Size), shape);
view._baseStorage = _baseStorage ?? this;
view.OnReshaped(); // a view never owns its buffer (NumPy: base != NULL ⟹ !OWNDATA)
return view;
}
}
/// <summary>
/// Gets a sub-array based on the given indices (long pointer version), returning a view that shares memory.
/// </summary>
/// <param name="dims">Pointer to an array of long dimension indices.</param>
/// <param name="ndims">The number of indices in the array.</param>
/// <returns>A new <see cref="UnmanagedStorage"/> representing the sub-array.</returns>
/// <seealso cref="GetData(int*)"/>
[MethodImpl(OptimizeAndInline)]
public unsafe UnmanagedStorage GetData(long* dims, int ndims)
{
var this_shape = Shape;
// ReSharper disable once ConvertIfStatementToReturnStatement
Shape.InferNegativeCoordinates(Shape.dimensions, dims, ndims);
if (this_shape.IsBroadcasted)
{
var (shape, offset) = this_shape.GetSubshape(dims, ndims);
// For non-broadcasted contiguous subshapes, use size (the actual data extent).
// Only use BufferSize when the subshape itself is broadcasted.
long sliceSize = shape.IsBroadcasted
? (shape.BufferSize > 0 ? shape.BufferSize : shape.size)
: shape.size;
// Create shape with offset=0 since InternalArray.Slice already accounts for the offset
var adjustedShape = new Shape(shape.dimensions, shape.strides, 0, sliceSize);
var view = UnmanagedStorage.CreateBroadcastedUnsafe(SliceForSubview(offset, sliceSize), adjustedShape);
view._baseStorage = _baseStorage ?? this;
view.OnReshaped(); // a view never owns its buffer (NumPy: base != NULL ⟹ !OWNDATA)
return view;
}
else if (!this_shape.IsContiguous)
{
// Non-contiguous shapes (stepped slices, transposed, etc.) cannot use
// memory slicing. Create a view with indexed slices instead.
var slices = new Slice[ndims];
for (int i = 0; i < ndims; i++)
{
slices[i] = Slice.Index(*(dims + i));
}
return GetView(slices);
}
else
{
// Contiguous shape: can take a direct memory slice.
// GetSubshape computes the correct offset accounting for shape.offset.
var (shape, offset) = this_shape.GetSubshape(dims, ndims);
// A sub-array view inherits writeability (see the int[] overload).
if (!this_shape.IsWriteable)
shape = shape.WithFlags(flagsToClear: ArrayFlags.WRITEABLE);
var view = new UnmanagedStorage(SliceForSubview(offset, shape.Size), shape);
view._baseStorage = _baseStorage ?? this;
view.OnReshaped(); // a view never owns its buffer (NumPy: base != NULL ⟹ !OWNDATA)
return view;
}
}
/// <summary>
/// Get reference to internal data storage and cast (also copies) elements to new dtype if necessary
/// </summary>
/// <typeparam name="T">new storage data type</typeparam>
/// <returns>reference to internal (casted) storage as T[]</returns>
/// <remarks>Copies if <typeparamref name="T"/> does not equal to <see cref="DType"/> or if Shape is sliced.</remarks>
public ArraySlice<T> GetData<T>() where T : unmanaged
{
if (!typeof(T).IsValidNPType())
throw new NotSupportedException($"Type {typeof(T).Name} is not a valid np.dtype");
if (!Shape.IsContiguous)
return CloneData<T>();
var internalArray = InternalArray;
// Contiguity is offset-independent (NumPy-aligned): a contiguous
// VIEW may occupy [offset, offset+size) inside a larger buffer —
// np.split / np.unstack children are built that way. Narrow to the
// logical window (zero-copy, still writes through) so both the
// same-type return and the cast below see exactly the view's
// elements rather than the whole backing buffer.
if (internalArray != null && (Shape.offset != 0 || internalArray.Count != Shape.size))
internalArray = internalArray.Slice(Shape.offset, Shape.size);
if (internalArray is ArraySlice<T> ret)
return ret;
return _ChangeTypeOfArray<T>(internalArray);
}
/// <summary>
/// Get single value from internal storage as type T and cast dtype to T
/// </summary>
/// <param name="indices">indices</param>
/// <typeparam name="T">new storage data type</typeparam>
/// <returns>element from internal storage</returns>
/// <exception cref="NullReferenceException">When <typeparamref name="T"/> does not equal to <see cref="DType"/></exception>
/// <remarks>If you provide less indices than there are dimensions, the rest are filled with 0.</remarks> //TODO! doc this in other similar methods
public T GetValue<T>(int[] indices) where T : unmanaged
{
Debug.Assert(typeof(T) == _dtype, $"GetValue<{typeof(T).Name}> called on {_dtype.Name} array.");
unsafe
{
return *((T*)Address + _shape.GetOffset(indices));
}
}
/// <summary>
/// Get element from internal storage as T
/// </summary>
/// <param name="indices">indices</param>
/// <typeparam name="T">new storage data type</typeparam>
/// <returns>element from internal storage</returns>
/// <exception cref="NullReferenceException">When <typeparamref name="T"/> does not equal to <see cref="DType"/></exception>
/// <remarks>If you provide less indices than there are dimensions, the rest are filled with 0.</remarks>
public T GetValue<T>(params long[] indices) where T : unmanaged
{
Debug.Assert(typeof(T) == _dtype, $"GetValue<{typeof(T).Name}> called on {_dtype.Name} array.");
unsafe
{
return *((T*)Address + _shape.GetOffset(indices));
}
}
/// <summary>
/// Get reference to internal data storage
/// </summary>
/// <returns>reference to internal storage as System.Array</returns>
[MethodImpl(Inline)]
public IArraySlice GetData() => InternalArray;
// #region Direct Getters
// %foreach supported_dtypes,supported_dtypes_lowercase%
// /// <summary>
// /// Retrieves value of type <see cref="#2"/> from internal storage.
// /// </summary>
// /// <param name="indices">The shape's indices to get.</param>
// /// <returns></returns>
// /// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="#2"/></exception>
// public #2 Get#1(int[] indices)
// => _slices.#1[_shape.GetOffset(indices)];
// %
// #endregion
#region Direct Getters
/// <summary>
/// Retrieves value of type <see cref="bool"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="bool"/></exception>
public bool GetBoolean(int[] indices)
=> _slices.Boolean[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="sbyte"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="sbyte"/></exception>
public sbyte GetSByte(int[] indices)
=> _slices.SByte[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="byte"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="byte"/></exception>
public byte GetByte(int[] indices)
=> _slices.Byte[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="short"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="short"/></exception>
public short GetInt16(int[] indices)
=> _slices.Int16[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="ushort"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="ushort"/></exception>
public ushort GetUInt16(int[] indices)
=> _slices.UInt16[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="int"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="int"/></exception>
public int GetInt32(int[] indices)
=> _slices.Int32[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="uint"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="uint"/></exception>
public uint GetUInt32(int[] indices)
=> _slices.UInt32[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="long"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="long"/></exception>
public long GetInt64(int[] indices)
=> _slices.Int64[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="ulong"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="ulong"/></exception>
public ulong GetUInt64(int[] indices)
=> _slices.UInt64[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="char"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="char"/></exception>
public char GetChar(int[] indices)
=> _slices.Char[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="Half"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="Half"/></exception>
public Half GetHalf(int[] indices)
=> _slices.Half[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="double"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="double"/></exception>
public double GetDouble(int[] indices)
=> _slices.Double[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="float"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="float"/></exception>
public float GetSingle(int[] indices)
=> _slices.Single[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="decimal"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="decimal"/></exception>
public decimal GetDecimal(int[] indices)
=> _slices.Decimal[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="System.Numerics.Complex"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get.</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="System.Numerics.Complex"/></exception>
public System.Numerics.Complex GetComplex(int[] indices)
=> _slices.Complex[_shape.GetOffset(indices)];
#endregion
#region Direct Getters (long[] overloads)
/// <summary>
/// Retrieves value of type <see cref="bool"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="bool"/></exception>
public bool GetBoolean(params long[] indices)
=> _slices.Boolean[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="sbyte"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="sbyte"/></exception>
public sbyte GetSByte(params long[] indices)
=> _slices.SByte[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="byte"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="byte"/></exception>
public byte GetByte(params long[] indices)
=> _slices.Byte[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="short"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="short"/></exception>
public short GetInt16(params long[] indices)
=> _slices.Int16[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="ushort"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="ushort"/></exception>
public ushort GetUInt16(params long[] indices)
=> _slices.UInt16[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="int"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="int"/></exception>
public int GetInt32(params long[] indices)
=> _slices.Int32[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="uint"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="uint"/></exception>
public uint GetUInt32(params long[] indices)
=> _slices.UInt32[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="long"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="long"/></exception>
public long GetInt64(params long[] indices)
=> _slices.Int64[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="ulong"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="ulong"/></exception>
public ulong GetUInt64(params long[] indices)
=> _slices.UInt64[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="char"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="char"/></exception>
public char GetChar(params long[] indices)
=> _slices.Char[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="Half"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="Half"/></exception>
public Half GetHalf(params long[] indices)
=> _slices.Half[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="double"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="double"/></exception>
public double GetDouble(params long[] indices)
=> _slices.Double[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="float"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="float"/></exception>
public float GetSingle(params long[] indices)
=> _slices.Single[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="decimal"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="decimal"/></exception>
public decimal GetDecimal(params long[] indices)
=> _slices.Decimal[_shape.GetOffset(indices)];
/// <summary>
/// Retrieves value of type <see cref="System.Numerics.Complex"/> from internal storage.
/// </summary>
/// <param name="indices">The shape's indices to get (long version).</param>
/// <returns></returns>
/// <exception cref="NullReferenceException">When <see cref="DType"/> is not <see cref="System.Numerics.Complex"/></exception>
public System.Numerics.Complex GetComplex(params long[] indices)
=> _slices.Complex[_shape.GetOffset(indices)];
#endregion
#endregion
}
}