-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgravity_actor_tree.cpp
More file actions
142 lines (109 loc) · 2.83 KB
/
gravity_actor_tree.cpp
File metadata and controls
142 lines (109 loc) · 2.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
#include "gravity_actor_tree.h"
#include "gravity_actor_extension.h"
#include <ranges>
using Node = ActorTreeNode;
constinit Node* Node::root;
[[gnu::target("thumb")]]
Node* Node::Rotate(Node* Node::* from, Node* Node::* to)
{
Node* const newParent = this->*to;
this->*to = std::exchange(newParent->*from, this);
this->UpdateHeight();
newParent->UpdateHeight();
return newParent;
}
[[gnu::target("thumb")]]
void Node::Insert(Node*& parent, Node& newNode)
{
if (parent == nullptr)
{
parent = &newNode;
return;
}
Insert(parent->right, newNode);
parent->UpdateHeight();
if (parent->GetHeightDiff() < -1)
{
if (newNode.uniqueID < parent->right->uniqueID)
RotateRight(parent->right);
RotateLeft(parent);
}
}
[[gnu::target("thumb")]]
void Node::Remove(Node*& target, unsigned uniqueID)
{
if (uniqueID < target->uniqueID)
Remove(target->left, uniqueID);
else if (uniqueID > target->uniqueID)
Remove(target->right, uniqueID);
else if (!target->left && !target->right) // if target is a leaf node
target = nullptr;
else if (target->right && !target->left) // if target only has the right child
target = target->right;
else if (target->left && !target->right) // if target only has the left child
target = target->left;
else if (target->left && target->right) // if target has both children
{
unsigned numParents = 0;
Node*& successor = [&]
{
auto s = std::ref(target->right);
while (s.get()->left)
{
s = s.get()->left;
numParents++;
}
return s;
}();
Node* const newSuccessor = successor->right;
successor->left = target->left;
if (successor != target->right)
successor->right = target->right;
target = successor;
successor = newSuccessor;
Node** parentArray[numParents];
std::ranges::subrange parents(&parentArray[0], &parentArray[numParents]);
for (auto lastParent = std::ref(target->right); Node**& parent : parents)
{
parent = &lastParent.get();
lastParent = lastParent.get()->left;
}
for (Node** parent : std::ranges::reverse_view(parents))
RestoreBalance(*parent);
}
RestoreBalance(target);
}
[[gnu::target("thumb")]]
void Node::RestoreBalance(Node*& node)
{
if (node == nullptr) return;
node->UpdateHeight();
const int heightDiff = node->GetHeightDiff();
if (heightDiff > 1)
{
if (node->left && node->left->GetHeightDiff() < 0)
RotateLeft(node->left);
RotateRight(node);
}
else if (heightDiff < -1)
{
if (node->right && node->right->GetHeightDiff() > 0)
RotateRight(node->right);
RotateLeft(node);
}
}
Actor* Node::Find(unsigned uniqueID)
{
Node* node = root;
while (node)
{
if (uniqueID < node->uniqueID)
node = node->left;
else if (uniqueID > node->uniqueID)
node = node->right;
else
return &static_cast<ActorExtension*>(node)->GetActor();
}
return nullptr;
}
asm("nsub_02010f3c = _ZN13ActorTreeNode4FindEj");