QSpan Class
template <typename T, std::size_t E = q20::dynamic_extent> class QSpan
The QSpan class provides a non-owning view of a contiguous sequence of elements. More…
| Header | #include <QSpan> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Types
| typedef | element_type |
| typedef | value_type |
| typedef | size_type |
| typedef | difference_type |
| typedef | pointer |
| typedef | const_pointer |
| typedef | reference |
| typedef | const_reference |
| typedef | iterator |
| typedef | const_iterator |
| typedef | reverse_iterator |
| typedef | const_reverse_iterator |
Public Functions
| QSpan() | |
| QSpan(T *first, qsizetype count) | |
| QSpan(T *first, End last) | |
| QSpan(T (&array)[N]) | |
| QSpan(std::array<U, N> &array) | |
| QSpan(const std::array<U, N> &array) | |
| QSpan(C &&c) | |
| QSpan(QSpan<U, F> other) | |
| qsizetype | size() const |
| qsizetype | count() const |
| qsizetype | length() const |
| qsizetype | size_bytes() const |
| bool | isEmpty() const |
| bool | empty() const |
| T * | data() const |
| T & | operator[](qsizetype i) const |
| T & | at(qsizetype i) const |
| T & | front() const |
| T & | back() const |
| iterator | begin() const |
| iterator | end() const |
| const_iterator | cbegin() const |
| const_iterator | cend() const |
| reverse_iterator | rbegin() const |
| reverse_iterator | rend() const |
| const_reverse_iterator | crbegin() const |
| const_reverse_iterator | crend() const |
| QSpan<T> | first(qsizetype c) const |
| QSpan<T> | last(qsizetype c) const |
| QSpan<T> | subspan(qsizetype pos) const |
| QSpan<T> | subspan(qsizetype pos, qsizetype c) const |
| QSpan<T> | sliced(qsizetype pos) const |
| QSpan<T> | sliced(qsizetype pos, qsizetype c) const |
| QSpan<T> | chopped(qsizetype c) const |
| void | chop(qsizetype c) |
| void | slice(qsizetype pos) |
| void | slice(qsizetype pos, qsizetype c) |
Static Public Members
| std::size_t | extent |
Detailed Description
A QSpan holds a pointer and an element count. It does not own or copy the elements, so the sequence it refers to must outlive the span. Spans are cheap to copy and are normally passed by value. A QSpan<const T> gives read-only access, and a QSpan<T> lets the elements be modified.
A QSpan can be constructed from a pointer and a count, a pair of pointers, a C array, a std::array, or any container providing data() and size() such as QList or std::vector:
QList<int> values = {1, 2, 3, 4};
QSpan<const int> view(values);
int sum = 0;
for (int v : view.first(2))
sum += v;
Differences from Qt
The extent is not enforced: every QSpan stores its size at run time, and first(), last() and the other slicing functions always return a dynamic-extent QSpan<T>, as do the free functions as_bytes() and as_writable_bytes(). Indexes and counts are not bounds-checked.
Member Type Documentation
typedef QSpan::const_iterator
The constant iterator type, a plain pointer const T *.
typedef QSpan::const_pointer
A pointer to a constant element, const T *.
typedef QSpan::const_reference
A reference to a constant element, const T &.
typedef QSpan::const_reverse_iterator
The constant reverse iterator type.
typedef QSpan::difference_type
The signed type used for the distance between iterators, qptrdiff.
typedef QSpan::element_type
The type of the elements, T, including any const qualifier.
typedef QSpan::iterator
The iterator type, a plain pointer T *.
typedef QSpan::pointer
A pointer to an element, T *.
typedef QSpan::reference
A reference to an element, T &.
typedef QSpan::reverse_iterator
The reverse iterator type.
typedef QSpan::size_type
The signed type used for sizes and indexes, qsizetype.
typedef QSpan::value_type
The type of the elements with any const or volatile qualifier removed.
Member Function Documentation
QSpan::QSpan()
Constructs an empty span that refers to no elements.
QSpan::QSpan(T *first, qsizetype count)
Constructs a span over count elements starting at first.
template <typename End, typename = std::enable_if_t<std::is_pointer_v<End> && std::is_convertible_v<End, T *>>> QSpan::QSpan(T *first, End last)
This is an overloaded function.
Constructs a span over the elements in the range from first up to, but not including, last.
template <std::size_t N> QSpan::QSpan(T (&array)[N])
This is an overloaded function.
Constructs a span over the elements of the C array array.
template <typename U, std::size_t N, typename = std::enable_if_t<std::is_convertible_v<U (*)[], T (*)[]>>> QSpan::QSpan(std::array<U, N> &array)
This is an overloaded function.
Constructs a span over the elements of the std::array array.
template <typename U, std::size_t N, typename = std::enable_if_t<std::is_convertible_v<const U (*)[], T (*)[]>>> QSpan::QSpan(const std::array<U, N> &array)
This is an overloaded function.
Constructs a span over the elements of the constant std::array array.
template <typename C, typename = std::enable_if_t< !std::is_array_v<std::remove_reference_t<C>> && std::is_convertible_v<std::remove_pointer_t<decltype(std::declval<C &>().data())> (*)[], T (*)[]>>, typename = decltype(std::declval<C &>().size())> QSpan::QSpan(C &&c)
This is an overloaded function.
Constructs a span over the elements of the container c, which must provide data() and size().
template <typename U, std::size_t F, typename = std::enable_if_t<std::is_convertible_v<U (*)[], T (*)[]>>> QSpan::QSpan(QSpan<U, F> other)
This is an overloaded function.
Constructs a span from the span other, whose element type is convertible to T, for example from QSpan<int> to QSpan<const int>.
T & QSpan::at(qsizetype i) const
Returns a reference to the element at index i. The index must be valid; it is not checked.
This is equivalent to the subscript operator.
T & QSpan::back() const
Returns a reference to the last element. The span must not be empty.
See also front().
iterator QSpan::begin() const
Returns an iterator to the first element.
See also end().
const_iterator QSpan::cbegin() const
Returns a constant iterator to the first element.
See also cend().
const_iterator QSpan::cend() const
Returns a constant iterator to the position after the last element.
See also cbegin().
void QSpan::chop(qsizetype c)
Removes the last c elements from the span.
This function was introduced in 6.9.
See also chopped() and slice().
QSpan<T> QSpan::chopped(qsizetype c) const
Returns a span over all but the last c elements.
qsizetype QSpan::count() const
Returns the number of elements in the span; the same as size().
const_reverse_iterator QSpan::crbegin() const
Returns a const reverse iterator pointing to the last element of the span.
See also crend() and rbegin().
const_reverse_iterator QSpan::crend() const
Returns a const reverse iterator pointing just before the first element of the span.
See also crbegin() and rend().
T * QSpan::data() const
Returns a pointer to the first element.
bool QSpan::empty() const
Returns true if the span has no elements; the same as isEmpty().
iterator QSpan::end() const
Returns an iterator to the position after the last element.
See also begin().
QSpan<T> QSpan::first(qsizetype c) const
Returns a span over the first c elements. c must not exceed size().
See also last() and subspan().
T & QSpan::front() const
Returns a reference to the first element. The span must not be empty.
See also back().
bool QSpan::isEmpty() const
Returns true if the span has no elements; otherwise returns false.
See also size().
QSpan<T> QSpan::last(qsizetype c) const
Returns a span over the last c elements. c must not exceed size().
See also first() and subspan().
qsizetype QSpan::length() const
Returns the number of elements in the span; the same as size().
T & QSpan::operator[](qsizetype i) const
Returns a reference to the element at index i. The index must be valid; it is not checked.
See also at().
reverse_iterator QSpan::rbegin() const
Returns a reverse iterator to the last element.
See also rend().
reverse_iterator QSpan::rend() const
Returns a reverse iterator to the position before the first element.
See also rbegin().
qsizetype QSpan::size() const
Returns the number of elements in the span.
See also size_bytes() and isEmpty().
qsizetype QSpan::size_bytes() const
Returns the size of the viewed elements in bytes.
See also size().
void QSpan::slice(qsizetype pos)
Removes the elements before index pos from the span.
This function was introduced in 6.9.
void QSpan::slice(qsizetype pos, qsizetype c)
This is an overloaded function.
Makes the span refer to c elements starting at index pos.
This function was introduced in 6.9.
QSpan<T> QSpan::sliced(qsizetype pos) const
Returns a span over the elements from index pos to the end; the same as subspan().
QSpan<T> QSpan::sliced(qsizetype pos, qsizetype c) const
This is an overloaded function.
Returns a span over c elements starting at index pos; the same as subspan().
QSpan<T> QSpan::subspan(qsizetype pos) const
Returns a span over the elements from index pos to the end.
See also first(), last() and sliced().
QSpan<T> QSpan::subspan(qsizetype pos, qsizetype c) const
This is an overloaded function.
Returns a span over c elements starting at index pos.
Member Variable Documentation
std::size_t QSpan::extent
The extent given as the second template argument, or q20::dynamic_extent. It does not constrain the size of the span.