dynamic range test works

This commit is contained in:
Christian Zimmermann 2018-10-29 14:19:42 +01:00
parent 9cf294f485
commit 305f45103c
8 changed files with 240 additions and 68 deletions

View file

@ -16,8 +16,8 @@ namespace MultiArrayTools
typedef SingleIndex<size_t,SpaceType::ANON> AnonymousIndex;
template <class R>
using SIZET = size_t;
//template <class R>
//using SIZET = size_t;
typedef SingleRange<size_t,SpaceType::ANON> AnonymousRange;

View file

@ -0,0 +1,43 @@
#ifndef __dynamic_meta_h__
#define __dynamic_meta_h__
#include <cstdlib>
#include <utility>
namespace MultiArrayTools
{
typedef std::pair<const char*,size_t> DynamicMetaElem;
class DynamicMetaT
{
private:
std::vector<DynamicMetaElem> mMeta;
public:
DynamicMetaT() = default;
DynamicMetaT(const DynamicMetaT& in) = default;
DynamicMetaT(DynamicMetaT&& in) = default;
DynamicMetaT& operator=(const DynamicMetaT& in) = default;
DynamicMetaT& operator=(DynamicMetaT&& in) = default;
template <typename... Us>
DynamicMetaT(const std::tuple<Us...>& meta)
{
}
bool operator==(const DynamicMetaT& in) const;
bool operator!=(const DynamicMetaT& in) const;
size_t size() const;
DynamicMetaElem& operator[](size_t pos);
const DynamicMetaElem& operator[](size_t pos) const;
};
} // namespace MultiArrayTools
#endif

View file

