This commit is contained in:
Christian Zimmermann 2022-12-12 02:15:42 +01:00
parent 854e1a0533
commit f41ada859a
9 changed files with 228 additions and 3 deletions

View file

@ -28,8 +28,10 @@ namespace CNORXZ
const T& CArrayBase<T>::at(const IndexInterface<I,M>& i) const
{
CXZ_ASSERT(i.lex() < this->size(), "index out of range");
auto beg = this->begin();
//assertCompatible(i,beg);
// check further compatibility of index/range format!!!
auto ai = this->begin() + i.lex();
auto ai = beg + i.lex();
return *ai;
}
@ -37,7 +39,13 @@ namespace CNORXZ
template <typename I, typename M>
Sptr<CArrayBase<T>> CArrayBase<T>::sl(const IndexInterface<I,M>& i) const
{
////auto r = mkSliceRange(i.range());
//auto beg = this->begin();
//assertCompatible(i,beg);
////auto bs = mkSliceBlockSizes(i, beg);
//auto it = beg + i.lex();
////return std::make_shared<CSlice>(r, this, bs, it.pos());
return std::make_shared<CSlice>();
}
template <typename T>
@ -73,6 +81,42 @@ namespace CNORXZ
}
*/
/******************************
* CArrayBase (protected) *
******************************/
template <typename T>
template <class IPack1, class IPack2>
inline Vector<SizeT> CArrayBase<T>::mkSliceBlockSize(const IPack1& ip1, const IPack2& ip2) const
{
const SizeT ip1dim = indexPackDim(ip1);
const SizeT ip2dim = indexPackDim(ip2);
if(ip1dim > ip2dim){
//const SizeT ip1sdim = indexPackSDim(ip1);
const SizeT ip2sdim = indexPackSDim(ip2);
CXZ_ASSERT(ip1dim == ip2sdim or ip2sdim == ip2dim,
"")
}
else if(ip1dim < ip2dim){
}
else {
}
}
template <typename T>
inline RangePtr CArrayBase<T>::mkSliceRange(const RangePtr& r) const
{
}
template <typename T>
inline void CArrayBase<T>::assertCompatible() const
{
}
/*****************
* ArrayBase *
*****************/
@ -100,6 +144,17 @@ namespace CNORXZ
return *ai;
}
template <typename T>
template <typename I, typename M>
Sptr<ArrayBase<T>> ArrayBase<T>::sl(const IndexInterface<I,M>& i)
{
auto r = mkSliceRange(i);
auto beg = this->begin();
auto bs = mkSliceBlockSizes(i, beg);
auto it = beg + i.lex();
return std::make_shared<Slice>(r, this, bs, it.pos());
}
template <typename T>
typename ArrayBase<T>::iterator ArrayBase<T>::begin()
{

View file

@ -22,6 +22,13 @@ namespace CNORXZ
protected:
RangePtr mRange;
template <typename I, typename M>
inline Vector<SizeT> mkSliceBlockSize(const I1& i, const I2& beg) const;
inline RangePtr mkSliceRange(const RangePtr& r) const;
inline void assertCompatible() const;
public:
CArrayBase(const RangePtr& range);
@ -89,7 +96,37 @@ namespace CNORXZ
//template <typename I, typename M>
//OperationRoot<T,I> operator()(const IndexPtr<I,M>& i);
};
// to extra header file !!!:
template <class... Indices>
constexpr decltype(auto) flattenIndexPack(const Tuple<Sptr<Indices>...>& ipack)
{
constexpr SizeT D = sizeof...(Indices);
}
inline Vector<XIndexPtr> flattenIndexPack(const Vector<XIndexPtr>& ipack)
{
}
template <class... Indices>
inline SizeT indexPackDim(Tuple<Sptr<Indices>...> ipack)
{
constexpr SizeT D = sizeof...(Indices);
return iter<0,D>([&](const auto& i) { return std::get<i>(ipack)->dim(); },
[](const auto&... e) { return (e + ...); });
}
inline SizeT indexPackDim(const Vector<XIndexPtr>& ipack)
{
return std::accumulate(ipack.begin(), ipack.end(), ipack[0]->dim(),
[](auto a, auto b) { return a->dim() + b->dim(); });
}
}
#endif

View file

@ -40,6 +40,9 @@ namespace CNORXZ
template <typename T>
using Uptr = std::unique_ptr<T>;
template <typename T, typename U>
using Pair = std::pair<T,U>;
template <typename... T>
using Tuple = std::tuple<T...>;

View file

@ -46,6 +46,7 @@ namespace CNORXZ
RangePtr range() const;
UPos stepSize(const IndexId<0>& id) const;
Vector<XIndexPtr> pack() const;
String stringMeta() const;
DType meta() const;

View file

