@@ -34,6 +34,7 @@ Box2DX Copyright (c) 2009 Ihar Kalasouski http://code.google.com/p/box2dx
34
34
using System . Threading . Tasks ;
35
35
using Box2D . NetStandard . Common ;
36
36
using Math = System . Math ;
37
+ using Proxy = System . Int32 ;
37
38
38
39
namespace Box2D . NetStandard . Collision
39
40
{
@@ -43,57 +44,7 @@ namespace Box2D.NetStandard.Collision
43
44
/// <typeparam name="T"></typeparam>
44
45
internal sealed class DynamicTree
45
46
{
46
- internal readonly struct Proxy : IEquatable < Proxy > , IComparable < Proxy >
47
- {
48
- private readonly int _value ;
49
-
50
- public static readonly Proxy Free = - 1 ;
51
-
52
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
53
- public Proxy ( int v ) => _value = v ;
54
-
55
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
56
- public bool Equals ( Proxy other )
57
- => _value == other . _value ;
58
-
59
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
60
- public int CompareTo ( Proxy other )
61
- => _value . CompareTo ( other . _value ) ;
62
-
63
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
64
- public override bool Equals ( object ? obj )
65
- => obj is Proxy other && Equals ( other ) ;
66
-
67
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
68
- public override int GetHashCode ( ) => _value ;
69
-
70
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
71
- public static implicit operator int ( Proxy n ) => n . _value ;
72
-
73
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
74
- public static implicit operator Proxy ( int v ) => new Proxy ( v ) ;
75
-
76
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
77
- public static bool operator == ( Proxy a , Proxy b ) => a . _value == b . _value ;
78
-
79
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
80
- public static bool operator != ( Proxy a , Proxy b ) => a . _value != b . _value ;
81
-
82
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
83
- public static bool operator > ( Proxy a , Proxy b ) => a . _value > b . _value ;
84
-
85
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
86
- public static bool operator < ( Proxy a , Proxy b ) => a . _value < b . _value ;
87
-
88
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
89
- public static bool operator >= ( Proxy a , Proxy b ) => a . _value >= b . _value ;
90
-
91
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
92
- public static bool operator <= ( Proxy a , Proxy b ) => a . _value <= b . _value ;
93
-
94
- public override string ToString ( )
95
- => _value . ToString ( ) ;
96
- }
47
+ private const Proxy ProxyFree = - 1 ;
97
48
98
49
private struct Node
99
50
{
@@ -110,7 +61,7 @@ private struct Node
110
61
public bool IsLeaf
111
62
{
112
63
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
113
- get => Child2 == Proxy . Free ;
64
+ get => Child2 == ProxyFree ;
114
65
}
115
66
116
67
public bool IsFree
@@ -120,7 +71,7 @@ public bool IsFree
120
71
}
121
72
122
73
public override string ToString ( )
123
- => $@ "Parent: { ( Parent == Proxy . Free ? "None" : Parent . ToString ( ) ) } , {
74
+ => $@ "Parent: { ( Parent == ProxyFree ? "None" : Parent . ToString ( ) ) } , {
124
75
( IsLeaf
125
76
? Height == 0
126
77
? $ "Leaf: { UserData } "
@@ -139,7 +90,7 @@ public override string ToString()
139
90
public int Height
140
91
{
141
92
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
142
- get => _root == Proxy . Free ? 0 : _nodes [ _root ] . Height ;
93
+ get => _root == ProxyFree ? 0 : _nodes [ _root ] . Height ;
143
94
}
144
95
145
96
public int NodeCount => _nodeCount ;
@@ -173,7 +124,7 @@ public float AreaRatio
173
124
{
174
125
[ MethodImpl ( MethodImplOptions . NoInlining ) ]
175
126
get {
176
- if ( _root == Proxy . Free )
127
+ if ( _root == ProxyFree )
177
128
{
178
129
return 0 ;
179
130
}
@@ -206,7 +157,7 @@ public float AreaRatio
206
157
207
158
public DynamicTree ( )
208
159
{
209
- _root = Proxy . Free ;
160
+ _root = ProxyFree ;
210
161
_nodes = new Node [ 256 ] ;
211
162
212
163
// Build a linked list for the free list.
@@ -221,7 +172,7 @@ public DynamicTree()
221
172
222
173
ref var lastNode = ref _nodes [ ^ 1 ] ;
223
174
224
- lastNode . Parent = Proxy . Free ;
175
+ lastNode . Parent = ProxyFree ;
225
176
lastNode . Height = - 1 ;
226
177
}
227
178
@@ -233,7 +184,7 @@ public DynamicTree()
233
184
private ref Node AllocateNode ( out Proxy proxy )
234
185
{
235
186
// Expand the node pool as needed.
236
- if ( _freeNodes == Proxy . Free )
187
+ if ( _freeNodes == ProxyFree )
237
188
{
238
189
// Separate method to aid inlining since this is a cold path.
239
190
Expand ( ) ;
@@ -245,9 +196,9 @@ private ref Node AllocateNode(out Proxy proxy)
245
196
Assert ( allocNode . IsFree ) ;
246
197
_freeNodes = allocNode . Parent ;
247
198
Assert ( _freeNodes == - 1 || _nodes [ _freeNodes ] . IsFree ) ;
248
- allocNode . Parent = Proxy . Free ;
249
- allocNode . Child1 = Proxy . Free ;
250
- allocNode . Child2 = Proxy . Free ;
199
+ allocNode . Parent = ProxyFree ;
200
+ allocNode . Child1 = ProxyFree ;
201
+ allocNode . Child2 = ProxyFree ;
251
202
allocNode . Height = 0 ;
252
203
++ _nodeCount ;
253
204
proxy = alloc ;
@@ -283,7 +234,7 @@ void Expand()
283
234
}
284
235
285
236
ref var lastNode = ref _nodes [ l ] ;
286
- lastNode . Parent = Proxy . Free ;
237
+ lastNode . Parent = ProxyFree ;
287
238
lastNode . Height = - 1 ;
288
239
_freeNodes = ( Proxy ) _nodeCount ;
289
240
}
@@ -299,8 +250,8 @@ private void FreeNode(Proxy proxy)
299
250
node . Parent = _freeNodes ;
300
251
node . Height = - 1 ;
301
252
#if DEBUG_DYNAMIC_TREE
302
- node . Child1 = Proxy . Free ;
303
- node . Child2 = Proxy . Free ;
253
+ node . Child1 = ProxyFree ;
254
+ node . Child2 = ProxyFree ;
304
255
#endif
305
256
node . UserData = default ;
306
257
_freeNodes = proxy ;
@@ -437,7 +388,7 @@ private void RemoveLeaf(Proxy leaf)
437
388
{
438
389
if ( leaf == _root )
439
390
{
440
- _root = Proxy . Free ;
391
+ _root = ProxyFree ;
441
392
return ;
442
393
}
443
394
@@ -452,7 +403,7 @@ private void RemoveLeaf(Proxy leaf)
452
403
453
404
ref var siblingNode = ref _nodes [ sibling ] ;
454
405
455
- if ( grandParent != Proxy . Free )
406
+ if ( grandParent != ProxyFree )
456
407
{
457
408
// Destroy parent and connect sibling to grandParent.
458
409
ref var grandParentNode = ref _nodes [ grandParent ] ;
@@ -474,7 +425,7 @@ private void RemoveLeaf(Proxy leaf)
474
425
else
475
426
{
476
427
_root = sibling ;
477
- siblingNode . Parent = Proxy . Free ;
428
+ siblingNode . Parent = ProxyFree ;
478
429
FreeNode ( parent ) ;
479
430
}
480
431
@@ -483,10 +434,10 @@ private void RemoveLeaf(Proxy leaf)
483
434
484
435
private void InsertLeaf ( Proxy leaf )
485
436
{
486
- if ( _root == Proxy . Free )
437
+ if ( _root == ProxyFree )
487
438
{
488
439
_root = leaf ;
489
- _nodes [ _root ] . Parent = Proxy . Free ;
440
+ _nodes [ _root ] . Parent = ProxyFree ;
490
441
return ;
491
442
}
492
443
@@ -557,7 +508,7 @@ private void InsertLeaf(Proxy leaf)
557
508
newParentNode . Height = 1 + siblingNode . Height ;
558
509
559
510
ref var proxyNode = ref _nodes [ leaf ] ;
560
- if ( oldParent != Proxy . Free )
511
+ if ( oldParent != ProxyFree )
561
512
{
562
513
// The sibling was not the root.
563
514
ref var oldParentNode = ref _nodes [ oldParent ] ;
@@ -609,7 +560,7 @@ private static float EstimateCost(in AABB baseAabb, in Node node)
609
560
[ MethodImpl ( MethodImplOptions . NoInlining ) ]
610
561
private void Balance ( Proxy index )
611
562
{
612
- while ( index != Proxy . Free )
563
+ while ( index != ProxyFree )
613
564
{
614
565
index = BalanceStep ( index ) ;
615
566
@@ -618,8 +569,8 @@ private void Balance(Proxy index)
618
569
var child1 = indexNode . Child1 ;
619
570
var child2 = indexNode . Child2 ;
620
571
621
- Assert ( child1 != Proxy . Free ) ;
622
- Assert ( child2 != Proxy . Free ) ;
572
+ Assert ( child1 != ProxyFree ) ;
573
+ Assert ( child2 != ProxyFree ) ;
623
574
624
575
ref var child1Node = ref _nodes [ child1 ] ;
625
576
ref var child2Node = ref _nodes [ child2 ] ;
@@ -677,7 +628,7 @@ private Proxy BalanceStep(Proxy iA)
677
628
c . Parent = a . Parent ;
678
629
a . Parent = iC ;
679
630
680
- if ( c . Parent == Proxy . Free )
631
+ if ( c . Parent == ProxyFree )
681
632
{
682
633
_root = iC ;
683
634
}
@@ -741,7 +692,7 @@ private Proxy BalanceStep(Proxy iA)
741
692
b . Parent = a . Parent ;
742
693
a . Parent = iB ;
743
694
744
- if ( b . Parent == Proxy . Free )
695
+ if ( b . Parent == ProxyFree )
745
696
{
746
697
_root = iB ;
747
698
}
@@ -830,7 +781,7 @@ public void RebuildBottomUp(int free = 0)
830
781
var proxy = ( Proxy ) i ;
831
782
if ( node . IsLeaf )
832
783
{
833
- node . Parent = Proxy . Free ;
784
+ node . Parent = ProxyFree ;
834
785
proxies [ count ++ ] = proxy ;
835
786
}
836
787
else
@@ -878,7 +829,7 @@ public void RebuildBottomUp(int free = 0)
878
829
parentNode . Child2 = child2 ;
879
830
parentNode . Height = Math . Max ( child1Node . Height , child2Node . Height ) + 1 ;
880
831
parentNode . Aabb = AABB . Combine ( child1Node . Aabb , child2Node . Aabb ) ;
881
- parentNode . Parent = Proxy . Free ;
832
+ parentNode . Parent = ProxyFree ;
882
833
883
834
child1Node . Parent = parent ;
884
835
child2Node . Parent = parent ;
@@ -913,7 +864,7 @@ public void Query(Func<int, bool> queryCallback, in AABB aabb)
913
864
while ( stack . _count != 0 )
914
865
{
915
866
var nodeId = stack . Pop ( ) ;
916
- if ( nodeId == Proxy . Free )
867
+ if ( nodeId == ProxyFree )
917
868
{
918
869
continue ;
919
870
}
@@ -970,7 +921,7 @@ public void RayCast(Func<RayCastInput, int, float> RayCastCallback, in RayCastIn
970
921
while ( stack . _count != 0 )
971
922
{
972
923
var nodeId = stack . Pop ( ) ;
973
- if ( nodeId == Proxy . Free )
924
+ if ( nodeId == ProxyFree )
974
925
{
975
926
continue ;
976
927
}
@@ -1030,7 +981,7 @@ private void Validate()
1030
981
1031
982
var freeCount = 0 ;
1032
983
var freeIndex = _freeNodes ;
1033
- while ( freeIndex != Proxy . Free )
984
+ while ( freeIndex != ProxyFree )
1034
985
{
1035
986
Assert ( 0 <= freeIndex ) ;
1036
987
Assert ( freeIndex < Capacity ) ;
@@ -1046,22 +997,22 @@ private void Validate()
1046
997
[ Conditional ( "DEBUG" ) ]
1047
998
private void Validate ( Proxy proxy )
1048
999
{
1049
- if ( proxy == Proxy . Free ) return ;
1000
+ if ( proxy == ProxyFree ) return ;
1050
1001
1051
1002
ref var node = ref _nodes [ proxy ] ;
1052
1003
1053
1004
if ( proxy == _root )
1054
1005
{
1055
- Assert ( node . Parent == Proxy . Free ) ;
1006
+ Assert ( node . Parent == ProxyFree ) ;
1056
1007
}
1057
1008
1058
1009
var child1 = node . Child1 ;
1059
1010
var child2 = node . Child2 ;
1060
1011
1061
1012
if ( node . IsLeaf )
1062
1013
{
1063
- Assert ( child1 == Proxy . Free ) ;
1064
- Assert ( child2 == Proxy . Free ) ;
1014
+ Assert ( child1 == ProxyFree ) ;
1015
+ Assert ( child2 == ProxyFree ) ;
1065
1016
Assert ( node . Height == 0 ) ;
1066
1017
return ;
1067
1018
}
@@ -1095,7 +1046,7 @@ private void Validate(Proxy proxy)
1095
1046
[ Conditional ( "DEBUG_DYNAMIC_TREE" ) ]
1096
1047
private void ValidateHeight ( Proxy proxy )
1097
1048
{
1098
- if ( proxy == Proxy . Free )
1049
+ if ( proxy == ProxyFree )
1099
1050
{
1100
1051
return ;
1101
1052
}
0 commit comments