base/isq.h + base/iter.* + functional/fundamental.h + adaptions in mrange (WIP)

This commit is contained in:
Christian Zimmermann 2022-11-08 22:36:16 +01:00
parent 0197e7db6e
commit fb94a382bb
9 changed files with 151 additions and 172 deletions

View file

@ -2,3 +2,4 @@
#include "dtype.cc.h"
#include "obj_handle.cc.h"
#include "to_string.cc.h"
#include "iter.cc.h"

View file

@ -22,6 +22,8 @@
#include "types.h"
#include "obj_handle.h"
#include "dtype.h"
#include "isq.h"
#include "iter.h"
#include "base.cc.h"

30
src/include/base/isq.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef __cxz_isq_h__
#define __cxz_isq_h__
#include <cstdlib>
#include <utility>
#include "types.h"
namespace CNORXZ
{
template <SizeT O, SizeT... Is>
std::index_sequence<(Is+O)...> mkIsqAdd(std::index_sequence<is...> is) { return {}; }
template <SizeT B, SizeT E>
struct MkIsq
{
static auto make()
{
static_assert(B <= E, "begin of sequence must be smaller than end of sequence");
return mkIsqAdd<B>( std::make_index_sequence<E-B> );
}
typedef decltype(make()) type;
};
template <SizeT B, SizeT E>
using Isqr = MkIsq<B,E>::type;
}
#endif

View file

@ -0,0 +1,28 @@
#ifndef __cxz_iter_cc_h__
#define __cxz_iter_cc_h__
#include "iter.h"
#include "functional/fundamental.h"
namespace CNORXZ
{
template <class G, class F, SizeT... Is>
constexpr decltype(auto) iteri(const G& g, const F& f, Isq<Is...> is)
{
if constexpr(std::is_same<F,NoF>::value){
( g(std::integral_constant<SizeT,Is>{}, args...), ... );
}
else {
return f( g(std::integral_constant<SizeT,Is>{}, args...) ... );
}
}
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) iter(const G& g, const F& f)
{
return iteri(g, f, Isqr<B,E>{});
}
}
#endif

17
src/include/base/iter.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef __cxz_iter_h__
#define __cxz_iter_h__
#include <cstdlib>
#include "types.h"
namespace CNORXZ
{
template <class G, class F, SizeT... Is>
constexpr decltype(auto) iteri(const G& g, const F& f, Isq<Is...> is);
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) iter(const G& g, const F& f);
}
#endif

View file

