Celeritas 0.7+cf8d83d
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | List of all members
celeritas::Span< T, Extent > Class Template Reference

Non-owning device-compatible reference to a contiguous span of data. More...

#include <Span.hh>

Public Types

Type aliases
using element_type = typename SpanTraitsT::element_type
 
using value_type = std::remove_cv_t< element_type >
 
using size_type = std::size_t
 
using pointer = typename SpanTraitsT::pointer
 
using const_pointer = typename SpanTraitsT::const_pointer
 
using reference = typename SpanTraitsT::reference
 
using const_reference = typename SpanTraitsT::const_reference
 
using iterator = typename SpanTraitsT::iterator
 
using const_iterator = typename SpanTraitsT::const_iterator
 

Public Member Functions

constexpr Span ()=default
 Construct with default null pointer and size zero.
 
constexpr Span (pointer d, std::size_t s) noexcept(ndebug_or_dyn)
 Construct from data and size.
 
template<class Iter >
constexpr Span (Iter first, Iter last) noexcept(ndebug_or_dyn)
 Construct from two contiguous random-access iterators.
 
template<std::size_t N, std::enable_if_t< N==Extent||Extent==dynamic_extent, bool > = true>
constexpr Span (element_type(&arr)[N]) noexcept
 Construct from a C array.
 
template<class U , std::size_t N, std::enable_if_t< detail::is_array_convertible_v< U, T > &&(N==Extent||Extent==dynamic_extent), bool > = true>
constexpr Span (Array< U, N > &arr) noexcept
 Construct implicitly from a mutable Array.
 
template<class U , std::size_t N, std::enable_if_t< detail::is_array_convertible_v< U const, T > &&(N==Extent||Extent==dynamic_extent), bool > = true>
constexpr Span (Array< U, N > const &arr) noexcept
 Construct implicitly from a const Array.
 
template<class U , std::size_t E2, std::enable_if_t< detail::is_array_convertible_v< U, T > &&(E2==Extent||Extent==dynamic_extent), bool > = true>
constexpr Span (Span< U, E2 > const &other) noexcept(ndebug_or_dyn)
 Construct implicitly from convertible span and extent.
 
template<class U , std::size_t E2, std::enable_if_t< detail::is_array_convertible_v< U, T > &&Extent !=dynamic_extent &&E2==dynamic_extent, bool > = true>
constexpr Span (Span< U, E2 > const &other) noexcept(ndebug_or_dyn)
 Require explicit conversion from dynamic to fixed extent.
 
 Span (Span const &)=default
 
Spanoperator= (Span const &)=default
 
 Span (Span &&)=default
 
Spanoperator= (Span &&)=default
 
Iterators
constexpr iterator begin () const
 
constexpr iterator end () const
 
Element access
constexpr reference operator[] (std::size_t i) const
 
constexpr reference front () const
 
constexpr reference back () const
 
constexpr pointer data () const
 
Observers
constexpr bool empty () const
 
constexpr std::size_t size () const
 
constexpr std::size_t size_bytes () const
 
Subviews
template<std::size_t Count>
constexpr Span< T, Countfirst () const noexcept(ndebug)
 
constexpr Span< T, dynamic_extentfirst (std::size_t count) const noexcept(ndebug)
 
template<std::size_t Offset, std::size_t Count = dynamic_extent>
constexpr Span< T, detail::subspan_extent(Extent, Offset, Count)> subspan () const noexcept(ndebug)
 
constexpr Span< T, dynamic_extentsubspan (std::size_t offset, std::size_t count=dynamic_extent) const noexcept(ndebug)
 
template<std::size_t Count>
constexpr Span< T, Countlast () const noexcept(ndebug)
 
constexpr Span< T, dynamic_extentlast (std::size_t count) const noexcept(ndebug)
 

Static Public Attributes

static constexpr std::size_t extent = Extent
 Size (may be dynamic)
 

Detailed Description

template<class T, std::size_t Extent = dynamic_extent>
class celeritas::Span< T, Extent >

Non-owning device-compatible reference to a contiguous span of data.

Template Parameters
Tvalue type
Extentfixed size; defaults to dynamic.

A Span, like std::string_view, provides access to externally managed data. In Celeritas, this class is typically used as a return result from accessing a range of elements in a Collection.

This implementation is a nonconforming backport of the C++20 std::span. Improvements for standards compatibility are welcome as long as they retain the same behavior in device code. Important differences from the standard std::span include:

Construction
Data access
Subviews and utilities

Constructor & Destructor Documentation

◆ Span() [1/5]

template<class T , std::size_t Extent = dynamic_extent>
template<class Iter >
constexpr celeritas::Span< T, Extent >::Span ( Iter  first,
Iter  last 
)
inlineconstexprnoexcept

Construct from two contiguous random-access iterators.

Note
If Extent is fixed-size, the SpanImpl will fire an assertion in debug mode if the size does not match.
Todo:
should be explicit unless dynamic size (requires C++20)

◆ Span() [2/5]

template<class T , std::size_t Extent = dynamic_extent>
template<class U , std::size_t N, std::enable_if_t< detail::is_array_convertible_v< U, T > &&(N==Extent||Extent==dynamic_extent), bool > = true>
constexpr celeritas::Span< T, Extent >::Span ( Array< U, N > &  arr)
inlineconstexprnoexcept

Construct implicitly from a mutable Array.

Enabled only when N matches Extent exactly or Extent is dynamic_extent, so the conversion is always statically safe and can be implicit unconditionally.

◆ Span() [3/5]

template<class T , std::size_t Extent = dynamic_extent>
template<class U , std::size_t N, std::enable_if_t< detail::is_array_convertible_v< U const, T > &&(N==Extent||Extent==dynamic_extent), bool > = true>
constexpr celeritas::Span< T, Extent >::Span ( Array< U, N > const arr)
inlineconstexprnoexcept

Construct implicitly from a const Array.

Enabled only when N matches Extent exactly or Extent is dynamic_extent, so the conversion is always statically safe and can be implicit unconditionally.

◆ Span() [4/5]

template<class T , std::size_t Extent = dynamic_extent>
template<class U , std::size_t E2, std::enable_if_t< detail::is_array_convertible_v< U, T > &&(E2==Extent||Extent==dynamic_extent), bool > = true>
constexpr celeritas::Span< T, Extent >::Span ( Span< U, E2 > const other)
inlineconstexprnoexcept

Construct implicitly from convertible span and extent.

Note that the enable-if prevents LdgSpan->Span conversion. Conversions that may require a runtime size check (dynamic-extent source to fixed-extent destination) are made explicit to avoid accidental UB, matching std::span's behavior.

Implicit conversion for cases that are statically safe (e.g., fixed->dynamic, fixed->same-fixed, or just element-type qualification changes).

◆ Span() [5/5]

template<class T , std::size_t Extent = dynamic_extent>
template<class U , std::size_t E2, std::enable_if_t< detail::is_array_convertible_v< U, T > &&Extent !=dynamic_extent &&E2==dynamic_extent, bool > = true>
constexpr celeritas::Span< T, Extent >::Span ( Span< U, E2 > const other)
inlineexplicitconstexprnoexcept

Require explicit conversion from dynamic to fixed extent.

Runtime size compatibility is checked in detail::SpanImpl.


The documentation for this class was generated from the following files: