From 13a27dbc982299ec92a411eeeb3ade9d9003d657 Mon Sep 17 00:00:00 2001 From: youngwolf Date: Sun, 4 Feb 2024 15:52:46 +0800 Subject: [PATCH] Code refactoring. --- question_exp.h | 50 ++++++++++++++------------------------------------ 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/question_exp.h b/question_exp.h index 3573919..16598e2 100644 --- a/question_exp.h +++ b/question_exp.h @@ -518,7 +518,6 @@ template class variable_data_exp : public data_exp public: variable_data_exp(const std::string& _variable_name) : variable_name(_variable_name) {} - virtual const std::string& get_variable_name() const {return variable_name;} virtual std::shared_ptr> to_negative() const {return std::make_shared>(variable_name);} virtual T operator()(const std::function& cb) const @@ -556,7 +555,6 @@ template class exponent_data_exp : public data_exp public: exponent_data_exp(const std::string& _variable_name, int _exponent) : variable_name(_variable_name), exponent(_exponent) {} - virtual const std::string& get_variable_name() const {return variable_name;} virtual void show_immediate_value() const {std::cout << ' ' << exponent;} virtual std::shared_ptr> to_negative() const @@ -952,26 +950,26 @@ template inline T safe_execute(const std::shared_ptr>& { std::list> res; travel_exp>>(dexp, - [&](const std::shared_ptr>& data) {res.emplace_back(data->safe_execute(cb));}, + [&](const std::shared_ptr>& left) {res.emplace_back(left->safe_execute(cb));}, [](const std::shared_ptr>&) {return false;}, - [&](const std::shared_ptr>& data) {res.emplace_back(data->safe_execute(cb));}, - [&](const std::shared_ptr>& data) { + [&](const std::shared_ptr>& right) {res.emplace_back(right->safe_execute(cb));}, + [&](const std::shared_ptr>& parent) { T re = res.back(); res.pop_back(); - res.back().merge_with(data->get_operator(), re); + res.back().merge_with(parent->get_operator(), re); } ); return res.front(); } -template inline void safe_delete(const std::shared_ptr>& dexp) +template class Exp> inline void safe_delete(const std::shared_ptr>& exp) { - travel_exp>>(dexp, - [](const std::shared_ptr>& data) {data->safe_delete();}, - [](const std::shared_ptr>&) {return false;}, - [](const std::shared_ptr>& data) {data->safe_delete();}, - [](const std::shared_ptr>& data) {data->clear();} + travel_exp>>(exp, + [](const std::shared_ptr>& left) {left->safe_delete();}, + [](const std::shared_ptr>&) {return false;}, + [](const std::shared_ptr>& right) {right->safe_delete();}, + [](const std::shared_ptr>& parent) {parent->clear();} ); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -997,36 +995,16 @@ template inline bool safe_execute(const std::shared_ptr>>(jexp, - [&](const std::shared_ptr>& judge) {re = judge->safe_execute(cb);}, - [&](const std::shared_ptr>& judge) { - auto& lop = judge->get_operator(); - if ("&&" == lop) //this if statement will impact efficiency, but we have no choice - { - if (!re) - return true; - } - else if (re) //"||" == lop - return true; - - return false; - }, - [&](const std::shared_ptr>& judge) {re = judge->safe_execute(cb);}, + [&](const std::shared_ptr>& left) {re = left->safe_execute(cb);}, + //this judgement will impact efficiency, but we have no choice, return false - continue, true - backtrack + [&](const std::shared_ptr>& parent) {return "&&" == parent->get_operator() ? !re : re;}, + [&](const std::shared_ptr>& right) {re = right->safe_execute(cb);}, [](const std::shared_ptr>&) {} ); return re; } -template inline void safe_delete(const std::shared_ptr>& jexp) -{ - travel_exp>>(jexp, - [](const std::shared_ptr>& judge) {judge->safe_delete();}, - [](const std::shared_ptr>&) {return false;}, - [](const std::shared_ptr>& judge) {judge->safe_delete();}, - [](const std::shared_ptr>& judge) {judge->clear();} - ); -} - template class unitary_judge_exp : public judge_exp { protected: -- GitLab