Skip to content

Commit cd4e987

Browse files
committed
slice index and strides.
1 parent 4b90c06 commit cd4e987

17 files changed

Lines changed: 112 additions & 188 deletions

src/NumSharp.Core/Backends/ArrayStorage.cs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -273,29 +273,6 @@ public Array CloneData()
273273
return (Array) _values.Clone();
274274
}
275275

276-
/// <summary>
277-
/// Get reference to internal data storage and cast elements to new dtype
278-
/// </summary>
279-
/// <param name="dtype">new storage data type</param>
280-
/// <returns>reference to internal (casted) storage as System.Array </returns>
281-
public Array GetData(Type dtype)
282-
{
283-
var methods = this.GetType().GetMethods().Where(x => x.Name.Equals("GetData") && x.IsGenericMethod && x.ReturnType.Name.Equals("T[]"));
284-
var genMethods = methods.First().MakeGenericMethod(dtype);
285-
286-
return (Array) genMethods.Invoke(this,null);
287-
}
288-
289-
/// <summary>
290-
/// Clone internal storage and cast elements to new dtype
291-
/// </summary>
292-
/// <param name="dtype">cloned storage data type</param>
293-
/// <returns>reference to cloned storage as System.Array</returns>
294-
public Array CloneData(Type dtype)
295-
{
296-
return (Array) this.GetData().Clone();
297-
}
298-
299276
/// <summary>
300277
/// Get reference to internal data storage and cast elements to new dtype
301278
/// </summary>
@@ -494,5 +471,10 @@ public Span<T> View<T>(Slice slice = null)
494471
{
495472
throw new NotImplementedException("View Slice");
496473
}
474+
475+
public Span<T> GetSpanData<T>(params int[] indice)
476+
{
477+
throw new NotImplementedException();
478+
}
497479
}
498480
}

src/NumSharp.Core/Backends/IStorage.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ public interface IStorage : ICloneable
7070
/// </summary>
7171
/// <returns>reference to cloned storage as System.Array</returns>
7272
Array CloneData();
73-
/// <summary>
74-
/// Get reference to internal data storage and cast elements to new dtype
75-
/// </summary>
76-
/// <param name="dtype">new storage data type</param>
77-
/// <returns>reference to internal (casted) storage as System.Array </returns>
78-
Array GetData(Type dtype);
7973

8074
/// <summary>
8175
/// Get reference to internal data storage and cast elements to new dtype
@@ -92,6 +86,8 @@ public interface IStorage : ICloneable
9286
/// <returns>element from internal storage</returns>
9387
T GetData<T>(params int[] indice);
9488

89+
Span<T> GetSpanData<T>(params int[] indice);
90+
9591
bool GetBoolean(params int[] indice);
9692
short GetInt16(params int[] indice);
9793
int GetInt32(params int[] indice);

src/NumSharp.Core/Backends/NDArray.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public partial class NDArray : ICloneable, IEnumerable
5959

6060
public Slice slice => Storage.Slice;
6161

62+
public int[] strides => Storage.Shape.Strides;
63+
6264
/// <summary>
6365
/// The internal storage for elements of NDArray
6466
/// </summary>
@@ -222,7 +224,7 @@ public NDArray view(Type dtype = null)
222224
if (dtype != null && dtype != this.dtype)
223225
Storage.SetData(Array, dtype);
224226

225-
var nd = new NDArray(Storage.View<int>().ToArray(), shape);
227+
var nd = new NDArray(Array, shape);
226228

227229
return nd;
228230
}

src/NumSharp.Core/Backends/TypedArrayStorage.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,6 @@ public void Allocate<T>(T[] values)
232232
_DType = elementType;
233233
}
234234

235-
/// <summary>
236-
/// Get Back Storage with Columnwise tensor Layout
237-
/// By this method the layout is changed if layout is not columnwise
238-
/// </summary>
239-
/// <returns>reference to storage (transformed or not)</returns>
240-
public IStorage GetColumWiseStorage()
241-
{
242-
//if ( _TensorLayout != 2 )
243-
//this._ChangeRowToColumnLayout();
244-
245-
return this;
246-
}
247-
248235
/// <summary>
249236
/// Get reference to internal data storage
250237
/// </summary>
@@ -287,19 +274,6 @@ public Array CloneData()
287274
return GetData().Clone() as Array;
288275
}
289276

