From 4a4fe26c41da7b3707905aa12fd5f697ad217961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Wed, 6 Nov 2024 16:05:27 +0100 Subject: [PATCH] C++20 modernization: more uses of std::ranges and std::views --- src/DataTree.cc | 15 ++++++++------- src/DynamicModel.cc | 10 ++-------- src/DynareBison.yy | 6 +++--- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/DataTree.cc b/src/DataTree.cc index 72ab5a44..58cff3dd 100644 --- a/src/DataTree.cc +++ b/src/DataTree.cc @@ -24,6 +24,7 @@ #include <fstream> #include <iostream> #include <iterator> +#include <ranges> #include "DataTree.hh" @@ -804,9 +805,9 @@ DataTree::AddSum(expr_t arg) bool DataTree::isSymbolUsed(int symb_id) const { - for (const auto& [symb_lag, expr] : variable_node_map) - if (symb_lag.first == symb_id) - return true; + if (ranges::any_of(views::keys(variable_node_map), + [=](const auto& symb_lag) { return symb_lag.first == symb_id; })) + return true; if (local_variables_table.contains(symb_id)) return true; @@ -852,8 +853,8 @@ DataTree::addAllParamDerivId([[maybe_unused]] set<int>& deriv_id_set) bool DataTree::isUnaryOpUsed(UnaryOpcode opcode) const { - return ranges::any_of(unary_op_node_map, - [=](const auto& it) { return get<1>(it.first) == opcode; }); + return ranges::any_of(views::keys(unary_op_node_map), + [=](const auto& key) { return get<1>(key) == opcode; }); } bool @@ -873,8 +874,8 @@ DataTree::isUnaryOpUsedOnType(SymbolType type, UnaryOpcode opcode) const bool DataTree::isBinaryOpUsed(BinaryOpcode opcode) const { - return ranges::any_of(binary_op_node_map, - [=](const auto& it) { return get<2>(it.first) == opcode; }); + return ranges::any_of(views::keys(binary_op_node_map), + [=](const auto& key) { return get<2>(key) == opcode; }); } bool diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index bea5e9d7..4f15c203 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -1043,14 +1043,8 @@ DynamicModel::writeDriverOutput(ostream& output, bool compute_xrefs) const << (static_only_equations.size() > 0) << ";" << endl; // Say if model contains an external function call - bool has_external_function = false; - for (auto equation : equations) - if (equation->containsExternalFunction()) - { - has_external_function = true; - break; - } - output << "M_.has_external_function = " << boolalpha << has_external_function << ';' << endl; + output << "M_.has_external_function = " << boolalpha + << ranges::any_of(equations, &ExprNode::containsExternalFunction) << ';' << endl; // Compute list of state variables, ordered in block-order vector<int> state_var; diff --git a/src/DynareBison.yy b/src/DynareBison.yy index 609eb9f2..ab5d0bc9 100644 --- a/src/DynareBison.yy +++ b/src/DynareBison.yy @@ -39,6 +39,7 @@ class ParsingDriver; #include <utility> #include <tuple> #include <variant> +#include <ranges> #include "CommonEnums.hh" #include "ExprNode.hh" @@ -4470,9 +4471,8 @@ vec_int_number : INT_NUMBER vec_int_elem : vec_int_number | INT_NUMBER ':' INT_NUMBER { - $$ = {}; - for (int i = stoi($1); i <= stoi($3); i++) - $$.push_back(i); + auto v = views::iota(stoi($1), stoi($3) + 1); + $$ = {v.begin(), v.end()}; } ; -- GitLab