From 3653188ad542b5fd52607463a6338cdb7a7e3f2b Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Sun, 12 Aug 2018 19:02:42 +1000 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html --- exprtk.hpp | 64 +++++++++++++------------- exprtk_test.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 32 deletions(-) diff --git a/exprtk.hpp b/exprtk.hpp index f55657a..35c139c 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -2305,8 +2305,8 @@ namespace exprtk inline std::string substr(const std::size_t& begin, const std::size_t& end) { - details::char_cptr begin_itr = ((base_itr_ + begin) < s_end_) ? (base_itr_ + begin) : s_end_; - details::char_cptr end_itr = ((base_itr_ + end) < s_end_) ? (base_itr_ + end) : s_end_; + const details::char_cptr begin_itr = ((base_itr_ + begin) < s_end_) ? (base_itr_ + begin) : s_end_; + const details::char_cptr end_itr = ((base_itr_ + end) < s_end_) ? (base_itr_ + end) : s_end_; return std::string(begin_itr,end_itr); } @@ -3655,7 +3655,7 @@ namespace exprtk bool operator() (const lexer::token& t0, const lexer::token& t1) { - set_t::value_type p = std::make_pair(t0.type,t1.type); + const set_t::value_type p = std::make_pair(t0.type,t1.type); if (invalid_bracket_check(t0.type,t1.type)) { @@ -3669,7 +3669,7 @@ namespace exprtk return true; } - std::size_t error_count() + std::size_t error_count() const { return error_list_.size(); } @@ -4757,8 +4757,8 @@ namespace exprtk static inline std::size_t min_size(control_block* cb0, control_block* cb1) { - std::size_t size0 = cb0->size; - std::size_t size1 = cb1->size; + const std::size_t size0 = cb0->size; + const std::size_t size1 = cb1->size; if (size0 && size1) return std::min(size0,size1); @@ -8677,7 +8677,7 @@ namespace exprtk range_t& range = str0_range_ptr_->range_ref(); - if (range(r0,r1,str0_base_ptr_->size())) + if (range(r0, r1, str0_base_ptr_->size())) { const std::size_t size = (r1 - r0) + 1; @@ -8695,7 +8695,7 @@ namespace exprtk range_t& range = str1_range_ptr_->range_ref(); - if (range(r0,r1,str1_base_ptr_->size())) + if (range(r0, r1, str1_base_ptr_->size())) { const std::size_t size = (r1 - r0) + 1; @@ -8815,7 +8815,7 @@ namespace exprtk std::size_t r0 = 0; std::size_t r1 = 0; - if (range(r0,r1,str0_base_ptr_->size())) + if (range(r0, r1, str0_base_ptr_->size())) { const std::size_t size = (r1 - r0) + 1; @@ -16631,11 +16631,11 @@ namespace exprtk #ifndef exprtk_disable_string_capabilities type_store,std::string> stringvar_store; #endif - type_store,ifunction > function_store; + type_store,ifunction > function_store; type_store,ivararg_function > vararg_function_store; type_store,igeneric_function > generic_function_store; type_store,igeneric_function > string_function_store; - type_store vector_store; + type_store vector_store; st_data() { @@ -17337,8 +17337,8 @@ namespace exprtk { /* Function will return true if symbol_name exists as either a - reserved symbol, variable, stringvar or function name in any - of the type stores. + reserved symbol, variable, stringvar, vector or function name + in any of the type stores. */ if (!valid()) return false; @@ -17348,6 +17348,8 @@ namespace exprtk else if (local_data().stringvar_store.symbol_exists(symbol_name)) return true; #endif + else if (local_data().vector_store.symbol_exists(symbol_name)) + return true; else if (local_data().function_store.symbol_exists(symbol_name)) return true; else if (check_reserved_symb && local_data().is_reserved_symbol(symbol_name)) @@ -17903,7 +17905,7 @@ namespace exprtk control_block_-> local_data_list.push_back( typename expression::control_block:: - data_pack(reinterpret_cast(data),dt,size)); + data_pack(reinterpret_cast(data), dt, size)); } } } @@ -22664,7 +22666,6 @@ namespace exprtk exprtk_error_location)); return false; - } else if (is_constant_node(r0)) { @@ -22730,7 +22731,6 @@ namespace exprtk rp.free(); return false; - } else if (is_constant_node(r1)) { @@ -26102,7 +26102,7 @@ namespace exprtk } #ifndef exprtk_disable_sc_andor - inline bool is_shortcircuit_expression(const details::operator_type& operation) + inline bool is_shortcircuit_expression(const details::operator_type& operation) const { return ( (details::e_scand == operation) || @@ -26110,13 +26110,13 @@ namespace exprtk ); } #else - inline bool is_shortcircuit_expression(const details::operator_type&) + inline bool is_shortcircuit_expression(const details::operator_type&) const { return false; } #endif - inline bool is_null_present(expression_node_ptr (&branch)[2]) + inline bool is_null_present(expression_node_ptr (&branch)[2]) const { return ( details::is_null_node(branch[0]) || @@ -26124,7 +26124,7 @@ namespace exprtk ); } - inline bool is_vector_eqineq_logic_operation(const details::operator_type& operation, expression_node_ptr (&branch)[2]) + inline bool is_vector_eqineq_logic_operation(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const { if (!is_ivector_node(branch[0]) && !is_ivector_node(branch[1])) return false; @@ -26146,7 +26146,7 @@ namespace exprtk ); } - inline bool is_vector_arithmetic_operation(const details::operator_type& operation, expression_node_ptr (&branch)[2]) + inline bool is_vector_arithmetic_operation(const details::operator_type& operation, expression_node_ptr (&branch)[2]) const { if (!is_ivector_node(branch[0]) && !is_ivector_node(branch[1])) return false; @@ -36290,8 +36290,8 @@ namespace exprtk { static const T lower_bound = T(-20); static const T upper_bound = T(+20); + static const T delta = T(0.1); - T delta = T(0.1); T total = T(0); for (x = lower_bound; x <= upper_bound; x += delta) @@ -36315,7 +36315,7 @@ namespace exprtk { for (std::size_t i = 0; i < 10000; ++i) { - T v = T(123.456 + i); + const T v = T(123.456 + i); if (details::is_true(details::numeric::nequal(details::numeric::fast_exp::result(v),details::numeric::pow(v,T( 1))))) return false; @@ -37877,7 +37877,7 @@ namespace exprtk if ((1 == ps_index) && !helper::load_vector_range::process(parameters, r0, r1, 3, 4, 1)) return std::numeric_limits::quiet_NaN(); - else if (helper::invalid_range(y,r0,r1)) + else if (helper::invalid_range(y, r0, r1)) return std::numeric_limits::quiet_NaN(); T a = scalar_t(parameters[0])(); @@ -37924,7 +37924,7 @@ namespace exprtk if ((1 == ps_index) && !helper::load_vector_range::process(parameters, r0, r1, 4, 5, 1)) return std::numeric_limits::quiet_NaN(); - else if (helper::invalid_range(y,r0,r1)) + else if (helper::invalid_range(y, r0, r1)) return std::numeric_limits::quiet_NaN(); const T a = scalar_t(parameters[0])(); @@ -37973,9 +37973,9 @@ namespace exprtk if ((1 == ps_index) && !helper::load_vector_range::process(parameters, r0, r1, 3, 4, 1)) return std::numeric_limits::quiet_NaN(); - else if (helper::invalid_range(y,r0,r1)) + else if (helper::invalid_range(y, r0, r1)) return std::numeric_limits::quiet_NaN(); - else if (helper::invalid_range(z,r0,r1)) + else if (helper::invalid_range(z, r0, r1)) return std::numeric_limits::quiet_NaN(); T a = scalar_t(parameters[0])(); @@ -38023,9 +38023,9 @@ namespace exprtk if ((1 == ps_index) && !helper::load_vector_range::process(parameters, r0, r1, 4, 5, 1)) return std::numeric_limits::quiet_NaN(); - else if (helper::invalid_range(y,r0,r1)) + else if (helper::invalid_range(y, r0, r1)) return std::numeric_limits::quiet_NaN(); - else if (helper::invalid_range(z,r0,r1)) + else if (helper::invalid_range(z, r0, r1)) return std::numeric_limits::quiet_NaN(); const T a = scalar_t(parameters[0])(); @@ -38073,7 +38073,7 @@ namespace exprtk if ((1 == ps_index) && !helper::load_vector_range::process(parameters, r0, r1, 4, 5, 1)) return std::numeric_limits::quiet_NaN(); - else if (helper::invalid_range(z,r0,r1)) + else if (helper::invalid_range(z, r0, r1)) return std::numeric_limits::quiet_NaN(); const T a = scalar_t(parameters[0])(); @@ -38120,7 +38120,7 @@ namespace exprtk if ((1 == ps_index) && !helper::load_vector_range::process(parameters, r0, r1, 2, 3, 0)) return std::numeric_limits::quiet_NaN(); - else if (helper::invalid_range(y,r0,r1)) + else if (helper::invalid_range(y, r0, r1)) return std::numeric_limits::quiet_NaN(); T result = T(0); @@ -38166,7 +38166,7 @@ namespace exprtk if ((1 == ps_index) && !helper::load_vector_range::process(parameters, r0, r1, 2, 3, 0)) return std::numeric_limits::quiet_NaN(); - else if (helper::invalid_range(y,r0,r1)) + else if (helper::invalid_range(y, r0, r1)) return std::numeric_limits::quiet_NaN(); T result = T(0); diff --git a/exprtk_test.cpp b/exprtk_test.cpp index b33c2bf..5f664fc 100644 --- a/exprtk_test.cpp +++ b/exprtk_test.cpp @@ -7904,6 +7904,126 @@ inline bool run_test20() } } + { + T var; + std::string str; + std::vector vec(10,0.0); + + typedef exprtk::symbol_table symbol_table_t; + + bool result = true; + + { + symbol_table_t symbol_table; + + symbol_table.add_variable ("val",var); + symbol_table.add_stringvar("str",str); + symbol_table.add_vector ("vec",vec); + + if (symbol_table.get_variable("val") == 0 || !symbol_table.symbol_exists("val")) + { + printf("run_test20() - [3] Failed to get 'val' from symbol_table\n"); + result = false; + } + + if (symbol_table.get_stringvar("str") == 0 || !symbol_table.symbol_exists("str")) + { + printf("run_test20() - [3] Failed to get 'str' from symbol_table\n"); + result = false; + } + + if (symbol_table.get_vector("vec") == 0 || !symbol_table.symbol_exists("vec")) + { + printf("run_test20() - [3] Failed to get 'vec' from symbol_table\n"); + result = false; + } + } + + if (!result) + return result; + + { + symbol_table_t symbol_table; + + symbol_table.add_variable("val",var); + + if (symbol_table.get_variable("val") == 0 || !symbol_table.symbol_exists("val")) + { + printf("run_test20() - [4] Failed to get 'val' from symbol_table\n"); + result = false; + } + + if (symbol_table.get_stringvar("str") != 0 || symbol_table.symbol_exists("str")) + { + printf("run_test20() - [4] Failed to get 'str' from symbol_table\n"); + result = false; + } + + if (symbol_table.get_vector("vec") != 0 || symbol_table.symbol_exists("vec")) + { + printf("run_test20() - [4] Failed to get 'vec' from symbol_table\n"); + result = false; + } + } + + if (!result) + return result; + + { + symbol_table_t symbol_table; + + symbol_table.add_stringvar("str",str); + + if (symbol_table.get_stringvar("str") == 0 || !symbol_table.symbol_exists("str")) + { + printf("run_test20() - [5] Failed to get 'str' from symbol_table\n"); + result = false; + } + + if (symbol_table.get_variable("val") != 0 || symbol_table.symbol_exists("val")) + { + printf("run_test20() - [5] Failed to get 'val' from symbol_table\n"); + result = false; + } + + if (symbol_table.get_vector("vec") != 0 || symbol_table.symbol_exists("vec")) + { + printf("run_test20() - [5] Failed to get 'vec' from symbol_table\n"); + result = false; + } + } + + if (!result) + return result; + + { + symbol_table_t symbol_table; + + symbol_table.add_vector("vec",vec); + + if (symbol_table.get_vector("vec") == 0 || !symbol_table.symbol_exists("vec")) + { + printf("run_test20() - [6] Failed to get 'vec' from symbol_table\n"); + result = false; + } + + if (symbol_table.get_variable("val") != 0 || symbol_table.symbol_exists("val")) + { + printf("run_test20() - [6] Failed to get 'val' from symbol_table\n"); + result = false; + } + + if (symbol_table.get_stringvar("str") != 0 || symbol_table.symbol_exists("str")) + { + printf("run_test20() - [6] Failed to get 'str' from symbol_table\n"); + result = false; + } + } + + if (!result) + return result; + } + return true; }