提振.如何用原语数组来代替std::vector
Boost.Proto : How to make an expression terminal of a primitive array instead of std::vector?
现在我正试图为向量表达式制作另一个迷你edsl(嵌入式特定于领域的语言)。实际上增加。Proto用户指南已经提供了这样一个EDSL示例,"Lazy Vector",其中向量表达式由std::vector<T>
组成。但是我必须用原始数组的表达式来代替。因为原始数组操作仍然是一些科学模拟程序的核心。
因此,我添加了一个数组包装类,ArrayWrapper
到"懒惰向量"代码,并将std::vector
替换为ArrayWrapper
。此修改的源代码已成功编译并链接。但是当我运行它时,core被转储了。
下面是修改后的源代码:
// The original version of this file is :
// "Lazy Vector: Controlling Operator Overloads"
// in Boost.Proto users' guide.
// Copyright 2008 Eric Niebler. Distributed under the Boost
// Software License, Version 1.0.
//
// It was modified to try protofying a primitive array
// on May 19 2015.
#include <vector>
#include <iostream>
#include <boost/mpl/int.hpp>
#include <boost/proto/core.hpp>
#include <boost/proto/context.hpp>
namespace mpl = boost::mpl;
namespace proto = boost::proto;
using proto::_;
template <typename T>
class ArrayWrapper {
private:
T* data;
size_t size_;
public:
typedef T value_type;
explicit ArrayWrapper(std::size_t size = 0, T const & value = T() ):
data( new T[size]), size_(size) {
for (std::size_t i = 0; i < size_; i++) data[i] = value;
}
~ArrayWrapper() {
std::cerr << "Now destructing an ArrayWrapper" << std::endl;
delete [] data;
}
std::size_t size() { return size_; }
T& operator[](std::size_t i) { return data[i]; }
T operator[](std::size_t i) const { return data[i]; }
};
template<typename Expr>
struct lazy_vector_expr;
// This grammar describes which lazy vector expressions
// are allowed; namely, vector terminals and addition
// and subtraction of lazy vector expressions.
struct LazyVectorGrammar
: proto::or_<
proto::terminal< ArrayWrapper<_> >
, proto::plus< LazyVectorGrammar, LazyVectorGrammar >
, proto::minus< LazyVectorGrammar, LazyVectorGrammar >
>
{};
// Tell proto that in the lazy_vector_domain, all
// expressions should be wrapped in laxy_vector_expr<>
// and must conform to the lazy vector grammar.
struct lazy_vector_domain
: proto::domain<proto::generator<lazy_vector_expr>, LazyVectorGrammar>
{};
// Here is an evaluation context that indexes into a lazy vector
// expression, and combines the result.
template<typename Size = std::size_t>
struct lazy_subscript_context
{
lazy_subscript_context(Size subscript)
: subscript_(subscript)
{}
// Use default_eval for all the operations ...
template<typename Expr, typename Tag = typename Expr::proto_tag>
struct eval
: proto::default_eval<Expr, lazy_subscript_context>
{};
// ... except for terminals, which we index with our subscript
template<typename Expr>
struct eval<Expr, proto::tag::terminal>
{
typedef typename proto::result_of::value<Expr>::type::value_type result_type;
result_type operator ()( Expr const & expr, lazy_subscript_context & ctx ) const
{
return proto::value( expr )[ ctx.subscript_ ];
}
};
Size subscript_;
};
// Here is the domain-specific expression wrapper, which overrides
// operator [] to evaluate the expression using the lazy_subscript_context.
template<typename Expr>
struct lazy_vector_expr
: proto::extends<Expr, lazy_vector_expr<Expr>, lazy_vector_domain>
{
lazy_vector_expr( Expr const & expr = Expr() )
: lazy_vector_expr::proto_extends( expr )
{}
// Use the lazy_subscript_context<> to implement subscripting
// of a lazy vector expression tree.
template< typename Size >
typename proto::result_of::eval< Expr, lazy_subscript_context<Size> >::type
operator []( Size subscript ) const
{
lazy_subscript_context<Size> ctx(subscript);
return proto::eval(*this, ctx);
}
};
// Here is our lazy_vector terminal, implemented in terms of lazy_vector_expr
template< typename T >
struct lazy_vector
: lazy_vector_expr< typename proto::terminal< ArrayWrapper<T> >::type >
{
typedef typename proto::terminal< ArrayWrapper<T> >::type expr_type;
lazy_vector( std::size_t size = 0, T const & value = T() )
: lazy_vector_expr<expr_type>( expr_type::make( ArrayWrapper<T>(size, value) ) )
{}
// Here we define a += operator for lazy vector terminals that
// takes a lazy vector expression and indexes it. expr[i] here
// uses lazy_subscript_context<> under the covers.
template< typename Expr >
lazy_vector & operator += (Expr const & expr)
{
std::size_t size = proto::value(*this).size();
for(std::size_t i = 0; i < size; ++i)
{
proto::value(*this)[i] += expr[i];
}
return *this;
}
};
int main()
{
// lazy_vectors with 4 elements each.
lazy_vector< double > v1( 4, 1.0 ), v2( 4, 2.0 ), v3( 4, 3.0 );
// Add two vectors lazily and get the 2nd element.
double d1 = ( v2 + v3 )[ 2 ]; // Look ma, no temporaries!
std::cout << d1 << std::endl;
// Subtract two vectors and add the result to a third vector.
v1 += v2 - v3; // Still no temporaries!
std::cout << '{' << v1[0] << ',' << v1[1]
<< ',' << v1[2] << ',' << v1[3] << '}' << std::endl;
// This expression is disallowed because it does not conform
// to the LazyVectorGrammar
//(v2 + v3) += v1;
return 0;
}
我假设我的数组包装器类具有其余"惰性向量"程序所需的所有必要的成员函数。我认为这些成员函数的接口和原来的"Lazy Vector"程序使用的std::vector
成员函数的接口是一样的。
可能我漏掉了一些重要的点。但是如何解决这个问题呢?(我应该如何使proto::terminal<T>
对象与原始数组?)如果您能给我一些建议或提示,我将非常感激。
最后,我找到了一种方法,使一个原始数组的表达式终端,使向量代数的最小EDSL。它可以在初始化表达式模板的终端对象时抑制临时对象的多余拷贝。消除对象复制的关键是在Vector
类中放入一个原始数组',为这个Vector
类定义一个返回true的trait,并使用BOOST_PROTO_DEFINE_OPERATORS()
。
源代码:
#include <iostream>
#include <boost/proto/proto.hpp>
namespace mpl = boost::mpl;
namespace proto = boost::proto;
// This grammar describes which vector expressions
// are allowed; namely, vector terminals and addition
// and subtraction of vector expressions.
struct VecGrammar : proto::or_<
proto::terminal< proto::_ >,
proto::plus< VecGrammar, VecGrammar>,
proto::minus< VecGrammar, VecGrammar>
> {};
// The above grammar is associated with this domain.
template<typename Expr> struct VecExpr;
struct VecDomain
: proto::domain<proto::generator<VecExpr>, VecGrammar> {};
//
// Context for evaluating an element of matrix expressions
//
struct SubscriptCntxt
: proto::callable_context<const SubscriptCntxt> {
typedef double result_type;
int index;
SubscriptCntxt(int index_) : index(index_) {}
// matrix element
template<typename Vector>
double operator()(proto::tag::terminal, const Vector& vec) const {
return vec[index];
}
// addition of vector expression terms
template<typename E1, typename E2>
double operator()(proto::tag::plus, const E1& e1, const E2& e2) const {
return proto::eval(e1, *this) + proto::eval(e2, *this);
}
// substraction of vector expression terms
template<typename E1, typename E2>
double operator()(proto::tag::minus, const E1& e1, const E2& e2) const {
return proto::eval(e1, *this) - proto::eval(e2, *this);
}
};
//
// Vector Expression Templates
//
template<typename Expr>
struct VecExpr
: proto::extends<Expr, VecExpr<Expr>, VecDomain> {
explicit VecExpr(const Expr& e)
: proto::extends<Expr, VecExpr<Expr>, VecDomain>(e) {
}
// Use a SubscriptCntxt instance to implement subscripting
// of a vector expression tree.
typename proto::result_of::eval< Expr, SubscriptCntxt>::type
operator [](int i) const {
const SubscriptCntxt ctx(i);
return proto::eval(*this, ctx);
}
};
//
// Matrix data are stored in an heap array.
//
class Vector {
private:
int sz;
double* data;
public:
explicit Vector(int sz_ = 1, double iniVal = 0.0) :
sz( sz_), data( new double[sz] ) {
for (int i = 0; i < sz; i++) data[i] = iniVal;
std::cout << "Created" << std::endl;
}
Vector(const Vector& vec) :
sz( vec.sz), data( new double[sz] ) {
for (int i = 0; i < sz; i++) data[i] = vec.data[i];
std::cout << "Copied" << std::endl;
}
~Vector() {
delete [] data;
std::cout << "Deleted" << std::endl;
}
// accesing to a vector element
double& operator[](int i) { return data[i]; }
const double& operator[](int i) const { return data[i]; }
// assigning the lhs of a vector expression into this matrix
template<typename Expr>
Vector& operator=( const Expr& expr ) {
for(int i=0; i < sz; ++i) {
// evaluating the i'th element of a matrix expression
const SubscriptCntxt ctx(i);
data[i] = proto::eval(proto::as_expr<VecDomain>(expr), ctx);
}
return *this;
}
// assigning and adding the lhs of a vector expression into this matrix
template<typename Expr>
Vector& operator+=( const Expr& expr ) {
for(int i=0; i < sz; ++i) {
// evaluating the (i,j) element of a matrix expression
const SubscriptCntxt ctx(i);
data[i] += proto::eval(proto::as_expr<VecDomain>(expr), ctx);
}
return *this;
}
};
// Define a trait for detecting vector terminals, to be used
// by the BOOST_PROTO_DEFINE_OPERATORS macro below.
template<typename> struct IsVector : mpl::false_ {};
template<> struct IsVector<Vector> : mpl::true_ {};
namespace VectorOps {
// This defines all the overloads to make expressions involving
// Vector objects to build expression templates.
BOOST_PROTO_DEFINE_OPERATORS(IsVector, VecDomain)
}
int main()
{
using namespace VectorOps;
// lazy_vectors with 4 elements each.
Vector v1( 4, 1.0 ), v2( 4, 2.0 ), v3( 4, 3.0 );
// Add two vectors lazily and get the 2nd element.
double d1 = ( v2 + v3 )[ 2 ]; // Look ma, no temporaries!
std::cout << d1 << std::endl;
// Subtract two vectors and add the result to a third vector.
v1 += v2 - v3; // Still no temporaries!
std::cout << '{' << v1[0] << ',' << v1[1]
<< ',' << v1[2] << ',' << v1[3] << '}' << std::endl;
// This expression is disallowed because it does not conform
// to the LazyVectorGrammar
//(v2 + v3) += v1;
return 0;
}
我确认这段代码可以工作,并且输出实际上与Boost中的"Lazy Vector"示例相同。原型用户指南。
虽然我仍然不确定的事情是如何在引擎盖下的Boost。Proto,用它来制作EDSL原型是非常有趣的。
我希望我可能已经部分解决了我问题中的问题。在ArrayWrapper类中添加复制构造函数后,Boost中的"Lazy Vector"示例的修改版本。原型用户指南工作无误
源代码:
// The original version of this file is :
// "Lazy Vector: Controlling Operator Overloads"
// in Boost.Proto users' guide.
// Copyright 2008 Eric Niebler. Distributed under the Boost
// Software License, Version 1.0.
//
// It was modified to try protofying a primitive array
// on May 20 2015.
#include <vector>
#include <iostream>
#include <boost/mpl/int.hpp>
#include <boost/proto/core.hpp>
#include <boost/proto/context.hpp>
namespace mpl = boost::mpl;
namespace proto = boost::proto;
using proto::_;
template <typename T>
class ArrayWrapper {
private:
T* data;
size_t size_;
public:
typedef T value_type;
explicit ArrayWrapper(std::size_t size = 0, T const & value = T() ):
data( new T[size]), size_(size) {
for (std::size_t i = 0; i < size_; i++) data[i] = value;
}
ArrayWrapper(const ArrayWrapper<T>& wrapper):
data( new T[ wrapper.size_] ), size_(wrapper.size_) {
for (std::size_t i = 0; i < size_; i++) data[i] = wrapper.data[i];
}
~ArrayWrapper() {
std::cerr << "Now destructing an ArrayWrapper" << std::endl;
delete [] data;
}
std::size_t size() {
return size_;
}
T& operator[](std::size_t i) { return data[i]; }
T operator[](std::size_t i) const { return data[i]; }
};
template<typename Expr>
struct lazy_vector_expr;
// This grammar describes which lazy vector expressions
// are allowed; namely, vector terminals and addition
// and subtraction of lazy vector expressions.
struct LazyVectorGrammar
: proto::or_<
proto::terminal< ArrayWrapper<_> >
, proto::plus< LazyVectorGrammar, LazyVectorGrammar >
, proto::minus< LazyVectorGrammar, LazyVectorGrammar >
>
{};
// Tell proto that in the lazy_vector_domain, all
// expressions should be wrapped in laxy_vector_expr<>
// and must conform to the lazy vector grammar.
struct lazy_vector_domain
: proto::domain<proto::generator<lazy_vector_expr>, LazyVectorGrammar>
{};
// Here is an evaluation context that indexes into a lazy vector
// expression, and combines the result.
template<typename Size = std::size_t>
struct lazy_subscript_context
{
lazy_subscript_context(Size subscript)
: subscript_(subscript)
{}
// Use default_eval for all the operations ...
template<typename Expr, typename Tag = typename Expr::proto_tag>
struct eval
: proto::default_eval<Expr, lazy_subscript_context>
{};
// ... except for terminals, which we index with our subscript
template<typename Expr>
struct eval<Expr, proto::tag::terminal>
{
typedef typename proto::result_of::value<Expr>::type::value_type result_type;
result_type operator ()( Expr const & expr, lazy_subscript_context & ctx ) const
{
return proto::value( expr )[ ctx.subscript_ ];
}
};
Size subscript_;
};
// Here is the domain-specific expression wrapper, which overrides
// operator [] to evaluate the expression using the lazy_subscript_context.
template<typename Expr>
struct lazy_vector_expr
: proto::extends<Expr, lazy_vector_expr<Expr>, lazy_vector_domain>
{
lazy_vector_expr( Expr const & expr = Expr() )
: lazy_vector_expr::proto_extends( expr )
{}
// Use the lazy_subscript_context<> to implement subscripting
// of a lazy vector expression tree.
template< typename Size >
typename proto::result_of::eval< Expr, lazy_subscript_context<Size> >::type
operator []( Size subscript ) const
{
lazy_subscript_context<Size> ctx(subscript);
return proto::eval(*this, ctx);
}
};
// Here is our lazy_vector terminal, implemented in terms of lazy_vector_expr
template< typename T >
struct lazy_vector
: lazy_vector_expr< typename proto::terminal< ArrayWrapper<T> >::type >
{
typedef typename proto::terminal< ArrayWrapper<T> >::type expr_type;
lazy_vector( std::size_t size = 0, T const & value = T() )
: lazy_vector_expr<expr_type>( expr_type::make( ArrayWrapper<T>(size, value) ) )
{}
// Here we define a += operator for lazy vector terminals that
// takes a lazy vector expression and indexes it. expr[i] here
// uses lazy_subscript_context<> under the covers.
template< typename Expr >
lazy_vector & operator += (Expr const & expr)
{
std::size_t size = proto::value(*this).size();
for(std::size_t i = 0; i < size; ++i)
{
proto::value(*this)[i] += expr[i];
}
return *this;
}
};
int main()
{
// lazy_vectors with 4 elements each.
lazy_vector< double > v1( 4, 1.0 ), v2( 4, 2.0 ), v3( 4, 3.0 );
// Add two vectors lazily and get the 2nd element.
double d1 = ( v2 + v3 )[ 2 ]; // Look ma, no temporaries!
std::cout << d1 << std::endl;
// Subtract two vectors and add the result to a third vector.
v1 += v2 - v3; // Still no temporaries!
std::cout << '{' << v1[0] << ',' << v1[1]
<< ',' << v1[2] << ',' << v1[3] << '}' << std::endl;
// This expression is disallowed because it does not conform
// to the LazyVectorGrammar
//(v2 + v3) += v1;
return 0;
}
但是我不确定为什么ArrayWrapper
的默认复制构造函数在我显式定义复制构造函数之前导致了核心转储。当lazy_vector
类构造函数中的expr_type::make( ArrayWrapper<T>(size, value) ) )
初始化expr_type
类的数据成员时,可能由proto::expr< proto::tag::terminal, proto::term< ArrayWrapper<T>>>
类调用复制构造函数(参见proto::expr的摘要)。正如您在lazy_vector
类的定义中看到的那样,expr_type
被typedef
定义为proto::expr< proto::tag::terminal, proto::term< ArrayWrapper<T>>>
,因此其数据成员proto_childN
的类型变为ArrayWrapper<T>
。
此外,剩下的问题是ArrayWrapper<T>
对象的复制操作减慢了程序的速度,这与表达式模板的目的相反。所以我应该承认我的答案不够好。
- 使用std::vector的OpenCL矩阵乘法
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- std::vector的包装器,使数组的结构看起来像结构的数组
- 编译器如何区分std::vector的构造函数
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 在std::vector上存储带有模板的类实例
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 为什么std::vector比数组慢
- std::vector::迭代器是否可以合法地作为指针
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 为什么std::vector和std::valarray初始化构造函数不同
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 在没有未定义行为的情况下实现类似std::vector的容器
- 如何调整 std::vector of Eigen::MatrixXd 的大小
- 使用 std::vector::reverse_iterator 将 int 序列化为字节向量?
- 如何将AERT_Allocate与 std:vector 一起使用
- 推导 std::vector::back() 的返回类型
- 如何将原始字节附加到 std::vector?
- std::vector 没有重载函数的实例与参数列表匹配
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?