@ -10,6 +10,7 @@
#include <array>
#include <map>
#include <typeinfo>
#include <utility>
namespace CNORXZ
{
@ -43,10 +44,20 @@ namespace CNORXZ
template <SizeT I, typename... T>
using TupleElem = std::tuple_element<I,Tuple<T...>>;
template <typename T>
using RemoveRef = std::remove_reference<T>::type;
template <typename K, typename V>
using Map = std::map<K,V>;
typedef std::type_info TypeInfo;
template <SizeT... Is>
using Isq = std::index_sequence<Is...>;
// cxz helper functions/classes: -> isq.h
template <class... T>
using Isqf = std::index_sequence_for<T...>;
/*********************
* library types *

View file

@ -0,0 +1,15 @@
#ifndef __cxz_fundamental_h__
#define __cxz_fundamental_h__
#include <cstdlib>
namespace CNORXZ
{
struct NoF
{
constexpr void operator()() const {};
};
}
#endif

View file

@ -12,85 +12,25 @@ namespace CNORXZ
* MIndex (private) *
***********************/
template <class... Indices>
template <SizeT P, SizeT... Is>
constexpr decltype(auto) MIndex<Indices...>::indexSequencePlus(std::index_sequence<Is...> is) const
{
return std::index_sequence<P+Is...> {};
}
template <class... Indices>
template <SizeT B, SizeT E>
constexpr decltype(auto) MIndex<Indices...>::mkIndexSequence() const
{
return indexSequencePlus<B>(std::make_index_sequence<E-B>{});
}
template <class... Indices>
template <class G, class F, SizeT... Is>
constexpr decltype(auto) MIndex<Indices...>::accumulatei(const G& g, const F& f, std::index_sequence<Is...> is) const
{
return f( g(std::get<Is>(mIPack))... );
}
template <class... Indices>
template <class G, class F, SizeT... Is>
constexpr decltype(auto) MIndex<Indices...>::accumulate2i(const G& g, const F& f, std::index_sequence<Is...> is) const
{
return f( g(std::get<Is>(mIPack), std::get<Is>(mBlockSizes))... );
}
template <class... Indices>
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) MIndex<Indices...>::accumulate(const G& g, const F& f) const
{
return accumulatei(g, f, mkIndexSequence<B,E>())
}
template <class... Indices>
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) MIndex<Indices...>::accumulate2(const G& g, const F& f) const
{
return accumulate2i(g, f, mkIndexSequence<B,E>())
}
template <class... Indices>
template <SizeT... Is>
constexpr decltype(auto) MIndex<Indices...>::mkIPack(SizeT pos, std::index_sequence<Is...> is) const
constexpr decltype(auto) MIndex<Indices...>::mkIPack(SizeT pos, Isq<Is...> is) const
{
static_assert(sizeof...(Is) == sizeof...(Indices),
static_assert(sizeof...(Is) == NI,
"sequence size does not match number of indices");
return std::make_tuple( std::make_shared<Indices>( mRange->sub(Is) )... );
//, (pos / std::get<Is>(mBlockSizes) ) % )... );
}
template <class... Indices>
template <SizeT... Is>
inline void MIndex<Indices...>::setIPack(std::index_sequence<Is...> is) const
constexpr decltype(auto) MIndex<Indices...>::mkBlockSizes() const
{
( (std::get<Is>(mIPack) = (IB::mPos / std::get<Is>(mBlockSize)) % std::get<Is>(mIPack)->max() ),... );
}
template <class... Indices>
template <SizeT... Is>
constexpr decltype(auto) MIndex<Indices...>::mkBlockSizes(std::index_sequence<Is...> is) const
{
static_assert(sizeof...(Is) == sizeof...(Indices)-1,
"set lowest block size manually");
return std::make_tuple
( accumulate<Is,sizeof...(Indices)>
( [](const auto& i) { return i->max(); },
[](const auto&... as) { return (as * ...); } )...,
( iter<Is,NI>
( [&](auto i) { return std::get<i>(mIPack)->max(); },
[&](const auto&... as) { return (as * ...); } )...,
SPos<1>() );
}
template <class... Indices>
template <SizeT... Is>
constexpr decltype(auto) MIndex<Indices...>::mkPos(std::index_sequence<Is...> is) const
{
return ( (std::get<Is>(mIPack)*std::get<Is>(mBlockSizes)) + ... );
}
template <class... Indices>
template <SizeT I>
inline void MIndex<Indices...>::up()
@ -125,33 +65,6 @@ namespace CNORXZ
--(*i);
}
template <class... Indices>
template <SizeT... Is>
inline String MIndex<Indices...>::mkStringMeta(std::index_sequence<Is...> is) const
{
const String blim = "[";
const String elim = "]";
const String dlim = ",";
static_assert(sizeof...(Is) == sizeof...(Indices)-1,
"set last position outside the index pack");
if constexpr(sizeof...(Is) == 0){
return blim + std::get<0>(mIPack)->stringMeta() + elim;
}
else {
return blim +
( ( std::get<Is>(mIPack)->stringMeta() + dlim ) + ... +
std::get<sizeof...(Indices)-1>(mIPack)->stringMeta() )
+ elim;
}
}
template <class... Indices>
template <SizeT... Is>
inline typename MIndex<Indices...>::MetaType MIndex<Indices...>::mkMeta(std::index_sequence<Is...> is) const
{
return std::make_tuple( std::get<Is>(mIPack)->meta()... );
}
template <class... Indices>
template <SizeT I, class Xpr, class F>
constexpr decltype(auto) MIndex<Indices...>::mkIFor(const Xpr& xpr, F&& f) const
@ -164,12 +77,6 @@ namespace CNORXZ
}
}
template <SizeT... Is>
inline void ati(const MetaType& meta, std::index_sequence<Is...> is) const
{
( std::get<Is>(mIPack)->at( std::get<Is>(meta) ), ... );
}
/**************
* MIndex *
**************/
@ -178,8 +85,8 @@ namespace CNORXZ
template <class MRange>
MIndex<Indices...>::MIndex(const Sptr<MRange>& range, SizeT pos) :
IndexInterface<MIndex<Indices...>,Tuple<typename Indices::MetaType...>>(0),
mIPack(mkIPack(IB::mPos, std::make_index_sequence<sizeof...(Indices)>{})),
mBlockSizes(mkBlockSizes(IB::mPos), std::make_index_sequence<sizeof...(Indices)>{}),
mIPack(mkIPack(IB::mPos, Isqr<0,NI>{})),
mBlockSizes(mkBlockSizes(IB::mPos), Isqr<0,NI>{}),
mRange(range),
{
(*this) = pos;
@ -189,7 +96,9 @@ namespace CNORXZ
MIndex<Indices...>& MIndex<Indices...>::operator()(const Sptr<MIndex>& mi)
{
mIPack = mi.mIPack;
IB::mPos = mkPos(std::make_index_sequence<sizeof...(Indices)>{});
IB::mPos = iter<0,NI>
( [&](auto i) { return std::get<i>(mIPack)*std::get<i>(mBlockSizes); },
[](const auto&... xs) { return (xs + ...); });
return *this;
}
@ -197,7 +106,7 @@ namespace CNORXZ
MIndex<Indices...>& MIndex<Indices...>::operator=(SizeT pos)
{
IB::mPos = pos;
setIPack(std::make_index_sequence<sizeof...(Indices)>{});
iter<0,NI>( [&](auto i) { std::get<i>(mIPack) = (IB::mPos / std::get<i>(mBlockSize)) % std::get<i>(mIPack)->max() }, NoF{} );
return *this;
}
@ -207,7 +116,7 @@ namespace CNORXZ
// End state is defined by high-index being end while all other indices are zero
auto& i0 = std::get<0>(mIPack);
if(i0->pos() != i0->max()){
up<sizeof...(Indices)-1>();
up<NI-1>();
}
return *this;
}
@ -216,7 +125,7 @@ namespace CNORXZ
MIndex<Indices...>& MIndex<Indices...>::operator--()
{
if(IB::mPos != 0){
down<sizeof...(Indices)-1>();
down<NI-1>();
}
return *this;
}
@ -224,27 +133,37 @@ namespace CNORXZ
template <class... Indices>
String MIndex<Indices...>::stringMeta() const
{
return mkStringMeta(std::make_index_sequence<sizeof...(Indices)-1>{});
const String blim = "[";
const String elim = "]";
const String dlim = ",";
return iter<1,NI>
( [&](auto i) { return std::get<i>(mIPack)->stringMeta(); },
[&](const auto&... xs) {
return blim + std::get<0>(mIPack)->stringMeta() + ( (dlim + xs) + ... ) + elim;
} );
}
template <class... Indices>
typename MIndex<Indices...>::MetaType MIndex<Indices...>::meta() const
{
return mkMeta(std::make_index_sequence<sizeof...(Indices)>{});
return iter<0,NI>( [&](auto i) { return std::get<i>(mIPack)->meta(); },
[](const auto&... xs) { return std::make_tuple(xs...); } );
}
template <class... Indices>
MIndex<Indices...>& MIndex<Indices...>::at(const MetaType& metaPos)
{
ati(metaPos, std::make_index_sequence<sizeof...(Indices)>{});
IB::mPos = mkPos(std::make_index_sequence<sizeof...(Indices)>{});
iter<0,NI>( [&](auto i) { std::get<Is>(mIPack)->at( std::get<Is>(meta) ) }, NoF {} );
IB::mPos = iter<0,NI>
( [&](auto i) { return std::get<i>(mIPack)*std::get<i>(mBlockSizes); },
[](const auto&... xs) { return (xs + ...); });
return *this;
}
template <class... Indices>
SizeT MIndex<Indices...>::dim()
{
return sizeof...(Indices);
return NI;
}
template <class... Indices>
@ -258,8 +177,9 @@ namespace CNORXZ
template <SizeT I>
decltype(auto) MIndex<Indices...>::stepSize(const IndexId<I>& id) const;
{
return accumulate2( [](const auto& i, const auto& b) { return i->stepSize(id) * b },
[](const auto&... ss) { return ( ss + ... ); });
return iter<0,NI>
( [&](auto i) { return std::get<i>(mIPack)->stepSize(id) * std::get<i>(mBlockSize); },
[](const auto&... ss) { return ( ss + ... ); });
}
template <class... Indices>
@ -307,7 +227,7 @@ namespace CNORXZ
template <class... Ranges>
MRange<Ranges...>::MRange(const Tuple<Sptr<Ranges>...>& rs) :
mRs(rs),
mA( mkA( std::make_index_sequence<sizeof...(Ranges)>{} ) )
mA( mkA( std::make_index_sequence<NR>{} ) )
{}
template <class... Ranges>
@ -320,13 +240,14 @@ namespace CNORXZ
template <class... Ranges>
SizeT MRange<Ranges...>::size() const
{
return this->sizei(std::make_index_sequence<sizeof...(Ranges)> {});
return iter<0,NR>( [&](auto i) { return std::get<i>(mRs)->size() },
[](const auto&... xs) { return (xs * ...); } );
}
template <class... Ranges>
SizeT MRange<Ranges...>::dim() const
{
return sizeof...(Ranges);
return NR;
}
template <class... Ranges>
@ -359,16 +280,10 @@ namespace CNORXZ
************************/
template <class... Ranges>
template <SizeT... Is>
decltype(auto) MRange<Ranges...>::mkA(std::index_sequence<Is...> is) const
decltype(auto) MRange<Ranges...>::mkA() const
{
return Arr<RangePtr,sizeof...(Ranges)> { std::get<Is>(mRs)... };
}
template <class... Ranges>
SizeT MRange<Ranges...>::sizei(std::index_sequence<Is...> is) const
{
return ( std::get<Is>(mRs)->size() * ... );
return iter<0,NR>([&](auto i) { return std::get<Is>(mRs); },
[](const auto&... xs) { return Arr<RangePtr,NR> { xs... } } );
}
}

