Skip to content

Commit 1d11a40

Browse files
facontidavideclaude
andcommitted
Pass ancestors set by reference instead of by value
Avoids copying the set at each recursive call. Uses insert-on-entry and erase-on-exit (DFS backtracking) so sibling subtrees sharing the same ID are handled correctly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b4e217e commit 1d11a40

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

src/xml_parsing.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ struct XMLParser::PImpl
198198
const std::string& prefix_path, Tree& output_tree,
199199
Blackboard::Ptr blackboard,
200200
const TreeNode::Ptr& root_node,
201-
std::unordered_set<std::string> ancestors = {});
201+
std::unordered_set<std::string>& ancestors);
202202

203203
void getPortsRecursively(const XMLElement* element,
204204
std::vector<std::string>& output_ports);
@@ -705,8 +705,9 @@ Tree XMLParser::instantiateTree(const Blackboard::Ptr& root_blackboard,
705705
"root_blackboard");
706706
}
707707

708+
std::unordered_set<std::string> ancestors;
708709
_p->recursivelyCreateSubtree(main_tree_ID, {}, {}, output_tree, root_blackboard,
709-
TreeNode::Ptr());
710+
TreeNode::Ptr(), ancestors);
710711
output_tree.initialize();
711712
return output_tree;
712713
}
@@ -1005,7 +1006,7 @@ TreeNode::Ptr XMLParser::PImpl::createNodeFromXML(const XMLElement* element,
10051006
void BT::XMLParser::PImpl::recursivelyCreateSubtree(
10061007
const std::string& tree_ID, const std::string& tree_path,
10071008
const std::string& prefix_path, Tree& output_tree, Blackboard::Ptr blackboard,
1008-
const TreeNode::Ptr& root_node, std::unordered_set<std::string> ancestors)
1009+
const TreeNode::Ptr& root_node, std::unordered_set<std::string>& ancestors)
10091010
{
10101011
if(!ancestors.insert(tree_ID).second)
10111012
{
@@ -1162,6 +1163,7 @@ void BT::XMLParser::PImpl::recursivelyCreateSubtree(
11621163
output_tree.subtrees.push_back(new_tree);
11631164

11641165
recursiveStep(root_node, new_tree, prefix_path, root_element);
1166+
ancestors.erase(tree_ID);
11651167
}
11661168

11671169
void XMLParser::PImpl::getPortsRecursively(const XMLElement* element,

0 commit comments

Comments
 (0)