-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTensor.Enumerator.cs
More file actions
76 lines (66 loc) · 2.06 KB
/
Copy pathTensor.Enumerator.cs
File metadata and controls
76 lines (66 loc) · 2.06 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
using System.Collections;
namespace Tensornet{
public class TensorEnumerator<T> : IEnumerator, IEnumerator<T> where T : struct, IEquatable<T>, IConvertible{
private readonly Tensor<T> _tensor;
private int _index;
private T _current;
private int[] indices;
internal TensorEnumerator(Tensor<T> tensor)
{
_tensor = tensor;
_index = 0;
_current = default;
indices = new int[4] { 0, 0, 0, 0 };
}
public void Dispose()
{
}
public bool MoveNext()
{
Tensor<T> localTensor = _tensor;
if ((uint)_index < (uint)localTensor.TLayout.TotalElemCount())
{
_current = localTensor.AsSpan()[_index];
// _index++;
IndexIncrease();
return true;
}
return MoveNextRare();
}
private bool MoveNextRare()
{
_index = _tensor.TLayout.TotalElemCount() + 1;
_current = default;
return false;
}
private void IndexIncrease(){
indices[_tensor.TLayout.NDim - 1]++;
_index += _tensor.TLayout.Stride[_tensor.TLayout.NDim - 1];
for (int i = _tensor.TLayout.NDim - 1; i >= 1; i--){
if(indices[i] < _tensor.TLayout.Shape[i]) break;
else{
indices[i - 1]++;
_index = _index + _tensor.TLayout.Stride[i - 1] - _tensor.TLayout.Stride[i] * indices[i];
indices[i] = 0;
}
}
}
public T Current => _current;
object IEnumerator.Current
{
get
{
if (_index == 0 || _index == _tensor.TLayout.TotalElemCount() + 1)
{
throw new InvalidOperationException();
}
return Current;
}
}
void IEnumerator.Reset()
{
_index = 0;
_current = default;
}
}
}