Skip to content

Commit a83a3f6

Browse files
committed
Added Arrays.Insert and Arrays.AppendAt
1 parent c2101f1 commit a83a3f6

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/NumSharp.Core/Utilities/Arrays.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,34 @@ namespace NumSharp.Utilities
1212
{
1313
public static class Arrays
1414
{
15+
/// <summary>
16+
/// Inserts item into a specific index.
17+
/// </summary>
18+
/// <param name="source">The array to insert the value to.</param>
19+
/// <param name="index">The index to insert to.</param>
20+
/// <returns></returns>
21+
public static void Insert<T>(ref T[] source, int index, T value)
22+
{
23+
if (index < 0 || index >= source.Length)
24+
throw new ArgumentOutOfRangeException(nameof(index));
25+
26+
Array.Resize(ref source, source.Length + 1);
27+
Array.Copy(source, index, source, index + 1, source.Length-index - 1);
28+
source[index] = value;
29+
}
30+
31+
/// <summary>
32+
/// Inserts item into a specific index.
33+
/// </summary>
34+
/// <param name="source">The array to insert copy and insert value to.</param>
35+
/// <param name="index">The index to insert to.</param>
36+
/// <returns>a copy of <see cref="source"/> with the appended value.</returns>
37+
public static T[] AppendAt<T>(T[] source, int index, T value)
38+
{
39+
var ret = (T[])source.Clone();
40+
Insert(ref source, index, value);
41+
return ret;
42+
}
1543
/// <summary>
1644
/// Removes a specific index from given array.
1745
/// </summary>

test/NumSharp.UnitTest/Utilities/ArraysTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,51 @@ public void Create_4()
3535
{
3636
Arrays.Create(NPTypeCode.NDArray, 1000).Should().BeOfType<NDArray[]>().And.HaveCount(1000);
3737
}
38+
39+
[TestMethod]
40+
public void Insert_0()
41+
{
42+
int index = 0;
43+
var a = Enumerable.Range(0, 10).ToArray();
44+
Arrays.Insert(ref a, index, 9);
45+
a[index].Should().Be(9);
46+
var l = a.ToList();
47+
l.RemoveAt(index);
48+
Enumerable.SequenceEqual(l, Enumerable.Range(0, 10)).Should().BeTrue();
49+
}
50+
[TestMethod]
51+
public void Insert_2()
52+
{
53+
int index = 2;
54+
var a = Enumerable.Range(0, 10).ToArray();
55+
Arrays.Insert(ref a, index, 9);
56+
a[index].Should().Be(9);
57+
var l = a.ToList();
58+
l.RemoveAt(index);
59+
Enumerable.SequenceEqual(l, Enumerable.Range(0, 10)).Should().BeTrue();
60+
}
61+
[TestMethod]
62+
public void Insert_5()
63+
{
64+
int index = 5;
65+
var a = Enumerable.Range(0, 10).ToArray();
66+
Arrays.Insert(ref a, index, 9);
67+
a[index].Should().Be(9);
68+
var l = a.ToList();
69+
l.RemoveAt(index);
70+
Enumerable.SequenceEqual(l, Enumerable.Range(0, 10)).Should().BeTrue();
71+
}
72+
[TestMethod]
73+
public void Insert_9()
74+
{
75+
int index = 9;
76+
var a = Enumerable.Range(0, 10).ToArray();
77+
Arrays.Insert(ref a, index, 3);
78+
a[index].Should().Be(3);
79+
var l = a.ToList();
80+
l.RemoveAt(index);
81+
Enumerable.SequenceEqual(l, Enumerable.Range(0, 10)).Should().BeTrue();
82+
}
83+
3884
}
3985
}

0 commit comments

Comments
 (0)