fix this ptr copy/overwrite issue in index interface class

This commit is contained in:
Christian Zimmermann 2018-09-18 14:30:49 +02:00
parent 6c404a272b
commit 8e5e5af924
3 changed files with 66 additions and 24 deletions

View file

@ -64,7 +64,7 @@ namespace MultiArrayTools
OpExpr& operator=(const OpExpr& in) = default;
OpExpr& operator=(OpExpr&& in) = default;
OpExpr(const MapF& mapf, const IndexPack& ipack, const std::shared_ptr<OIType> oind, size_t step, Expr ex);
OpExpr(const MapF& mapf, const IndexPack& ipack, const std::shared_ptr<OIType>& oind, size_t step, Expr ex);
inline void operator()(size_t mlast, ExtType last) const;
inline void operator()(size_t mlast = 0) const;
@ -302,27 +302,17 @@ namespace MultiArrayTools
template <class MapF, class IndexPack, class Expr>
OpExpr<MapF,IndexPack,Expr>::OpExpr(const MapF& mapf, const IndexPack& ipack,
const std::shared_ptr<OIType> oind, size_t step, Expr ex) :
mIndPtr(oind.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()), mStep(step), mExpr(ex),
const std::shared_ptr<OIType>& oind, size_t step, Expr ex) :
mIndPtr(oind.get()), mSPos(mIndPtr->pos()), mMax(mIndPtr->max()),
mStep(step), mExpr( std::forward<Expr>(ex) ),
mOp(mkMapOp(mapf, ipack)),
//mExt(ex.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr )))
mExt( mOp.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr) ).extend
( mExpr.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr) ) ) )
mExt( mOp.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr ) ).extend
( ex.rootSteps( reinterpret_cast<std::intptr_t>( mIndPtr ) ) ) )
{
assert(mIndPtr != nullptr);
//VCHECK(mIndPtr->id());
//VCHECK(mIndPtr->max());
}
template <typename T>
inline void printxxx(const T& a) {}
template <>
inline void printxxx(const std::array<int,2>& a)
{
std::cout << "( " << std::get<0>(a) << " , " << std::get<1>(a) << " )" << std::endl;
}
template <class MapF, class IndexPack, class Expr>
inline void OpExpr<MapF,IndexPack,Expr>::operator()(size_t mlast,
ExtType last) const
@ -575,10 +565,12 @@ namespace MultiArrayTools
template <class Exprs>
auto MapIndex<MapF,Indices...>::ifor(size_t step, Exprs exs) const
-> decltype(RPackNum<sizeof...(Indices)-1>::mkForh
(step, mIPack, mBlockSizes, OpExpr<MapF,IndexPack,Exprs>( range()->map(), mIPack, mOutIndex, step, exs ) ) )
(step, mIPack, mBlockSizes, OpExpr<MapF,IndexPack,Exprs>
( range()->map(), mIPack, mOutIndex, step, exs ) ) )
{
return RPackNum<sizeof...(Indices)-1>::mkForh
(step, mIPack, mBlockSizes, OpExpr<MapF,IndexPack,Exprs>( range()->map(), mIPack, mOutIndex, step, exs ) );
(step, mIPack, mBlockSizes, OpExpr<MapF,IndexPack,Exprs>
( range()->map(), mIPack, mOutIndex, step, exs ) );
}
/*
template <class MapF, class... Indices>

View file

@ -28,7 +28,7 @@ namespace MultiArrayTools
I const& THIS() const { return static_cast<I const&>(*this); }
static constexpr bool ISINDEX = true;
~IndexInterface() = default;
constexpr IndexType type() const { return THIS().type(); }
@ -82,10 +82,10 @@ namespace MultiArrayTools
friend I;
IndexInterface();
IndexInterface(const IndexInterface& in) = default;
IndexInterface& operator=(const IndexInterface& in) = default;
IndexInterface(IndexInterface&& in) = default;
IndexInterface& operator=(IndexInterface&& in) = default;
IndexInterface(const IndexInterface& in);
IndexInterface& operator=(const IndexInterface& in);
IndexInterface(IndexInterface&& in);
IndexInterface& operator=(IndexInterface&& in);
IndexInterface(const std::shared_ptr<RangeBase>& range, size_t pos);
@ -120,6 +120,40 @@ namespace MultiArrayTools
{
mPtrNum = reinterpret_cast<std::intptr_t>(this);
}
template <class I, typename MetaType>
IndexInterface<I,MetaType>::IndexInterface(const IndexInterface& in) : mRangePtr(in.mRangePtr),
mPos(in.mPos),
mMax(in.mMax)
{
mId = indexId();
mPtrNum = reinterpret_cast<std::intptr_t>(this);
}
template <class I, typename MetaType>
IndexInterface<I,MetaType>::IndexInterface(IndexInterface&& in) : mRangePtr(in.mRangePtr),
mPos(in.mPos),
mMax(in.mMax)
{
mId = indexId();
mPtrNum = reinterpret_cast<std::intptr_t>(this);
}
template <class I, typename MetaType>
IndexInterface<I,MetaType>& IndexInterface<I,MetaType>::operator=(const IndexInterface& in)
{
mRangePtr = in.mRangePtr;
mPos = in.mPos;
mMax = in.mMax;
}
template <class I, typename MetaType>
IndexInterface<I,MetaType>& IndexInterface<I,MetaType>::operator=(IndexInterface&& in)
{
mRangePtr = in.mRangePtr;
mPos = in.mPos;
mMax = in.mMax;
}
template <class I, typename MetaType>
IndexInterface<I,MetaType>::IndexInterface(const std::shared_ptr<RangeBase>& range,

View file

@ -325,14 +325,19 @@ namespace {
auto mr = mkMapR(std::make_shared<plus<size_t>>(),sr1ptr,sr2ptr);
MultiArray<double,MpRange> res(mr);
MultiArray<double,MpRange> res2(mr);
auto jj = getIndex( mr );
(*jj)(ii1,ii2);
///auto jj = mkMapI( std::make_shared<plus<size_t> >(), ii1, ii1 );
res(jj) = ma1(ii1,ii2);
auto mult = mr->mapMultiplicity();
auto jjx = jj->outIndex();
res2(jj) = ma1(ii1,ii2) / staticcast<double>( mult(jjx) );
MultiArray<double,TRange> form = res.format(mpr1ptr->outRange());
MultiArray<double,TRange> form2 = res2.format(mpr1ptr->outRange());
EXPECT_EQ( jj->range()->outRange()->size(), 10 );
EXPECT_EQ( jj->range()->mapMultiplicity().at(9), 3 );
EXPECT_EQ( jj->range()->mapMultiplicity().at(3), 1 );
@ -347,6 +352,17 @@ namespace {
EXPECT_EQ( form.at(12), -42.53 );
EXPECT_EQ( form.at(16), 80.41 );
EXPECT_EQ( form.at(18), 6.35 );
EXPECT_EQ( form2.at(3), -31.71 );
EXPECT_EQ( form2.at(7), -77.16 );
EXPECT_EQ( xround( form2.at(9) ), xround( (-18.81 + 72.31 -50.91) / 3. ) );
EXPECT_EQ( form2.at(5), -67.06 );
EXPECT_EQ( form2.at(11), -54.48 );
EXPECT_EQ( form2.at(13), -11.62 );
EXPECT_EQ( form2.at(15), -59.57 );
EXPECT_EQ( form2.at(12), -42.53 );
EXPECT_EQ( form2.at(16), 80.41 );
EXPECT_EQ( form2.at(18), 6.35 );
}
TEST_F(MetaOp_Test, SimpleCall)