diff --git a/src/include/ranges/crange.h b/src/include/ranges/crange.h index 45d2ef6..8e96d1a 100644 --- a/src/include/ranges/crange.h +++ b/src/include/ranges/crange.h @@ -21,7 +21,7 @@ namespace CNORXZ { /** **** - specific index for CRange + Specific index for CRange. */ class CIndex : public IndexInterface { @@ -34,7 +34,7 @@ namespace CNORXZ INDEX_RANDOM_ACCESS_ITERATOR_DEFS(MetaType); DEFAULT_MEMBERS(CIndex); /**< default constructors and assignments */ - /** constrcut index from range and position + /** Construct index from range and position. @param range Range to iterate over @param pos lexicographic position */ @@ -98,9 +98,6 @@ namespace CNORXZ /** @copydoc IndexInterface::at() */ CIndex& at(const SizeT& metaPos); - /** @copydoc IndexInterface::xpr() */ - COpRoot xpr(const Sptr& _this) const; - /** @copydoc IndexInterface::prange() */ RangePtr prange(const CIndex& last) const; @@ -120,6 +117,9 @@ namespace CNORXZ /** @copydoc IndexInterface::formatIsTrivial() */ bool formatIsTrivial() const; + /** @copydoc IndexInterface::xpr() */ + COpRoot xpr(const Sptr& _this) const; + private: Sptr mRangePtr; }; diff --git a/src/include/ranges/dindex.cc.h b/src/include/ranges/dindex.cc.h index 5d8451c..6637298 100644 --- a/src/include/ranges/dindex.cc.h +++ b/src/include/ranges/dindex.cc.h @@ -44,7 +44,9 @@ namespace CNORXZ I indexAs(const DIndex& i) { static_assert(is_index::value, "expected index type"); - return std::dynamic_pointer_cast>( i.xptr() )->get(); + auto ptr = std::dynamic_pointer_cast>( i.xptr() ); + CXZ_ASSERT(ptr, "invalid cast"); + return ptr->get(); } } diff --git a/src/include/ranges/dindex.h b/src/include/ranges/dindex.h index 7a87fe1..041eb50 100644 --- a/src/include/ranges/dindex.h +++ b/src/include/ranges/dindex.h @@ -20,6 +20,9 @@ namespace CNORXZ { + /** **** + index of compile-time-unspecified type + */ class DIndex : public IndexInterface { public: @@ -27,64 +30,145 @@ namespace CNORXZ typedef DType MetaType; typedef RangeBase RangeType; - DEFAULT_C(DIndex); + DEFAULT_C(DIndex); /**< default constructor */ + + /** copy constructor + @param i index to copy + */ DIndex(const DIndex& i); + + /** move constructor + @param i index to move + */ DIndex(DIndex&& i); + + /** copy assignment + @param i index to copy + */ DIndex& operator=(const DIndex& i); + + /** move assignment + @param i index to move + */ DIndex& operator=(DIndex&& i); + + /** construct from XIndex instance + @param i XIndex to use for construction + */ DIndex(const XIndexPtr& i); + + /** construct from range pointer + @param r Range to iterate over + @param lexpos initial lexicographic position + */ DIndex(const RangePtr& r, SizeT lexpos = 0); - + + /** construct from index of arbitrary type + @param i Index to use for construction + */ template DIndex(const IndexInterface& i); + /** @copydoc IndexInterface::operator=(SizeT) */ DIndex& operator=(SizeT lexpos); + + /** @copydoc IndexInterface::operator++() */ DIndex& operator++(); + + /** @copydoc IndexInterface::operator--() */ DIndex& operator--(); + + /** @copydoc IndexInterface::operator+() */ DIndex operator+(Int n) const; + + /** @copydoc IndexInterface::operator-() */ DIndex operator-(Int n) const; + + /** @copydoc IndexInterface::operator-(CIndex) */ SizeT operator-(const DIndex& i) const; + + /** @copydoc IndexInterface::operator+=() */ DIndex& operator+=(Int n); + + /** @copydoc IndexInterface::operator-=() */ DIndex& operator-=(Int n); + /** @copydoc IndexInterface::lex() */ SizeT lex() const; + + /** @copydoc IndexInterface::pmax() */ UPos pmax() const; + + /** @copydoc IndexInterface::lmax() */ UPos lmax() const; + + /** @copydoc IndexInterface::id() */ IndexId<0> id() const; + /** @copydoc IndexInterface::operator*() */ DType operator*() const; + /** @copydoc IndexInterface::dim() */ SizeT dim() const; + + /** @copydoc IndexInterface::range() */ RangePtr range() const; + /** @copydoc IndexInterface::stepSize() */ UPos stepSize(const IndexId<0>& id) const; + /** @copydoc IndexInterface::stringMeta() */ String stringMeta() const; + + /** @copydoc IndexInterface::meta() */ DType meta() const; + + /** @copydoc IndexInterface::at() */ DIndex& at(const DType& meta); + + /** @copydoc IndexInterface::prange() */ RangePtr prange(const DIndex& end) const; + + /** @copydoc IndexInterface::deepFormat() */ Vector deepFormat() const; + + /** @copydoc IndexInterface::deepMax() */ Vector deepMax() const; /** @copydoc IndexInterface::reformat() */ DIndex& reformat(const Vector& f, const Vector& s); + /** @copydoc IndexInterface::ifor() */ template decltype(auto) ifor(const Xpr& xpr, F&& f) const; - const XIndexPtr& xptr() const; - /** @copydoc IndexInterface::formatIsTrivial() */ bool formatIsTrivial() const; + /** get internal XIndex instance + @return shared pointer to XIndex instance + */ + const XIndexPtr& xptr() const; + private: XIndexPtr mI; }; + /** Trait-specialization: + DIndex can have sub-indices + */ template <> struct has_sub { static constexpr bool value = true; }; + /** dynamically cast DIndex to given type. + If type does not match the index type of the underlying index + and exception is thrown + + @tparam I index type to be returned + @param i DIndex to by casted + @return copy of underlying index instance + */ template I indexAs(const DIndex& i); } diff --git a/src/include/ranges/index_pack.h b/src/include/ranges/index_pack.h index adaa628..aab655d 100644 --- a/src/include/ranges/index_pack.h +++ b/src/include/ranges/index_pack.h @@ -2,12 +2,17 @@ /** @file include/ranges/index_pack.h - @brief ... + @brief Index pack declaration + Index packs are sets of indices. In contrast to multi-indices like + (G)MIndex or YIndex, there is no format that can be used to determine + a linearized memory position. Hence, only a lexicographic position is well defined. + Nevertheless, a function pos() is implemented, which just returns + the same value as lex(). Copyright (c) 2022 Christian Zimmermann. All rights reserved. Mail: chizeta@f3l.de - + **/ #ifndef __cxz_index_pack_h__ @@ -18,76 +23,203 @@ namespace CNORXZ { + /** **** + Static index pack. + Containes tuple of shared pointers to index instances. + @tparam Indices Index types. + */ template class SPack { public: - SP_DEFAULT_MEMBERS(constexpr,SPack); + SP_DEFAULT_MEMBERS(constexpr,SPack); /**< default constructors and assignments */ + + /** Construct from index pointers + @param is index pointers + */ constexpr SPack(const Sptr&... is); + + /** Construct from index pointer tuple + @param is index pointer tuple + */ constexpr SPack(const Tuple...>& is); + /** get underlying index pointer tuple + @return underlying index pointer tuple + */ constexpr const Tuple...>& all() const; + /** get pack size i.e. dimension (non-recursive) + @return pack size + */ constexpr SizeT size() const; - + + /** get pack element + @return i'th element of the pack + @tparam I static position + @param i static position instance + */ template constexpr decltype(auto) get(CSizeT i) const; + /** get pack element + @return i'th element of the pack + @tparam I static position + @param i static position instance + */ template constexpr decltype(auto) operator[](CSizeT i) const; + /** Append index on the r.h.s. of the pack. + @tparam Index Type of the index to be appended + @param i index to be appended + */ template constexpr decltype(auto) rmul(const Sptr& i) const; + /** Append index on the l.h.s. of the pack. + @tparam Index Type of the index to be appended + @param i Index to be appended + */ template constexpr decltype(auto) lmul(const Sptr& i) const; + /** Append another index pack on the r.h.s. of this pack + @tparam Indices2 Types of the indices in the pack to be appended + @param p Index pack to be appended + */ template constexpr decltype(auto) mul(const SPack& p) const; + /** create a range from the index pack + @return created range + */ decltype(auto) mkRange() const; + /** Get lexicographic position if pack was transformed to a (trivial) multi-index. + @return lexicographic position + */ SizeT lex() const; + + /** Get lexicographic position if pack was transformed to a (trivial) multi-index. + @return lexicographic position + */ SizeT pos() const; private: Tuple...> mIs; }; + /** Create static index pack + @tparam Indices index types + @param inds Indices to create the pack from + @return Created pack + */ template constexpr decltype(auto) spack(const Indices&... inds); + /** Create static index pack from index pointers + @tparam Indices index types + @param inds Indices to create the pack from + @return Created pack + */ template constexpr decltype(auto) spackp(const Sptr&... inds); + /** ****** + Dynamic index pack. + Containes array of shared pointers to XIndex instances. + */ class DPack { public: - DEFAULT_MEMBERS(DPack); + DEFAULT_MEMBERS(DPack); /**< default constructors and assignments */ + + /** Create pack from XIndex instance + @param is Vector of XIndex pointers + */ explicit DPack(const Vector& is); + + /** Create pack from XIndex instance (move input vector) + @param is Vector of XIndex pointers + */ explicit DPack(Vector&& is); + /** Create pack from a static pack (SPack) + @param p static pack used for construction + */ template explicit DPack(const SPack& p); - + + /** get underlying index pointer vector + @return underlying index pointer vector + */ const Vector& all() const; + + /** get pack size i.e. dimension (non-recursive) + @return pack size + */ SizeT size() const; + + /** get pack element + @return i'th element of the pack + @param i Position + */ const XIndexPtr& get(SizeT i) const; + + /** get pack element + @return i'th element of the pack + @param i Position + */ const XIndexPtr& operator[](SizeT i) const; + + /** Append index on the r.h.s. + @param i DIndex to be appended + */ DPack rmul(const Sptr& i) const; + + /** Append index on the l.h.s. + @param i DIndex to be appended + */ DPack lmul(const Sptr& i) const; + + /** Append pack on the r.h.s. + @param p Pack to be appnded + */ DPack mul(const DPack& p) const; + + /** create a range from the index pack + @return created range + */ RangePtr mkRange() const; + + /** Get lexicographic position if pack was transformed to a (trivial) multi-index. + @return lexicographic position + */ SizeT lex() const; + + /** Get lexicographic position if pack was transformed to a (trivial) multi-index. + @return lexicographic position + */ SizeT pos() const; private: Vector mIs; }; + /** Create dynamic index pack + @tparam Indices index types + @param inds Indices to create the pack from + @return Created pack + */ template DPack dpack(const Indices&... inds); + /** Create dynamic index pack from index pointers + @tparam Indices index types + @param inds Indices to create the pack from + @return Created pack + */ template DPack dpackp(const Sptr&... inds); }