@ -0,0 +1,76 @@
#ifndef __cxz_index_utils_cc_h__
#define __cxz_index_utils_cc_h__
#include "index_utils.h"
namespace CNORZX
{
inline decltype(auto) getPack(const XIndexPtr& i)
{
typedef Vector<XIndexPtr> OutT;
OutT p = i.pack();
while(p.size() == 1u){
p = p[0]->pack();
}
return p;
}
// -> base
template <typename... T>
Vector<T> mkVector(const T&... t) { return Vector<T>({t...}); }
template <class F>
inline decltype(auto) indexZip(const XIndexPtr& a, const XIndexPtr& b, F&& f)
{
const auto ap = getPack(a);
const auto bp = getPack(b);
if(ap.size() == 1u or bp.size() == 1u){
return mkVector( f(a, b) );
}
else {
return indexPackZip(ap, bp, std::forward<F>(f));
}
}
template <class F>
inline decltype(auto) indexPackZip(const Vector<XIndexPtr>& a, const Vector<XIndexPtr>& b, F&& f)
{
if(a.size() > b.size()) {
Vector<XIndexPtr> bn;
for(const auto& x: b){
auto p = getPack(x);
if(p.size() == 0){
bn.push_back(x);
}
else {
bn.insert(bn.end(), p.begin(), p.end());
}
}
return indexPackZip(a, bn, std::forward<F>(f));
}
else if(a.size() < b.size()) {
Vector<XIndexPtr> an;
for(const auto& x: a){
auto p = getPack(x);
if(p.size() == 0){
an.push_back(x);
}
else {
an.insert(bn.end(), p.begin(), p.end());
}
}
return indexPackZip(an, b, std::forward<F>(f));
}
else {
typedef decltype(indexZip(a[0], b[0], std::forward<T>(f))) OutT;
OutT o(a.size());
std::transform(o.begin(), o.end(), a.begin(), b.begin(),
[](const auto& ax, const auto& bx) { F fc = f; return indexZip(ax, bx, std::move(fc)); });
return o;
}
}
}
#endif

View file

@ -0,0 +1,18 @@
#ifndef __cxz_index_utils_h__
#define __cxz_index_utils_h__
#include "base/base.h"
namespace CNORZX
{
inline decltype(auto) getPack(const XIndexPtr& i);
template <class F>
inline decltype(auto) indexZip(const XIndexPtr& a, const XIndexPtr& b, F&& f);
template <class F>
inline decltype(auto) indexPackZip(const Vector<XIndexPtr>& a, const Vector<XIndexPtr>& b, F&& f);
}
#endif

View file

@ -126,6 +126,24 @@ namespace CNORXZ
{
return mI->stepSize(id);
}
template <class Index, typename Meta>
Vector<XIndexPtr> XIndex<Index,Meta>::pack() const
{
constexpr SizeT D = index_dim<Index>::value;
// replace by traits: has_sub, has_static_sub (-> to be implemented!!!)
if constexpr(D > 1){
return iter<0,D>
( [&](auto i) { return mkXIndex(std::get<i>(mI->pack())); },
[](const auto&... e) { return { e ... }; } );
}
else if constexpr(std::is_same<Index,YIndex>::value){
return mI->pack();
}
else {
return {};
}
}
template <class Index, typename Meta>
String XIndex<Index,Meta>::stringMeta() const
@ -154,6 +172,13 @@ namespace CNORXZ
return DXpr<SizeT>(mI->ifor(xpr, std::forward<std::function<SizeT(SizeT,SizeT)>>(f)));
}
template <class Index>
XIndexPtr mkXIndex(const Sptr<Index>& i)
{
typedef typename Index::MetaType Meta;
return std::make_shared<XIndex<Index,Meta>>
(std::dynamic_pointer_cast<IndexInterface<Index,Meta>>(i));
}
}
#endif

View file

@ -36,7 +36,8 @@ namespace CNORXZ
virtual SizeT dim() const = 0;
virtual RangePtr range() const = 0;
virtual UPos stepSize(const IndexId<0>& id) const = 0;
virtual Vector<XIndexPtr> pack() const = 0;
virtual String stringMeta() const = 0;
virtual DType meta() const = 0;
virtual XIndexBase& at(const DType& meta) = 0;
@ -84,6 +85,7 @@ namespace CNORXZ
virtual SizeT dim() const override final;
virtual RangePtr range() const override final;
virtual UPos stepSize(const IndexId<0>& id) const override final;
virtual Vector<XIndexPtr> pack() const override final;
virtual String stringMeta() const override final;
virtual DType meta() const override final;
@ -97,6 +99,9 @@ namespace CNORXZ
};
template <class Index>
XIndexPtr mkXIndex(const Sptr<Index>& i);
}
#endif

View file

@ -129,6 +129,11 @@ namespace CNORXZ
return mI->stepSize(id);
}
Vector<XIndexPtr> DIndex::pack() const
{
return mI->pack();
}
String DIndex::stringMeta() const
{
return mI->stringMeta();