add first slice tests + corresp fixes

This commit is contained in:
Christian Zimmermann 2023-10-24 02:01:29 +02:00
parent 2a763d67be
commit cb2db9c80d
12 changed files with 134 additions and 27 deletions

View file

@ -19,6 +19,13 @@ namespace CNORXZ
mCData(data) mCData(data)
{} {}
template <typename T>
AIndex<T>::AIndex(const T* data, const AIndex<T>& aindex) :
AIndex<T>(aindex)
{
mCData = data;
}
template <typename T> template <typename T>
AIndex<T> AIndex<T>::operator+(Int n) const AIndex<T> AIndex<T>::operator+(Int n) const
{ {
@ -55,7 +62,7 @@ namespace CNORXZ
template <typename T> template <typename T>
BIndex<T>::BIndex(T* data, const AIndex<T>& ai) : BIndex<T>::BIndex(T* data, const AIndex<T>& ai) :
AIndex<T>(data, ai.range(), ai.lex()), AIndex<T>(data, ai),
mData(data) mData(data)
{} {}

View file

@ -21,6 +21,7 @@ namespace CNORXZ
DEFAULT_MEMBERS(AIndex); DEFAULT_MEMBERS(AIndex);
AIndex(const T* data, const RangePtr& range, SizeT lexpos = 0); AIndex(const T* data, const RangePtr& range, SizeT lexpos = 0);
AIndex(const T* data, const YIndex& yindex); AIndex(const T* data, const YIndex& yindex);
AIndex(const T* data, const AIndex<T>& aindex);
AIndex operator+(Int n) const; AIndex operator+(Int n) const;
AIndex operator-(Int n) const; AIndex operator-(Int n) const;

View file

@ -69,7 +69,7 @@ namespace CNORXZ
{ {
auto ai = itLexSave(begin); auto ai = itLexSave(begin);
auto aj = itLexSave(end); auto aj = itLexSave(end);
return std::make_shared<CSlice>(ai.prange(aj), this, ai.format(), ai.pos()); return std::make_shared<CSlice<T>>(ai.prange(aj), this, ai.format(), ai.pos());
} }
template <typename T> template <typename T>
@ -231,12 +231,12 @@ namespace CNORXZ
template <typename T> template <typename T>
template <typename I, typename M> template <typename I, typename M>
Sptr<ArrayBase<T>> ArrayBase<T>::sl(const IndexInterface<I,M>& i) Sptr<ArrayBase<T>> ArrayBase<T>::sl(const IndexInterface<I,M>& begin,
const IndexInterface<I,M>& end)
{ {
auto beg = std::make_shared<const_iterator>(this->cbegin()); auto ai = itLexSave(begin);
auto si = i.slice(beg); auto aj = itLexSave(end);
auto it = *beg + i.lex(); return std::make_shared<Slice<T>>(ai.prange(aj), this, ai.format(), ai.pos());
return std::make_shared<Slice>(this, *si, it.pos());
} }
template <typename T> template <typename T>

View file

@ -91,6 +91,7 @@ namespace CNORXZ
using CAB::end; using CAB::end;
using CAB::cbegin; using CAB::cbegin;
using CAB::cend; using CAB::cend;
using CAB::sl;
//using CAB::operator(); //using CAB::operator();
ArrayBase(const RangePtr& range); ArrayBase(const RangePtr& range);
@ -120,7 +121,8 @@ namespace CNORXZ
inline decltype(auto) operator()(const DPack& pack); inline decltype(auto) operator()(const DPack& pack);
template <typename I, typename M> template <typename I, typename M>
Sptr<ArrayBase<T>> sl(const IndexInterface<I,M>& i); Sptr<ArrayBase<T>> sl(const IndexInterface<I,M>& begin,
const IndexInterface<I,M>& end);
virtual T* data() = 0; virtual T* data() = 0;

View file

@ -12,7 +12,7 @@ namespace CNORXZ
template <typename T> template <typename T>
CSlice<T>::CSlice(const RangePtr& range, const CArrayBase<T>* parent, CSlice<T>::CSlice(const RangePtr& range, const CArrayBase<T>* parent,
const Vector<SizeT>& blockSizes, SizeT off) : const YFormat& blockSizes, SizeT off) :
CArrayBase<T>(range), CArrayBase<T>(range),
mCParent(parent), mCParent(parent),
mBlockSizes(blockSizes), mBlockSizes(blockSizes),
@ -28,13 +28,13 @@ namespace CNORXZ
template <typename T> template <typename T>
typename CSlice<T>::const_iterator CSlice<T>::cbegin() const typename CSlice<T>::const_iterator CSlice<T>::cbegin() const
{ {
return const_iterator(YIndex(AB::mRange, mBlockSizes, 0)); return const_iterator(data(),YIndex(AB::mRange, mBlockSizes, 0));
} }
template <typename T> template <typename T>
typename CSlice<T>::const_iterator CSlice<T>::cend() const typename CSlice<T>::const_iterator CSlice<T>::cend() const
{ {
return const_iterator(YIndex(AB::mRange, mBlockSizes, AB::mRange->size())); return const_iterator(data(),YIndex(AB::mRange, mBlockSizes, AB::mRange->size()));
} }
template <typename T> template <typename T>
@ -49,15 +49,41 @@ namespace CNORXZ
template <typename T> template <typename T>
Slice<T>::Slice(const RangePtr& range, ArrayBase<T>* parent, Slice<T>::Slice(const RangePtr& range, ArrayBase<T>* parent,
const Vector<SizeT>& blockSizes, SizeT off) : const YFormat& blockSizes, SizeT off) :
CSlice<T>(range, parent, blockSizes, off), ArrayBase<T>(range),
mParent(parent) mParent(parent),
mBlockSizes(blockSizes),
mOff(off)
{} {}
template <typename T> template <typename T>
T* Slice<T>::data() T* Slice<T>::data()
{ {
return mParent->data(); return mParent->data() + mOff;
}
template <typename T>
const T* Slice<T>::data() const
{
return mParent->data() + mOff;
}
template <typename T>
typename Slice<T>::const_iterator Slice<T>::cbegin() const
{
return const_iterator(data(),YIndex(AB::mRange, mBlockSizes, 0));
}
template <typename T>
typename Slice<T>::const_iterator Slice<T>::cend() const
{
return const_iterator(data(),YIndex(AB::mRange, mBlockSizes, AB::mRange->size()));
}
template <typename T>
bool Slice<T>::isView() const
{
return true;
} }
} }

View file

@ -16,13 +16,13 @@ namespace CNORXZ
protected: protected:
const CArrayBase<T>* mCParent = nullptr; const CArrayBase<T>* mCParent = nullptr;
Vector<SizeT> mBlockSizes; YFormat mBlockSizes;
SizeT mOff = 0; SizeT mOff = 0;
public: public:
DEFAULT_MEMBERS(CSlice); DEFAULT_MEMBERS(CSlice);
CSlice(const RangePtr& range, const CArrayBase<T>* parent, CSlice(const RangePtr& range, const CArrayBase<T>* parent,
const Vector<SizeT>& blockSizes, SizeT off); const YFormat& blockSizes, SizeT off);
virtual const T* data() const override; virtual const T* data() const override;
virtual const_iterator cbegin() const override; virtual const_iterator cbegin() const override;
@ -31,22 +31,28 @@ namespace CNORXZ
}; };
template <typename T> template <typename T>
class Slice : public virtual ArrayBase<T>, class Slice : public ArrayBase<T>
public virtual CSlice<T>
{ {
public: public:
typedef CArrayBase<T> AB; typedef CArrayBase<T> AB;
//typedef CSlice<T> CS;
typedef typename AB::const_iterator const_iterator; typedef typename AB::const_iterator const_iterator;
private: private:
ArrayBase<T>* mParent = nullptr; ArrayBase<T>* mParent = nullptr;
YFormat mBlockSizes;
SizeT mOff = 0;
public: public:
DEFAULT_MEMBERS(Slice); DEFAULT_MEMBERS(Slice);
Slice(const RangePtr& range, ArrayBase<T>* parent, Slice(const RangePtr& range, ArrayBase<T>* parent,
const Vector<SizeT>& blockSizes, SizeT off); const YFormat& blockSizes, SizeT off);
virtual const T* data() const override;
virtual T* data() override; virtual T* data() override;
virtual const_iterator cbegin() const override;
virtual const_iterator cend() const override;
virtual bool isView() const override final;
}; };
} }