@ -6,33 +6,20 @@
#include "ranges/range_base.h"
#include "ranges/index_base.h"
#include "xfor/xfor.h"
#include <map>
#include "ranges/rpheader.h"
#include "ranges/x_to_string.h"
#include "ranges/type_map.h"
#include "ranges/dynamic_meta.h"
namespace MultiArrayTools
{
typedef std::pair<const char*,size_t> DynamicMetaElem;
using MultiArrayHelper::DynamicalExpression;
class DynamicMetaT
{
private:
std::vector<DynamicMetaElem> mMeta;
public:
DynamicMetaT() = default;
DynamicMetaT(const DynamicMetaT& in) = default;
DynamicMetaT(DynamicMetaT&& in) = default;
DynamicMetaT& operator=(const DynamicMetaT& in) = default;
DynamicMetaT& operator=(DynamicMetaT&& in) = default;
template <typename... Us>
DynamicMetaT(const std::tuple<Us...>& meta);
bool operator==(const DynamicMetaT& in) const;
bool operator!=(const DynamicMetaT& in) const;
DynamicMetaElem& operator[](size_t pos);
const DynamicMetaElem& operator[](size_t pos) const;
};
class IndexWrapperBase
{
public:
@ -53,13 +40,15 @@ namespace MultiArrayTools
virtual int mm(std::intptr_t idxPtrNum) = 0;
virtual std::string stringMeta() const = 0;
virtual DynamicMetaT meta() const = 0;
//virtual DynamicMetaT meta() const = 0;
virtual const DynamicMetaT* metaPtr() const = 0;
//virtual IndexWrapperBase& at(const U& metaPos) = 0;
//virtual size_t posAt(const U& metaPos) const = 0;
//virtual bool isMeta(const U& metaPos) const = 0;
virtual size_t pos() const = 0;
virtual size_t max() const = 0;
virtual size_t dim() const = 0;
virtual bool last() const = 0;
virtual bool first() const = 0;
@ -67,7 +56,8 @@ namespace MultiArrayTools
virtual std::shared_ptr<RangeBase> range() const = 0;
virtual size_t getStepSize(size_t n) const = 0;
virtual size_t getStepSizeComp(std::intptr_t j) const = 0;
virtual std::intptr_t get() const = 0;
virtual DynamicalExpression ifor(size_t step, DynamicalExpression ex) const = 0;
@ -99,6 +89,9 @@ namespace MultiArrayTools
virtual IndexWrapperBase& operator++() final { ++(*mI); return *this; }
virtual IndexWrapperBase& operator--() final { --(*mI); return *this; }
virtual size_t pos() const final { return mI->pos(); }
virtual size_t max() const final { return mI->max(); }
virtual int pp(std::intptr_t idxPtrNum) final { return mI->pp(idxPtrNum); }
virtual int mm(std::intptr_t idxPtrNum) final { return mI->mm(idxPtrNum); }
@ -117,7 +110,8 @@ namespace MultiArrayTools
virtual std::shared_ptr<RangeBase> range() const final { return mI->range(); }
virtual size_t getStepSize(size_t n) const final { return mI->getStepSize(n); }
virtual size_t getStepSizeComp(std::intptr_t j) const final { return getStepSize(*this, j); }
virtual std::intptr_t get() const final { return reinterpret_cast<std::intptr_t>(mI.get()); }
virtual DynamicalExpression ifor(size_t step, DynamicalExpression ex) const final { return mI->ifor(step, ex); }
@ -130,7 +124,9 @@ namespace MultiArrayTools
class DynamicIndex : public IndexInterface<DynamicIndex,DynamicMetaT>
{
private:
std::vector<std::pair<std::shared_ptr<IndexW>,size_t>> mIVec;
typedef std::vector<std::pair<std::shared_ptr<IndexW>,size_t>> IVecT;
IVecT mIVec;
inline DynamicalExpression mkFor(size_t i, size_t step,
DynamicalExpression ex, bool hidden = false) const;
@ -155,6 +151,8 @@ namespace MultiArrayTools
DynamicIndex& operator++();
DynamicIndex& operator--();
DynamicIndex& operator()(const IVecT& ivec);
int pp(std::intptr_t idxPtrNum);
int mm(std::intptr_t idxPtrNum);
@ -166,17 +164,19 @@ namespace MultiArrayTools
//bool isMeta(const MetaType& metaPos) const;
size_t dim();
bool last();
bool first();
size_t dim() const;
bool last() const;
bool first() const;
const IndexW& get(size_t n) const;
std::shared_ptr<RangeType> range();
template <size_t N>
void getPtr();
size_t getStepSize(size_t n);
size_t getStepSize(size_t n) const;
std::string id() const;
void print(size_t offset);
@ -357,6 +357,21 @@ namespace MultiArrayHelper
}
}
template <>
inline size_t getStepSize<DynamicIndex>(const DynamicIndex& ii, std::intptr_t j)
{
size_t ss = 0;
size_t sx = 1;
for(size_t i = ii.dim(); i != 0; --i){
const auto& ni = ii.get(i);
const size_t max = ni.max();
const size_t tmp = ni.getStepSizeComp(j);
ss += tmp * ii.getStepSize(i);
sx *= max;
}
return ss;
}
}
namespace MultiArrayTools

View file

@ -3,8 +3,14 @@
#ifndef __ranges_base_def_h__
#define __ranges_base_def_h__
#include <cstdlib>
namespace MultiArrayTools
{
template <class R>
using SIZET = size_t;
/***********************
* Provided Types *
***********************/

View file

@ -7,6 +7,8 @@
#include <array>
#include <tuple>
#include "ranges/dynamic_meta.h"
namespace MultiArrayHelper
{
template <typename T>
@ -19,6 +21,11 @@ namespace MultiArrayHelper
template <>
inline std::string xToString<std::string>(const std::string& x);
using MultiArrayTools::DynamicMetaT;
template <>
inline std::string xToString<DynamicMetaT>(const DynamicMetaT& x);
template <typename T>
inline std::string xToString(const std::vector<T>& x);
@ -69,6 +76,19 @@ namespace MultiArrayHelper
return x;
}
template <>
inline std::string xToString<DynamicMetaT>(const DynamicMetaT& x)
{
std::string out = "[ ";
for(size_t i = 0; i != x.size(); ++i){
out += x[i].first;
out += " , ";
}
out.pop_back();
out.back() = ']';
return out;
}
template <typename T>
inline std::string xToString(const std::vector<T>& x)
{

View file

@ -2,6 +2,7 @@
set(libmultiarray_a_SOURCES
${CMAKE_SOURCE_DIR}/src/lib/ranges/range_base.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/anonymous_range.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/dynamic_range.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/multi_range_factory_product_map.cc
${CMAKE_SOURCE_DIR}/src/lib/map_range_factory_product_map.cc
)

View file

@ -1,16 +1,54 @@
#include "ranges/dynamic_range.h"
#include "ranges/dynamic_meta.h"
namespace MultiArrayTools
{
/**********************
/*********************
* DynamicMetaT *
*********************/
bool DynamicMetaT::operator==(const DynamicMetaT& in) const
{
if(in.size() != mMeta.size()) { return false; }
for(size_t i = 0; i != mMeta.size(); ++i){
if(in[i].second != mMeta[i].second) { return false; }
for(size_t j = 0; j != mMeta[i].second; ++j){
if(in[i].first[j] != mMeta[i].first[j]) { return false; }
}
}
return true;
}
bool DynamicMetaT::operator!=(const DynamicMetaT& in) const
{
return not operator==(in);
}
size_t DynamicMetaT::size() const
{
return mMeta.size();
}
DynamicMetaElem& DynamicMetaT::operator[](size_t pos)
{
return mMeta[pos];
}
const DynamicMetaElem& DynamicMetaT::operator[](size_t pos) const
{
return mMeta[pos];
}
/*********************
* DynamicIndex *
**********************/
*********************/
DynamicIndex::DynamicIndex(const std::shared_ptr<DynamicRange >& range) :
IndexInterface<DynamicIndex,MetaType>(range, 0),
mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr)),
IndexInterface<DynamicIndex,MetaType>(range, 0)//,
//mExplicitRangePtr(std::dynamic_pointer_cast<RangeType>(IB::mRangePtr)),
{}
@ -25,21 +63,38 @@ namespace MultiArrayTools
IB::mPos = pos;
return *this;
}
DynamicIndex& DynamicIndex::operator++()
{
// !!!
size_t ipos = mIVec.size()-1;
auto& ii = mIVec[ipos].first;
auto& jj = mIVec[ipos-1].first;
while(ii->pos() == ii->max()-1 and ipos != 0) {
(*ii) = 0;
++(*jj);
--ipos;
}
return *this;
}
DynamicIndex& DynamicIndex::operator--()
{
// !!!
size_t ipos = mIVec.size()-1;
auto& ii = mIVec[ipos].first;
auto& jj = mIVec[ipos-1].first;
while(ii->pos() == 0 and ipos != 0) {
(*ii) = ii->max()-1;
--(*jj);
--ipos;
}
return *this;
}
DynamicIndex& DynamicIndex::operator()(const IVecT& ivec)
{
mIVec = ivec;
return *this;
}
int DynamicIndex::pp(std::intptr_t idxPtrNum)
{
@ -61,13 +116,13 @@ namespace MultiArrayTools
}
MetaType DynamicIndex::meta() const
DynamicIndex::MetaType DynamicIndex::meta() const
{
return mExplicitRangePtr->meta(IB::mPos);
return std::dynamic_pointer_cast<DynamicRange const>( IB::mRangePtr )->get(IB::mPos);
}
const MetaType* DynamicIndex::metaPtr() const
const DynamicIndex::MetaType* DynamicIndex::metaPtr() const
{
return nullptr;
}
@ -86,29 +141,33 @@ namespace MultiArrayTools
}
size_t DynamicIndex::posAt(const U& metaPos) const
size_t DynamicIndex::posAt(const MetaType& metaPos) const
{
return std::dynamic_pointer_cast<DynamicRange const>( IB::mRangePtr )->getMeta( metaPos );
}
size_t DynamicIndex::dim() // = 1
size_t DynamicIndex::dim() const // = 1
{
return 1;
}
bool DynamicIndex::last()
bool DynamicIndex::last() const
{
return IB::mPos == IB::mMax - 1;
}
bool DynamicIndex::first()
bool DynamicIndex::first() const
{
return IB::mPos == 0;
}
const IndexW& DynamicIndex::get(size_t n) const
{
return *mIVec[n].first;
}
std::shared_ptr<typename DynamicIndex::RangeType> DynamicIndex::range()
{
@ -120,12 +179,11 @@ namespace MultiArrayTools
void DynamicIndex::getPtr() {}
size_t DynamicIndex::getStepSize(size_t n)
size_t DynamicIndex::getStepSize(size_t n) const
{
return 1;
return mIVec[n].second;
}
std::string DynamicIndex::id() const
{
return std::string("dyn") + std::to_string(IB::mId);
@ -139,7 +197,7 @@ namespace MultiArrayTools
}
for(size_t j = 0; j != offset; ++j) { std::cout << "\t"; }
std::cout << id() << "[" << reinterpret_cast<std::intptr_t>(this)
<< "](" << IB::mRangePtr << "): " << meta() << std::endl;
<< "](" << IB::mRangePtr << "): " /*<< meta().first*/ << std::endl;
}
@ -197,14 +255,14 @@ namespace MultiArrayTools
* DynamicRange *
***********************/
size_t DynamicRange::get(size_t pos) const
DynamicRange::MetaType DynamicRange::get(size_t pos) const
{
return pos;
return MetaType(); // !!!
}
size_t DynamicRange::getMeta(size_t metaPos) const
size_t DynamicRange::getMeta(const MetaType& metaPos) const
{
return metaPos;
return 0; // !!!
}
size_t DynamicRange::size() const
@ -214,7 +272,7 @@ namespace MultiArrayTools
size_t DynamicRange::dim() const
{
return 1;
return mOrig.size();
}
SpaceType DynamicRange::spaceType() const
@ -258,11 +316,6 @@ namespace MultiArrayTools
return out;
}
size_t DynamicRange::anonymousDim() const
{
return mOrig.size();
}
typename DynamicRange::IndexType DynamicRange::begin() const
{
DynamicIndex i
@ -296,7 +349,7 @@ namespace MultiArrayTools
/*****************
* Functions *
*****************/
/*
std::shared_ptr<DynamicRange> defaultRange(size_t size )
{
DynamicRangeFactory arf
@ -304,7 +357,7 @@ namespace MultiArrayTools
(DynamicRange::factory().create() ) );
return std::dynamic_pointer_cast<DynamicRange>( arf.create() );
}
*/
} // end namespace MultiArrayTools

