C++ Mathematical Expression Library (ExprTk) http://www.partow.net/programming/exprtk/index.html

This commit is contained in:
Arash Partow 2013-12-17 07:46:18 +11:00
parent 3c933cd8cf
commit 40a9551e5c
2 changed files with 152 additions and 178 deletions

View File

@ -467,7 +467,7 @@ namespace exprtk
template <typename T>
inline bool is_false_impl(const T v)
{
return (v == T(0));
return (T(0) == v);
}
template <typename T>
@ -1754,7 +1754,7 @@ namespace exprtk
if ('/' == c1) { mode = 1; incr = 2; }
else if ('*' == c1) { mode = 2; incr = 2; }
}
return (mode != 0);
return (0 != mode);
}
static inline bool comment_end(const char c0, const char c1, const int mode)
@ -5454,17 +5454,6 @@ namespace exprtk
typename Allocator,
template <typename,typename> class Sequence>
static inline T process(const Sequence<Type,Allocator>& arglist)
{
if (arglist.size() > 5)
{
T result = T(0);
for (std::size_t i = 0; i < arglist.size(); ++i)
{
result += value(arglist[i]);
}
return result;
}
else
{
switch (arglist.size())
{
@ -5474,7 +5463,14 @@ namespace exprtk
case 3 : return process_3(arglist);
case 4 : return process_4(arglist);
case 5 : return process_5(arglist);
default : return std::numeric_limits<T>::quiet_NaN();
default :
{
T result = T(0);
for (std::size_t i = 0; i < arglist.size(); ++i)
{
result += value(arglist[i]);
}
return result;
}
}
}
@ -5523,17 +5519,6 @@ namespace exprtk
typename Allocator,
template <typename,typename> class Sequence>
static inline T process(const Sequence<Type,Allocator>& arglist)
{
if (arglist.size() > 5)
{
T result = T(value(arglist[0]));
for (std::size_t i = 1; i < arglist.size(); ++i)
{
result *= value(arglist[i]);
}
return result;
}
else
{
switch (arglist.size())
{
@ -5543,7 +5528,14 @@ namespace exprtk
case 3 : return process_3(arglist);
case 4 : return process_4(arglist);
case 5 : return process_5(arglist);
default : return std::numeric_limits<T>::quiet_NaN();
default :
{
T result = T(value(arglist[0]));
for (std::size_t i = 1; i < arglist.size(); ++i)
{
result *= value(arglist[i]);
}
return result;
}
}
}
@ -5592,12 +5584,6 @@ namespace exprtk
typename Allocator,
template <typename,typename> class Sequence>
static inline T process(const Sequence<Type,Allocator>& arglist)
{
if (arglist.size() > 5)
{
return vararg_add_op<T>::process(arglist) / arglist.size();
}
else
{
switch (arglist.size())
{
@ -5607,8 +5593,7 @@ namespace exprtk
case 3 : return process_3(arglist);
case 4 : return process_4(arglist);
case 5 : return process_5(arglist);
default : return std::numeric_limits<T>::quiet_NaN();
}
default : return vararg_add_op<T>::process(arglist) / arglist.size();
}
}
@ -5656,7 +5641,15 @@ namespace exprtk
template <typename,typename> class Sequence>
static inline T process(const Sequence<Type,Allocator>& arglist)
{
if (arglist.size() > 5)
switch (arglist.size())
{
case 0 : return T(0);
case 1 : return process_1(arglist);
case 2 : return process_2(arglist);
case 3 : return process_3(arglist);
case 4 : return process_4(arglist);
case 5 : return process_5(arglist);
default :
{
T result = T(value(arglist[0]));
for (std::size_t i = 1; i < arglist.size(); ++i)
@ -5667,18 +5660,6 @@ namespace exprtk
}
return result;
}
else
{
switch (arglist.size())
{
case 0 : return T(0);
case 1 : return process_1(arglist);
case 2 : return process_2(arglist);
case 3 : return process_3(arglist);
case 4 : return process_4(arglist);
case 5 : return process_5(arglist);
default : return std::numeric_limits<T>::quiet_NaN();
}
}
}
@ -5728,7 +5709,15 @@ namespace exprtk
template <typename,typename> class Sequence>
static inline T process(const Sequence<Type,Allocator>& arglist)
{
if (arglist.size() > 5)
switch (arglist.size())
{
case 0 : return T(0);
case 1 : return process_1(arglist);
case 2 : return process_2(arglist);
case 3 : return process_3(arglist);
case 4 : return process_4(arglist);
case 5 : return process_5(arglist);
default :
{
T result = T(value(arglist[0]));
for (std::size_t i = 1; i < arglist.size(); ++i)
@ -5739,18 +5728,6 @@ namespace exprtk
}
return result;
}
else
{
switch (arglist.size())
{
case 0 : return T(0);
case 1 : return process_1(arglist);
case 2 : return process_2(arglist);
case 3 : return process_3(arglist);
case 4 : return process_4(arglist);
case 5 : return process_5(arglist);
default : return std::numeric_limits<T>::quiet_NaN();
}
}
}
@ -5799,17 +5776,6 @@ namespace exprtk
typename Allocator,
template <typename,typename> class Sequence>
static inline T process(const Sequence<Type,Allocator>& arglist)
{
if (arglist.size() > 5)
{
for (std::size_t i = 0; i < arglist.size(); ++i)
{
if (T(0) == value(arglist[i]))
return T(0);
}
return T(1);
}
else
{
switch (arglist.size())
{
@ -5818,7 +5784,14 @@ namespace exprtk
case 3 : return process_3(arglist);
case 4 : return process_4(arglist);
case 5 : return process_5(arglist);
default : return T(0);
default :
{
for (std::size_t i = 0; i < arglist.size(); ++i)
{
if (T(0) == value(arglist[i]))
return T(0);
}
return T(1);
}
}
}
@ -5833,7 +5806,8 @@ namespace exprtk
static inline T process_2(const Sequence& arglist)
{
return (
(T(0) != value(arglist[0])) && (T(0) != value(arglist[1]))
(T(0) != value(arglist[0])) &&
(T(0) != value(arglist[1]))
) ? T(1) : T(0);
}
@ -5841,7 +5815,8 @@ namespace exprtk
static inline T process_3(const Sequence& arglist)
{
return (
(T(0) != value(arglist[0])) && (T(0) != value(arglist[1])) &&
(T(0) != value(arglist[0])) &&
(T(0) != value(arglist[1])) &&
(T(0) != value(arglist[2]))
) ? T(1) : T(0);
}
@ -5850,8 +5825,10 @@ namespace exprtk
static inline T process_4(const Sequence& arglist)
{
return (
(T(0) != value(arglist[0])) && (T(0) != value(arglist[1])) &&
(T(0) != value(arglist[2])) && (T(0) != value(arglist[3]))
(T(0) != value(arglist[0])) &&
(T(0) != value(arglist[1])) &&
(T(0) != value(arglist[2])) &&
(T(0) != value(arglist[3]))
) ? T(1) : T(0);
}
@ -5859,8 +5836,10 @@ namespace exprtk
static inline T process_5(const Sequence& arglist)
{
return (
(T(0) != value(arglist[0])) && (T(0) != value(arglist[1])) &&
(T(0) != value(arglist[2])) && (T(0) != value(arglist[3])) &&
(T(0) != value(arglist[0])) &&
(T(0) != value(arglist[1])) &&
(T(0) != value(arglist[2])) &&
(T(0) != value(arglist[3])) &&
(T(0) != value(arglist[4]))
) ? T(1) : T(0);
}
@ -5875,17 +5854,6 @@ namespace exprtk
typename Allocator,
template <typename,typename> class Sequence>
static inline T process(const Sequence<Type,Allocator>& arglist)
{
if (arglist.size() > 5)
{
for (std::size_t i = 0; i < arglist.size(); ++i)
{
if (T(0) != value(arglist[i]))
return T(1);
}
return T(0);
}
else
{
switch (arglist.size())
{
@ -5894,7 +5862,14 @@ namespace exprtk
case 3 : return process_3(arglist);
case 4 : return process_4(arglist);
case 5 : return process_5(arglist);
default : return T(0);
default :
{
for (std::size_t i = 0; i < arglist.size(); ++i)
{
if (T(0) != value(arglist[i]))
return T(1);
}
return T(0);
}
}
}
@ -5909,7 +5884,8 @@ namespace exprtk
static inline T process_2(const Sequence& arglist)
{
return (
(T(0) != value(arglist[0])) || (T(0) != value(arglist[1]))
(T(0) != value(arglist[0])) ||
(T(0) != value(arglist[1]))
) ? T(1) : T(0);
}
@ -5917,7 +5893,8 @@ namespace exprtk
static inline T process_3(const Sequence& arglist)
{
return (
(T(0) != value(arglist[0])) || (T(0) != value(arglist[1])) ||
(T(0) != value(arglist[0])) ||
(T(0) != value(arglist[1])) ||
(T(0) != value(arglist[2]))
) ? T(1) : T(0);
}
@ -5926,8 +5903,10 @@ namespace exprtk
static inline T process_4(const Sequence& arglist)
{
return (
(T(0) != value(arglist[0])) || (T(0) != value(arglist[1])) ||
(T(0) != value(arglist[2])) || (T(0) != value(arglist[3]))
(T(0) != value(arglist[0])) ||
(T(0) != value(arglist[1])) ||
(T(0) != value(arglist[2])) ||
(T(0) != value(arglist[3]))
) ? T(1) : T(0);
}
@ -5935,8 +5914,10 @@ namespace exprtk
static inline T process_5(const Sequence& arglist)
{
return (
(T(0) != value(arglist[0])) || (T(0) != value(arglist[1])) ||
(T(0) != value(arglist[2])) || (T(0) != value(arglist[3])) ||
(T(0) != value(arglist[0])) ||
(T(0) != value(arglist[1])) ||
(T(0) != value(arglist[2])) ||
(T(0) != value(arglist[3])) ||
(T(0) != value(arglist[4]))
) ? T(1) : T(0);
}
@ -5951,24 +5932,10 @@ namespace exprtk
typename Allocator,
template <typename,typename> class Sequence>
static inline T process(const Sequence<Type,Allocator>& arglist)
{
if (arglist.size() > 8)
{
if (arglist.empty())
return std::numeric_limits<T>::quiet_NaN();
else
{
for (std::size_t i = 0; i < (arglist.size() - 1); ++i)
{
value(arglist[i]);
}
}
return value(arglist.back());
}
else
{
switch (arglist.size())
{
case 0 : return std::numeric_limits<T>::quiet_NaN();
case 1 : return process_1(arglist);
case 2 : return process_2(arglist);
case 3 : return process_3(arglist);
@ -5977,7 +5944,13 @@ namespace exprtk
case 6 : return process_6(arglist);
case 7 : return process_7(arglist);
case 8 : return process_8(arglist);
default : return std::numeric_limits<T>::quiet_NaN();
default :
{
for (std::size_t i = 0; i < (arglist.size() - 1); ++i)
{
value(arglist[i]);
}
return value(arglist.back());
}
}
}
@ -11232,7 +11205,7 @@ namespace exprtk
{
if (expression_list.empty())
return error_node();
if (expression_list.size() == 1)
if (1 == expression_list.size())
return expression_list[0];
Sequence<expression_node_ptr,Allocator> tmp_expression_list;

View File

@ -988,6 +988,7 @@ static const test_t test_list[] =
test_t("switch { case [(1 <= 2)] : {1}; default: 1.12345; }",1.0),
test_t("switch { case ([1 > 2]) : [0]; case ([1 <= 2]) : 1; default: 1.12345; }",1.0),
test_t("switch { case {(1 <= 2)} : switch { case ({1 <= 2}) : 1; default: 1.12345; }; default: 1.12345; }",1.0),
test_t("switch { case 1 > 1 : 1; case 2 > 2 : 2; case 3 = 3 : 3; case 4 > 4 : 4; default : 5; }",3.0),
test_t("repeat 1.1 + 2.2 until (1 < 2)",3.3),
test_t("repeat (1.1 + 2.2) until (1 < 2)",3.3),
test_t("repeat 1.1 + 2.2; until (1 < 2)",3.3),