forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridIndexRange.cs
More file actions
218 lines (173 loc) · 8.83 KB
/
Copy pathGridIndexRange.cs
File metadata and controls
218 lines (173 loc) · 8.83 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
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
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace System.Grid
{
[Serializable]
public readonly partial struct GridIndexRange : IRange<GridIndex, GridIndexRange.Enumerator>,
IEquatableReadOnlyStruct<GridIndexRange>, ISerializable
{
public GridIndex Start { get; }
public GridIndex End { get; }
public bool IsFromEnd { get; }
public GridDirection Direction { get; }
public GridIndexRange(in GridIndex start, in GridIndex end, bool fromEnd = false, GridDirection direction = default)
{
this.Start = start;
this.End = end;
this.IsFromEnd = fromEnd;
this.Direction = direction;
}
private GridIndexRange(SerializationInfo info, StreamingContext context)
{
this.Start = info.GetValueOrDefault<GridIndex>(nameof(this.Start));
this.End = info.GetValueOrDefault<GridIndex>(nameof(this.End));
this.IsFromEnd = info.GetBooleanOrDefault(nameof(this.IsFromEnd));
this.Direction = info.GetValueOrDefault<GridDirection>(nameof(this.Direction));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.Start), this.Start);
info.AddValue(nameof(this.End), this.End);
info.AddValue(nameof(this.IsFromEnd), this.IsFromEnd);
info.AddValue(nameof(this.Direction), this.Direction);
}
public void Deconstruct(out GridIndex start, out GridIndex end, out bool fromEnd, out GridDirection direction)
{
start = this.Start;
end = this.End;
fromEnd = this.IsFromEnd;
direction = this.Direction;
}
public GridIndexRange With(in GridIndex? Start = null, in GridIndex? End = null, bool? IsFromEnd = null, GridDirection? Direction = null)
=> new GridIndexRange(
Start ?? this.Start,
End ?? this.End,
IsFromEnd ?? this.IsFromEnd,
Direction ?? this.Direction
);
public GridIndexRange ByRow()
=> new GridIndexRange(this.Start, this.End, this.IsFromEnd, GridDirection.Row);
public GridIndexRange ByColumn()
=> new GridIndexRange(this.Start, this.End, this.IsFromEnd, GridDirection.Column);
public GridIndexRange FromStart()
=> new GridIndexRange(this.Start, this.End, false, this.Direction);
public GridIndexRange FromEnd()
=> new GridIndexRange(this.Start, this.End, true, this.Direction);
IRange<GridIndex> IRange<GridIndex>.FromStart()
=> FromStart();
IRange<GridIndex> IRange<GridIndex>.FromEnd()
=> FromEnd();
public bool Contains(in GridIndex value)
{
var containsRow = this.Start.Row.CompareTo(this.End.Row) <= 0
? value.Row >= this.Start.Row && value.Row <= this.End.Row
: value.Row >= this.End.Row && value.Row <= this.Start.Row;
var containsCol = this.Start.Column.CompareTo(this.End.Column) <= 0
? value.Column >= this.Start.Column && value.Column <= this.End.Column
: value.Column >= this.End.Column && value.Column <= this.Start.Column;
return containsRow && containsCol;
}
public int Count()
{
var row = Math.Max(this.End.Row - this.Start.Row + 1, 0);
var col = Math.Max(this.End.Column - this.Start.Column + 1, 0);
return row * col;
}
public override bool Equals(object obj)
=> obj is GridIndexRange other &&
this.Start == other.Start &&
this.End == other.End &&
this.IsFromEnd == other.IsFromEnd &&
this.Direction == other.Direction;
public bool Equals(in GridIndexRange other)
=> this.Start == other.Start &&
this.End == other.End &&
this.IsFromEnd == other.IsFromEnd &&
this.Direction == other.Direction;
public bool Equals(GridIndexRange other)
=> this.Start == other.Start &&
this.End == other.End &&
this.IsFromEnd == other.IsFromEnd &&
this.Direction == other.Direction;
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.Start, this.End, this.IsFromEnd, this.Direction);
#endif
#pragma warning disable CS0162 // Unreachable code detected
var hashCode = -1418356749;
hashCode = hashCode * -1521134295 + this.Start.GetHashCode();
hashCode = hashCode * -1521134295 + this.End.GetHashCode();
hashCode = hashCode * -1521134295 + this.IsFromEnd.GetHashCode();
hashCode = hashCode * -1521134295 + this.Direction.GetHashCode();
return hashCode;
#pragma warning restore CS0162 // Unreachable code detected
}
public override string ToString()
=> $"{{ {nameof(this.Start)}={this.Start}, {nameof(this.End)}={this.End}, {nameof(this.IsFromEnd)}={this.IsFromEnd}, {nameof(this.Direction)}={this.Direction} }}";
public Enumerator GetEnumerator()
=> new Enumerator(this);
public Enumerator Range()
=> GetEnumerator();
IEnumerator<GridIndex> IRange<GridIndex>.Range()
=> GetEnumerator();
public GridIndexRange Normalize()
=> Normal(this.Start, this.End, this.IsFromEnd, this.Direction);
public bool Intersects(in GridIndexRange other)
{
var nThis = Normalize();
var nOther = other.Normalize();
return (nOther.Start.Row <= nThis.End.Row && nOther.Start.Column <= nThis.End.Column &&
nOther.End.Row >= nThis.Start.Row && nOther.End.Column >= nThis.Start.Column);
}
/// <summary>
/// Create a normal range from (a, b) where <see cref="Start"/> is lesser than <see cref="End"/>.
/// </summary>
public static GridIndexRange Normal(in GridIndex a, in GridIndex b, bool fromEnd = false, GridDirection direction = default)
{
var rowIncreasing = a.Row.CompareTo(b.Row) <= 0;
var colIncreasing = a.Column.CompareTo(b.Column) <= 0;
var start = new GridIndex(
rowIncreasing ? a.Row : b.Row,
colIncreasing ? a.Column : b.Column
);
var end = new GridIndex(
rowIncreasing ? b.Row : a.Row,
colIncreasing ? b.Column : a.Column
);
return new GridIndexRange(start, end, fromEnd, direction);
}
/// <summary>
/// Create a range from a size whose row and column are greater than 0
/// </summary>
/// <exception cref="InvalidOperationException">Row and column must be greater than 0</exception>
public static GridIndexRange FromSize(in GridIndex value, bool fromEnd = false)
{
if (value.Row <= 0 || value.Column <= 0)
throw new InvalidOperationException("Row and column must be greater than 0");
return new GridIndexRange(GridIndex.Zero, value - GridIndex.One, fromEnd);
}
public static GridIndexRange FromStart(in GridIndex start, in GridIndex end)
=> new GridIndexRange(start, end, false);
public static GridIndexRange FromEnd(in GridIndex start, in GridIndex end)
=> new GridIndexRange(start, end, true);
public static implicit operator GridIndexRange(in (GridIndex start, GridIndex end) value)
=> new GridIndexRange(value.start, value.end);
public static implicit operator GridIndexRange(in (GridIndex start, GridIndex end, bool fromEnd) value)
=> new GridIndexRange(value.start, value.end, value.fromEnd);
public static implicit operator GridIndexRange(in (GridIndex start, GridIndex end, bool fromEnd, GridDirection direction) value)
=> new GridIndexRange(value.start, value.end, value.fromEnd, value.direction);
public static implicit operator GridIndexRange(in ReadRange<GridIndex> value)
=> new GridIndexRange(value.Start, value.End, value.IsFromEnd);
public static bool operator ==(in GridIndexRange lhs, in GridIndexRange rhs)
=> lhs.Start == rhs.Start &&
lhs.End == rhs.End &&
lhs.IsFromEnd == rhs.IsFromEnd &&
lhs.Direction == rhs.Direction;
public static bool operator !=(in GridIndexRange lhs, in GridIndexRange rhs)
=> lhs.Start != rhs.Start ||
lhs.End != rhs.End ||
lhs.IsFromEnd != rhs.IsFromEnd ||
lhs.Direction != rhs.Direction;
}
}