14 DMLC_REGISTRY_FILE_TAG(fold_code);
18 double log_root_data_count;
19 double log_root_sum_hess;
20 bool create_new_translation_unit;
26 if (node->node_id == 0) {
27 if (node->data_count) {
28 context->log_root_data_count = std::log(node->data_count.value());
30 context->log_root_data_count = std::numeric_limits<double>::quiet_NaN();
33 context->log_root_sum_hess = std::log(node->sum_hess.value());
35 context->log_root_sum_hess = std::numeric_limits<double>::quiet_NaN();
39 if ( (node->data_count && !std::isnan(context->log_root_data_count)
40 && context->log_root_data_count - std::log(node->data_count.value())
41 >= context->magnitude_req)
42 || (node->sum_hess && !std::isnan(context->log_root_sum_hess)
43 && context->log_root_sum_hess - std::log(node->sum_hess.value())
44 >= context->magnitude_req) ) {
46 ASTNode* parent_node = node->parent;
49 if (context->create_new_translation_unit) {
54 tu_node->children.push_back(ac);
55 ac->children.push_back(folder_node);
60 for (
size_t i = 0; i < parent_node->children.size(); ++i) {
61 if (parent_node->children[i] == node) {
66 CHECK_NE(node_loc, -1);
67 parent_node->children[node_loc]
68 = context->create_new_translation_unit ? tu_node : folder_node;
69 folder_node->children.push_back(node);
70 node->parent = folder_node;
73 bool folded_at_least_once =
false;
74 for (
ASTNode* child : node->children) {
75 folded_at_least_once |= fold_code(child, context, builder);
77 return folded_at_least_once;
81 int count_tu_nodes(
ASTNode* node);
83 bool ASTBuilder::FoldCode(
double magnitude_req,
84 bool create_new_translation_unit) {
86 std::numeric_limits<double>::quiet_NaN(),
87 std::numeric_limits<double>::quiet_NaN(),
88 create_new_translation_unit,
89 count_tu_nodes(this->main_node)};
90 return fold_code(this->main_node, &context,
this);