All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TfSpan< T > Class Template Reference

Represents a range of contiguous elements. More...

#include <span.h>

Public Types

using element_type = T
 
using value_type = typename std::remove_cv< T >::type
 
using pointer = T *
 
using reference = T &
 
using index_type = std::size_t
 
using difference_type = std::ptrdiff_t
 
using iterator = T *
 
using const_iterator = const T *
 
using reverse_iterator = std::reverse_iterator< iterator >
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 

Public Member Functions

 TfSpan (pointer ptr, index_type count)
 Construct a span over the range of [ptr, ptr+count).
 
 TfSpan (pointer first, pointer last)
 Construct a span over the range [first, last).
 
template<class Container >
 TfSpan (Container &cont, typename std::enable_if< !std::is_const< element_type >::value &&std::is_same< typename Container::value_type, value_type >::value, Container >::type *=0)
 Construct a span from a container.
 
template<class Container >
 TfSpan (const Container &cont, typename std::enable_if< std::is_same< typename Container::value_type, value_type >::value, Container >::type *=0)
 Construct a span from a container.
 
pointer data () const noexcept
 Return a pointer to the first element of the span.

 
index_type size () const noexcept
 Return the total number of elements in the span.
 
bool empty () const noexcept
 Returns true if this span contains no elements, false otherwise.
 
reference operator[] (index_type idx) const
 Returns a reference to the idx'th element of the span.
 
reference front () const
 Return a reference to the first element in the span.
 
reference back () const
 Return a reference to the last element in the span.
 
iterator begin () const noexcept
 Returns a non-const iterator the start of the span.
 
const_iterator cbegin () const noexcept
 Returns a cons iterator to the start of the span.
 
iterator end () const noexcept
 Returns a non-const iterator to the end of the span.
 
const_iterator cend () const noexcept
 Returns a const iterator to the end of the span.
 
reverse_iterator rbegin () const noexcept
 Returns a non-const reverse iterator the start of the span.
 
const_reverse_iterator crbegin () const noexcept
 Returns a cons reverse iterator to the start of the span.
 
reverse_iterator rend () const noexcept
 Returns a non-const reverse iterator to the end of the span.
 
const_reverse_iterator crend () const noexcept
 Returns a const reverse iterator to the end of the span.
 
TfSpan< T > subspan (difference_type offset, difference_type count=-1) const
 Returns a new span referencing a sub-range of this span.
 
TfSpan< T > first (size_t count) const
 Return a subspan consisting of the first count elements of this span.
 
TfSpan< T > last (size_t count) const
 Return a subspan consisting of the last count elements of this span.
 

Detailed Description

template<typename T>
class TfSpan< T >

Represents a range of contiguous elements.

This simply pairs a pointer with a size, while adding a common array interface.

A span allows ranges of elements to be referenced in a container-neutral manner. While it is possible to achieve that effect by simply passing around raw pointers, a span has the advantage of carrying around additional size information, both enabling use of common array patterns, as well as providing sufficient information to perform boundary tests.

A TfSpan is implicitly convertible from common array types, as well as from other spans, but preserves const-ness:

std::vector<int> data;
TfSpan<int> span(data); // Okay
VtIntArray data;
TfSpan<int> span = data; // Okay
TfSpan<const int> cspan = span; // Okay
const std::vector<int> data;
TfSpan<const int> span = data; // Okay
const std::vector<int> data;
TfSpan<int> span = data; // Error! Discards cv-qualifier.
Represents a range of contiguous elements.
Definition: span.h:71
pointer data() const noexcept
Return a pointer to the first element of the span.
Definition: span.h:133

Helper methods TfMakeSpan and TfMakeConstSpan are also provided to enable auto-typing when constructing spans:

VtIntArray data;
auto readOnlySpan = TfMakeConstSpan(data); // TfSpan<const int>
auto readWriteSpan = TfMakeSpan(data); // TfSpan<int>
TfSpan< const typename Container::value_type > TfMakeConstSpan(const Container &cont)
Helper for constructing a const TfSpan from a container.
Definition: span.h:233
TfSpan< typename Container::value_type > TfMakeSpan(Container &cont)
Helper for constructing a non-const TfSpan from a container.
Definition: span.h:224

Spans do not own the data they reference. It is up to the user of the span to ensure that the underlying data is not destructed while the span is in use.

This is modelled after std::span (C++20), but does not currently include any specialization for static extents.

Definition at line 70 of file span.h.

Member Typedef Documentation

◆ const_iterator

using const_iterator = const T*

Definition at line 81 of file span.h.

◆ const_reverse_iterator

using const_reverse_iterator = std::reverse_iterator<const_iterator>

Definition at line 83 of file span.h.

◆ difference_type

using difference_type = std::ptrdiff_t

Definition at line 78 of file span.h.

