| 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 |
|
| 17 |
#pragma once |
| 18 |
#include "dynamic_extent.hpp" |
| 19 |
#include "utility.hpp" |
| 20 |
|
| 21 |
#ifdef __cpp_lib_span |
| 22 |
#include <span> |
| 23 |
#endif |
| 24 |
#include <array> |
| 25 |
#include <type_traits> |
| 26 |
|
| 27 |
#include <cassert> |
| 28 |
#include <cinttypes> |
| 29 |
|
| 30 |
namespace MDSPAN_IMPL_STANDARD_NAMESPACE { |
| 31 |
namespace detail { |
| 32 |
|
| 33 |
// Function used to check compatibility of extents in converting constructor |
| 34 |
// can't be a private member function for some reason. |
| 35 |
template <size_t... Extents, size_t... OtherExtents> |
| 36 |
MDSPAN_INLINE_FUNCTION |
| 37 |
static constexpr std::integral_constant<bool, false> __check_compatible_extents( |
| 38 |
std::integral_constant<bool, false>, |
| 39 |
std::integer_sequence<size_t, Extents...>, |
| 40 |
std::integer_sequence<size_t, OtherExtents...>) noexcept { |
| 41 |
return {}; |
| 42 |
} |
| 43 |
|
| 44 |
// This helper prevents ICE's on MSVC. |
| 45 |
template <size_t Lhs, size_t Rhs> |
| 46 |
struct __compare_extent_compatible : std::integral_constant<bool, |
| 47 |
Lhs == dynamic_extent || |
| 48 |
Rhs == dynamic_extent || |
| 49 |
Lhs == Rhs> |
| 50 |
{}; |
| 51 |
|
| 52 |
template <size_t... Extents, size_t... OtherExtents> |
| 53 |
MDSPAN_INLINE_FUNCTION |
| 54 |
static constexpr std::integral_constant< |
| 55 |
bool, _MDSPAN_FOLD_AND(__compare_extent_compatible<Extents, OtherExtents>::value)> |
| 56 |
__check_compatible_extents( |
| 57 |
std::integral_constant<bool, true>, |
| 58 |
std::integer_sequence<size_t, Extents...>, |
| 59 |
std::integer_sequence<size_t, OtherExtents...>) noexcept { |
| 60 |
return {}; |
| 61 |
} |
| 62 |
|
| 63 |
template<class IndexType, class ... Arguments> |
| 64 |
MDSPAN_INLINE_FUNCTION |
| 65 |
static constexpr bool are_valid_indices() { |
| 66 |
return |
| 67 |
_MDSPAN_FOLD_AND(std::is_convertible<Arguments, IndexType>::value) && |
| 68 |
_MDSPAN_FOLD_AND(std::is_nothrow_constructible<IndexType, Arguments>::value); |
| 69 |
} |
| 70 |
|
| 71 |
// ------------------------------------------------------------------ |
| 72 |
// ------------ static_array ---------------------------------------- |
| 73 |
// ------------------------------------------------------------------ |
| 74 |
|
| 75 |
// array like class which provides an array of static values with get |
| 76 |
// function and operator []. |
| 77 |
|
| 78 |
// Implementation of Static Array with recursive implementation of get. |
| 79 |
template <size_t R, class T, T... Extents> struct static_array_impl; |
| 80 |
|
| 81 |
template <size_t R, class T, T FirstExt, T... Extents> |
| 82 |
struct static_array_impl<R, T, FirstExt, Extents...> { |
| 83 |
MDSPAN_INLINE_FUNCTION |
| 84 |
constexpr static T get(size_t r) { |
| 85 |
if (r == R) |
| 86 |
return FirstExt; |
| 87 |
else |
| 88 |
return static_array_impl<R + 1, T, Extents...>::get(r); |
| 89 |
} |
| 90 |
template <size_t r> MDSPAN_INLINE_FUNCTION constexpr static T get() { |
| 91 |
#if MDSPAN_HAS_CXX_17 |
| 92 |
if constexpr (r == R) |
| 93 |
return FirstExt; |
| 94 |
else |
| 95 |
return static_array_impl<R + 1, T, Extents...>::template get<r>(); |
| 96 |
#else |
| 97 |
get(r); |
| 98 |
#endif |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
// End the recursion |
| 103 |
template <size_t R, class T, T FirstExt> |
| 104 |
struct static_array_impl<R, T, FirstExt> { |
| 105 |
MDSPAN_INLINE_FUNCTION |
| 106 |
constexpr static T get(size_t) { return FirstExt; } |
| 107 |
template <size_t> MDSPAN_INLINE_FUNCTION constexpr static T get() { |
| 108 |
return FirstExt; |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
// Don't start recursion if size 0 |
| 113 |
template <class T> struct static_array_impl<0, T> { |
| 114 |
MDSPAN_INLINE_FUNCTION |
| 115 |
constexpr static T get(size_t) { return T(); } |
| 116 |
template <size_t> MDSPAN_INLINE_FUNCTION constexpr static T get() { |
| 117 |
return T(); |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
// Static array, provides get<r>(), get(r) and operator[r] |
| 122 |
template <class T, T... Values> struct static_array: |
| 123 |
public static_array_impl<0, T, Values...> { |
| 124 |
|
| 125 |
public: |
| 126 |
using value_type = T; |
| 127 |
|
| 128 |
MDSPAN_INLINE_FUNCTION |
| 129 |
constexpr static size_t size() { return sizeof...(Values); } |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
// ------------------------------------------------------------------ |
| 134 |
// ------------ index_sequence_scan --------------------------------- |
| 135 |
// ------------------------------------------------------------------ |
| 136 |
|
| 137 |
// index_sequence_scan takes compile time values and provides get(r) |
| 138 |
// and get<r>() which return the sum of the first r-1 values. |
| 139 |
|
| 140 |
// Recursive implementation for get |
| 141 |
template <size_t R, size_t... Values> struct index_sequence_scan_impl; |
| 142 |
|
| 143 |
template <size_t R, size_t FirstVal, size_t... Values> |
| 144 |
struct index_sequence_scan_impl<R, FirstVal, Values...> { |
| 145 |
MDSPAN_INLINE_FUNCTION |
| 146 |
constexpr static size_t get(size_t r) { |
| 147 |
if (r > R) |
| 148 |
return FirstVal + index_sequence_scan_impl<R + 1, Values...>::get(r); |
| 149 |
else |
| 150 |
return 0; |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
template <size_t R, size_t FirstVal> |
| 155 |
struct index_sequence_scan_impl<R, FirstVal> { |
| 156 |
#if defined(__NVCC__) || defined(__NVCOMPILER) || \ |
| 157 |
defined(_MDSPAN_COMPILER_INTEL) |
| 158 |
// NVCC warns about pointless comparison with 0 for R==0 and r being const |
| 159 |
// evaluatable and also 0. |
| 160 |
MDSPAN_INLINE_FUNCTION |
| 161 |
constexpr static size_t get(size_t r) { |
| 162 |
return static_cast<int64_t>(R) > static_cast<int64_t>(r) ? FirstVal : 0; |
| 163 |
} |
| 164 |
#else |
| 165 |
MDSPAN_INLINE_FUNCTION |
| 166 |
constexpr static size_t get(size_t r) { return R > r ? FirstVal : 0; } |
| 167 |
#endif |
| 168 |
}; |
| 169 |
template <> struct index_sequence_scan_impl<0> { |
| 170 |
MDSPAN_INLINE_FUNCTION |
| 171 |
constexpr static size_t get(size_t) { return 0; } |
| 172 |
}; |
| 173 |
|
| 174 |
// ------------------------------------------------------------------ |
| 175 |
// ------------ possibly_empty_array ------------------------------- |
| 176 |
// ------------------------------------------------------------------ |
| 177 |
|
| 178 |
// array like class which provides get function and operator [], and |
| 179 |
// has a specialization for the size 0 case. |
| 180 |
// This is needed to make the maybe_static_array be truly empty, for |
| 181 |
// all static values. |
| 182 |
|
| 183 |
template <class T, size_t N> struct possibly_empty_array { |
| 184 |
T vals[N]{}; |
| 185 |
MDSPAN_INLINE_FUNCTION |
| 186 |
constexpr T &operator[](size_t r) { return vals[r]; } |
| 187 |
MDSPAN_INLINE_FUNCTION |
| 188 |
constexpr const T &operator[](size_t r) const { return vals[r]; } |
| 189 |
}; |
| 190 |
|
| 191 |
template <class T> struct possibly_empty_array<T, 0> { |
| 192 |
MDSPAN_INLINE_FUNCTION |
| 193 |
constexpr T operator[](size_t) { return T(); } |
| 194 |
MDSPAN_INLINE_FUNCTION |
| 195 |
constexpr const T operator[](size_t) const { return T(); } |
| 196 |
}; |
| 197 |
|
| 198 |
// ------------------------------------------------------------------ |
| 199 |
// ------------ maybe_static_array ---------------------------------- |
| 200 |
// ------------------------------------------------------------------ |
| 201 |
|
| 202 |
// array like class which has a mix of static and runtime values but |
| 203 |
// only stores the runtime values. |
| 204 |
// The type of the static and the runtime values can be different. |
| 205 |
// The position of a dynamic value is indicated through a tag value. |
| 206 |
template <class TDynamic, class TStatic, TStatic dyn_tag, TStatic... Values> |
| 207 |
struct maybe_static_array { |
| 208 |
|
| 209 |
static_assert(std::is_convertible<TStatic, TDynamic>::value, "maybe_static_array: TStatic must be convertible to TDynamic"); |
| 210 |
static_assert(std::is_convertible<TDynamic, TStatic>::value, "maybe_static_array: TDynamic must be convertible to TStatic"); |
| 211 |
|
| 212 |
private: |
| 213 |
// Static values member |
| 214 |
using static_vals_t = static_array<TStatic, Values...>; |
| 215 |
constexpr static size_t m_size = sizeof...(Values); |
| 216 |
constexpr static size_t m_size_dynamic = |
| 217 |
_MDSPAN_FOLD_PLUS_RIGHT((Values == dyn_tag), 0); |
| 218 |
|
| 219 |
// Dynamic values member |
| 220 |
_MDSPAN_NO_UNIQUE_ADDRESS possibly_empty_array<TDynamic, m_size_dynamic> |
| 221 |
m_dyn_vals; |
| 222 |
|
| 223 |
// static mapping of indices to the position in the dynamic values array |
| 224 |
using dyn_map_t = index_sequence_scan_impl<0, static_cast<size_t>(Values == dyn_tag)...>; |
| 225 |
public: |
| 226 |
|
| 227 |
// two types for static and dynamic values |
| 228 |
using value_type = TDynamic; |
| 229 |
using static_value_type = TStatic; |
| 230 |
// tag value indicating dynamic value |
| 231 |
constexpr static static_value_type tag_value = dyn_tag; |
| 232 |
|
| 233 |
constexpr maybe_static_array() = default; |
| 234 |
|
| 235 |
// constructor for all static values |
| 236 |
// TODO: add precondition check? |
| 237 |
MDSPAN_TEMPLATE_REQUIRES(class... Vals, |
| 238 |
/* requires */ ((m_size_dynamic == 0) && |
| 239 |
(sizeof...(Vals) > 0))) |
| 240 |
MDSPAN_INLINE_FUNCTION |
| 241 |
constexpr maybe_static_array(Vals...) : m_dyn_vals{} {} |
| 242 |
|
| 243 |
// constructors from dynamic values only |
| 244 |
MDSPAN_TEMPLATE_REQUIRES(class... DynVals, |
| 245 |
/* requires */ (sizeof...(DynVals) == |
| 246 |
m_size_dynamic && |
| 247 |
m_size_dynamic > 0)) |
| 248 |
MDSPAN_INLINE_FUNCTION |
| 249 |
constexpr maybe_static_array(DynVals... vals) |
| 250 |
: m_dyn_vals{static_cast<TDynamic>(vals)...} {} |
| 251 |
|
| 252 |
|
| 253 |
MDSPAN_TEMPLATE_REQUIRES(class T, size_t N, |
| 254 |
/* requires */ (N == m_size_dynamic && N > 0)) |
| 255 |
MDSPAN_INLINE_FUNCTION |
| 256 |
constexpr maybe_static_array(const std::array<T, N> &vals) { |
| 257 |
for (size_t r = 0; r < N; r++) |
| 258 |
m_dyn_vals[r] = static_cast<TDynamic>(vals[r]); |
| 259 |
} |
| 260 |
|
| 261 |
MDSPAN_TEMPLATE_REQUIRES(class T, size_t N, |
| 262 |
/* requires */ (N == m_size_dynamic && N == 0)) |
| 263 |
MDSPAN_INLINE_FUNCTION |
| 264 |
constexpr maybe_static_array(const std::array<T, N> &) : m_dyn_vals{} {} |
| 265 |
|
| 266 |
#ifdef __cpp_lib_span |
| 267 |
MDSPAN_TEMPLATE_REQUIRES(class T, size_t N, |
| 268 |
/* requires */ (N == m_size_dynamic && N > 0)) |
| 269 |
MDSPAN_INLINE_FUNCTION |
| 270 |
constexpr maybe_static_array(const std::span<T, N> &vals) { |
| 271 |
for (size_t r = 0; r < N; r++) |
| 272 |
m_dyn_vals[r] = static_cast<TDynamic>(vals[r]); |
| 273 |
} |
| 274 |
|
| 275 |
MDSPAN_TEMPLATE_REQUIRES(class T, size_t N, |
| 276 |
/* requires */ (N == m_size_dynamic && N == 0)) |
| 277 |
MDSPAN_INLINE_FUNCTION |
| 278 |
constexpr maybe_static_array(const std::span<T, N> &) : m_dyn_vals{} {} |
| 279 |
#endif |
| 280 |
|
| 281 |
// constructors from all values |
| 282 |
MDSPAN_TEMPLATE_REQUIRES(class... DynVals, |
| 283 |
/* requires */ (sizeof...(DynVals) != |
| 284 |
m_size_dynamic && |
| 285 |
m_size_dynamic > 0)) |
| 286 |
MDSPAN_INLINE_FUNCTION |
| 287 |
constexpr maybe_static_array(DynVals... vals) |
| 288 |
: m_dyn_vals{} { |
| 289 |
static_assert((sizeof...(DynVals) == m_size), "Invalid number of values."); |
| 290 |
TDynamic values[m_size]{static_cast<TDynamic>(vals)...}; |
| 291 |
for (size_t r = 0; r < m_size; r++) { |
| 292 |
TStatic static_val = static_vals_t::get(r); |
| 293 |
if (static_val == dyn_tag) { |
| 294 |
m_dyn_vals[dyn_map_t::get(r)] = values[r]; |
| 295 |
} |
| 296 |
// Precondition check |
| 297 |
#ifdef _MDSPAN_DEBUG |
| 298 |
else { |
| 299 |
assert(values[r] == static_cast<TDynamic>(static_val)); |
| 300 |
} |
| 301 |
#endif |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
MDSPAN_TEMPLATE_REQUIRES( |
| 306 |
class T, size_t N, |
| 307 |
/* requires */ (N != m_size_dynamic && m_size_dynamic > 0)) |
| 308 |
MDSPAN_INLINE_FUNCTION |
| 309 |
constexpr maybe_static_array(const std::array<T, N> &vals) { |
| 310 |
static_assert((N == m_size), "Invalid number of values."); |
| 311 |
// Precondition check |
| 312 |
#ifdef _MDSPAN_DEBUG |
| 313 |
assert(N == m_size); |
| 314 |
#endif |
| 315 |
for (size_t r = 0; r < m_size; r++) { |
| 316 |
TStatic static_val = static_vals_t::get(r); |
| 317 |
if (static_val == dyn_tag) { |
| 318 |
m_dyn_vals[dyn_map_t::get(r)] = static_cast<TDynamic>(vals[r]); |
| 319 |
} |
| 320 |
// Precondition check |
| 321 |
#ifdef _MDSPAN_DEBUG |
| 322 |
else { |
| 323 |
assert(static_cast<TDynamic>(vals[r]) == |
| 324 |
static_cast<TDynamic>(static_val)); |
| 325 |
} |
| 326 |
#endif |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
#ifdef __cpp_lib_span |
| 331 |
MDSPAN_TEMPLATE_REQUIRES( |
| 332 |
class T, size_t N, |
| 333 |
/* requires */ (N != m_size_dynamic && m_size_dynamic > 0)) |
| 334 |
MDSPAN_INLINE_FUNCTION |
| 335 |
constexpr maybe_static_array(const std::span<T, N> &vals) { |
| 336 |
static_assert((N == m_size) || (m_size == dynamic_extent)); |
| 337 |
#ifdef _MDSPAN_DEBUG |
| 338 |
assert(N == m_size); |
| 339 |
#endif |
| 340 |
for (size_t r = 0; r < m_size; r++) { |
| 341 |
TStatic static_val = static_vals_t::get(r); |
| 342 |
if (static_val == dyn_tag) { |
| 343 |
m_dyn_vals[dyn_map_t::get(r)] = static_cast<TDynamic>(vals[r]); |
| 344 |
} |
| 345 |
#ifdef _MDSPAN_DEBUG |
| 346 |
else { |
| 347 |
assert(static_cast<TDynamic>(vals[r]) == |
| 348 |
static_cast<TDynamic>(static_val)); |
| 349 |
} |
| 350 |
#endif |
| 351 |
} |
| 352 |
} |
| 353 |
#endif |
| 354 |
|
| 355 |
// access functions |
| 356 |
MDSPAN_INLINE_FUNCTION |
| 357 |
constexpr static TStatic static_value(size_t r) { return static_vals_t::get(r); } |
| 358 |
|
| 359 |
MDSPAN_INLINE_FUNCTION |
| 360 |
constexpr TDynamic value(size_t r) const { |
| 361 |
TStatic static_val = static_vals_t::get(r); |
| 362 |
return static_val == dyn_tag ? m_dyn_vals[dyn_map_t::get(r)] |
| 363 |
: static_cast<TDynamic>(static_val); |
| 364 |
} |
| 365 |
MDSPAN_INLINE_FUNCTION |
| 366 |
constexpr TDynamic operator[](size_t r) const { return value(r); } |
| 367 |
|
| 368 |
|
| 369 |
// observers |
| 370 |
MDSPAN_INLINE_FUNCTION |
| 371 |
constexpr static size_t size() { return m_size; } |
| 372 |
MDSPAN_INLINE_FUNCTION |
| 373 |
constexpr static size_t size_dynamic() { return m_size_dynamic; } |
| 374 |
}; |
| 375 |
|
| 376 |
} // namespace detail |
| 377 |
} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE |
| 378 |
|
| 379 |
namespace MDSPAN_IMPL_STANDARD_NAMESPACE { |
| 380 |
|
| 381 |
// ------------------------------------------------------------------ |
| 382 |
// ------------ extents --------------------------------------------- |
| 383 |
// ------------------------------------------------------------------ |
| 384 |
|
| 385 |
// Class to describe the extents of a multi dimensional array. |
| 386 |
// Used by mdspan, mdarray and layout mappings. |
| 387 |
// See ISO C++ standard [mdspan.extents] |
| 388 |
|
| 389 |
template <class IndexType, size_t... Extents> class extents { |
| 390 |
public: |
| 391 |
// typedefs for integral types used |
| 392 |
using index_type = IndexType; |
| 393 |
using size_type = std::make_unsigned_t<index_type>; |
| 394 |
using rank_type = size_t; |
| 395 |
|
| 396 |
static_assert(std::is_integral<index_type>::value && !std::is_same<index_type, bool>::value, |
| 397 |
MDSPAN_IMPL_STANDARD_NAMESPACE_STRING "::extents::index_type must be a signed or unsigned integer type"); |
| 398 |
private: |
| 399 |
constexpr static rank_type m_rank = sizeof...(Extents); |
| 400 |
constexpr static rank_type m_rank_dynamic = |
| 401 |
_MDSPAN_FOLD_PLUS_RIGHT((Extents == dynamic_extent), /* + ... + */ 0); |
| 402 |
|
| 403 |
// internal storage type using maybe_static_array |
| 404 |
using vals_t = |
| 405 |
detail::maybe_static_array<IndexType, size_t, dynamic_extent, Extents...>; |
| 406 |
_MDSPAN_NO_UNIQUE_ADDRESS vals_t m_vals; |
| 407 |
|
| 408 |
public: |
| 409 |
// [mdspan.extents.obs], observers of multidimensional index space |
| 410 |
MDSPAN_INLINE_FUNCTION |
| 411 |
constexpr static rank_type rank() noexcept { return m_rank; } |
| 412 |
MDSPAN_INLINE_FUNCTION |
| 413 |
constexpr static rank_type rank_dynamic() noexcept { return m_rank_dynamic; } |
| 414 |
|
| 415 |
MDSPAN_INLINE_FUNCTION |
| 416 |
constexpr index_type extent(rank_type r) const noexcept { return m_vals.value(r); } |
| 417 |
MDSPAN_INLINE_FUNCTION |
| 418 |
constexpr static size_t static_extent(rank_type r) noexcept { |
| 419 |
return vals_t::static_value(r); |
| 420 |
} |
| 421 |
|
| 422 |
// [mdspan.extents.cons], constructors |
| 423 |
MDSPAN_INLINE_FUNCTION_DEFAULTED |
| 424 |
constexpr extents() noexcept = default; |
| 425 |
|
| 426 |
// Construction from just dynamic or all values. |
| 427 |
// Precondition check is deferred to maybe_static_array constructor |
| 428 |
MDSPAN_TEMPLATE_REQUIRES( |
| 429 |
class... OtherIndexTypes, |
| 430 |
/* requires */ ( |
| 431 |
_MDSPAN_FOLD_AND(_MDSPAN_TRAIT(std::is_convertible, OtherIndexTypes, |
| 432 |
index_type) /* && ... */) && |
| 433 |
_MDSPAN_FOLD_AND(_MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, |
| 434 |
OtherIndexTypes) /* && ... */) && |
| 435 |
(sizeof...(OtherIndexTypes) == m_rank || |
| 436 |
sizeof...(OtherIndexTypes) == m_rank_dynamic))) |
| 437 |
MDSPAN_INLINE_FUNCTION |
| 438 |
constexpr explicit extents(OtherIndexTypes... dynvals) noexcept |
| 439 |
: m_vals(static_cast<index_type>(dynvals)...) {} |
| 440 |
|
| 441 |
MDSPAN_TEMPLATE_REQUIRES( |
| 442 |
class OtherIndexType, size_t N, |
| 443 |
/* requires */ |
| 444 |
( |
| 445 |
_MDSPAN_TRAIT(std::is_convertible, const OtherIndexType&, index_type) && |
| 446 |
_MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, |
| 447 |
const OtherIndexType&) && |
| 448 |
(N == m_rank || N == m_rank_dynamic))) |
| 449 |
MDSPAN_INLINE_FUNCTION |
| 450 |
MDSPAN_CONDITIONAL_EXPLICIT(N != m_rank_dynamic) |
| 451 |
constexpr extents(const std::array<OtherIndexType, N> &exts) noexcept |
| 452 |
: m_vals(std::move(exts)) {} |
| 453 |
|
| 454 |
#ifdef __cpp_lib_span |
| 455 |
MDSPAN_TEMPLATE_REQUIRES( |
| 456 |
class OtherIndexType, size_t N, |
| 457 |
/* requires */ |
| 458 |
(_MDSPAN_TRAIT(std::is_convertible, const OtherIndexType&, index_type) && |
| 459 |
_MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, const OtherIndexType&) && |
| 460 |
(N == m_rank || N == m_rank_dynamic))) |
| 461 |
MDSPAN_INLINE_FUNCTION |
| 462 |
MDSPAN_CONDITIONAL_EXPLICIT(N != m_rank_dynamic) |
| 463 |
constexpr extents(const std::span<OtherIndexType, N> &exts) noexcept |
| 464 |
: m_vals(std::move(exts)) {} |
| 465 |
#endif |
| 466 |
|
| 467 |
private: |
| 468 |
// Function to construct extents storage from other extents. |
| 469 |
// With C++ 17 the first two variants could be collapsed using if constexpr |
| 470 |
// in which case you don't need all the requires clauses. |
| 471 |
// in C++ 14 mode that doesn't work due to infinite recursion |
| 472 |
MDSPAN_TEMPLATE_REQUIRES( |
| 473 |
size_t DynCount, size_t R, class OtherExtents, class... DynamicValues, |
| 474 |
/* requires */ ((R < m_rank) && (static_extent(R) == dynamic_extent))) |
| 475 |
MDSPAN_INLINE_FUNCTION |
| 476 |
constexpr |
| 477 |
vals_t __construct_vals_from_extents(std::integral_constant<size_t, DynCount>, |
| 478 |
std::integral_constant<size_t, R>, |
| 479 |
const OtherExtents &exts, |
| 480 |
DynamicValues... dynamic_values) noexcept { |
| 481 |
return __construct_vals_from_extents( |
| 482 |
std::integral_constant<size_t, DynCount + 1>(), |
| 483 |
std::integral_constant<size_t, R + 1>(), exts, dynamic_values..., |
| 484 |
exts.extent(R)); |
| 485 |
} |
| 486 |
|
| 487 |
MDSPAN_TEMPLATE_REQUIRES( |
| 488 |
size_t DynCount, size_t R, class OtherExtents, class... DynamicValues, |
| 489 |
/* requires */ ((R < m_rank) && (static_extent(R) != dynamic_extent))) |
| 490 |
MDSPAN_INLINE_FUNCTION |
| 491 |
constexpr |
| 492 |
vals_t __construct_vals_from_extents(std::integral_constant<size_t, DynCount>, |
| 493 |
std::integral_constant<size_t, R>, |
| 494 |
const OtherExtents &exts, |
| 495 |
DynamicValues... dynamic_values) noexcept { |
| 496 |
return __construct_vals_from_extents( |
| 497 |
std::integral_constant<size_t, DynCount>(), |
| 498 |
std::integral_constant<size_t, R + 1>(), exts, dynamic_values...); |
| 499 |
} |
| 500 |
|
| 501 |
MDSPAN_TEMPLATE_REQUIRES( |
| 502 |
size_t DynCount, size_t R, class OtherExtents, class... DynamicValues, |
| 503 |
/* requires */ ((R == m_rank) && (DynCount == m_rank_dynamic))) |
| 504 |
MDSPAN_INLINE_FUNCTION |
| 505 |
constexpr |
| 506 |
vals_t __construct_vals_from_extents(std::integral_constant<size_t, DynCount>, |
| 507 |
std::integral_constant<size_t, R>, |
| 508 |
const OtherExtents &, |
| 509 |
DynamicValues... dynamic_values) noexcept { |
| 510 |
return vals_t{static_cast<index_type>(dynamic_values)...}; |
| 511 |
} |
| 512 |
|
| 513 |
public: |
| 514 |
|
| 515 |
// Converting constructor from other extents specializations |
| 516 |
MDSPAN_TEMPLATE_REQUIRES( |
| 517 |
class OtherIndexType, size_t... OtherExtents, |
| 518 |
/* requires */ |
| 519 |
( |
| 520 |
/* multi-stage check to protect from invalid pack expansion when sizes |
| 521 |
don't match? */ |
| 522 |
decltype(detail::__check_compatible_extents( |
| 523 |
// using: sizeof...(Extents) == sizeof...(OtherExtents) as the second argument fails with MSVC+NVCC with some obscure expansion error |
| 524 |
// MSVC: 19.38.33133 NVCC: 12.0 |
| 525 |
std::integral_constant<bool, extents<int, Extents...>::rank() == extents<int, OtherExtents...>::rank()>{}, |
| 526 |
std::integer_sequence<size_t, Extents...>{}, |
| 527 |
std::integer_sequence<size_t, OtherExtents...>{}))::value |
| 528 |
) |
| 529 |
) |
| 530 |
MDSPAN_INLINE_FUNCTION |
| 531 |
MDSPAN_CONDITIONAL_EXPLICIT((((Extents != dynamic_extent) && |
| 532 |
(OtherExtents == dynamic_extent)) || |
| 533 |
...) || |
| 534 |
(std::numeric_limits<index_type>::max() < |
| 535 |
std::numeric_limits<OtherIndexType>::max())) |
| 536 |
constexpr extents(const extents<OtherIndexType, OtherExtents...> &other) noexcept |
| 537 |
: m_vals(__construct_vals_from_extents( |
| 538 |
std::integral_constant<size_t, 0>(), |
| 539 |
std::integral_constant<size_t, 0>(), other)) {} |
| 540 |
|
| 541 |
// Comparison operator |
| 542 |
template <class OtherIndexType, size_t... OtherExtents> |
| 543 |
MDSPAN_INLINE_FUNCTION friend constexpr bool |
| 544 |
operator==(const extents &lhs, |
| 545 |
const extents<OtherIndexType, OtherExtents...> &rhs) noexcept { |
| 546 |
return |
| 547 |
rank() == extents<OtherIndexType, OtherExtents...>::rank() && |
| 548 |
detail::rankwise_equal(detail::with_rank<rank()>{}, rhs, lhs, detail::extent); |
| 549 |
} |
| 550 |
|
| 551 |
#if !(MDSPAN_HAS_CXX_20) |
| 552 |
template <class OtherIndexType, size_t... OtherExtents> |
| 553 |
MDSPAN_INLINE_FUNCTION friend constexpr bool |
| 554 |
operator!=(extents const &lhs, |
| 555 |
extents<OtherIndexType, OtherExtents...> const &rhs) noexcept { |
| 556 |
return !(lhs == rhs); |
| 557 |
} |
| 558 |
#endif |
| 559 |
}; |
| 560 |
|
| 561 |
// Recursive helper classes to implement dextents alias for extents |
| 562 |
namespace detail { |
| 563 |
|
| 564 |
template <class IndexType, size_t Rank, |
| 565 |
class Extents = ::MDSPAN_IMPL_STANDARD_NAMESPACE::extents<IndexType>> |
| 566 |
struct __make_dextents; |
| 567 |
|
| 568 |
template <class IndexType, size_t Rank, size_t... ExtentsPack> |
| 569 |
struct __make_dextents< |
| 570 |
IndexType, Rank, ::MDSPAN_IMPL_STANDARD_NAMESPACE::extents<IndexType, ExtentsPack...>> |
| 571 |
{ |
| 572 |
using type = typename __make_dextents< |
| 573 |
IndexType, Rank - 1, |
| 574 |
::MDSPAN_IMPL_STANDARD_NAMESPACE::extents<IndexType, |
| 575 |
::MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent, |
| 576 |
ExtentsPack...>>::type; |
| 577 |
}; |
| 578 |
|
| 579 |
template <class IndexType, size_t... ExtentsPack> |
| 580 |
struct __make_dextents< |
| 581 |
IndexType, 0, ::MDSPAN_IMPL_STANDARD_NAMESPACE::extents<IndexType, ExtentsPack...>> |
| 582 |
{ |
| 583 |
using type = ::MDSPAN_IMPL_STANDARD_NAMESPACE::extents<IndexType, ExtentsPack...>; |
| 584 |
}; |
| 585 |
|
| 586 |
} // end namespace detail |
| 587 |
|
| 588 |
// [mdspan.extents.dextents], alias template |
| 589 |
template <class IndexType, size_t Rank> |
| 590 |
using dextents = typename detail::__make_dextents<IndexType, Rank>::type; |
| 591 |
|
| 592 |
// Deduction guide for extents |
| 593 |
#if defined(_MDSPAN_USE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION) |
| 594 |
template <class... IndexTypes> |
| 595 |
extents(IndexTypes...) |
| 596 |
-> extents<size_t, |
| 597 |
((void) sizeof(IndexTypes), ::MDSPAN_IMPL_STANDARD_NAMESPACE::dynamic_extent)...>; |
| 598 |
#endif |
| 599 |
|
| 600 |
// Helper type traits for identifying a class as extents. |
| 601 |
namespace detail { |
| 602 |
|
| 603 |
template <class T> struct __is_extents : ::std::false_type {}; |
| 604 |
|
| 605 |
template <class IndexType, size_t... ExtentsPack> |
| 606 |
struct __is_extents<::MDSPAN_IMPL_STANDARD_NAMESPACE::extents<IndexType, ExtentsPack...>> |
| 607 |
: ::std::true_type {}; |
| 608 |
|
| 609 |
template <class T> |
| 610 |
#if MDSPAN_HAS_CXX_17 |
| 611 |
inline |
| 612 |
#else |
| 613 |
static |
| 614 |
#endif |
| 615 |
constexpr bool __is_extents_v = __is_extents<T>::value; |
| 616 |
|
| 617 |
template<class InputIndexType, class ExtentsIndexType> |
| 618 |
MDSPAN_INLINE_FUNCTION |
| 619 |
constexpr void |
| 620 |
check_lower_bound(InputIndexType user_index, |
| 621 |
ExtentsIndexType /* current_extent */, |
| 622 |
std::true_type /* is_signed */) |
| 623 |
{ |
| 624 |
(void) user_index; // prevent unused variable warning |
| 625 |
#ifdef _MDSPAN_DEBUG |
| 626 |
assert(static_cast<ExtentsIndexType>(user_index) >= 0); |
| 627 |
#endif |
| 628 |
} |
| 629 |
|
| 630 |
template<class InputIndexType, class ExtentsIndexType> |
| 631 |
MDSPAN_INLINE_FUNCTION |
| 632 |
constexpr void |
| 633 |
check_lower_bound(InputIndexType /* user_index */, |
| 634 |
ExtentsIndexType /* current_extent */, |
| 635 |
std::false_type /* is_signed */) |
| 636 |
{} |
| 637 |
|
| 638 |
template<class InputIndexType, class ExtentsIndexType> |
| 639 |
MDSPAN_INLINE_FUNCTION |
| 640 |
constexpr void |
| 641 |
check_upper_bound(InputIndexType user_index, |
| 642 |
ExtentsIndexType current_extent) |
| 643 |
{ |
| 644 |
(void) user_index; // prevent unused variable warnings |
| 645 |
(void) current_extent; |
| 646 |
#ifdef _MDSPAN_DEBUG |
| 647 |
assert(static_cast<ExtentsIndexType>(user_index) < current_extent); |
| 648 |
#endif |
| 649 |
} |
| 650 |
|
| 651 |
// Returning true to use AND fold instead of comma |
| 652 |
// CPP14 mode doesn't like the use of void expressions |
| 653 |
// with the way the _MDSPAN_FOLD_AND is set up |
| 654 |
template<class InputIndex, class ExtentsIndexType> |
| 655 |
MDSPAN_INLINE_FUNCTION |
| 656 |
constexpr bool |
| 657 |
check_one_index(InputIndex user_index, |
| 658 |
ExtentsIndexType current_extent) |
| 659 |
{ |
| 660 |
check_lower_bound(user_index, current_extent, |
| 661 |
std::integral_constant<bool, std::is_signed<ExtentsIndexType>::value>{}); |
| 662 |
check_upper_bound(user_index, current_extent); |
| 663 |
return true; |
| 664 |
} |
| 665 |
|
| 666 |
template<size_t ... RankIndices, |
| 667 |
class ExtentsIndexType, size_t ... Exts, |
| 668 |
class ... Indices> |
| 669 |
MDSPAN_INLINE_FUNCTION |
| 670 |
constexpr void |
| 671 |
check_all_indices_helper(std::index_sequence<RankIndices...>, |
| 672 |
const extents<ExtentsIndexType, Exts...>& exts, |
| 673 |
Indices... indices) |
| 674 |
{ |
| 675 |
// Suppress warning about statement has no effect |
| 676 |
(void) _MDSPAN_FOLD_AND( |
| 677 |
(check_one_index(indices, exts.extent(RankIndices))) |
| 678 |
); |
| 679 |
} |
| 680 |
|
| 681 |
template<class ExtentsIndexType, size_t ... Exts, |
| 682 |
class ... Indices> |
| 683 |
MDSPAN_INLINE_FUNCTION |
| 684 |
constexpr void |
| 685 |
check_all_indices(const extents<ExtentsIndexType, Exts...>& exts, |
| 686 |
Indices... indices) |
| 687 |
{ |
| 688 |
check_all_indices_helper(std::make_index_sequence<sizeof...(Indices)>(), |
| 689 |
exts, indices...); |
| 690 |
} |
| 691 |
|
| 692 |
} // namespace detail |
| 693 |
} // namespace MDSPAN_IMPL_STANDARD_NAMESPACE |