View file

@ -79,6 +79,12 @@ namespace CNORXZ
get(CSizeT<sizeof...(Indices)-1>{})->lex() ; get(CSizeT<sizeof...(Indices)-1>{})->lex() ;
} }
template <class... Indices>
SizeT SPack<Indices...>::pos() const
{
return lex();
}
/************************** /**************************
* SPack (non-member) * * SPack (non-member) *
**************************/ **************************/

View file

@ -37,6 +37,7 @@ namespace CNORXZ
decltype(auto) mkRange() const; decltype(auto) mkRange() const;
SizeT lex() const; SizeT lex() const;
SizeT pos() const;
private: private:
Tuple<Sptr<Indices>...> mIs; Tuple<Sptr<Indices>...> mIs;
@ -67,6 +68,7 @@ namespace CNORXZ
DPack mul(const DPack& p) const; DPack mul(const DPack& p) const;
RangePtr mkRange() const; RangePtr mkRange() const;
SizeT lex() const; SizeT lex() const;
SizeT pos() const;
private: private:
Vector<XIndexPtr> mIs; Vector<XIndexPtr> mIs;

View file

@ -512,7 +512,7 @@ namespace CNORXZ
decltype(auto) replaceFormat(const BT1& bs1, const Sptr<GMIndex<BT2,Indices...>>& gmi) decltype(auto) replaceFormat(const BT1& bs1, const Sptr<GMIndex<BT2,Indices...>>& gmi)
{ {
return iter<0,sizeof...(Indices)> return iter<0,sizeof...(Indices)>
( [&](auto i) { return std::get<i>(gmi->pack()); }, ( [&](auto i) { return gmi->pack()[CSizeT<i>{}]; },
[&](const auto&... e) { return std::make_shared<GMIndex<BT1,Indices...>> [&](const auto&... e) { return std::make_shared<GMIndex<BT1,Indices...>>
( bs1, e... ); } ); ( bs1, e... ); } );
} }

View file

@ -77,4 +77,9 @@ namespace CNORXZ
} }
return o; return o;
} }
SizeT DPack::pos() const
{
return lex();
}
} }

View file