◆ element_type

using element_type = T

Definition at line 73 of file span.h.

◆ index_type

using index_type = std::size_t

Definition at line 77 of file span.h.

◆ iterator

using iterator = T*

Definition at line 80 of file span.h.

◆ pointer

using pointer = T*

Definition at line 75 of file span.h.

◆ reference

using reference = T&

Definition at line 76 of file span.h.

◆ reverse_iterator

using reverse_iterator = std::reverse_iterator<iterator>

Definition at line 82 of file span.h.

◆ value_type

using value_type = typename std::remove_cv<T>::type

Definition at line 74 of file span.h.

Constructor & Destructor Documentation

◆ TfSpan() [1/4]

TfSpan ( pointer  ptr,
index_type  count 
)
inline

Construct a span over the range of [ptr, ptr+count).

In debug builds, a runtime assertion will fail if count > 0 and ptr is null. The behavior is otherwise undefined for invalid ranges.

Definition at line 90 of file span.h.

◆ TfSpan() [2/4]

TfSpan ( pointer  first,
pointer  last 
)
inline

Construct a span over the range [first, last).

Definition at line 97 of file span.h.

◆ TfSpan() [3/4]

TfSpan ( Container &  cont,
typename std::enable_if< !std::is_const< element_type >::value &&std::is_same< typename Container::value_type, value_type >::value, Container >::type *  = 0 
)
inline

Construct a span from a container.

The resulting span has a range of [cont.data(), cont.data()+cont.size())

Definition at line 107 of file span.h.

◆ TfSpan() [4/4]

TfSpan ( const Container &  cont,
typename std::enable_if< std::is_same< typename Container::value_type, value_type >::value, Container >::type *  = 0 
)
inline

Construct a span from a container.

The resulting span has a range of [cont.data(), cont.data()+cont.size())

Definition at line 122 of file span.h.

Member Function Documentation

◆ back()

reference back ( ) const
inline

Return a reference to the last element in the span.

Definition at line 156 of file span.h.

◆ begin()

iterator begin ( ) const
inlinenoexcept

Returns a non-const iterator the start of the span.

Definition at line 162 of file span.h.

◆ cbegin()

const_iterator cbegin ( ) const
inlinenoexcept

Returns a cons iterator to the start of the span.

Definition at line 165 of file span.h.

◆ cend()

const_iterator cend ( ) const
inlinenoexcept

Returns a const iterator to the end of the span.

Definition at line 171 of file span.h.

◆ crbegin()

const_reverse_iterator crbegin ( ) const
inlinenoexcept

Returns a cons reverse iterator to the start of the span.

Definition at line 178 of file span.h.

◆ crend()

const_reverse_iterator crend ( ) const
inlinenoexcept

Returns a const reverse iterator to the end of the span.

Definition at line 186 of file span.h.

◆ data()

pointer data ( ) const
inlinenoexcept

Return a pointer to the first element of the span.

Definition at line 133 of file span.h.

◆ empty()

bool empty ( ) const
inlinenoexcept

Returns true if this span contains no elements, false otherwise.

Definition at line 139 of file span.h.

◆ end()

iterator end ( ) const
inlinenoexcept

Returns a non-const iterator to the end of the span.

Definition at line 168 of file span.h.

◆ first()

TfSpan< T > first ( size_t  count) const
inline

Return a subspan consisting of the first count elements of this span.

Definition at line 205 of file span.h.

◆ front()

reference front ( ) const
inline

Return a reference to the first element in the span.

Definition at line 150 of file span.h.

◆ last()

TfSpan< T > last ( size_t  count) const
inline

Return a subspan consisting of the last count elements of this span.

Definition at line 210 of file span.h.

◆ operator[]()

reference operator[] ( index_type  idx) const
inline

Returns a reference to the idx'th element of the span.

In debug builds, a runtime assertion will fail if idx is out of range. The behavior is otherwise undefined if idx is out of range.

Definition at line 144 of file span.h.

◆ rbegin()

reverse_iterator rbegin ( ) const
inlinenoexcept

Returns a non-const reverse iterator the start of the span.

Definition at line 174 of file span.h.

◆ rend()

reverse_iterator rend ( ) const
inlinenoexcept

Returns a non-const reverse iterator to the end of the span.

Definition at line 182 of file span.h.

◆ size()

index_type size ( ) const
inlinenoexcept

Return the total number of elements in the span.

Definition at line 136 of file span.h.

◆ subspan()

TfSpan< T > subspan ( difference_type  offset,
difference_type  count = -1 
) const
inline

Returns a new span referencing a sub-range of this span.

If count == -1 (or std::dynamic_extent in C++20), the new span has a range of [data()+offset, data()+size()). Otherwise, the new span has range [data()+offset, data()+offset+count).

Definition at line 193 of file span.h.


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