|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using NumSharp.Backends; |
| 7 | +using NumSharp.Backends.Unmanaged; |
| 8 | + |
| 9 | +namespace NumSharp |
| 10 | +{ |
| 11 | + public partial class NDArray |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Converts a string to a vector ndarray of bytes. |
| 15 | + /// </summary> |
| 16 | + /// <param name="str"></param> |
| 17 | + /// <returns></returns> |
| 18 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 19 | + public static NDArray FromString(string str) => np.array(str); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Converts the entire <see cref="NDArray"/> to a string. |
| 23 | + /// </summary> |
| 24 | + /// <remarks>Performs a copy due to String .net-framework limitations.</remarks> |
| 25 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 26 | + public static string AsString(NDArray arr) |
| 27 | + { |
| 28 | + unsafe |
| 29 | + { |
| 30 | + Debug.Assert(arr.typecode == NPTypeCode.Char); |
| 31 | + return new string((char*)arr.Address, 0, arr.size); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Get a string out of a vector of chars. |
| 37 | + /// </summary> |
| 38 | + /// <param name="indices"></param> |
| 39 | + /// <returns></returns> |
| 40 | + /// <remarks>Performs a copy due to String .net-framework limitations.</remarks> |
| 41 | + public string GetString(params int[] indices) |
| 42 | + { |
| 43 | + unsafe |
| 44 | + { |
| 45 | + if (Shape.dimensions.Length - 1 != indices.Length) |
| 46 | + throw new ArgumentOutOfRangeException(nameof(indices), "GetString(int[]) can only accept coordinates that point to a vector of chars."); |
| 47 | + |
| 48 | + Debug.Assert(typecode == NPTypeCode.Char); |
| 49 | + |
| 50 | + UnmanagedStorage arr = Storage.GetData(indices); |
| 51 | + Debug.Assert(arr.Shape.NDim == 1); |
| 52 | + |
| 53 | + if (!Shape.IsContagious) |
| 54 | + { |
| 55 | + //this works faster than cloning. |
| 56 | + var ret = new string('\0', arr.Count); |
| 57 | + fixed (char* retChars = ret) |
| 58 | + { |
| 59 | + var dst = new UnmanagedStorage(new ArraySlice<char>(new UnmanagedMemoryBlock<char>(retChars, ret.Length)), arr.Shape.Clean()); |
| 60 | + MultiIterator.Assign(dst, arr); |
| 61 | + } |
| 62 | + |
| 63 | + return ret; |
| 64 | + } |
| 65 | + |
| 66 | + //new string always performs a copy, there is no need to keep reference to arr's unmanaged storage. |
| 67 | + return new string((char*)arr.Address, 0, arr.Count); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public void SetString(string value, params int[] indices) |
| 72 | + { |
| 73 | + Debug.Assert(typecode == NPTypeCode.Char); |
| 74 | + |
| 75 | + // ReSharper disable once ReplaceWithStringIsNullOrEmpty |
| 76 | + if (value == null || value.Length == 0) |
| 77 | + throw new ArgumentException("Value cannot be null or empty.", nameof(value)); |
| 78 | + |
| 79 | + if (Shape.dimensions.Length - 1 != indices.Length) |
| 80 | + throw new ArgumentOutOfRangeException(nameof(indices), "SetString(string, int[] indices) can only accept coordinates that point to a vector of chars."); |
| 81 | + |
| 82 | + unsafe |
| 83 | + { |
| 84 | + if (Shape.IsContagious) |
| 85 | + { |
| 86 | + var dst = (char*)Address + Shape.GetOffset(indices); |
| 87 | + fixed (char* strChars = value) |
| 88 | + { |
| 89 | + var src = strChars; |
| 90 | + Parallel.For(0, value.Length, i => *(dst + i) = *(src + i)); |
| 91 | + } |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + fixed (char* strChars = value) |
| 96 | + { |
| 97 | + SetData(new ArraySlice<char>(new UnmanagedMemoryBlock<char>(strChars, value.Length)), indices); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Get a string out of a vector of chars. |
| 105 | + /// </summary> |
| 106 | + /// <remarks>Performs a copy due to String .net-framework limitations.</remarks> |
| 107 | + public string GetStringAt(int offset) |
| 108 | + { |
| 109 | + Debug.Assert(typecode == NPTypeCode.Char); |
| 110 | + Debug.Assert(offset < Array.Count); |
| 111 | + |
| 112 | + return GetString(Shape.GetCoordinates(offset)); |
| 113 | + } |
| 114 | + |
| 115 | + public void SetStringAt(string value, int offset) |
| 116 | + { |
| 117 | + Debug.Assert(typecode == NPTypeCode.Char); |
| 118 | + Debug.Assert(offset < Array.Count); |
| 119 | + |
| 120 | + SetString(value, Shape.GetCoordinates(offset)); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments