-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTensorSpan.cs
More file actions
112 lines (95 loc) · 3.18 KB
/
TensorSpan.cs
File metadata and controls
112 lines (95 loc) · 3.18 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System;
namespace TensorStack.Common.Tensor
{
/// <summary>
/// TensorSpan for short lifespan operations (stack allocated)
/// </summary>
/// <typeparam name="T"></typeparam>
public readonly ref struct TensorSpan<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="TensorSpan{T}"/> struct.
/// </summary>
/// <param name="dataSpan">The data span.</param>
/// <param name="dimensions">The dimensions.</param>
public TensorSpan(Span<T> dataSpan, ReadOnlySpan<int> dimensions)
{
Span = dataSpan;
Dimensions = dimensions;
Strides = dimensions.GetStrides();
}
public TensorSpan(ReadOnlySpan<int> dimensions)
: this(new T[dimensions.GetProduct()], dimensions) { }
/// <summary>
/// Gets element type.
/// </summary>
public Type Type => typeof(T);
/// <summary>
/// Gets the data span.
/// </summary>
public Span<T> Span { get; }
/// <summary>
/// Gets the length.
/// </summary>
public long Length => Span.Length;
/// <summary>
/// Gets the strides.
/// </summary>
public ReadOnlySpan<int> Strides { get; }
/// <summary>
/// Gets the dimensions.
/// </summary>
public ReadOnlySpan<int> Dimensions { get; }
/// <summary>
/// Gets or sets the <see cref="T"/> with the specified indices.
/// </summary>
/// <param name="indices">The indices.</param>
/// <returns>T.</returns>
/// <exception cref="ArgumentNullException"></exception>
public T this[params int[] indices]
{
get
{
ArgumentNullException.ThrowIfNull(indices);
var span = new ReadOnlySpan<int>(indices);
return this[span];
}
set
{
ArgumentNullException.ThrowIfNull(indices);
var span = new ReadOnlySpan<int>(indices);
this[span] = value;
}
}
/// <summary>
/// Gets or sets the <see cref="T"/> with the specified indices.
/// </summary>
/// <param name="indices">The indices.</param>
/// <returns>T.</returns>
public T this[ReadOnlySpan<int> indices]
{
get { return GetValue(indices.GetIndex(Strides)); }
set { SetValue(indices.GetIndex(Strides), value); }
}
/// <summary>
/// Gets the value with the specified index.
/// </summary>
/// <param name="index">The index.</param>
/// <returns>T.</returns>
public T GetValue(int index)
{
return Span[index];
}
/// <summary>
/// Sets the value with the specified index.
/// </summary>
/// <param name="index">The index.</param>
/// <param name="value">The value.</param>
public void SetValue(int index, T value)
{
Span[index] = value;
}
}
}