forked from SciSharp/NumSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.array.string.cs
More file actions
36 lines (34 loc) · 1.13 KB
/
np.array.string.cs
File metadata and controls
36 lines (34 loc) · 1.13 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
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using NumSharp.Backends;
using NumSharp.Backends.Unmanaged;
namespace NumSharp
{
public static partial class np
{
/// <summary>
/// Create a vector ndarray of type <see cref="string"/>.
/// Encode string array.
/// format: [numOfRow lenOfRow1 lenOfRow2 contents]
/// sample: [2 2 4 aacccc]
/// </summary>
/// <param name="strArray"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static NDArray array(string[] strArray)
{
if (strArray == null)
throw new ArgumentNullException(nameof(strArray));
if (strArray.Length == 0)
return new NDArray(NPTypeCode.String, 0);
// convert to bytes
string meta = $"{strArray.Length}";
foreach (var str in strArray)
meta += $" {str.Length}";
meta += $":{string.Join("", strArray)}";
return new NDArray(meta.ToCharArray());
}
}
}