| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
#pragma once |
| 17 |
|
| 18 |
#include "macros.hpp" |
| 19 |
#include "extents.hpp" |
| 20 |
#include "trait_backports.hpp" |
| 21 |
#include "compressed_pair.hpp" |
| 22 |
#include "utility.hpp" |
| 23 |
|
| 24 |
#if !defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 25 |
# include "no_unique_address.hpp" |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <array> |
| 29 |
#include <type_traits> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
#ifdef __cpp_lib_span |
| 33 |
#include <span> |
| 34 |
#endif |
| 35 |
#if defined(_MDSPAN_USE_CONCEPTS) && MDSPAN_HAS_CXX_20 && defined(__cpp_lib_concepts) |
| 36 |
# include <concepts> |
| 37 |
#endif |
| 38 |
|
| 39 |
namespace MDSPAN_IMPL_STANDARD_NAMESPACE { |
| 40 |
|
| 41 |
struct layout_left { |
| 42 |
template<class Extents> |
| 43 |
class mapping; |
| 44 |
}; |
| 45 |
struct layout_right { |
| 46 |
template<class Extents> |
| 47 |
class mapping; |
| 48 |
}; |
| 49 |
|
| 50 |
namespace detail { |
| 51 |
template<class Layout, class Mapping> |
| 52 |
constexpr bool __is_mapping_of = |
| 53 |
std::is_same<typename Layout::template mapping<typename Mapping::extents_type>, Mapping>::value; |
| 54 |
|
| 55 |
#if defined(_MDSPAN_USE_CONCEPTS) && MDSPAN_HAS_CXX_20 |
| 56 |
# if !defined(__cpp_lib_concepts) |
| 57 |
namespace internal { |
| 58 |
namespace detail { |
| 59 |
template <typename _Tp, typename _Up> |
| 60 |
concept __same_as = std::is_same_v<_Tp, _Up>; |
| 61 |
} // namespace detail |
| 62 |
template <class T, class U> |
| 63 |
concept __same_as = detail::__same_as<T, U> && detail::__same_as<U, T>; |
| 64 |
} // namespace internal |
| 65 |
# endif |
| 66 |
|
| 67 |
template<class M> |
| 68 |
concept __layout_mapping_alike = requires { |
| 69 |
requires __is_extents<typename M::extents_type>::value; |
| 70 |
#if defined(__cpp_lib_concepts) |
| 71 |
{ M::is_always_strided() } -> std::same_as<bool>; |
| 72 |
{ M::is_always_exhaustive() } -> std::same_as<bool>; |
| 73 |
{ M::is_always_unique() } -> std::same_as<bool>; |
| 74 |
#else |
| 75 |
{ M::is_always_strided() } -> internal::__same_as<bool>; |
| 76 |
{ M::is_always_exhaustive() } -> internal::__same_as<bool>; |
| 77 |
{ M::is_always_unique() } -> internal::__same_as<bool>; |
| 78 |
#endif |
| 79 |
std::bool_constant<M::is_always_strided()>::value; |
| 80 |
std::bool_constant<M::is_always_exhaustive()>::value; |
| 81 |
std::bool_constant<M::is_always_unique()>::value; |
| 82 |
}; |
| 83 |
#endif |
| 84 |
|
| 85 |
} // namespace detail |
| 86 |
|
| 87 |
struct layout_stride { |
| 88 |
template <class Extents> |
| 89 |
class mapping |
| 90 |
#if !defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 91 |
: private detail::__no_unique_address_emulation< |
| 92 |
detail::__compressed_pair< |
| 93 |
Extents, |
| 94 |
detail::possibly_empty_array<typename Extents::index_type, Extents::rank()> |
| 95 |
> |
| 96 |
> |
| 97 |
#endif |
| 98 |
{ |
| 99 |
public: |
| 100 |
using extents_type = Extents; |
| 101 |
using index_type = typename extents_type::index_type; |
| 102 |
using size_type = typename extents_type::size_type; |
| 103 |
using rank_type = typename extents_type::rank_type; |
| 104 |
using layout_type = layout_stride; |
| 105 |
|
| 106 |
// This could be a `requires`, but I think it's better and clearer as a `static_assert`. |
| 107 |
static_assert(detail::__is_extents_v<Extents>, |
| 108 |
MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::layout_stride::mapping must be instantiated with a specialization of " MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::extents."); |
| 109 |
|
| 110 |
|
| 111 |
private: |
| 112 |
|
| 113 |
//---------------------------------------------------------------------------- |
| 114 |
|
| 115 |
using __strides_storage_t = detail::possibly_empty_array<index_type, extents_type::rank()>; |
| 116 |
using __member_pair_t = detail::__compressed_pair<extents_type, __strides_storage_t>; |
| 117 |
|
| 118 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 119 |
_MDSPAN_NO_UNIQUE_ADDRESS __member_pair_t __members; |
| 120 |
#else |
| 121 |
using __base_t = detail::__no_unique_address_emulation<__member_pair_t>; |
| 122 |
#endif |
| 123 |
|
| 124 |
MDSPAN_FORCE_INLINE_FUNCTION constexpr __strides_storage_t const& |
| 125 |
__strides_storage() const noexcept { |
| 126 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 127 |
return __members.__second(); |
| 128 |
#else |
| 129 |
return this->__base_t::__ref().__second(); |
| 130 |
#endif |
| 131 |
} |
| 132 |
MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 __strides_storage_t& |
| 133 |
__strides_storage() noexcept { |
| 134 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 135 |
return __members.__second(); |
| 136 |
#else |
| 137 |
return this->__base_t::__ref().__second(); |
| 138 |
#endif |
| 139 |
} |
| 140 |
|
| 141 |
template<class SizeType, size_t ... Ep, size_t ... Idx> |
| 142 |
_MDSPAN_HOST_DEVICE |
| 143 |
constexpr index_type __get_size(::MDSPAN_IMPL_STANDARD_NAMESPACE::extents<SizeType, Ep...>,std::integer_sequence<size_t, Idx...>) const { |
| 144 |
return _MDSPAN_FOLD_TIMES_RIGHT( static_cast<index_type>(extents().extent(Idx)), 1 ); |
| 145 |
} |
| 146 |
|
| 147 |
//---------------------------------------------------------------------------- |
| 148 |
|
| 149 |
template <class> |
| 150 |
friend class mapping; |
| 151 |
|
| 152 |
//---------------------------------------------------------------------------- |
| 153 |
|
| 154 |
// Workaround for non-deducibility of the index sequence template parameter if it's given at the top level |
| 155 |
template <class> |
| 156 |
struct __deduction_workaround; |
| 157 |
|
| 158 |
template <size_t... Idxs> |
| 159 |
struct __deduction_workaround<std::index_sequence<Idxs...>> |
| 160 |
{ |
| 161 |
template <class OtherExtents> |
| 162 |
MDSPAN_INLINE_FUNCTION |
| 163 |
static constexpr bool _eq_impl(mapping const& self, mapping<OtherExtents> const& other) noexcept { |
| 164 |
using common_t = std::common_type_t<index_type, typename OtherExtents::index_type>; |
| 165 |
return _MDSPAN_FOLD_AND((static_cast<common_t>(self.stride(Idxs)) == static_cast<common_t>(other.stride(Idxs))) /* && ... */) |
| 166 |
&& _MDSPAN_FOLD_AND((static_cast<common_t>(self.extents().extent(Idxs)) == static_cast<common_t>(other.extents().extent(Idxs))) /* || ... */); |
| 167 |
} |
| 168 |
template <class OtherExtents> |
| 169 |
MDSPAN_INLINE_FUNCTION |
| 170 |
static constexpr bool _not_eq_impl(mapping const& self, mapping<OtherExtents> const& other) noexcept { |
| 171 |
using common_t = std::common_type_t<index_type, typename OtherExtents::index_type>; |
| 172 |
return _MDSPAN_FOLD_OR((static_cast<common_t>(self.stride(Idxs)) != static_cast<common_t>(other.stride(Idxs))) /* || ... */) |
| 173 |
|| _MDSPAN_FOLD_OR((static_cast<common_t>(self.extents().extent(Idxs)) != static_cast<common_t>(other.extents().extent(Idxs))) /* || ... */); |
| 174 |
} |
| 175 |
|
| 176 |
template <class... Integral> |
| 177 |
MDSPAN_FORCE_INLINE_FUNCTION |
| 178 |
static constexpr size_t _call_op_impl(mapping const& self, Integral... idxs) noexcept { |
| 179 |
return _MDSPAN_FOLD_PLUS_RIGHT((idxs * self.stride(Idxs)), /* + ... + */ 0); |
| 180 |
} |
| 181 |
|
| 182 |
MDSPAN_INLINE_FUNCTION |
| 183 |
static constexpr size_t _req_span_size_impl(mapping const& self) noexcept { |
| 184 |
// assumes no negative strides; not sure if I'm allowed to assume that or not |
| 185 |
return __impl::_call_op_impl(self, (self.extents().template __extent<Idxs>() - 1)...) + 1; |
| 186 |
} |
| 187 |
|
| 188 |
template<class OtherMapping> |
| 189 |
MDSPAN_INLINE_FUNCTION |
| 190 |
static constexpr const __strides_storage_t fill_strides(const OtherMapping& map) { |
| 191 |
return __strides_storage_t{static_cast<index_type>(map.stride(Idxs))...}; |
| 192 |
} |
| 193 |
|
| 194 |
MDSPAN_INLINE_FUNCTION |
| 195 |
static constexpr const __strides_storage_t& fill_strides(const __strides_storage_t& s) { |
| 196 |
return s; |
| 197 |
} |
| 198 |
|
| 199 |
template<class IntegralType> |
| 200 |
static constexpr const __strides_storage_t fill_strides(const std::array<IntegralType,extents_type::rank()>& s) { |
| 201 |
return __strides_storage_t{static_cast<index_type>(s[Idxs])...}; |
| 202 |
} |
| 203 |
|
| 204 |
MDSPAN_TEMPLATE_REQUIRES( |
| 205 |
class IntegralType, |
| 206 |
(std::is_convertible<IntegralType, typename extents_type::index_type>::value) |
| 207 |
) |
| 208 |
MDSPAN_INLINE_FUNCTION |
| 209 |
// Need to avoid zero length c-array |
| 210 |
static constexpr const __strides_storage_t fill_strides(mdspan_non_standard_tag, const IntegralType (&s)[extents_type::rank()>0?extents_type::rank():1]) { |
| 211 |
return __strides_storage_t{static_cast<index_type>(s[Idxs])...}; |
| 212 |
} |
| 213 |
|
| 214 |
#ifdef __cpp_lib_span |
| 215 |
template<class IntegralType> |
| 216 |
static constexpr const __strides_storage_t fill_strides(const std::span<IntegralType,extents_type::rank()>& s) { |
| 217 |
return __strides_storage_t{static_cast<index_type>(s[Idxs])...}; |
| 218 |
} |
| 219 |
#endif |
| 220 |
|
| 221 |
MDSPAN_INLINE_FUNCTION |
| 222 |
static constexpr std::array<index_type, extents_type::rank()> return_strides(const __strides_storage_t& s) { |
| 223 |
return std::array<index_type, extents_type::rank()>{s[Idxs]...}; |
| 224 |
} |
| 225 |
|
| 226 |
template<size_t K> |
| 227 |
MDSPAN_INLINE_FUNCTION |
| 228 |
static constexpr size_t __return_zero() { return 0; } |
| 229 |
|
| 230 |
template<class Mapping> |
| 231 |
MDSPAN_INLINE_FUNCTION |
| 232 |
static constexpr typename Mapping::index_type |
| 233 |
__OFFSET(const Mapping& m) { return m(__return_zero<Idxs>()...); } |
| 234 |
}; |
| 235 |
|
| 236 |
// Can't use defaulted parameter in the __deduction_workaround template because of a bug in MSVC warning C4348. |
| 237 |
using __impl = __deduction_workaround<std::make_index_sequence<Extents::rank()>>; |
| 238 |
|
| 239 |
MDSPAN_FUNCTION |
| 240 |
static constexpr __strides_storage_t strides_storage(detail::with_rank<0>) { |
| 241 |
return {}; |
| 242 |
} |
| 243 |
|
| 244 |
template <std::size_t N> |
| 245 |
MDSPAN_FUNCTION |
| 246 |
static constexpr __strides_storage_t strides_storage(detail::with_rank<N>) { |
| 247 |
__strides_storage_t s{}; |
| 248 |
|
| 249 |
extents_type e; |
| 250 |
index_type stride = 1; |
| 251 |
for(int r = static_cast<int>(extents_type::rank() - 1); r >= 0; r--) { |
| 252 |
s[r] = stride; |
| 253 |
stride *= e.extent(r); |
| 254 |
} |
| 255 |
|
| 256 |
return s; |
| 257 |
} |
| 258 |
|
| 259 |
//---------------------------------------------------------------------------- |
| 260 |
|
| 261 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 262 |
MDSPAN_INLINE_FUNCTION constexpr explicit |
| 263 |
mapping(__member_pair_t&& __m) : __members(::std::move(__m)) {} |
| 264 |
#else |
| 265 |
MDSPAN_INLINE_FUNCTION constexpr explicit |
| 266 |
mapping(__base_t&& __b) : __base_t(::std::move(__b)) {} |
| 267 |
#endif |
| 268 |
|
| 269 |
public: |
| 270 |
|
| 271 |
//-------------------------------------------------------------------------------- |
| 272 |
|
| 273 |
MDSPAN_INLINE_FUNCTION constexpr mapping() noexcept |
| 274 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 275 |
: __members{ |
| 276 |
#else |
| 277 |
: __base_t(__base_t{__member_pair_t( |
| 278 |
#endif |
| 279 |
extents_type(), |
| 280 |
__strides_storage_t(strides_storage(detail::with_rank<extents_type::rank()>{})) |
| 281 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 282 |
} |
| 283 |
#else |
| 284 |
)}) |
| 285 |
#endif |
| 286 |
{} |
| 287 |
|
| 288 |
MDSPAN_INLINE_FUNCTION_DEFAULTED constexpr mapping(mapping const&) noexcept = default; |
| 289 |
|
| 290 |
MDSPAN_TEMPLATE_REQUIRES( |
| 291 |
class IntegralTypes, |
| 292 |
/* requires */ ( |
| 293 |
// MSVC 19.32 does not like using index_type here, requires the typename Extents::index_type |
| 294 |
// error C2641: cannot deduce template arguments for 'MDSPAN_IMPL_STANDARD_NAMESPACE::layout_stride::mapping' |
| 295 |
_MDSPAN_TRAIT(std::is_convertible, const std::remove_const_t<IntegralTypes>&, typename Extents::index_type) && |
| 296 |
_MDSPAN_TRAIT(std::is_nothrow_constructible, typename Extents::index_type, const std::remove_const_t<IntegralTypes>&) |
| 297 |
) |
| 298 |
) |
| 299 |
constexpr |
| 300 |
mapping( |
| 301 |
extents_type const& e, |
| 302 |
std::array<IntegralTypes, extents_type::rank()> const& s |
| 303 |
) noexcept |
| 304 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 305 |
: __members{ |
| 306 |
#else |
| 307 |
: __base_t(__base_t{__member_pair_t( |
| 308 |
#endif |
| 309 |
e, __strides_storage_t(__impl::fill_strides(s)) |
| 310 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 311 |
} |
| 312 |
#else |
| 313 |
)}) |
| 314 |
#endif |
| 315 |
{ |
| 316 |
/* |
| 317 |
* TODO: check preconditions |
| 318 |
* - s[i] > 0 is true for all i in the range [0, rank_ ). |
| 319 |
* - REQUIRED-SPAN-SIZE(e, s) is a representable value of type index_type ([basic.fundamental]). |
| 320 |
* - If rank_ is greater than 0, then there exists a permutation P of the integers in the |
| 321 |
* range [0, rank_), such that s[ pi ] >= s[ pi − 1 ] * e.extent( pi − 1 ) is true for |
| 322 |
* all i in the range [1, rank_ ), where pi is the ith element of P. |
| 323 |
*/ |
| 324 |
} |
| 325 |
|
| 326 |
MDSPAN_TEMPLATE_REQUIRES( |
| 327 |
class IntegralTypes, |
| 328 |
/* requires */ ( |
| 329 |
// MSVC 19.32 does not like using index_type here, requires the typename Extents::index_type |
| 330 |
// error C2641: cannot deduce template arguments for 'MDSPAN_IMPL_STANDARD_NAMESPACE::layout_stride::mapping' |
| 331 |
_MDSPAN_TRAIT(std::is_convertible, const std::remove_const_t<IntegralTypes>&, typename Extents::index_type) && |
| 332 |
_MDSPAN_TRAIT(std::is_nothrow_constructible, typename Extents::index_type, const std::remove_const_t<IntegralTypes>&) |
| 333 |
) |
| 334 |
) |
| 335 |
MDSPAN_INLINE_FUNCTION |
| 336 |
constexpr |
| 337 |
mapping( |
| 338 |
mdspan_non_standard_tag, |
| 339 |
extents_type const& e, |
| 340 |
// Need to avoid zero-length c-array |
| 341 |
const IntegralTypes (&s)[extents_type::rank()>0?extents_type::rank():1] |
| 342 |
) noexcept |
| 343 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 344 |
: __members{ |
| 345 |
#else |
| 346 |
: __base_t(__base_t{__member_pair_t( |
| 347 |
#endif |
| 348 |
e, __strides_storage_t(__impl::fill_strides(mdspan_non_standard, s)) |
| 349 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 350 |
} |
| 351 |
#else |
| 352 |
)}) |
| 353 |
#endif |
| 354 |
{ |
| 355 |
/* |
| 356 |
* TODO: check preconditions |
| 357 |
* - s[i] > 0 is true for all i in the range [0, rank_ ). |
| 358 |
* - REQUIRED-SPAN-SIZE(e, s) is a representable value of type index_type ([basic.fundamental]). |
| 359 |
* - If rank_ is greater than 0, then there exists a permutation P of the integers in the |
| 360 |
* range [0, rank_), such that s[ pi ] >= s[ pi − 1 ] * e.extent( pi − 1 ) is true for |
| 361 |
* all i in the range [1, rank_ ), where pi is the ith element of P. |
| 362 |
*/ |
| 363 |
} |
| 364 |
|
| 365 |
#ifdef __cpp_lib_span |
| 366 |
MDSPAN_TEMPLATE_REQUIRES( |
| 367 |
class IntegralTypes, |
| 368 |
/* requires */ ( |
| 369 |
// MSVC 19.32 does not like using index_type here, requires the typename Extents::index_type |
| 370 |
// error C2641: cannot deduce template arguments for 'MDSPAN_IMPL_STANDARD_NAMESPACE::layout_stride::mapping' |
| 371 |
_MDSPAN_TRAIT(std::is_convertible, const std::remove_const_t<IntegralTypes>&, typename Extents::index_type) && |
| 372 |
_MDSPAN_TRAIT(std::is_nothrow_constructible, typename Extents::index_type, const std::remove_const_t<IntegralTypes>&) |
| 373 |
) |
| 374 |
) |
| 375 |
constexpr |
| 376 |
mapping( |
| 377 |
extents_type const& e, |
| 378 |
std::span<IntegralTypes, extents_type::rank()> const& s |
| 379 |
) noexcept |
| 380 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 381 |
: __members{ |
| 382 |
#else |
| 383 |
: __base_t(__base_t{__member_pair_t( |
| 384 |
#endif |
| 385 |
e, __strides_storage_t(__impl::fill_strides(s)) |
| 386 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 387 |
} |
| 388 |
#else |
| 389 |
)}) |
| 390 |
#endif |
| 391 |
{ |
| 392 |
/* |
| 393 |
* TODO: check preconditions |
| 394 |
* - s[i] > 0 is true for all i in the range [0, rank_ ). |
| 395 |
* - REQUIRED-SPAN-SIZE(e, s) is a representable value of type index_type ([basic.fundamental]). |
| 396 |
* - If rank_ is greater than 0, then there exists a permutation P of the integers in the |
| 397 |
* range [0, rank_), such that s[ pi ] >= s[ pi − 1 ] * e.extent( pi − 1 ) is true for |
| 398 |
* all i in the range [1, rank_ ), where pi is the ith element of P. |
| 399 |
*/ |
| 400 |
} |
| 401 |
#endif // __cpp_lib_span |
| 402 |
|
| 403 |
#if !(defined(_MDSPAN_USE_CONCEPTS) && MDSPAN_HAS_CXX_20) |
| 404 |
MDSPAN_TEMPLATE_REQUIRES( |
| 405 |
class StridedLayoutMapping, |
| 406 |
/* requires */ ( |
| 407 |
_MDSPAN_TRAIT(std::is_constructible, extents_type, typename StridedLayoutMapping::extents_type) && |
| 408 |
detail::__is_mapping_of<typename StridedLayoutMapping::layout_type, StridedLayoutMapping> && |
| 409 |
StridedLayoutMapping::is_always_unique() && |
| 410 |
StridedLayoutMapping::is_always_strided() |
| 411 |
) |
| 412 |
) |
| 413 |
#else |
| 414 |
template<class StridedLayoutMapping> |
| 415 |
requires( |
| 416 |
detail::__layout_mapping_alike<StridedLayoutMapping> && |
| 417 |
_MDSPAN_TRAIT(std::is_constructible, extents_type, typename StridedLayoutMapping::extents_type) && |
| 418 |
StridedLayoutMapping::is_always_unique() && |
| 419 |
StridedLayoutMapping::is_always_strided() |
| 420 |
) |
| 421 |
#endif |
| 422 |
MDSPAN_CONDITIONAL_EXPLICIT( |
| 423 |
!(std::is_convertible<typename StridedLayoutMapping::extents_type, extents_type>::value && |
| 424 |
(detail::__is_mapping_of<layout_left, StridedLayoutMapping> || |
| 425 |
detail::__is_mapping_of<layout_right, StridedLayoutMapping> || |
| 426 |
detail::__is_mapping_of<layout_stride, StridedLayoutMapping>)) |
| 427 |
) // needs two () due to comma |
| 428 |
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 |
| 429 |
mapping(StridedLayoutMapping const& other) noexcept // NOLINT(google-explicit-constructor) |
| 430 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 431 |
: __members{ |
| 432 |
#else |
| 433 |
: __base_t(__base_t{__member_pair_t( |
| 434 |
#endif |
| 435 |
other.extents(), __strides_storage_t(__impl::fill_strides(other)) |
| 436 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 437 |
} |
| 438 |
#else |
| 439 |
)}) |
| 440 |
#endif |
| 441 |
{ |
| 442 |
/* |
| 443 |
* TODO: check preconditions |
| 444 |
* - other.stride(i) > 0 is true for all i in the range [0, rank_ ). |
| 445 |
* - other.required_span_size() is a representable value of type index_type ([basic.fundamental]). |
| 446 |
* - OFFSET(other) == 0 |
| 447 |
*/ |
| 448 |
} |
| 449 |
|
| 450 |
//-------------------------------------------------------------------------------- |
| 451 |
|
| 452 |
MDSPAN_INLINE_FUNCTION_DEFAULTED _MDSPAN_CONSTEXPR_14_DEFAULTED |
| 453 |
mapping& operator=(mapping const&) noexcept = default; |
| 454 |
|
| 455 |
MDSPAN_INLINE_FUNCTION constexpr const extents_type& extents() const noexcept { |
| 456 |
#if defined(_MDSPAN_USE_ATTRIBUTE_NO_UNIQUE_ADDRESS) |
| 457 |
return __members.__first(); |
| 458 |
#else |
| 459 |
return this->__base_t::__ref().__first(); |
| 460 |
#endif |
| 461 |
}; |
| 462 |
|
| 463 |
MDSPAN_INLINE_FUNCTION |
| 464 |
constexpr std::array< index_type, extents_type::rank() > strides() const noexcept { |
| 465 |
return __impl::return_strides(__strides_storage()); |
| 466 |
} |
| 467 |
|
| 468 |
MDSPAN_INLINE_FUNCTION |
| 469 |
constexpr index_type required_span_size() const noexcept { |
| 470 |
index_type span_size = 1; |
| 471 |
// using int here to avoid warning about pointless comparison to 0 |
| 472 |
for(int r = 0; r < static_cast<int>(extents_type::rank()); r++) { |
| 473 |
// Return early if any of the extents are zero |
| 474 |
if(extents().extent(r)==0) return 0; |
| 475 |
span_size += ( static_cast<index_type>(extents().extent(r) - 1 ) * __strides_storage()[r]); |
| 476 |
} |
| 477 |
return span_size; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
MDSPAN_TEMPLATE_REQUIRES( |
| 482 |
class... Indices, |
| 483 |
/* requires */ ( |
| 484 |
sizeof...(Indices) == Extents::rank() && |
| 485 |
(detail::are_valid_indices<index_type, Indices...>()) |
| 486 |
) |
| 487 |
) |
| 488 |
MDSPAN_FORCE_INLINE_FUNCTION |
| 489 |
constexpr index_type operator()(Indices... idxs) const noexcept { |
| 490 |
#if ! defined(NDEBUG) |
| 491 |
detail::check_all_indices(this->extents(), idxs...); |
| 492 |
#endif // ! NDEBUG |
| 493 |
return static_cast<index_type>(__impl::_call_op_impl(*this, static_cast<index_type>(idxs)...)); |
| 494 |
} |
| 495 |
|
| 496 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_always_unique() noexcept { return true; } |
| 497 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_always_exhaustive() noexcept { |
| 498 |
return false; |
| 499 |
} |
| 500 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_always_strided() noexcept { return true; } |
| 501 |
|
| 502 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_unique() noexcept { return true; } |
| 503 |
|
| 504 |
private: |
| 505 |
MDSPAN_INLINE_FUNCTION |
| 506 |
constexpr bool exhaustive_for_nonzero_span_size() const |
| 507 |
{ |
| 508 |
return required_span_size() == __get_size(extents(), std::make_index_sequence<extents_type::rank()>()); |
| 509 |
} |
| 510 |
|
| 511 |
MDSPAN_INLINE_FUNCTION |
| 512 |
constexpr bool is_exhaustive_impl(detail::with_rank<0>) const |
| 513 |
{ |
| 514 |
return true; |
| 515 |
} |
| 516 |
MDSPAN_INLINE_FUNCTION |
| 517 |
constexpr bool is_exhaustive_impl(detail::with_rank<1>) const |
| 518 |
{ |
| 519 |
if (required_span_size() != static_cast<index_type>(0)) { |
| 520 |
return exhaustive_for_nonzero_span_size(); |
| 521 |
} |
| 522 |
return stride(0) == 1; |
| 523 |
} |
| 524 |
template <std::size_t N> |
| 525 |
MDSPAN_INLINE_FUNCTION |
| 526 |
constexpr bool is_exhaustive_impl(detail::with_rank<N>) const |
| 527 |
{ |
| 528 |
if (required_span_size() != static_cast<index_type>(0)) { |
| 529 |
return exhaustive_for_nonzero_span_size(); |
| 530 |
} |
| 531 |
|
| 532 |
rank_type r_largest = 0; |
| 533 |
for (rank_type r = 1; r < extents_type::rank(); r++) { |
| 534 |
if (stride(r) > stride(r_largest)) { |
| 535 |
r_largest = r; |
| 536 |
} |
| 537 |
} |
| 538 |
for (rank_type r = 0; r < extents_type::rank(); r++) { |
| 539 |
if (extents().extent(r) == 0 && r != r_largest) { |
| 540 |
return false; |
| 541 |
} |
| 542 |
} |
| 543 |
return true; |
| 544 |
} |
| 545 |
|
| 546 |
public: |
| 547 |
MDSPAN_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 bool is_exhaustive() const noexcept { |
| 548 |
return is_exhaustive_impl(detail::with_rank<extents_type::rank()>{}); |
| 549 |
} |
| 550 |
MDSPAN_INLINE_FUNCTION static constexpr bool is_strided() noexcept { return true; } |
| 551 |
|
| 552 |
|
| 553 |
MDSPAN_INLINE_FUNCTION |
| 554 |
constexpr index_type stride(rank_type r) const noexcept { |
| 555 |
return __strides_storage()[r]; |
| 556 |
} |
| 557 |
|
| 558 |
#if !(defined(_MDSPAN_USE_CONCEPTS) && MDSPAN_HAS_CXX_20) |
| 559 |
MDSPAN_TEMPLATE_REQUIRES( |
| 560 |
class StridedLayoutMapping, |
| 561 |
/* requires */ ( |
| 562 |
detail::__is_mapping_of<typename StridedLayoutMapping::layout_type, StridedLayoutMapping> && |
| 563 |
(extents_type::rank() == StridedLayoutMapping::extents_type::rank()) && |
| 564 |
StridedLayoutMapping::is_always_strided() |
| 565 |
) |
| 566 |
) |
| 567 |
#else |
| 568 |
template<class StridedLayoutMapping> |
| 569 |
requires( |
| 570 |
detail::__layout_mapping_alike<StridedLayoutMapping> && |
| 571 |
(extents_type::rank() == StridedLayoutMapping::extents_type::rank()) && |
| 572 |
StridedLayoutMapping::is_always_strided() |
| 573 |
) |
| 574 |
#endif |
| 575 |
MDSPAN_INLINE_FUNCTION |
| 576 |
friend constexpr bool operator==(const mapping& x, const StridedLayoutMapping& y) noexcept { |
| 577 |
return (x.extents() == y.extents()) && |
| 578 |
(__impl::__OFFSET(y) == static_cast<typename StridedLayoutMapping::index_type>(0)) && |
| 579 |
detail::rankwise_equal(detail::with_rank<extents_type::rank()>{}, x, y, detail::stride); |
| 580 |
} |
| 581 |
|
| 582 |
// This one is not technically part of the proposal. Just here to make implementation a bit more optimal hopefully |
| 583 |
MDSPAN_TEMPLATE_REQUIRES( |
| 584 |
class OtherExtents, |
| 585 |
/* requires */ ( |
| 586 |
(extents_type::rank() == OtherExtents::rank()) |
| 587 |
) |
| 588 |
) |
| 589 |
MDSPAN_INLINE_FUNCTION |
| 590 |
friend constexpr bool operator==(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept { |
| 591 |
return __impl::_eq_impl(lhs, rhs); |
| 592 |
} |
| 593 |
|
| 594 |
#if !MDSPAN_HAS_CXX_20 |
| 595 |
MDSPAN_TEMPLATE_REQUIRES( |
| 596 |
class StridedLayoutMapping, |
| 597 |
/* requires */ ( |
| 598 |
detail::__is_mapping_of<typename StridedLayoutMapping::layout_type, StridedLayoutMapping> && |
| 599 |
(extents_type::rank() == StridedLayoutMapping::extents_type::rank()) && |
| 600 |
StridedLayoutMapping::is_always_strided() |
| 601 |
) |
| 602 |
) |
| 603 |
MDSPAN_INLINE_FUNCTION |
| 604 |
friend constexpr bool operator!=(const mapping& x, const StridedLayoutMapping& y) noexcept { |
| 605 |
return !(x == y); |
| 606 |
} |
| 607 |
|
| 608 |
MDSPAN_TEMPLATE_REQUIRES( |
| 609 |
class OtherExtents, |
| 610 |
/* requires */ ( |
| 611 |
(extents_type::rank() == OtherExtents::rank()) |
| 612 |
) |
| 613 |
) |
| 614 |
MDSPAN_INLINE_FUNCTION |
| 615 |
friend constexpr bool operator!=(mapping const& lhs, mapping<OtherExtents> const& rhs) noexcept { |
| 616 |
return __impl::_not_eq_impl(lhs, rhs); |
| 617 |
} |
| 618 |
#endif |
| 619 |
|
| 620 |
// [mdspan.submdspan.mapping], submdspan mapping specialization |
| 621 |
template<class... SliceSpecifiers> |
| 622 |
MDSPAN_INLINE_FUNCTION |
| 623 |
constexpr auto submdspan_mapping_impl( |
| 624 |
SliceSpecifiers... slices) const; |
| 625 |
|
| 626 |
template<class... SliceSpecifiers> |
| 627 |
MDSPAN_INLINE_FUNCTION |
| 628 |
friend constexpr auto submdspan_mapping( |
| 629 |
const mapping& src, SliceSpecifiers... slices) { |
| 630 |
return src.submdspan_mapping_impl(slices...); |
| 631 |
} |
| 632 |
}; |
| 633 |
}; |
| 634 |
|
| 635 |
namespace detail { |
| 636 |
|
| 637 |
template <class Layout, class Extents, class Mapping> |
| 638 |
MDSPAN_INLINE_FUNCTION |
| 639 |
constexpr void validate_strides(with_rank<0>, Layout, const Extents&, const Mapping&) |
| 640 |
{} |
| 641 |
|
| 642 |
template <std::size_t N, class Layout, class Extents, class Mapping> |
| 643 |
MDSPAN_INLINE_FUNCTION |
| 644 |
constexpr void validate_strides(with_rank<N>, Layout, const Extents& ext, const Mapping& other) |
| 645 |
{ |
| 646 |
static_assert(std::is_same<typename Mapping::layout_type, layout_stride>::value && |
| 647 |
(std::is_same<Layout, layout_left>::value || |
| 648 |
std::is_same<Layout, layout_right>::value) |
| 649 |
, "This function is only intended to validate construction of " |
| 650 |
"a layout_left or layout_right mapping from a layout_stride mapping."); |
| 651 |
|
| 652 |
constexpr auto is_left = std::is_same<Layout, layout_left>::value; |
| 653 |
|
| 654 |
typename Extents::index_type expected_stride = 1; |
| 655 |
|
| 656 |
for (std::size_t r = 0; r < N; r++) { |
| 657 |
const std::size_t s = is_left ? r : N - 1 - r; |
| 658 |
|
| 659 |
MDSPAN_IMPL_PRECONDITION(common_integral_compare(expected_stride, other.stride(s)) |
| 660 |
&& "invalid strides for layout_{left,right}"); |
| 661 |
|
| 662 |
expected_stride *= ext.extent(s); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
} // namespace detail |
| 667 |
} // end namespace MDSPAN_IMPL_STANDARD_NAMESPACE |