-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSGridIndex.cs
188 lines (143 loc) · 7.15 KB
/
SGridIndex.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
using System.Runtime.Serialization;
namespace System.Grid
{
/// <summary>
/// Represent the signed coordinates of the 2D grid.
/// </summary>
[Serializable]
public readonly struct SGridIndex : IEquatableReadOnlyStruct<SGridIndex>, ISerializable
{
public readonly int Row;
public readonly int Column;
public int this[int index]
{
get
{
if (index == 0) return this.Row;
if (index == 1) return this.Column;
throw new IndexOutOfRangeException();
}
}
public SGridIndex(int row, int column)
{
this.Row = row;
this.Column = column;
}
private SGridIndex(SerializationInfo info, StreamingContext context)
{
this.Row = info.GetInt32OrDefault(nameof(this.Row));
this.Column = info.GetInt32OrDefault(nameof(this.Column));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.Row), this.Row);
info.AddValue(nameof(this.Column), this.Column);
}
public void Deconstruct(out int row, out int column)
{
row = this.Row;
column = this.Column;
}
public SGridIndex With(int? Row = null, int? Column = null)
=> new SGridIndex(
Row ?? this.Row,
Column ?? this.Column
);
public int ToIndex1(int columnCount)
=> columnCount <= 0 ? 0 : this.Column + this.Row * columnCount;
public int ToIndex1(in GridIndex size)
=> size.Column <= 0 ? 0 : this.Column + this.Row * size.Column;
public override bool Equals(object obj)
=> obj is SGridIndex other && this.Row == other.Row && this.Column == other.Column;
public bool Equals(SGridIndex other)
=> this.Row == other.Row && this.Column == other.Column;
public bool Equals(in SGridIndex other)
=> this.Row == other.Row && this.Column == other.Column;
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.Row, this.Column);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = 240067226;
hashCode = hashCode * -1521134295 + this.Row.GetHashCode();
hashCode = hashCode * -1521134295 + this.Column.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override string ToString()
=> $"({this.Row}, {this.Column})";
/// <summary>
/// Shorthand for writing SGridIndex(0, 0)
/// </summary>
public static SGridIndex Zero { get; } = new SGridIndex(0, 0);
/// <summary>
/// Shorthand for writing SGridIndex(1, 1)
/// </summary>
public static SGridIndex One { get; } = new SGridIndex(1, 1);
/// <summary>
/// Shorthand for writing SGridIndex(0, -1)
/// </summary>
public static SGridIndex Left { get; } = new SGridIndex(0, -1);
/// <summary>
/// Shorthand for writing SGridIndex(0, 1)
/// </summary>
public static SGridIndex Right { get; } = new SGridIndex(0, 1);
/// <summary>
/// Shorthand for writing SGridIndex(1, 0)
/// </summary>
public static SGridIndex Up { get; } = new SGridIndex(-1, 0);
/// <summary>
/// Shorthand for writing SGridIndex(-1, 0)
/// </summary>
public static SGridIndex Down { get; } = new SGridIndex(1, 0);
public static implicit operator SGridIndex(in (int row, int column) value)
=> new SGridIndex(value.row, value.column);
public static implicit operator SGridIndex(in GridIndex value)
=> new SGridIndex(value.Row, value.Column);
public static implicit operator GridIndex(in SGridIndex value)
=> new GridIndex(value.Row, value.Column);
public static bool operator ==(in SGridIndex lhs, in SGridIndex rhs)
=> lhs.Row == rhs.Row && lhs.Column == rhs.Column;
public static bool operator !=(in SGridIndex lhs, in SGridIndex rhs)
=> lhs.Row != rhs.Row || lhs.Column != rhs.Column;
public static SGridIndex operator +(in SGridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row + rhs.Row, lhs.Column + rhs.Column);
public static SGridIndex operator -(in SGridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row - rhs.Row, lhs.Column - rhs.Column);
public static SGridIndex operator *(in SGridIndex lhs, int rhs)
=> new SGridIndex(lhs.Row * rhs, lhs.Column * rhs);
public static SGridIndex operator *(int lhs, in SGridIndex rhs)
=> new SGridIndex(rhs.Row * lhs, rhs.Column * lhs);
public static SGridIndex operator *(in SGridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row * rhs.Row, lhs.Column * rhs.Column);
public static SGridIndex operator /(in SGridIndex lhs, int rhs)
=> new SGridIndex(lhs.Row / rhs, lhs.Column / rhs);
public static SGridIndex operator /(in SGridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row / rhs.Row, lhs.Column / rhs.Column);
public static SGridIndex operator %(in SGridIndex lhs, int rhs)
=> new SGridIndex(lhs.Row % rhs, lhs.Column % rhs);
public static SGridIndex operator %(in SGridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row % rhs.Row, lhs.Column % rhs.Column);
public static SGridIndex operator +(in SGridIndex lhs, in GridIndex rhs)
=> new SGridIndex(lhs.Row + rhs.Row, lhs.Column + rhs.Column);
public static SGridIndex operator -(in SGridIndex lhs, in GridIndex rhs)
=> new SGridIndex(lhs.Row - rhs.Row, lhs.Column - rhs.Column);
public static SGridIndex operator *(in SGridIndex lhs, in GridIndex rhs)
=> new SGridIndex(lhs.Row * rhs.Row, lhs.Column * rhs.Column);
public static SGridIndex operator /(in SGridIndex lhs, in GridIndex rhs)
=> new SGridIndex(lhs.Row / rhs.Row, lhs.Column / rhs.Column);
public static SGridIndex operator %(in SGridIndex lhs, in GridIndex rhs)
=> new SGridIndex(lhs.Row % rhs.Row, lhs.Column % rhs.Column);
public static SGridIndex operator +(in GridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row + rhs.Row, lhs.Column + rhs.Column);
public static SGridIndex operator -(in GridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row - rhs.Row, lhs.Column - rhs.Column);
public static SGridIndex operator *(in GridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row * rhs.Row, lhs.Column * rhs.Column);
public static SGridIndex operator /(in GridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row / rhs.Row, lhs.Column / rhs.Column);
public static SGridIndex operator %(in GridIndex lhs, in SGridIndex rhs)
=> new SGridIndex(lhs.Row % rhs.Row, lhs.Column % rhs.Column);
}
}