@@ -193,7 +193,7 @@ namespace xt
193193 * @throws Assertion failure if `i != 0` for integral slices.
194194 * @throws Assertion failure if `i >= slice.size()` for non-integral slices.
195195 */
196- template <size_t I, std::integral Index>
196+ template <size_t I, access_t ACCESS, std::integral Index>
197197 size_t map_ith_index (const view_type& view, const Index i) const ;
198198
199199 /* *
@@ -490,16 +490,16 @@ namespace xt
490490 {
491491 if constexpr (ACCESS == access_t ::SAFE)
492492 {
493- return container.at (map_ith_index<Is>(view, indices[Is])...);
493+ return container.at (map_ith_index<Is, ACCESS >(view, indices[Is])...);
494494 }
495495 else
496496 {
497- return container (map_ith_index<Is>(view, indices[Is])...);
497+ return container (map_ith_index<Is, ACCESS >(view, indices[Is])...);
498498 }
499499 }
500500
501501 template <class UnderlyingContainer , class ... Slices>
502- template <size_t I, std::integral Index>
502+ template <size_t I, access_t ACCESS, std::integral Index>
503503 auto
504504 index_mapper<xt::xview<UnderlyingContainer, Slices...>>::map_ith_index(const view_type& view, const Index i) const
505505 -> size_t
@@ -515,14 +515,51 @@ namespace xt
515515
516516 if constexpr (std::is_integral_v<current_slice>)
517517 {
518- assert (i == 0 );
518+ if constexpr (ACCESS == access_t ::SAFE)
519+ {
520+ if (i != 0 )
521+ {
522+ XTENSOR_THROW (std::out_of_range, " Index out of range in index_mapper access" );
523+ }
524+ }
525+ else
526+ {
527+ assert (i == 0 );
528+ }
519529 return size_t (slice);
520530 }
531+ else if constexpr (xt::detail::is_xall_slice<std::decay_t <current_slice>>::value)
532+ {
533+ return size_t (i);
534+ }
521535 else
522536 {
523537 using slice_size_type = typename current_slice::size_type;
524- assert (i < slice.size ());
525- return size_t (slice (static_cast <slice_size_type>(i)));
538+ const auto slice_index = static_cast <slice_size_type>(i);
539+
540+ if constexpr (ACCESS == access_t ::SAFE)
541+ {
542+ if constexpr (std::is_signed_v<slice_size_type>)
543+ {
544+ if (slice_index < 0 || slice_index >= slice.size ())
545+ {
546+ XTENSOR_THROW (std::out_of_range, " Index out of range in index_mapper access" );
547+ }
548+ }
549+ else if (slice_index >= slice.size ())
550+ {
551+ XTENSOR_THROW (std::out_of_range, " Index out of range in index_mapper access" );
552+ }
553+ }
554+ else
555+ {
556+ if constexpr (std::is_signed_v<slice_size_type>)
557+ {
558+ assert (slice_index >= 0 );
559+ }
560+ assert (slice_index < slice.size ());
561+ }
562+ return size_t (slice (slice_index));
526563 }
527564 }
528565 else
@@ -541,4 +578,4 @@ namespace xt
541578
542579} // namespace xt
543580
544- #endif // XTENSOR_INDEX_MAPPER_HPP
581+ #endif // XTENSOR_INDEX_MAPPER_HPP
0 commit comments