Skip to content

Commit

Permalink
broadphase: fix compatibility with recent APPLE compilers
Browse files Browse the repository at this point in the history
nil is used as macro definition on recent APPLE platforms, as shown by this compilation error:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/MacTypes.h:92:19: note: expanded from macro 'nil'
      #define nil nullptr
  • Loading branch information
jcarpent committed Mar 3, 2024
1 parent 692ac14 commit 38bea19
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion include/hpp/fcl/broadphase/detail/interval_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class HPP_FCL_DLLAPI IntervalTree {
protected:
IntervalTreeNode* root;

IntervalTreeNode* nil;
IntervalTreeNode* invalid_node;

/// @brief left rotation of tree node
void leftRotate(IntervalTreeNode* node);
Expand Down
4 changes: 2 additions & 2 deletions include/hpp/fcl/broadphase/detail/interval_tree_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class HPP_FCL_DLLAPI IntervalTreeNode {

~IntervalTreeNode();

/// @brief Print the interval node information: set left = nil and right =
/// root
/// @brief Print the interval node information: set left = invalid_node and
/// right = root
void print(IntervalTreeNode* left, IntervalTreeNode* right) const;

protected:
Expand Down
71 changes: 37 additions & 34 deletions src/broadphase/detail/interval_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ namespace detail {

//==============================================================================
IntervalTree::IntervalTree() {
nil = new IntervalTreeNode;
nil->left = nil->right = nil->parent = nil;
nil->red = false;
nil->key = nil->high = nil->max_high =
invalid_node = new IntervalTreeNode;
invalid_node->left = invalid_node->right = invalid_node->parent =
invalid_node;
invalid_node->red = false;
invalid_node->key = invalid_node->high = invalid_node->max_high =
-(std::numeric_limits<FCL_REAL>::max)();
nil->stored_interval = nullptr;
invalid_node->stored_interval = nullptr;

root = new IntervalTreeNode;
root->parent = root->left = root->right = nil;
root->parent = root->left = root->right = invalid_node;
root->key = root->high = root->max_high =
(std::numeric_limits<FCL_REAL>::max)();
root->red = false;
Expand All @@ -75,28 +76,28 @@ IntervalTree::~IntervalTree() {
IntervalTreeNode* x = root->left;
std::deque<IntervalTreeNode*> nodes_to_free;

if (x != nil) {
if (x->left != nil) {
if (x != invalid_node) {
if (x->left != invalid_node) {
nodes_to_free.push_back(x->left);
}
if (x->right != nil) {
if (x->right != invalid_node) {
nodes_to_free.push_back(x->right);
}

delete x;
while (nodes_to_free.size() > 0) {
x = nodes_to_free.back();
nodes_to_free.pop_back();
if (x->left != nil) {
if (x->left != invalid_node) {
nodes_to_free.push_back(x->left);
}
if (x->right != nil) {
if (x->right != invalid_node) {
nodes_to_free.push_back(x->right);
}
delete x;
}
}
delete nil;
delete invalid_node;
delete root;
free(recursion_node_stack);
}
Expand All @@ -108,7 +109,7 @@ void IntervalTree::leftRotate(IntervalTreeNode* x) {
y = x->right;
x->right = y->left;

if (y->left != nil) y->left->parent = x;
if (y->left != invalid_node) y->left->parent = x;

y->parent = x->parent;

Expand All @@ -132,7 +133,7 @@ void IntervalTree::rightRotate(IntervalTreeNode* y) {
x = y->left;
y->left = x->right;

if (nil != x->right) x->right->parent = y;
if (invalid_node != x->right) x->right->parent = y;

x->parent = y->parent;
if (y == y->parent->left)
Expand All @@ -153,10 +154,10 @@ void IntervalTree::recursiveInsert(IntervalTreeNode* z) {
IntervalTreeNode* x;
IntervalTreeNode* y;

z->left = z->right = nil;
z->left = z->right = invalid_node;
y = root;
x = root->left;
while (x != nil) {
while (x != invalid_node) {
y = x;
if (x->key > z->key)
x = x->left;
Expand Down Expand Up @@ -234,16 +235,16 @@ IntervalTreeNode* IntervalTree::insert(SimpleInterval* new_interval) {
IntervalTreeNode* IntervalTree::getSuccessor(IntervalTreeNode* x) const {
IntervalTreeNode* y;

if (nil != (y = x->right)) {
while (y->left != nil) y = y->left;
if (invalid_node != (y = x->right)) {
while (y->left != invalid_node) y = y->left;
return y;
} else {
y = x->parent;
while (x == y->right) {
x = y;
y = y->parent;
}
if (y == root) return nil;
if (y == root) return invalid_node;
return y;
}
}
Expand All @@ -252,13 +253,13 @@ IntervalTreeNode* IntervalTree::getSuccessor(IntervalTreeNode* x) const {
IntervalTreeNode* IntervalTree::getPredecessor(IntervalTreeNode* x) const {
IntervalTreeNode* y;

if (nil != (y = x->left)) {
while (y->right != nil) y = y->right;
if (invalid_node != (y = x->left)) {
while (y->right != invalid_node) y = y->right;
return y;
} else {
y = x->parent;
while (x == y->left) {
if (y == root) return nil;
if (y == root) return invalid_node;
x = y;
y = y->parent;
}
Expand All @@ -268,9 +269,9 @@ IntervalTreeNode* IntervalTree::getPredecessor(IntervalTreeNode* x) const {

//==============================================================================
void IntervalTree::recursivePrint(IntervalTreeNode* x) const {
if (x != nil) {
if (x != invalid_node) {
recursivePrint(x->left);
x->print(nil, root);
x->print(invalid_node, root);
recursivePrint(x->right);
}
}
Expand Down Expand Up @@ -346,16 +347,16 @@ void IntervalTree::deleteNode(SimpleInterval* ivl) {
//==============================================================================
IntervalTreeNode* IntervalTree::recursiveSearch(IntervalTreeNode* node,
SimpleInterval* ivl) const {
if (node != nil) {
if (node != invalid_node) {
if (node->stored_interval == ivl) return node;

IntervalTreeNode* left = recursiveSearch(node->left, ivl);
if (left != nil) return left;
if (left != invalid_node) return left;
IntervalTreeNode* right = recursiveSearch(node->right, ivl);
if (right != nil) return right;
if (right != invalid_node) return right;
}

return nil;
return invalid_node;
}

//==============================================================================
Expand All @@ -364,8 +365,10 @@ SimpleInterval* IntervalTree::deleteNode(IntervalTreeNode* z) {
IntervalTreeNode* x;
SimpleInterval* node_to_delete = z->stored_interval;

y = ((z->left == nil) || (z->right == nil)) ? z : getSuccessor(z);
x = (y->left == nil) ? y->right : y->left;
y = ((z->left == invalid_node) || (z->right == invalid_node))
? z
: getSuccessor(z);
x = (y->left == invalid_node) ? y->right : y->left;
if (root == (x->parent = y->parent)) {
root->left = x;
} else {
Expand All @@ -376,7 +379,7 @@ SimpleInterval* IntervalTree::deleteNode(IntervalTreeNode* z) {
}
}

/// @brief y should not be nil in this case
/// @brief y should not be invalid_node in this case
/// y is the node to splice out and x is its child
if (y != z) {
y->max_high = -(std::numeric_limits<FCL_REAL>::max)();
Expand Down Expand Up @@ -419,7 +422,7 @@ bool overlap(FCL_REAL a1, FCL_REAL a2, FCL_REAL b1, FCL_REAL b2) {
std::deque<SimpleInterval*> IntervalTree::query(FCL_REAL low, FCL_REAL high) {
std::deque<SimpleInterval*> result_stack;
IntervalTreeNode* x = root->left;
bool run = (x != nil);
bool run = (x != invalid_node);

current_parent = 0;

Expand All @@ -445,14 +448,14 @@ std::deque<SimpleInterval*> IntervalTree::query(FCL_REAL low, FCL_REAL high) {
} else
x = x->right;

run = (x != nil);
run = (x != invalid_node);
while ((!run) && (recursion_node_stack_top > 1)) {
if (recursion_node_stack[--recursion_node_stack_top].try_right_branch) {
x = recursion_node_stack[recursion_node_stack_top].start_node->right;
current_parent =
recursion_node_stack[recursion_node_stack_top].parent_index;
recursion_node_stack[current_parent].try_right_branch = true;
run = (x != nil);
run = (x != invalid_node);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/broadphase/detail/interval_tree_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ IntervalTreeNode::~IntervalTreeNode() {
}

//==============================================================================
void IntervalTreeNode::print(IntervalTreeNode* nil,
void IntervalTreeNode::print(IntervalTreeNode* invalid_node,
IntervalTreeNode* root) const {
stored_interval->print();
std::cout << ", k = " << key << ", h = " << high << ", mH = " << max_high;
std::cout << " l->key = ";
if (left == nil)
if (left == invalid_node)
std::cout << "nullptr";
else
std::cout << left->key;
std::cout << " r->key = ";
if (right == nil)
if (right == invalid_node)
std::cout << "nullptr";
else
std::cout << right->key;
Expand Down

0 comments on commit 38bea19

Please sign in to comment.