290-
/// <summary>
291-
/// Get reference to internal data storage and cast elements to new dtype
292-
/// </summary>
293-
/// <param name="dtype">new storage data type</param>
294-
/// <returns>reference to internal (casted) storage as System.Array </returns>
295-
public Array GetData(Type dtype)
296-
{
297-
var methods = this.GetType().GetMethods().Where(x => x.Name.Equals("GetData") && x.IsGenericMethod && x.ReturnType.Name.Equals("T[]"));
298-
var genMethods = methods.First().MakeGenericMethod(dtype);
299-
300-
return (Array) genMethods.Invoke(this,null);
301-
}
302-
303277
/// <summary>
304278
/// Get reference to internal data storage and cast elements to new dtype
305279
/// </summary>
@@ -413,6 +387,9 @@ public void SetData(Array values)
413387
case "String":
414388
_arrayString = values as string[];
415389
break;
390+
case "NDArray":
391+
_arrayNDArray = values as NDArray[];
392+
break;
416393
case "Object":
417394
_arrayObject = values as object[];
418395
break;
@@ -577,7 +554,7 @@ public object Clone()
577554
{
578555
var puffer = new ArrayStorage(_DType);
579556
puffer.Allocate(new Shape(_Shape.Dimensions));
580-
puffer.SetData((Array)GetData(_DType).Clone());
557+
puffer.SetData((Array)GetData().Clone());
581558

582559
return puffer;
583560
}
@@ -598,5 +575,14 @@ public Span<T> View<T>(Slice slice = null)
598575
return GetData<T>().AsSpan(slice.Start.Value * offset, slice.Length.Value * offset);
599576
}
600577
}
578+
579+
public Span<T> GetSpanData<T>(params int[] indice)
580+
{
581+
int stride = Shape.Strides[indice.Length - 1];
582+
int idx = Shape.GetIndexInShape(indice);
583+
int offset = idx + (Slice is null ? 0 : Slice.Start.Value) * stride;
584+
585+
return GetData<T>().AsSpan(offset, stride);
586+
}
601587
}
602588
}

src/NumSharp.Core/Casting/Implicit/NdArray.Implicit.Array.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ public static implicit operator NDArray(string str)
9191

9292
var shape = new Shape(new int[] { dim0, dim1 });
9393

94-
nd = new NDArray(typeof(double));
95-
96-
nd.Storage.Allocate(shape);
94+
nd = new NDArray(typeof(double), shape);
9795

9896
for (int idx = 0; idx < splitted.Length; idx++)
9997
{

src/NumSharp.Core/Casting/Implicit/NdArray.Implicit.ValueTypes.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public static implicit operator float(NDArray nd)
6666
if (nd.ndim > 0)
6767
throw new IncorrectShapeException();
6868

69-
return nd.Data<float>(0);
69+
if (nd.dtype.Name == "NDArray")
70+
return (nd.Array.GetValue(0) as NDArray).GetSingle(0);
71+
else
72+
return nd.GetSingle(0);
7073
}
7174

7275
public static implicit operator NDArray(double d)

src/NumSharp.Core/Casting/NdArray.ToString.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,24 @@ protected string _ToVectorString()
3030

3131
string[] dataParsed = new string[size];
3232

33-
Array strg = Storage.View<int>().ToArray();
33+
Array strg = null;
34+
switch (Type.GetTypeCode(dtype))
35+
{
36+
case TypeCode.Int32:
37+
strg = Storage.View<int>().ToArray();
38+
break;
39+
case TypeCode.Int64:
40+
strg = Storage.View<long>().ToArray();
41+
break;
42+
case TypeCode.Single:
43+
strg = Storage.View<float>().ToArray();
44+
break;
45+
case TypeCode.Double:
46+
strg = Storage.View<double>().ToArray();
47+
break;
48+
default:
49+
break;
50+
}
3451

3552
for (int idx = 0; idx < dataParsed.Length;idx++)
3653
dataParsed[idx] = _ParseNumber(strg.GetValue(idx),ref digitBefore, ref digitAfter);

