7 #include <dmlc/registry.h> 15 DMLC_REGISTRY_FILE_TAG(fold_code);
19 double log_root_data_count;
20 double log_root_sum_hess;
21 bool create_new_translation_unit;
27 if (node->node_id == 0) {
28 if (node->data_count) {
29 context->log_root_data_count = std::log(node->data_count.value());
31 context->log_root_data_count = std::numeric_limits<double>::quiet_NaN();
34 context->log_root_sum_hess = std::log(node->sum_hess.value());
36 context->log_root_sum_hess = std::numeric_limits<double>::quiet_NaN();
40 if ( (node->data_count && !std::isnan(context->log_root_data_count)
41 && context->log_root_data_count - std::log(node->data_count.value())
42 >= context->magnitude_req)
43 || (node->sum_hess && !std::isnan(context->log_root_sum_hess)
44 && context->log_root_sum_hess - std::log(node->sum_hess.value())
45 >= context->magnitude_req) ) {
47 ASTNode* parent_node = node->parent;
50 if (context->create_new_translation_unit) {
55 tu_node->children.push_back(ac);
56 ac->children.push_back(folder_node);
61 for (
size_t i = 0; i < parent_node->children.size(); ++i) {
62 if (parent_node->children[i] == node) {
67 CHECK_NE(node_loc, -1);
68 parent_node->children[node_loc]
69 = context->create_new_translation_unit ? tu_node : folder_node;
70 folder_node->children.push_back(node);
71 node->parent = folder_node;
74 bool folded_at_least_once =
false;
75 for (
ASTNode* child : node->children) {
76 folded_at_least_once |= fold_code(child, context, builder);
78 return folded_at_least_once;
82 int count_tu_nodes(
ASTNode* node);
84 bool ASTBuilder::FoldCode(
double magnitude_req,
85 bool create_new_translation_unit) {
87 std::numeric_limits<double>::quiet_NaN(),
88 std::numeric_limits<double>::quiet_NaN(),
89 create_new_translation_unit,
90 count_tu_nodes(this->main_node)};
91 return fold_code(this->main_node, &context,
this);