-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIndex3.cs
228 lines (181 loc) · 7.51 KB
/
Index3.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System.Runtime.Serialization;
namespace System
{
/// <summary>
/// Represents an index of the 3D variable size array
/// </summary>
[Serializable]
public readonly struct Index3 : IEquatableReadOnlyStruct<Index3>, IComparableReadOnlyStruct<Index3>, ISerializable
{
public readonly int A;
public readonly int B;
public readonly int C;
public int this[int index]
{
get
{
if (index == 0) return this.A;
if (index == 1) return this.B;
if (index == 2) return this.C;
throw new IndexOutOfRangeException();
}
}
public Index3(int a, int b, int c)
{
this.A = a;
this.B = b;
this.C = c;
}
public void Deconstruct(out int a, out int b, out int c)
{
a = this.A;
b = this.B;
c = this.C;
}
public Index3 With(int? A = null, int? B = null, int? C = null)
=> new Index3(
A ?? this.A,
B ?? this.B,
C ?? this.C
);
/// <summary>
/// Converts to 1D index.
/// </summary>
/// <param name="lengthA">Length of the dimension <see cref="A"/> of the 3D array.</param>
/// <param name="lengthB">Length of the dimension <see cref="B"/> of the 3D array.</param>
/// <returns>
/// <para>If any length is less than or equal to zero, returns zero.</para>
/// <para>Otherwise, returns the converted value.</para>
/// </returns>
public int ToIndex1(int lengthA, int lengthB)
{
if (lengthA <= 0 || lengthB <= 0)
return 0;
return this.A + (this.B * lengthA) + (this.C * lengthA * lengthB);
}
public int ToIndex1(in Length2 length)
{
if (length.A <= 0 || length.B <= 0)
return 0;
return this.A + (this.B * length.A) + (this.C * length.A * length.B);
}
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.A, this.B, this.C);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = 793064651;
hashCode = hashCode * -1521134295 + this.A.GetHashCode();
hashCode = hashCode * -1521134295 + this.B.GetHashCode();
hashCode = hashCode * -1521134295 + this.C.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override bool Equals(object obj)
=> obj is Index3 other &&
this.A == other.A &&
this.B == other.B &&
this.C == other.C;
public int CompareTo(Index3 other)
{
var comp = this.A.CompareTo(other.A);
if (comp != 0)
return comp;
var comp1 = this.B.CompareTo(other.B);
if (comp1 == 0)
return this.C.CompareTo(other.C);
return comp1;
}
public int CompareTo(in Index3 other)
{
var comp = this.A.CompareTo(other.A);
if (comp != 0)
return comp;
var comp1 = this.B.CompareTo(other.B);
if (comp1 == 0)
return this.C.CompareTo(other.C);
return comp1;
}
public bool Equals(Index3 other)
=> this.A == other.A && this.B == other.B && this.C == other.C;
public bool Equals(in Index3 other)
=> this.A == other.A && this.B == other.B && this.C == other.C;
public override string ToString()
=> $"({this.A}, {this.B}, {this.C})";
private Index3(SerializationInfo info, StreamingContext context)
{
this.A = info.GetInt32OrDefault(nameof(this.A));
this.B = info.GetInt32OrDefault(nameof(this.B));
this.C = info.GetInt32OrDefault(nameof(this.C));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.A), this.A);
info.AddValue(nameof(this.B), this.B);
info.AddValue(nameof(this.C), this.C);
}
/// <summary>
/// Shorthand for writing <see cref="Index3"/>(0, 0, 0).
/// </summary>
public static Index3 Zero { get; } = new Index3(0, 0, 0);
public static implicit operator Index3(in (int, int, int) value)
=> new Index3(value.Item1, value.Item2, value.Item3);
public static Index3 operator +(in Index3 lhs, in Index3 rhs)
=> new Index3(lhs.A + rhs.A, lhs.B + rhs.B, lhs.C + rhs.C);
public static Index3 operator -(in Index3 lhs, in Index3 rhs)
=> new Index3(lhs.A - rhs.A, lhs.B - rhs.B, lhs.C - rhs.C);
public static Index3 operator -(in Index3 a)
=> new Index3(-a.A, -a.B, -a.C);
public static Index3 operator *(in Index3 lhs, int rhs)
=> new Index3(lhs.A * rhs, lhs.B * rhs, lhs.C * rhs);
public static Index3 operator *(int lhs, in Index3 rhs)
=> new Index3(lhs * rhs.A, lhs * rhs.B, lhs * rhs.C);
public static Index3 operator *(in Index3 lhs, in Index3 rhs)
=> new Index3(lhs.A * rhs.A, lhs.B * rhs.B, lhs.C * rhs.C);
public static Index3 operator /(in Index3 lhs, int rhs)
=> new Index3(lhs.A / rhs, lhs.B / rhs, lhs.C / rhs);
public static Index3 operator /(in Index3 lhs, in Index3 rhs)
=> new Index3(lhs.A / rhs.A, lhs.B / rhs.B, lhs.C / rhs.C);
public static Index3 operator %(in Index3 lhs, int rhs)
=> new Index3(lhs.A % rhs, lhs.B % rhs, lhs.C % rhs);
public static Index3 operator %(in Index3 lhs, in Index3 rhs)
=> new Index3(lhs.A % rhs.A, lhs.B % rhs.B, lhs.C % rhs.C);
public static bool operator ==(in Index3 lhs, in Index3 rhs)
=> lhs.A == rhs.A && lhs.B == rhs.B && lhs.C == rhs.C;
public static bool operator !=(in Index3 lhs, in Index3 rhs)
=> lhs.A != rhs.A || lhs.B != rhs.B || lhs.C != rhs.C;
/// <summary>
/// Converts 1D index to 3D index.
/// </summary>
/// <param name="index1">1D index.</param>
/// <param name="lengthA">Length of the dimension <see cref="A"/> of the 3D array.</param>
/// <param name="lengthB">Length of the dimension <see cref="B"/> of the 3D array.</param>
/// <returns>
/// <para>If any length is less than or equal to zero, returns <see cref="Zero"/>.</para>
/// <para>Otherwise, returns the converted value.</para>
/// </returns>
public static Index3 Convert(int index1, int lengthA, int lengthB)
{
if (lengthA <= 0 || lengthB <= 0)
return Zero;
var alengthB = lengthA * lengthB;
var c = index1 / alengthB;
var i_ab = index1 - (c * alengthB);
var b = i_ab / lengthA;
var a = i_ab % lengthA;
return new Index3(a, b, c);
}
public static Index3 Convert(int index1, in Length2 length)
{
if (length.A <= 0 || length.B <= 0)
return Zero;
var alengthB = length.A * length.B;
var c = index1 / alengthB;
var i_ab = index1 - (c * alengthB);
var b = i_ab / length.A;
var a = i_ab % length.A;
return new Index3(a, b, c);
}
}
}