Skip to content

Commit 24e7895

Browse files
author
dotchris90
committed
Feat : Add Shape Test + Compution indexes by 1D array index
1 parent 818427d commit 24e7895

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/NumSharp.Core/Shape.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public partial class Shape
88
{
99
private readonly IReadOnlyList<int> shape;
1010
private readonly IReadOnlyList<int> dimOffset;
11+
private readonly int dimOffsetTotal;
1112

1213
public int Size
1314
{
@@ -37,6 +38,7 @@ public Shape(params int[] shape)
3738
{
3839
temp[i - 1] = temp[i] * shape[i];
3940
}
41+
4042
dimOffset = temp;
4143
}
4244

@@ -67,6 +69,30 @@ public int GetIndexInShape(params int[] select)
6769

6870
return idx;
6971
}
72+
public int[] GetDimIndexOutShape(int select)
73+
{
74+
int[] dimIndexes = null;
75+
if (this.dimOffset.Count == 1)
76+
dimIndexes = new int[] {select};
77+
else if (this.dimOffset.Count == 2)
78+
{
79+
dimIndexes = new int[dimOffset.Count];
80+
81+
int remaining = select;
82+
83+
for (int idx = 0;idx < dimOffset.Count;idx++)
84+
{
85+
dimIndexes[idx] = remaining / dimOffset[idx];
86+
remaining -= (dimIndexes[idx] * dimOffset[idx] );
87+
}
88+
}
89+
else
90+
{
91+
throw new IncorrectShapeException();
92+
}
93+
94+
return dimIndexes;
95+
}
7096

7197
public int UniShape => shape[0];
7298

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using NumSharp.Core.Extensions;
6+
using System.Linq;
7+
using NumSharp.Core;
8+
9+
namespace NumSharp.UnitTest
10+
{
11+
[TestClass]
12+
public class NDStorageTest
13+
{
14+
[TestMethod]
15+
public void Index()
16+
{
17+
var shape0 = new Shape(4,3);
18+
19+
int idx0 = shape0.GetIndexInShape(2,1);
20+
int[] idx00 = shape0.GetDimIndexOutShape(idx0);
21+
22+
Assert.IsTrue(idx00[0] == 2);
23+
Assert.IsTrue(idx00[1] == 1);
24+
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)