Skip to content

Commit a0fc321

Browse files
committed
Merge branch 'master' of https://github.com/SciSharp/NumSharp
2 parents 69ca992 + 9d1bee2 commit a0fc321

File tree

5 files changed

+85
-29
lines changed

5 files changed

+85
-29
lines changed

src/NumSharp.Core/Backends/NDArray.String.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ public static string AsString(NDArray arr)
3232
}
3333
}
3434

35+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
36+
public static string[] AsStringArray(NDArray arr)
37+
{
38+
var chars = arr.ToString();
39+
int pos = chars.IndexOf(' ');
40+
int numOfRows = int.Parse(chars.Substring(0, pos));
41+
int posOfContent = chars.IndexOf(':');
42+
string contents = chars.Substring(posOfContent + 1);
43+
int[] strLengths = chars.Substring(pos + 1, posOfContent - pos - 1)
44+
.Split(' ')
45+
.Select(x => int.Parse(x))
46+
.ToArray();
47+
string[] strArray = new string[numOfRows];
48+
int currentPos = 0;
49+
for (int i = 0; i < numOfRows; i++)
50+
{
51+
strArray[i] = contents.Substring(currentPos, strLengths[i]);
52+
currentPos += strLengths[i];
53+
}
54+
55+
return strArray;
56+
}
57+
3558
/// <summary>
3659
/// Get a string out of a vector of chars.
3760
/// </summary>

src/NumSharp.Core/Creation/np.array.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,6 @@ public static NDArray array(Array array, Type dtype = null, int ndmin = 1, bool
4949

5050
public static NDArray array<T>(params T[] data) where T : unmanaged => new NDArray(ArraySlice.FromArray(data), Shape.Vector(data.Length));
5151

52-
/// <summary>
53-
/// Create a vector ndarray of type <see cref="char"/>.
54-
/// </summary>
55-
/// <param name="chars"></param>
56-
/// <returns></returns>
57-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
58-
public static NDArray array(string chars)
59-
{
60-
if (chars == null)
61-
throw new ArgumentNullException(nameof(chars));
62-
if (chars.Length == 0)
63-
return new NDArray(NPTypeCode.Char, 0);
64-
65-
unsafe
66-
{
67-
var ret = new ArraySlice<char>(new UnmanagedMemoryBlock<char>(chars.Length));
68-
fixed (char* strChars = chars)
69-
{
70-
var src = strChars;
71-
var dst = ret.Address;
72-
var len = sizeof(char) * chars.Length;
73-
Buffer.MemoryCopy(src, dst, len, len);
74-
}
75-
76-
return new NDArray(ret);
77-
}
78-
}
79-
8052
public static NDArray array<T>(T[][] data)
8153
{
8254
var array = data.SelectMany(inner => inner).ToArray(); //todo! not use selectmany.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
using System.Text;
5+
using NumSharp.Backends;
6+
using NumSharp.Backends.Unmanaged;
7+
8+
namespace NumSharp
9+
{
10+
public static partial class np
11+
{
12+
/// <summary>
13+
/// Create a vector ndarray of type <see cref="string"/>.
14+
/// Encode string array.
15+
/// format: [numOfRow lenOfRow1 lenOfRow2 contents]
16+
/// sample: [2 2 4 aacccc]
17+
/// </summary>
18+
/// <param name="strArray"></param>
19+
/// <returns></returns>
20+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21+
public static NDArray array(string[] strArray)
22+
{
23+
if (strArray == null)
24+
throw new ArgumentNullException(nameof(strArray));
25+
if (strArray.Length == 0)
26+
return new NDArray(NPTypeCode.String, 0);
27+
28+
// convert to bytes
29+
string meta = $"{strArray.Length}";
30+
foreach (var str in strArray)
31+
meta += $" {str.Length}";
32+
meta += $":{string.Join("", strArray)}";
33+
return new NDArray(meta.ToCharArray());
34+
}
35+
}
36+
}

src/NumSharp.Core/NumSharp.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<Version>0.20.0</Version>
3737
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
3838
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
39-
<SignAssembly>true</SignAssembly>
39+
<SignAssembly>false</SignAssembly>
4040
<AssemblyOriginatorKeyFile>Open.snk</AssemblyOriginatorKeyFile>
4141
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
4242
<Platforms>AnyCPU;x64</Platforms>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Linq;
3+
using FluentAssertions;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using NumSharp.UnitTest.Utilities;
6+
7+
namespace NumSharp.UnitTest.Backends.Unmanaged
8+
{
9+
[TestClass]
10+
public class StringArrayApiTests
11+
{
12+
private static string[] strArray = new string[] { "Hello,", " SciSharp Team!"};
13+
14+
[TestMethod]
15+
public void StringArrayConverting()
16+
{
17+
var nd = np.array(strArray);
18+
nd.Should().BeOfType<char>()
19+
.And.BeShaped(28)
20+
.And.BeOfValues(50, 32, 54, 32, 49, 53, 58, 72, 101, 108, 108, 111, 44, 32, 83, 99, 105, 83, 104, 97, 114, 112, 32, 84, 101, 97, 109, 33);
21+
22+
NDArray.AsStringArray(nd).SequenceEqual(strArray);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)