forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex4.cs
268 lines (215 loc) · 9.36 KB
/
Index4.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System.Runtime.Serialization;
namespace System
{
/// <summary>
/// Represents an index of the 4D variable size array
/// </summary>
[Serializable]
public readonly struct Index4 : IEquatableReadOnlyStruct<Index4>, IComparableReadOnlyStruct<Index4>, ISerializable
{
public readonly int A;
public readonly int B;
public readonly int C;
public readonly int D;
public int this[int index]
{
get
{
switch (index)
{
case 0: return this.A;
case 1: return this.B;
case 2: return this.C;
case 3: return this.D;
default: throw new IndexOutOfRangeException();
}
}
}
public Index4(int a, int b, int c, int d)
{
this.A = a;
this.B = b;
this.C = c;
this.D = d;
}
public void Deconstruct(out int a, out int b, out int c, out int d)
{
a = this.A;
b = this.B;
c = this.C;
d = this.D;
}
public Index4 With(int? A = null, int? B = null, int? C = null, int? D = null)
=> new Index4(
A ?? this.A,
B ?? this.B,
C ?? this.C,
D ?? this.D
);
/// <summary>
/// Converts to 1D index.
/// </summary>
/// <param name="lengthA">Length of the dimension <see cref="A"/> of the 4D array.</param>
/// <param name="lengthB">Length of the dimension <see cref="B"/> of the 4D array.</param>
/// <param name="lengthC">Length of the dimension <see cref="C"/> of the 4D 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, int lengthC)
{
if (lengthA <= 0 || lengthB <= 0 || lengthC <= 0)
return 0;
var ab = lengthA * lengthB;
return this.A + (this.B * lengthA) + (this.C * ab) + (this.D * ab * lengthC);
}
public int ToIndex1(in Length3 length)
{
if (length.A <= 0 || length.B <= 0 || length.C <= 0)
return 0;
var ab = length.A * length.B;
return this.A + (this.B * length.A) + (this.C * ab) + (this.D * ab * length.C);
}
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.A, this.B, this.C, this.D);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = -1408250474;
hashCode = hashCode * -1521134295 + this.A.GetHashCode();
hashCode = hashCode * -1521134295 + this.B.GetHashCode();
hashCode = hashCode * -1521134295 + this.C.GetHashCode();
hashCode = hashCode * -1521134295 + this.D.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override bool Equals(object obj)
=> obj is Index4 other &&
this.A == other.A &&
this.B == other.B &&
this.C == other.C &&
this.D == other.D;
public int CompareTo(Index4 other)
{
var comp = this.A.CompareTo(other.A);
if (comp != 0)
return comp;
var comp1 = this.B.CompareTo(other.B);
if (comp1 != 0)
return comp1;
var comp2 = this.C.CompareTo(other.C);
if (comp2 == 0)
return this.D.CompareTo(other.D);
return comp2;
}
public int CompareTo(in Index4 other)
{
var comp = this.A.CompareTo(other.A);
if (comp != 0)
return comp;
var comp1 = this.B.CompareTo(other.B);
if (comp1 != 0)
return comp1;
var comp2 = this.C.CompareTo(other.C);
if (comp2 == 0)
return this.D.CompareTo(other.D);
return comp2;
}
public bool Equals(Index4 other)
=> this.A == other.A && this.B == other.B &&
this.C == other.C && this.D == other.D;
public bool Equals(in Index4 other)
=> this.A == other.A && this.B == other.B &&
this.C == other.C && this.D == other.D;
public override string ToString()
=> $"({this.A}, {this.B}, {this.C}, {this.D})";
private Index4(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));
this.D = info.GetInt32OrDefault(nameof(this.D));
}
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);
info.AddValue(nameof(this.D), this.D);
}
/// <summary>
/// Shorthand for writing <see cref="Index4"/>(0, 0, 0, 0).
/// </summary>
public static Index4 Zero { get; } = new Index4(0, 0, 0, 0);
public static implicit operator Index4(in (int, int, int, int) value)
=> new Index4(value.Item1, value.Item2, value.Item3, value.Item4);
public static Index4 operator +(in Index4 lhs, in Index4 rhs)
=> new Index4(lhs.A + rhs.A, lhs.B + rhs.B, lhs.C + rhs.C, lhs.D + rhs.D);
public static Index4 operator -(in Index4 lhs, in Index4 rhs)
=> new Index4(lhs.A - rhs.A, lhs.B - rhs.B, lhs.C - rhs.C, lhs.D - rhs.D);
public static Index4 operator -(in Index4 a)
=> new Index4(-a.A, -a.B, -a.C, -a.D);
public static Index4 operator *(in Index4 lhs, int rhs)
=> new Index4(lhs.A * rhs, lhs.B * rhs, lhs.C * rhs, lhs.D * rhs);
public static Index4 operator *(int lhs, in Index4 rhs)
=> new Index4(lhs * rhs.A, lhs * rhs.B, lhs * rhs.C, lhs * rhs.D);
public static Index4 operator *(in Index4 lhs, in Index4 rhs)
=> new Index4(lhs.A * rhs.A, lhs.B * rhs.B, lhs.C * rhs.C, lhs.D * rhs.D);
public static Index4 operator /(in Index4 lhs, int rhs)
=> new Index4(lhs.A / rhs, lhs.B / rhs, lhs.C / rhs, lhs.D / rhs);
public static Index4 operator /(in Index4 lhs, in Index4 rhs)
=> new Index4(lhs.A / rhs.A, lhs.B / rhs.B, lhs.C / rhs.C, lhs.D / rhs.D);
public static Index4 operator %(in Index4 lhs, int rhs)
=> new Index4(lhs.A % rhs, lhs.B % rhs, lhs.C % rhs, lhs.D % rhs);
public static Index4 operator %(in Index4 lhs, in Index4 rhs)
=> new Index4(lhs.A % rhs.A, lhs.B % rhs.B, lhs.C % rhs.C, lhs.D % rhs.D);
public static bool operator ==(in Index4 lhs, in Index4 rhs)
=> lhs.A == rhs.A && lhs.B == rhs.B && lhs.C == rhs.C && lhs.D == rhs.D;
public static bool operator !=(in Index4 lhs, in Index4 rhs)
=> lhs.A != rhs.A || lhs.B != rhs.B || lhs.C != rhs.C || lhs.D != rhs.D;
public static implicit operator Index2(in Index4 value)
=> new Index2(value.A, value.B);
public static implicit operator Index3(in Index4 value)
=> new Index3(value.A, value.B, value.C);
/// <summary>
/// Converts 1D index to 4D index.
/// </summary>
/// <param name="index1">Index in the 1D array.</param>
/// <param name="lengthA">Length of the dimension <see cref="A"/> of the 4D array.</param>
/// <param name="lengthB">Length of the dimension <see cref="B"/> of the 4D array.</param>
/// <param name="lengthC">Length of the dimension <see cref="C"/> of the 4D 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 Index4 Convert(int index1, int lengthA, int lengthB, int lengthC)
{
if (lengthA <= 0 || lengthB <= 0 || lengthC <= 0)
return Zero;
var alengthB = lengthA * lengthB;
var ablengthC = alengthB * lengthC;
var d = index1 / ablengthC;
var i_abc = index1 - (d * ablengthC);
var c = i_abc / alengthB;
var i_ab = i_abc - (c * alengthB);
var b = i_ab / lengthA;
var a = i_ab % lengthA;
return new Index4(a, b, c, d);
}
public static Index4 Convert(int index1, in Length3 length)
{
if (length.A <= 0 || length.B <= 0 || length.C <= 0)
return Zero;
var alengthB = length.A * length.B;
var ablengthC = alengthB * length.C;
var d = index1 / ablengthC;
var i_abc = index1 - (d * ablengthC);
var c = i_abc / alengthB;
var i_ab = i_abc - (c * alengthB);
var b = i_ab / length.A;
var a = i_ab % length.A;
return new Index4(a, b, c, d);
}
}
}