index format

This commit is contained in:
Christian Zimmermann 2022-12-31 19:43:06 +01:00
parent 0980934706
commit 53aa87c362
6 changed files with 250 additions and 0 deletions

View file

@ -0,0 +1,128 @@
#ifndef __cxz_index_format_cc_h__
#define __cxz_index_format_cc_h__
#include "index_format.h"
namespace CNORXZ
{
/***************
* MFormat *
***************/
template <SizeT N>
constexpr MFormat<N>::MFormat(const Arr<UPos,N>& b) :
mB(b)
{}
template <SizeT N>
template <class FormatT>
constexpr MFormat<N>::MFormat(const FormatT& f)
{
static_assert(f.size() == N, "try to assign format of wrong dimension");
iter<0,N>( [&](auto i) { mB[i] = f[i]; }, NoF{} );
}
template <SizeT N>
const Arr<UPos,N>& MFormat<N>::all() const
{
return mB;
}
template <SizeT N>
constexpr decltype(auto) MFormat<N>::size() const
{
return std::integral_constant<SizeT,N>{};
}
template <SizeT N>
template <SizeT I>
constexpr decltype(auto) MFormat<N>::get(std::integral_constant<SizeT,I> i) const
{
return std::get<I>(mB);
}
template <SizeT N>
template <SizeT I>
constexpr decltype(auto) MFormat<N>::operator[](std::integral_constant<SizeT,I> i) const
{
return get(i);
}
/****************
* GMFormat *
****************/
template <class... PosT>
constexpr GMFormat<PosT...>::GMFormat(const Tuple<PosT...>& b) :
mB(b)
{}
template <class... PosT>
template <class FormatT>
constexpr GMFormat<PosT...>::GMFormat(const FormatT& f)
{
static_assert(f.size() == size(), "try to assign format of wrong dimension");
iter<0,sizeof...(PosT)>( [&](auto i) { mB[i] = f[i]; }, NoF{} );
}
template <class... PosT>
const Tuple<PosT...>& GMFormat<PosT...>::all() const
{
return mB;
}
template <class... PosT>
constexpr decltype(auto) GMFormat<PosT...>::size() const
{
return std::integral_constant<SizeT,sizeof...(PosT)>{};
}
template <class... PosT>
template <SizeT I>
constexpr decltype(auto) GMFormat<PosT...>::get(std::integral_constant<SizeT,I> i) const
{
return std::get<I>(mB);
}
template <class... PosT>
template <SizeT I>
constexpr decltype(auto) GMFormat<PosT...>::operator[](std::integral_constant<SizeT,I> i) const
{
return get(i);
}
/***************
* YFormat *
***************/
template <class FormatT>
YFormat::YFormat(const FormatT& f) : mB(f.size())
{
// replace by check if integral constant
if constexpr(is_static_format<FormatT>::value){
iter<0,f.size()>( [&](auto i) { mB[i] = f[i]; }, NoF{} );
}
else {
for(SizeT i = 0; i != mB.size(); ++i){
mB[i] = f[i];
}
}
}
template <SizeT I>
const UPos& YFormat::get(std::integral_constant<SizeT,I> i) const
{
return mB[i];
}
template <SizeT I>
const UPos& YFormat::operator[](std::integral_constant<SizeT,I> i) const
{
return mB[i];
}
}
#endif

View file

@ -0,0 +1,89 @@
#ifndef __cxz_index_format_h__
#define __cxz_index_format_h__
#include "base/base.h"
#include "xpr/xpr.h"
namespace CNORXZ
{
template <class FormatT> struct is_static_format { CXZ_CVAL_FALSE; };
template <SizeT N>
class MFormat
{
public:
SP_DEFAULT_MEMBERS(constexpr,MFormat);
explicit constexpr MFormat(const Arr<UPos,N>& b);
template <class FormatT>
constexpr MFormat(const FormatT& f);
const Arr<UPos,N>& all() const;
constexpr decltype(auto) size() const;
template <SizeT I>
constexpr decltype(auto) get(std::integral_constant<SizeT,I> i) const;
template <SizeT I>
constexpr decltype(auto) operator[](std::integral_constant<SizeT,I> i) const;
private:
Arr<UPos,N> mB;
};
template <SizeT N> struct is_static_format<MFormat<N>> { CXZ_CVAL_TRUE; };
template <class... PosT>
class GMFormat
{
public:
SP_DEFAULT_MEMBERS(constexpr,GMFormat);
explicit constexpr GMFormat(const Tuple<PosT...>& b);
template <class FormatT>
constexpr GMFormat(const FormatT& f);
const Tuple<PosT...>& all() const;
constexpr decltype(auto) size() const;
template <SizeT I>
constexpr decltype(auto) get(std::integral_constant<SizeT,I> i) const;
template <SizeT I>
constexpr decltype(auto) operator[](std::integral_constant<SizeT,I> i) const;
private:
Tuple<PosT...> mB;
};
template <class... PosT> struct is_static_format<GMFormat<PosT...>> { CXZ_CVAL_TRUE; };
class YFormat
{
public:
DEFAULT_MEMBERS(YFormat);
explicit YFormat(const Vector<UPos>& b);
template <class FormatT>
YFormat(const FormatT& f);
const Vector<UPos>& all() const;
SizeT size() const;
template <SizeT I>
const UPos& get(std::integral_constant<SizeT,I> i) const;
template <SizeT I>
const UPos& operator[](std::integral_constant<SizeT,I> i) const;
const UPos& get(SizeT i) const;
const UPos& operator[](SizeT i) const;
private:
Vector<UPos> mB;
};
}
#endif

View file

@ -9,3 +9,4 @@
#include "lindex.cc.h" #include "lindex.cc.h"
#include "index_mul.cc.h" #include "index_mul.cc.h"
#include "index_pack.cc.h" #include "index_pack.cc.h"
#include "index_format.cc.h"

View file

@ -11,5 +11,6 @@
#include "lindex.h" #include "lindex.h"
#include "index_mul.h" #include "index_mul.h"
#include "index_pack.h" #include "index_pack.h"
#include "index_format.h"
#include "ranges.cc.h" #include "ranges.cc.h"

View file

@ -7,6 +7,7 @@ set(libcnorxz_a_SOURCES
${CMAKE_SOURCE_DIR}/src/lib/ranges/crange.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/crange.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/dindex.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/dindex.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/index_pack.cc ${CMAKE_SOURCE_DIR}/src/lib/ranges/index_pack.cc
${CMAKE_SOURCE_DIR}/src/lib/ranges/index_format.cc
) )
add_library(cnorxz_obj OBJECT add_library(cnorxz_obj OBJECT

View file

@ -0,0 +1,30 @@
#include "ranges/index_format.h"
namespace CNORXZ
{
YFormat::YFormat(const Vector<UPos>& b) :
mB(b)
{}
const Vector<UPos>& YFormat::all() const
{
return mB;
}
SizeT YFormat::size() const
{
return mB.size();
}
const UPos& YFormat::get(SizeT i) const
{
return mB[i];
}
const UPos& YFormat::operator[](SizeT i) const
{
return mB[i];
}
}