View file

@ -22,6 +22,7 @@ namespace CNORXZ
typedef Tuple<Sptr<Indices>...> IndexPack;
typedef Tuple<typename Indices::MetaType...> MetaType;
typedef MRange<typename Indices::RangeType...> RangeType;
static constexpr SizeT NI = sizeof...(Indices);
// NO DEFAULT HERE !!!
// ( have to assign sub-indices (ptr!) correctly )
@ -69,43 +70,14 @@ namespace CNORXZ
MIndex() = default;
IndexPack mIPack;
//Arr<SizeT,sizeof...(Indices)> mBlockSizes;
typedef decltype(mkBlockSizes(std::make_index_sequence<sizeof...(Indices)-1>{})) BlockTuple;
typedef decltype(mkBlockSizes(Isqr<0,NI-1>{})) BlockTuple;
BlockTuple mBlockSizes;
Sptr<RangeType> mRange;
// shift to utils:
template <SizeT P, SizeT... Is>
constexpr decltype(auto) indexSequencePlus(std::index_sequence<Is...> is) const;
// shift to utils:
template <SizeT B, SizeT E>
constexpr decltype(auto) mkIndexSequence() const;
template <class G, class F, SizeT... Is>
constexpr decltype(auto) accumulatei(const G& g, const F& f, std::index_sequence<Is...> is) const;
template <class G, class F, SizeT... Is>
constexpr decltype(auto) accumulate2i(const G& g, const F& f, std::index_sequence<Is...> is) const;
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) accumulate(const G& g, const F& f) const;
template <SizeT B, SizeT E, class G, class F>
constexpr decltype(auto) accumulate2(const G& g, const F& f) const;
template <SizeT... Is>
constexpr decltype(auto) mkIPack(SizeT pos, std::index_sequence<Is...> is) const;
constexpr decltype(auto) mkIPack(SizeT pos, Isq<Is...> is) const;
template <SizeT... Is>
inline void setIPack(std::index_sequence<Is...> is) const;
template <SizeT... Is>
constexpr decltype(auto) mkBlockSizes(std::index_sequence<Is...> is) const;
template <SizeT... Is>
constexpr decltype(auto) mkPos(std::index_sequence<Is...> is) const;
constexpr decltype(auto) mkBlockSizes() const;
template <SizeT I>
inline void up();
@ -113,17 +85,8 @@ namespace CNORXZ
template <SizeT I>
inline void down();
template <SizeT... Is>
inline String mkStringMeta(std::index_sequence<Is...> is) const;
template <SizeT... Is>
inline MetaType mkMeta(std::index_sequence<Is...> is) const;
template <SizeT I, class Xpr, class F>
constexpr decltype(auto) mkIFor(const Xpr& xpr, F&& f) const;
template <SizeT... Is>
inline void ati(const MetaType& meta, std::index_sequence<Is...> is) const;
};
// modified blockSizes; to be used for Slices; can be created from MIndices
@ -142,7 +105,7 @@ namespace CNORXZ
BlockType mBlockSizes;
template <SizeT... Is>
constexpr decltype(auto) mkPos(std::index_sequence<Is...> is) const;
constexpr decltype(auto) mkPos(Isq<Is...> is) const;
};
// NOT THREAD SAVE
@ -169,6 +132,7 @@ namespace CNORXZ
typedef RangeBase RB;
typedef MIndex<typename Ranges::IndexType...> IndexType;
typedef Tuple<typename Ranges::IndexType::MetaType...> MetaType;
static constexpr SizeT NR = sizeof...(Ranges);
friend MRangeFactory<Ranges...>;
@ -190,14 +154,10 @@ namespace CNORXZ
MRange(const Tuple<Sptr<Ranges>...>& rs);
Tuple<Sptr<Ranges>...> mRs;
Arr<RangePtr,sizeof...(Ranges)> mA;
Arr<RangePtr,NR> mA;
private:
template <SizeT... Is>
decltype(auto) mkA(std::index_sequence<Is...> is) const;
template <SizeT... Is>
SizeT sizei(std::index_sequence<Is...> is) const;
decltype(auto) mkA() const;
};
}