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

Represents a range of contiguous elements. More...

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

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.
*

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>
*

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 87 of file span.h.

Constructor & Destructor Documentation

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 107 of file span.h.

TfSpan ( pointer  first,
pointer  last 
)
inline

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

Definition at line 114 of file span.h.

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 124 of file span.h.

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 139 of file span.h.

Member Function Documentation

reference back ( ) const
inline

Return a reference to the last element in the span.

Definition at line 173 of file span.h.

iterator begin ( ) const
inlinenoexcept

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

Definition at line 179 of file span.h.

const_iterator cbegin ( ) const
inlinenoexcept

Returns a cons iterator to the start of the span.

Definition at line 182 of file span.h.

const_iterator cend ( ) const
inlinenoexcept

Returns a const iterator to the end of the span.

Definition at line 188 of file span.h.

const_reverse_iterator crbegin ( ) const
inlinenoexcept

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

Definition at line 195 of file span.h.

const_reverse_iterator crend ( ) const
inlinenoexcept

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

Definition at line 203 of file span.h.

pointer data ( ) const
inlinenoexcept

Return a pointer to the first element of the span.

Definition at line 150 of file span.h.

bool empty ( ) const
inlinenoexcept

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

Definition at line 156 of file span.h.

iterator end ( ) const
inlinenoexcept

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

Definition at line 185 of file span.h.

TfSpan<T> first ( size_t  count) const
inline

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

Definition at line 222 of file span.h.

reference front ( ) const
inline

Return a reference to the first element in the span.

Definition at line 167 of file span.h.

TfSpan<T> last ( size_t  count) const
inline

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

Definition at line 227 of file span.h.

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 161 of file span.h.

reverse_iterator rbegin ( ) const
inlinenoexcept

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

Definition at line 191 of file span.h.

reverse_iterator rend ( ) const
inlinenoexcept

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

Definition at line 199 of file span.h.

index_type size ( ) const
inlinenoexcept

Return the total number of elements in the span.

Definition at line 153 of file span.h.

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 210 of file span.h.


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