src/NumSharp.Core/Creation/NdArray.HStack.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public NDArray hstack<T>(params NDArray[] nps)
4848
for (int k = 0; k < npAll.Length; k++)
4949
{
5050
var pufferShape = new Shape(npAll[k].shape);
51-
int pageSize = npAll[k].ndim == 1 ? npAll[k].shape[0] : pufferShape.DimOffset[0];
51+
int pageSize = npAll[k].ndim == 1 ? npAll[k].shape[0] : pufferShape.Strides[0];
5252
for (int j = i * pageSize; j < (i + 1) * pageSize; j++)
5353
{
5454
var ele = npAll[k].Data<T>()[j];

src/NumSharp.Core/Selection/NDArray.Indexing.cs

Lines changed: 21 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -183,102 +183,41 @@ public NDArray this[NDArray<bool> booleanArray]
183183
/// <returns>element from internal storage</returns>
184184
private NDArray GetData(params int[] indice)
185185
{
186-
int offset = 0;
187-
188186
Shape s1 = shape.Skip(indice.Length).ToArray();
189-
int idx = s1.GetIndexInShape(indice);
190-
191-
int stride = Shape.GetSize(s1.Dimensions);
192-
193-
if (ndim == 1)
194-
offset = idx + (slice is null ? 0 : slice.Start.Value);
195-
else
196-
offset = idx + (slice is null ? 0 : slice.Start.Value) * stride;
197-
187+
var nd = new NDArray(dtype, s1);
188+
//nd.Storage.Slice = new Slice($"{}");
198189
switch (Type.GetTypeCode(dtype))
199190
{
200191
case TypeCode.Boolean:
201-
return GetBoolean(indice);
192+
nd.Array = Storage.GetSpanData<bool>(indice).ToArray();
193+
break;
202194
case TypeCode.Int16:
203-
return GetInt16(indice);
195+
nd.Array = Storage.GetSpanData<short>(indice).ToArray();
196+
break;
204197
case TypeCode.Int32:
205-
return GetInt32(indice);
198+
nd.Array = Storage.GetSpanData<int>(indice).ToArray();
199+
break;
206200
case TypeCode.Int64:
207-
return GetInt64(indice);
201+
nd.Array = Storage.GetSpanData<long>(indice).ToArray();
202+
break;
208203
case TypeCode.Single:
209-
return GetSingle(indice);
204+
nd.Array = Storage.GetSpanData<float>(indice).ToArray();
205+
break;
210206
case TypeCode.Double:
211-
return GetDouble(indice);
207+
nd.Array = Storage.GetSpanData<double>(indice).ToArray();
208+
break;
212209
case TypeCode.Decimal:
213-
return GetDecimal(indice);
210+
nd.Array = Storage.GetSpanData<decimal>(indice).ToArray();
211+
break;
214212
case TypeCode.String:
215-
return GetString(indice);
213+
nd.Array = Storage.GetSpanData<string>(indice).ToArray();
214+
break;
216215
default:
217-
return GetNDArray(indice);
218-
}
219-
220-
/*if (indexes.Length == ndim ||
221-
shape.Last() == 1)
222-
{
223-
switch (Type.GetTypeCode(dtype))
224-
{
225-
case TypeCode.Boolean:
226-
return GetBoolean(indexes);
227-
case TypeCode.Int16:
228-
return GetInt16(indexes);
229-
case TypeCode.Int32:
230-
return GetInt32(indexes);
231-
case TypeCode.Int64:
232-
return GetInt64(indexes);
233-
case TypeCode.Single:
234-
return GetSingle(indexes);
235-
case TypeCode.Double:
236-
return GetDouble(indexes);
237-
case TypeCode.Decimal:
238-
return GetDecimal(indexes);
239-
case TypeCode.String:
240-
return GetString(indexes);
241-
default:
242-
return GetNDArray(indexes);
243-
}
216+
nd.Array = Storage.GetSpanData<NDArray>(indice).ToArray();
217+
break;
244218
}
245-
else if (indexes.Length == ndim - 1)
246-
{
247-
var offset = new int[ndim];
248-
for (int i = 0; i < ndim - 1; i++)
249-
offset[i] = indexes[i];
250-
251-
var nd = new NDArray(dtype, shape[ndim - 1]);
252-
var data = GetData();
253-
for (int i = 0; i < shape[ndim - 1]; i++)
254-
{
255-
offset[offset.Length - 1] = i;
256-
//nd.SetData(data.GetValue(Shape.GetIndexInShape(offset)), i);
257-
}
258219

259-
return nd;
260-
}
261-
// 3 Dim
262-
else if (indexes.Length == ndim - 2)
263-
{
264-
var offset = new int[ndim];
265-
var nd = new NDArray(dtype, new int[] { shape[ndim - 2], shape[ndim - 1] });
266-
var data = GetData();
267-
for (int i = 0; i < shape[ndim - 2]; i++)
268-
{
269-
for (int j = 0; j < shape[ndim - 1]; j++)
270-
{
271-
offset[0] = 0;
272-
offset[1] = i;
273-
offset[2] = j;
274-
//nd.SetData(data.GetValue(Shape.GetIndexInShape(offset)), i, j);
275-
}
276-
}
277-
278-
return nd;
279-
}*/
280-
281-
throw new Exception("NDStorage.GetData");
220+
return nd;
282221
}
283222
}
284223
}

src/NumSharp.Core/Selection/NdArray.AMax.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private NDArray amax<T>(int? axis = null) where T : struct
8686

8787
//Fill in data
8888
index = 0; //index for result data set
89-
int sameSetOffset = Storage.Shape.DimOffset[axis.Value];
89+
int sameSetOffset = Storage.Shape.Strides[axis.Value];
9090
int increments = cur * post;
9191
switch (dtype.Name)
9292
{

0 commit comments

Comments
 (0)