From b1d52c62f3021caee1dd25dad1c0f402e8263452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Chocholat=C3=BD?= Date: Mon, 25 Sep 2023 08:55:35 +0200 Subject: [PATCH 1/7] Reserve space for children --- include/mata/parser/inter-aut.hh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/mata/parser/inter-aut.hh b/include/mata/parser/inter-aut.hh index 45d46d380..ba0e72239 100644 --- a/include/mata/parser/inter-aut.hh +++ b/include/mata/parser/inter-aut.hh @@ -128,8 +128,8 @@ struct FormulaGraph { std::vector children{}; FormulaGraph() = default; - FormulaGraph(const FormulaNode& n) : node(n), children() {} - FormulaGraph(FormulaNode&& n) : node(std::move(n)), children() {} + explicit FormulaGraph(const FormulaNode& n) : node(n), children() { children.reserve(2); } + explicit FormulaGraph(FormulaNode&& n) : node(std::move(n)), children() { children.reserve(2); } FormulaGraph(const FormulaGraph& g) : node(g.node), children(g.children) {} FormulaGraph(FormulaGraph&& g) : node(std::move(g.node)), children(std::move(g.children)) {} From 536d4e9000ecfe19414d5610b8d17c5944d89dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Chocholat=C3=BD?= Date: Mon, 25 Sep 2023 08:56:57 +0200 Subject: [PATCH 2/7] Fix formatting --- src/inter-aut.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/inter-aut.cc b/src/inter-aut.cc index 43fc4f2bd..a24dec748 100644 --- a/src/inter-aut.cc +++ b/src/inter-aut.cc @@ -20,11 +20,11 @@ #include "mata/parser/inter-aut.hh" namespace { - bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) - { - return !(!(aut.node_naming == mata::IntermediateAut::Naming::AUTO && - aut.symbol_naming == mata::IntermediateAut::Naming::AUTO) && - (aut.state_naming == mata::IntermediateAut::Naming::AUTO)); + +bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) { + return !(!(aut.node_naming == mata::IntermediateAut::Naming::AUTO && + aut.symbol_naming == mata::IntermediateAut::Naming::AUTO) && + (aut.state_naming == mata::IntermediateAut::Naming::AUTO)); } bool is_logical_operator(char ch) @@ -130,7 +130,7 @@ namespace { return lhs + " " + graph.node.raw + " " + rhs; } - mata::FormulaNode create_node(const mata::IntermediateAut &mata, const std::string &token) { + mata::FormulaNode create_node(const mata::IntermediateAut &mata, const std::string& token) { if (is_logical_operator(token[0])) { switch (token[0]) { case '&': From 08cf090a8621ed391c593d7b3cbd06ebdfd60f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Chocholat=C3=BD?= Date: Mon, 25 Sep 2023 09:07:44 +0200 Subject: [PATCH 3/7] Apply moves on tokens --- src/inter-aut.cc | 2 +- src/parser.cc | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/inter-aut.cc b/src/inter-aut.cc index a24dec748..a3a4968c3 100644 --- a/src/inter-aut.cc +++ b/src/inter-aut.cc @@ -111,7 +111,7 @@ bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) { if (graph.children.size() == 1) { // unary operator const auto& child = graph.children.front(); std::string child_name = child.node.is_operand() ? child.node.raw : - "(" + serialize_graph(child.node) + ")"; + "(" + serialize_graph(mata::FormulaGraph{ std::move(child.node) }) + ")"; return graph.node.raw + child_name; } diff --git a/src/parser.cc b/src/parser.cc index 78d000ed6..e83b934bc 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -199,14 +199,14 @@ std::vector> tokenize_line(const std::string& line) std::vector> split_tokens(std::vector> tokens) { std::vector> result; - for (const auto& token : tokens) { + for (auto& token: tokens) { if (token.second) { // is quoted? result.push_back(std::move(token)); continue; } - const std::string_view token_string = token.first; - size_t last_operator = 0; + const std::string_view token_string{ token.first }; + size_t last_operator{ 0 }; for (size_t i = 0, token_string_size{ token_string.size() }; i < token_string_size; ++i) { if (is_logical_operator(token_string[i])) { const std::string_view token_candidate = token_string.substr(last_operator, i - last_operator); From 47e0c86016afbffed895614a0a636cb3af382f1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Chocholat=C3=BD?= Date: Mon, 25 Sep 2023 09:09:50 +0200 Subject: [PATCH 4/7] Optimize section parsing --- src/inter-aut.cc | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/inter-aut.cc b/src/inter-aut.cc index a3a4968c3..4f921beb3 100644 --- a/src/inter-aut.cc +++ b/src/inter-aut.cc @@ -307,9 +307,9 @@ bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) { * @param postfix Postfix to which operators are eventually added * @return A postfix with eventually added operators */ - std::vector add_disjunction_implicitly(std::vector postfix) - { - if (postfix.size() == 1) // no need to add operators + std::vector add_disjunction_implicitly(std::vector postfix) { + const size_t postfix_size{ postfix.size() }; + if (postfix_size == 1) // no need to add operators return postfix; for (const auto& op : postfix) { @@ -318,15 +318,15 @@ bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) { } std::vector res; - if (postfix.size() >= 2) { - res.push_back(postfix[0]); - res.push_back(postfix[1]); + if (postfix_size >= 2) { + res.push_back(std::move(postfix[0])); + res.push_back(std::move(postfix[1])); res.emplace_back( mata::FormulaNode::Type::OPERATOR, "|", "|", mata::FormulaNode::OperatorType::OR); } - for (size_t i = 2; i < postfix.size(); ++i) { - res.push_back(postfix[i]); + for (size_t i{ 2 }; i < postfix_size; ++i) { + res.push_back(std::move(postfix[i])); res.emplace_back( mata::FormulaNode::Type::OPERATOR, "|", "|", mata::FormulaNode::OperatorType::OR); } @@ -375,20 +375,19 @@ bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) { for (const auto& keypair : section.dict) { const std::string &key = keypair.first; - if (key.find("Initial") != std::string::npos) { + if (key.starts_with("Initial")) { auto postfix = infix_to_postfix(aut, keypair.second); if (no_operators(postfix)) { aut.initial_enumerated = true; postfix = add_disjunction_implicitly(std::move(postfix)); } aut.initial_formula = postfix_to_graph(std::move(postfix)); - } else if (key.find("Final") != std::string::npos) { + } else if (key.starts_with("Final")) { auto postfix = infix_to_postfix(aut, keypair.second); if (no_operators(postfix)) { postfix = add_disjunction_implicitly(std::move(postfix)); aut.final_enumerated = true; } - aut.final_formula = postfix_to_graph(std::move(postfix));; } } @@ -403,7 +402,7 @@ bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) { return aut; } -} // anonymous +} // Anonymous namespace. size_t mata::IntermediateAut::get_number_of_disjuncts() const { From 3c0541239dc9b8f22cab614d3f252fb2af7ad0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Chocholat=C3=BD?= Date: Mon, 25 Sep 2023 09:24:47 +0200 Subject: [PATCH 5/7] Omit creating local variable when unnecessary --- src/inter-aut.cc | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/inter-aut.cc b/src/inter-aut.cc index 4f921beb3..ca144344c 100644 --- a/src/inter-aut.cc +++ b/src/inter-aut.cc @@ -269,35 +269,41 @@ bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) { std::vector opstack{}; opstack.reserve(4); for (mata::FormulaNode& node: postfix) { - mata::FormulaGraph gr(std::move(node)); - switch (gr.node.type) { + switch (node.type) { case mata::FormulaNode::Type::OPERAND: - opstack.emplace_back(std::move(gr)); + opstack.emplace_back(std::move(node)); break; - case mata::FormulaNode::Type::OPERATOR: - switch (gr.node.operator_type) { - case mata::FormulaNode::OperatorType::NEG: + case mata::FormulaNode::Type::OPERATOR: { + switch (node.operator_type) { + case mata::FormulaNode::OperatorType::NEG: { // 1 child: graph will be a NEG node. assert(!opstack.empty()); - gr.children.emplace_back(std::move(opstack.back())); + mata::FormulaGraph child{ std::move(opstack.back()) }; opstack.pop_back(); - opstack.emplace_back(std::move(gr)); + mata::FormulaGraph& gr{ opstack.emplace_back(std::move(node)) }; + gr.children.emplace_back(std::move(child)); break; - default: + } + default: { // 2 children: Graph will be either an AND node, or an OR node. assert(opstack.size() > 1); mata::FormulaGraph second_child{ std::move(opstack.back()) }; opstack.pop_back(); - gr.children.emplace_back(std::move(opstack.back())); + mata::FormulaGraph first_child{ std::move(opstack.back()) }; opstack.pop_back(); + mata::FormulaGraph& gr{ opstack.emplace_back(std::move(node)) }; + gr.children.emplace_back(std::move(first_child)); gr.children.emplace_back(std::move(second_child)); - opstack.emplace_back(std::move(gr)); + break; + } } break; - default: assert(false && "Unknown type of node"); + } + default: + assert(false && "Unknown type of node"); } } assert(opstack.size() == 1); - return std::move(opstack[0]); + return std::move(*opstack.begin()); } /** From d93ed344bc53eeccca0064a121d91b1bf208a70d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Chocholat=C3=BD?= Date: Mon, 25 Sep 2023 10:40:28 +0200 Subject: [PATCH 6/7] Fix move constructors by adding noexcept --- include/mata/parser/inter-aut.hh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/include/mata/parser/inter-aut.hh b/include/mata/parser/inter-aut.hh index ba0e72239..514b6d67a 100644 --- a/include/mata/parser/inter-aut.hh +++ b/include/mata/parser/inter-aut.hh @@ -110,10 +110,10 @@ public: FormulaNode(const FormulaNode& n) : type(n.type), raw(n.raw), name(n.name), operator_type(n.operator_type), operand_type(n.operand_type) {} - FormulaNode(FormulaNode&&) = default; + FormulaNode(FormulaNode&&) noexcept = default; FormulaNode& operator=(const FormulaNode& other) = default; - FormulaNode& operator=(FormulaNode&& other) = default; + FormulaNode& operator=(FormulaNode&& other) noexcept = default; }; /** @@ -123,7 +123,8 @@ public: * E.g., a formula q1 & s1 will be transformed to a tree with & as a root node * and q1 and s2 being children nodes of the root. */ -struct FormulaGraph { +class FormulaGraph { +public: FormulaNode node{}; std::vector children{}; @@ -131,7 +132,7 @@ struct FormulaGraph { explicit FormulaGraph(const FormulaNode& n) : node(n), children() { children.reserve(2); } explicit FormulaGraph(FormulaNode&& n) : node(std::move(n)), children() { children.reserve(2); } FormulaGraph(const FormulaGraph& g) : node(g.node), children(g.children) {} - FormulaGraph(FormulaGraph&& g) : node(std::move(g.node)), children(std::move(g.children)) {} + FormulaGraph(FormulaGraph&& g) noexcept : node(std::move(g.node)), children(std::move(g.children)) {} FormulaGraph& operator=(const mata::FormulaGraph&) = default; FormulaGraph& operator=(mata::FormulaGraph&&) noexcept = default; @@ -146,7 +147,7 @@ struct FormulaGraph { * and type of alphabet. It contains also the transitions formula and formula for initial and final * states. The formulas are represented as tree where nodes are either operands or operators. */ -struct IntermediateAut { +class IntermediateAut { public: /** * Type of automaton. So far we support nondeterministic finite automata (NFA) and @@ -218,7 +219,8 @@ public: const FormulaGraph& get_symbol_part_of_transition(const std::pair& trans) const; /** - * A method for building a vector of IntermediateAut for a parsed input. + * @brief A method for building a vector of IntermediateAut for a parsed input. + * * For each section in input is created one IntermediateAut. * It parses basic information about type of automata, its naming conventions etc. * Then it parses input and final formula of automaton. From ef48e3941bcd73b1d641219d14d9a0ee253cc6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Chocholat=C3=BD?= Date: Mon, 25 Sep 2023 13:01:09 +0200 Subject: [PATCH 7/7] Use iterator for quicker erase --- src/inter-aut.cc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/inter-aut.cc b/src/inter-aut.cc index ca144344c..d860d85c0 100644 --- a/src/inter-aut.cc +++ b/src/inter-aut.cc @@ -206,8 +206,8 @@ bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) { * @param tokens Series of tokens representing transition formula parsed from the input text * @return A postfix notation for input */ - std::vector infix_to_postfix( - const mata::IntermediateAut &aut, const std::vector& tokens) { + std::vector infix_to_postfix(const mata::IntermediateAut &aut, + const std::vector& tokens) { std::vector opstack; std::vector output; @@ -229,13 +229,14 @@ bool has_atmost_one_auto_naming(const mata::IntermediateAut& aut) { break; case mata::FormulaNode::Type::OPERATOR: for (int j = static_cast(opstack.size())-1; j >= 0; --j) { - size_t j_size_t{ static_cast(j) }; - assert(!opstack[j_size_t].is_operand()); - if (opstack[j_size_t].is_leftpar()) + auto formula_node_opstack_it{ opstack.begin() + j }; + assert(!formula_node_opstack_it->is_operand()); + if (formula_node_opstack_it->is_leftpar()) { break; - if (lower_precedence(node.operator_type, opstack[j_size_t].operator_type)) { - output.emplace_back(std::move(opstack[j_size_t])); - opstack.erase(opstack.begin()+j); + } + if (lower_precedence(node.operator_type, formula_node_opstack_it->operator_type)) { + output.emplace_back(std::move(*formula_node_opstack_it)); + opstack.erase(formula_node_opstack_it); } } opstack.emplace_back(std::move(node));