View file

@ -123,6 +123,40 @@ namespace {
EXPECT_EQ(mr->template getPtr<1>()->size(), m3rptr->size() * sr2ptr->size());
}
TEST_F(AnonymousTest, DCast1)
{
DynamicRangeFactory arf1(sr1ptr,m3rptr);
auto ar1a = std::dynamic_pointer_cast<DynamicRange>( arf1.create() );
arf1.append(sr2ptr);
auto ar1b = std::dynamic_pointer_cast<DynamicRange>( arf1.create() );
DynamicRangeFactory arf2(sr1ptr,m3rptr,sr2ptr);
auto ar2 = std::dynamic_pointer_cast<DynamicRange>( arf2.create() );
EXPECT_EQ(ar1b.get(), ar2.get());
EXPECT_EQ(ar1a->size(), sr1ptr->size() * m3rptr->size());
EXPECT_EQ(ar2->size(), sr1ptr->size() * m3rptr->size() * sr2ptr->size());
EXPECT_EQ(ar1a->sub(0)->size(), sr1ptr->size());
EXPECT_EQ(ar1a->sub(1)->size(), m3rptr->size());
EXPECT_EQ(ar2->sub(0)->size(), sr1ptr->size());
EXPECT_EQ(ar2->sub(1)->size(), m3rptr->size());
EXPECT_EQ(ar2->sub(2)->size(), sr2ptr->size());
}
TEST_F(AnonymousTest, DCast2)
{
DynamicRangeFactory arf2(sr1ptr,m3rptr,sr2ptr);
auto ar = std::dynamic_pointer_cast<DynamicRange>( arf2.create() );
auto mr = ar->template scast<SRange,DynamicRange>(1,2);
EXPECT_EQ(mr->template getPtr<0>()->size(), sr1ptr->size());
EXPECT_EQ(mr->template getPtr<1>()->size(), m3rptr->size() * sr2ptr->size());
}
TEST_F(AnonymousTest, ToString1)
{
std::vector<char> vv = sr1ptr->data();