Skip to content

Commit 6908ddd

Browse files
committed
Arrays: Added Arrays.Slice
1 parent 5a135d9 commit 6908ddd

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/NumSharp.Core/Utilities/Arrays.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,45 @@
44
using System.Numerics;
55
using System.Reflection;
66
using System.Runtime.CompilerServices;
7+
using System.Threading.Tasks;
78
using NumSharp.Backends;
89

910
namespace NumSharp.Utilities
1011
{
1112
public static class Arrays
1213
{
14+
/// <summary>
15+
/// Slice an array.
16+
/// </summary>
17+
/// <remarks>Supports negative <paramref name="end"/> index</remarks>
18+
public static T[] Slice<T>(this T[] source, int start, int end)
19+
{
20+
// Handles negative ends.
21+
if (end < 0) end = source.Length + end;
22+
var len = end - start;
23+
24+
// Return new array.
25+
var res = new T[len];
26+
Parallel.For(0, len, i => res[i] = source[i + start]);
27+
return res;
28+
}
29+
30+
/// <summary>
31+
/// Slice an array.
32+
/// </summary>
33+
/// <remarks>Supports negative <paramref name="end"/> index</remarks>
34+
public static T[] Slice<T>(this T[] source, long start, long end)
35+
{
36+
// Handles negative ends.
37+
if (end < 0) end = source.Length + end;
38+
var len = end - start;
39+
40+
// Return new array.
41+
var res = new T[len];
42+
Parallel.For(0, len, i => res[i] = source[i + start]);
43+
return res;
44+
}
45+
1346
/// <summary>
1447
/// Inserts item into a specific index.
1548
/// </summary>

0 commit comments

Comments
 (0)