From b6cdf0d2f9c32ccedb9e80ec77904154d800dd0f Mon Sep 17 00:00:00 2001 From: Arash Partow Date: Sat, 6 Dec 2014 12:35:39 +1100 Subject: [PATCH] C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html --- exprtk.hpp | 96 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 36 deletions(-) diff --git a/exprtk.hpp b/exprtk.hpp index b50ca76..a4b31c5 100644 --- a/exprtk.hpp +++ b/exprtk.hpp @@ -4410,9 +4410,9 @@ namespace exprtk construct_branch_pair 9)>::process(branch,b9); } - template struct cleanup_branches { + template static inline void execute(std::pair*,bool> (&branch)[N]) { for (std::size_t i = 0; i < N; ++i) @@ -4424,6 +4424,21 @@ namespace exprtk } } } + + template class Sequence> + static inline void execute(Sequence*,bool>,Allocator>& branch) + { + for (std::size_t i = 0; i < branch.size(); ++i) + { + if (branch[i].first && branch[i].second) + { + delete branch[i].first; + branch[i].first = 0; + } + } + } }; template @@ -4444,7 +4459,7 @@ namespace exprtk ~binary_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -4495,7 +4510,7 @@ namespace exprtk ~binary_ext_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -4549,7 +4564,7 @@ namespace exprtk ~trinary_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -4603,7 +4618,7 @@ namespace exprtk ~quaternary_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -4653,7 +4668,7 @@ namespace exprtk ~quinary_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -4705,7 +4720,7 @@ namespace exprtk ~senary_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -8150,7 +8165,7 @@ namespace exprtk ~function_N_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } template @@ -8555,14 +8570,7 @@ namespace exprtk ~generic_function_node() { - for (std::size_t i = 0; i < branch_.size(); ++i) - { - if (branch_[i].first && branch_[i].second) - { - delete branch_[i].first; - branch_[i].first = 0; - } - } + cleanup_branches::execute(branch_); } bool init_branches() @@ -8738,15 +8746,27 @@ namespace exprtk mutable range_list_t range_list_; }; - #define exprtk_define_unary_op(OpName) \ - template \ - struct OpName##_op \ - { \ - typedef typename functor_t::Type Type; \ - static inline T process(Type v) { return numeric:: OpName (v); } \ - static inline typename expression_node::node_type type() { return expression_node::e_##OpName; } \ - static inline details::operator_type operation() { return details::e_##OpName; } \ - }; \ + #define exprtk_define_unary_op(OpName) \ + template \ + struct OpName##_op \ + { \ + typedef typename functor_t::Type Type; \ + \ + static inline T process(Type v) \ + { \ + return numeric:: OpName (v); \ + } \ + \ + static inline typename expression_node::node_type type()\ + { \ + return expression_node::e_##OpName; \ + } \ + \ + static inline details::operator_type operation() \ + { \ + return details::e_##OpName; \ + } \ + }; \ exprtk_define_unary_op(abs ) exprtk_define_unary_op(acos ) @@ -11093,7 +11113,7 @@ namespace exprtk ~vob_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -11143,7 +11163,7 @@ namespace exprtk ~bov_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -11193,7 +11213,7 @@ namespace exprtk ~cob_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -11254,7 +11274,7 @@ namespace exprtk ~boc_node() { - cleanup_branches::execute(branch_); + cleanup_branches::execute(branch_); } inline T value() const @@ -19202,7 +19222,7 @@ namespace exprtk expression_node_ptr& branch, const bool brkcont = false) const { - if (details::is_constant_node(condition)) + if (!brkcont && details::is_constant_node(condition)) { expression_node_ptr result = error_node(); if (details::is_true(condition)) @@ -19236,7 +19256,7 @@ namespace exprtk expression_node_ptr branch, const bool brkcont = false) const { - if (details::is_constant_node(condition)) + if (!brkcont && details::is_constant_node(condition)) { if (details::is_true(condition) && details::is_constant_node(branch)) { @@ -19272,7 +19292,7 @@ namespace exprtk expression_node_ptr loop_body, bool brkcont = false) const { - if (details::is_constant_node(condition)) + if (!brkcont && details::is_constant_node(condition)) { expression_node_ptr result = error_node(); @@ -26375,8 +26395,10 @@ namespace exprtk inline void load_binary_operations_map(binary_op_map_t& m) { - #define register_binary_op(Op,BinaryFunctor) \ - m.insert(typename binary_op_map_t::value_type(Op,BinaryFunctor::process)); \ + typedef typename binary_op_map_t::value_type value_type; + + #define register_binary_op(Op,BinaryFunctor) \ + m.insert(value_type(Op,BinaryFunctor::process)); \ register_binary_op(details:: e_add,details:: add_op) register_binary_op(details:: e_sub,details:: sub_op) @@ -26401,8 +26423,10 @@ namespace exprtk inline void load_inv_binary_operations_map(inv_binary_op_map_t& m) { - #define register_binary_op(Op,BinaryFunctor) \ - m.insert(typename inv_binary_op_map_t::value_type(BinaryFunctor::process,Op)); \ + typedef typename inv_binary_op_map_t::value_type value_type; + + #define register_binary_op(Op,BinaryFunctor) \ + m.insert(value_type(BinaryFunctor::process,Op)); \ register_binary_op(details:: e_add,details:: add_op) register_binary_op(details:: e_sub,details:: sub_op)