Implements the parts of layout_left that don't depend on any of the
other layouts.

libstdc++/ChangeLog:

        * include/std/mdspan (layout_left): New class.

Signed-off-by: Luc Grosheintz <luc.groshei...@gmail.com>
---
 libstdc++-v3/include/std/mdspan | 179 ++++++++++++++++++++++++++++++++
 1 file changed, 179 insertions(+)

diff --git a/libstdc++-v3/include/std/mdspan b/libstdc++-v3/include/std/mdspan
index 39ced1d6301..e05048a5b93 100644
--- a/libstdc++-v3/include/std/mdspan
+++ b/libstdc++-v3/include/std/mdspan
@@ -286,6 +286,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   namespace __mdspan
   {
+    template<typename _Extents>
+      constexpr typename _Extents::index_type
+      __fwd_prod(const _Extents& __exts, size_t __r) noexcept
+      {
+       typename _Extents::index_type __fwd = 1;
+       for(size_t __i = 0; __i < __r; ++__i)
+         __fwd *= __exts.extent(__i);
+       return __fwd;
+      }
+
+    template<typename _Extents>
+      constexpr typename _Extents::index_type
+      __rev_prod(const _Extents& __exts, size_t __r) noexcept
+      {
+       typename _Extents::index_type __rev = 1;
+       for(size_t __i = __r + 1; __i < __exts.rank(); ++__i)
+         __rev *= __exts.extent(__i);
+       return __rev;
+      }
+
     template<typename _IndexType, size_t... _Counts>
       auto __build_dextents_type(integer_sequence<size_t, _Counts...>)
        -> extents<_IndexType, ((void) _Counts, dynamic_extent)...>;
@@ -304,6 +324,165 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     explicit extents(_Integrals...) ->
       extents<size_t, __mdspan::__dynamic_extent<_Integrals>()...>;
 
+  struct layout_left
+  {
+    template<typename _Extents>
+      class mapping;
+  };
+
+  namespace __mdspan
+  {
+    template<typename _Tp>
+      constexpr bool __is_extents = false;
+
+    template<typename _IndexType, size_t... _Extents>
+      constexpr bool __is_extents<extents<_IndexType, _Extents...>> = true;
+
+    template<size_t _Count>
+    struct _LinearIndexLeft
+    {
+      template<typename _Extents, typename... _Indices>
+       static constexpr typename _Extents::index_type
+       _S_value(const _Extents& __exts, typename _Extents::index_type __idx,
+                _Indices... __indices) noexcept
+       {
+         return __idx + __exts.extent(_Count)
+           * _LinearIndexLeft<_Count + 1>::_S_value(__exts, __indices...);
+       }
+
+      template<typename _Extents>
+       static constexpr typename _Extents::index_type
+       _S_value(const _Extents&) noexcept
+       { return 0; }
+    };
+
+    template<typename _Extents, typename... _Indices>
+      constexpr typename _Extents::index_type
+      __linear_index_left(const _Extents& __exts, _Indices... __indices)
+      {
+       return _LinearIndexLeft<0>::_S_value(__exts, __indices...);
+      }
+
+    template<typename _IndexType, typename _Tp, size_t _Nm>
+      consteval bool
+      __is_representable_product(array<_Tp, _Nm> __factors)
+      {
+       size_t __rest = numeric_limits<_IndexType>::max();
+       for(size_t __i = 0; __i < _Nm; ++__i)
+       {
+         if (__factors[__i] == 0)
+           return true;
+         __rest /= _IndexType(__factors[__i]);
+       }
+       return __rest > 0;
+      }
+
+    template<typename _Extents>
+      consteval array<typename _Extents::index_type, _Extents::rank()>
+      __static_extents_array()
+      {
+       array<typename _Extents::index_type, _Extents::rank()> __exts;
+       for(size_t __i = 0; __i < _Extents::rank(); ++__i)
+         __exts[__i] = _Extents::static_extent(__i);
+       return __exts;
+      }
+
+    template<typename _Extents, typename _IndexType>
+      concept __representable_size = _Extents::rank_dynamic() != 0
+      || __is_representable_product<_IndexType>(
+         __static_extents_array<_Extents>());
+
+    template<typename _Extents>
+      concept __layout_extent = __representable_size<
+       _Extents, typename _Extents::index_type>;
+  }
+
+  template<typename _Extents>
+    class layout_left::mapping
+    {
+      static_assert(__mdspan::__layout_extent<_Extents>,
+       "The size of extents_type is not representable as index_type.");
+    public:
+      using extents_type = _Extents;
+      using index_type = typename extents_type::index_type;
+      using size_type = typename extents_type::size_type;
+      using rank_type = typename extents_type::rank_type;
+      using layout_type = layout_left;
+
+      constexpr
+      mapping() noexcept = default;
+
+      constexpr
+      mapping(const mapping&) noexcept = default;
+
+      constexpr
+      mapping(const extents_type& __extents) noexcept
+      : _M_extents(__extents)
+      { }
+
+      template<typename _OExtents>
+       requires (is_constructible_v<extents_type, _OExtents>)
+       constexpr explicit(!is_convertible_v<_OExtents, extents_type>)
+       mapping(const mapping<_OExtents>& __other) noexcept
+       : _M_extents(__other.extents())
+       { }
+
+      constexpr mapping&
+      operator=(const mapping&) noexcept = default;
+
+      constexpr const extents_type&
+      extents() const noexcept { return _M_extents; }
+
+      constexpr index_type
+      required_span_size() const noexcept
+      { return __mdspan::__fwd_prod(_M_extents, _M_extents.rank()); }
+
+      template<__mdspan::__valid_index_type<index_type>... _Indices>
+       requires (sizeof...(_Indices) == extents_type::rank())
+       constexpr index_type
+       operator()(_Indices... __indices) const noexcept
+       {
+         return __mdspan::__linear_index_left(
+           _M_extents, static_cast<index_type>(__indices)...);
+       }
+
+      static constexpr bool
+      is_always_unique() noexcept { return true; }
+
+      static constexpr bool
+      is_always_exhaustive() noexcept { return true; }
+
+      static constexpr bool
+      is_always_strided() noexcept { return true; }
+
+      static constexpr bool
+      is_unique() noexcept { return true; }
+
+      static constexpr bool
+      is_exhaustive() noexcept { return true; }
+
+      static constexpr bool
+      is_strided() noexcept { return true; }
+
+      constexpr index_type
+      stride(rank_type __i) const noexcept
+      requires (extents_type::rank() > 0)
+      {
+       __glibcxx_assert(__i < extents_type::rank());
+       return __mdspan::__fwd_prod(_M_extents, __i);
+      }
+
+      template<typename _OExtents>
+       requires (extents_type::rank() == _OExtents::rank())
+       friend constexpr bool
+       operator==(const mapping& __self, const mapping<_OExtents>& __other)
+       noexcept
+       { return __self.extents() == __other.extents(); }
+
+    private:
+      [[no_unique_address]] extents_type _M_extents;
+    };
+
 _GLIBCXX_END_NAMESPACE_VERSION
 }
 #endif
-- 
2.49.0

Reply via email to