Skip to content

Commit e49d8e8

Browse files
committed
Some Span<T>.DangerousCreate calls removed
1 parent e9b9d85 commit e49d8e8

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

NeuralNetwork.NET/APIs/Structs/DatasetSample.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public readonly ref struct DatasetSample
1010
/// <summary>
1111
/// Gets the <see cref="Span{T}"/> referencing the current sample inputs
1212
/// </summary>
13-
public Span<float> X { get; }
13+
public ReadOnlySpan<float> X { get; }
1414

1515
/// <summary>
1616
/// Gets the <see cref="Span{T}"/> referencing the current sample expected outputs
1717
/// </summary>
18-
public Span<float> Y { get; }
18+
public ReadOnlySpan<float> Y { get; }
1919

20-
internal DatasetSample(Span<float> x, Span<float> y)
20+
internal DatasetSample(ReadOnlySpan<float> x, ReadOnlySpan<float> y)
2121
{
2222
X = x;
2323
Y = y;

NeuralNetwork.NET/Extensions/SpanExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ public static unsafe int GetContentHashCode<T>(this Span<T> span) where T : unma
8282
/// </summary>
8383
/// <param name="m">The source matrix</param>
8484
/// <param name="row">The target row to return</param>
85-
[Pure]
85+
[Pure, NotNull]
8686
[CollectionAccess(CollectionAccessType.Read)]
87-
public static Span<T> Slice<T>([NotNull] this T[,] m, int row) where T : struct
87+
public static unsafe T[] Slice<T>([NotNull] this T[,] m, int row) where T : unmanaged
8888
{
8989
if (row < 0 || row > m.GetLength(0) - 1) throw new ArgumentOutOfRangeException(nameof(row), "The row index isn't valid");
90-
return Span<T>.DangerousCreate(m, ref m[row, 0], m.GetLength(1));
90+
int wm = m.GetLength(1);
91+
fixed (T* p = m) return new Span<T>(p + row * wm, wm).ToArray();
9192
}
9293

9394
#endregion

NeuralNetwork.NET/SupervisedLearning/Data/BatchesCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public void Expand(params Func<float[], float[]>[] factories)
105105
for (int i = 0; i < n; i++)
106106
{
107107
float[]
108-
x = b.X.Slice(i).ToArray(),
109-
y = b.Y.Slice(i).ToArray();
108+
x = b.X.Slice(i),
109+
y = b.Y.Slice(i);
110110
yield return () => (x, y);
111111
foreach (Func<float[], float[]> f in factories)
112112
yield return () => (f(x), y);

0 commit comments

Comments
 (0)