@ -129,8 +129,8 @@ namespace CNORXZ
IndexInterface<YIndex,Vector<DType>>(i), IndexInterface<YIndex,Vector<DType>>(i),
mRange(rangeCast<YRange>(i.range())), mRange(rangeCast<YRange>(i.range())),
mIs(mkIndices()), mIs(mkIndices()),
mFormat(mkFormat()), mFormat(i.mFormat),
mLexFormat(mkLexFormat()), mLexFormat(i.mLexFormat),
mPMax(mkPMax()), mPMax(mkPMax()),
mLMax(mkLMax()) mLMax(mkLMax())
{ {
@ -142,8 +142,8 @@ namespace CNORXZ
IndexInterface<YIndex,Vector<DType>>::operator=(i); IndexInterface<YIndex,Vector<DType>>::operator=(i);
mRange = rangeCast<YRange>(i.range()); mRange = rangeCast<YRange>(i.range());
mIs = mkIndices(); mIs = mkIndices();
mFormat = mkFormat(); mFormat = i.mFormat;
mLexFormat = mkLexFormat(); mLexFormat = i.mLexFormat;
mPMax = mkPMax(); mPMax = mkPMax();
mLMax = mkLMax(); mLMax = mkLMax();
return *this = i.lex(); return *this = i.lex();

View file

@ -12,7 +12,7 @@ namespace
{ {
using namespace CNORXZ; using namespace CNORXZ;
using Test::Numbers; using Test::Numbers;
class MA_1D_Test : public ::testing::Test class MA_1D_Test : public ::testing::Test
{ {
protected: protected:
@ -52,7 +52,7 @@ namespace
MArray<Double> mA; MArray<Double> mA;
MArray<Double> mA2; MArray<Double> mA2;
}; };
TEST_F(MA_1D_Test, Basics) TEST_F(MA_1D_Test, Basics)
{ {
auto crx = std::dynamic_pointer_cast<CRange>(mCR1); auto crx = std::dynamic_pointer_cast<CRange>(mCR1);
@ -69,6 +69,28 @@ namespace
EXPECT_THROW({mA.at(ei);}, std::runtime_error); EXPECT_THROW({mA.at(ei);}, std::runtime_error);
} }
TEST_F(MA_1D_Test, Slice)
{
auto crx = std::dynamic_pointer_cast<CRange>(mCR1);
auto beg = crx->begin() + 2;
auto last = crx->begin() + 5;
//const auto& a = mA;
auto sl = mA.sl(beg,last);
EXPECT_EQ(sl->size(), last.lex()-beg.lex()+1);
SizeT c = 0;
auto ii = crx->begin();
for(auto i = sl->begin() ; i != sl->end(); ++i, ++c, ++ii){
EXPECT_EQ(i.lex(), c);
EXPECT_EQ(ii.lex(), c);
EXPECT_EQ((*sl)[i], mA.data()[i.lex()+beg.lex()]);
EXPECT_EQ((*sl)[ii], mA.data()[i.lex()+beg.lex()]);
EXPECT_EQ(sl->at(i), mA.data()[i.lex()+beg.lex()]);
EXPECT_EQ(sl->at(ii), mA.data()[i.lex()+beg.lex()]);
}
}
TEST_F(MA_2D_Test, Basics) TEST_F(MA_2D_Test, Basics)
{ {
const SizeT ssize = mStrMeta.size(); const SizeT ssize = mStrMeta.size();
@ -99,6 +121,36 @@ namespace
} }
} }
TEST_F(MA_2D_Test, Slice)
{
auto crx = std::dynamic_pointer_cast<CRange>(mCR1);
auto beg = crx->begin() + 2;
auto last = crx->begin() + 5;
auto uip1 = std::make_shared<UIndex<String>>(mUR1);
auto cip1 = std::make_shared<CIndex>(mCR1);
auto beg1 = (*cip1)+1;
auto last1 = (*cip1)+3;
auto beg2 = (*uip1)+2;
auto last2 = (*uip1)+5;
auto sl = mA.sl(mindex(beg1*beg2),mindex(last1*last2));
EXPECT_EQ(sl->size(), (last1.lex()-beg1.lex()+1)*(last2.lex()-beg2.lex()+1));
MIndex<PIndex<CIndex>,PIndex<UIndex<String>>> mi(sl->range());
EXPECT_EQ(mi.lmax().val(), sl->size());
auto i1 = *mi.pack()[CSizeT<0>{}];
auto i2 = *mi.pack()[CSizeT<1>{}];
EXPECT_EQ(i1.lmax().val(), last1.lex()-beg1.lex()+1);
EXPECT_EQ(i2.lmax().val(), last2.lex()-beg2.lex()+1);
for(i1 = 0; i1.lex() != i1.lmax().val(); ++ i1){
for(i2 = 0; i2.lex() != i2.lmax().val(); ++i2){
const SizeT p = (i1.lex()+beg1.lex())*uip1->lmax().val() + i2.lex()+beg2.lex();
EXPECT_EQ((*sl)[i1*i2], mA.data()[p]);
}
}
}
} // end namespace } // end namespace
int main(int argc, char** argv) int main(int argc, char** argv)