[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 334ac64 6/8: Add a function template to trans
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 334ac64 6/8: Add a function template to transform a PETE expression to a std::vector |
Date: |
Sat, 20 Mar 2021 09:55:30 -0400 (EDT) |
branch: master
commit 334ac647ee059f0e6805819f51251286feb02721
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Add a function template to transform a PETE expression to a std::vector
This permits
std::vector<double> const v = Eval<double>(pete_expression);
or
auto const v = Eval<double>(pete_expression);
which is more natural than
std::vector<double> v(Rho(pete_expression));
assign(v, pete_expression);
and allows 'v' to be const.
Ideally the expression above would be written as
auto const v = Eval(pete_expression);
That can be done if the result type can be deduced from the expression.
Alternatively, the type could be defaulted to double.
---
et_vector_test.cpp | 12 ++++++++++++
tools/pete-2.1.1/et_vector.hpp | 13 +++++++++++++
2 files changed, 25 insertions(+)
diff --git a/et_vector_test.cpp b/et_vector_test.cpp
index 3050703..ae8a03c 100644
--- a/et_vector_test.cpp
+++ b/et_vector_test.cpp
@@ -54,6 +54,18 @@ int test_main(int, char*[])
LMI_TEST_THROW(v0 *= v1, std::runtime_error, s);
}
+ // Test assignment to vector.
+ {
+ std::vector<double> v0 = {1.0, 1.25, 1.5};
+ std::vector<double> v1 = {0.0, 0.25, 0.5};
+ std::vector<double> const x = Eval<double>(v0 + v1);
+ auto const y = Eval<double>(v0 + v1 + x);
+ std::vector<double> const r0 = {1.0, 1.5, 2.0};
+ LMI_TEST(r0 == x);
+ std::vector<double> const r1 = {2.0, 3.0, 4.0};
+ LMI_TEST(r1 == y);
+ }
+
// Test peteCast().
{
std::vector<double> v0 = {0.0, 1.25, 2.5};
diff --git a/tools/pete-2.1.1/et_vector.hpp b/tools/pete-2.1.1/et_vector.hpp
index 88147a5..78fb1f6 100644
--- a/tools/pete-2.1.1/et_vector.hpp
+++ b/tools/pete-2.1.1/et_vector.hpp
@@ -198,4 +198,17 @@ inline void evaluate(std::vector<T>& t, Op const& op,
Expression<U> const& u)
}
}
+template<typename T, typename U>
+inline std::vector<T> Eval(Expression<U> const& u)
+{
+ int const n {Rho(u)};
+ std::vector<T> z;
+ z.reserve(n);
+ for(int i = 0; i < n; ++i)
+ {
+ z.push_back(forEach(u, EvalLeaf1(i), OpCombine()));
+ }
+ return z;
+}
+
#endif // et_vector_hpp
- [lmi-commits] [lmi] master updated (1be41b7 -> 1d15a4a), Greg Chicares, 2021/03/20
- [lmi-commits] [lmi] master d5f870a 1/8: Improve documentation, Greg Chicares, 2021/03/20
- [lmi-commits] [lmi] master 4220f5e 2/8: Assume default allocator always, Greg Chicares, 2021/03/20
- [lmi-commits] [lmi] master 44b780e 7/8: Deduce Eval()'s return type, Greg Chicares, 2021/03/20
- [lmi-commits] [lmi] master de67a28 5/8: Improve an error message, Greg Chicares, 2021/03/20
- [lmi-commits] [lmi] master cd6c9ea 3/8: Favor the One True type-parameter-key, Greg Chicares, 2021/03/20
- [lmi-commits] [lmi] master 4c4a0e4 4/8: Add a PETE length-of function, Greg Chicares, 2021/03/20
- [lmi-commits] [lmi] master 1d15a4a 8/8: Isolate PETE directory weirdness to a redirection header, Greg Chicares, 2021/03/20
- [lmi-commits] [lmi] master 334ac64 6/8: Add a function template to transform a PETE expression to a std::vector,
Greg Chicares <=