All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
span.h
Go to the documentation of this file.
1//
2// Copyright 2019 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_SPAN_H
8#define PXR_BASE_TF_SPAN_H
9
11
12#include "pxr/pxr.h"
13#include "pxr/base/tf/api.h"
15
16#include <cstddef>
17#include <iterator>
18#include <type_traits>
19
20
21PXR_NAMESPACE_OPEN_SCOPE
22
23
69template <typename T>
70class TfSpan
71{
72public:
73 using element_type = T;
74 using value_type = typename std::remove_cv<T>::type;
75 using pointer = T*;
76 using reference = T&;
77 using index_type = std::size_t;
78 using difference_type = std::ptrdiff_t;
79
80 using iterator = T*;
81 using const_iterator = const T*;
82 using reverse_iterator = std::reverse_iterator<iterator>;
83 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
84
85 TfSpan() noexcept = default;
86
90 TfSpan(pointer ptr, index_type count)
91 : _data(ptr), _size(count)
92 {
93 TF_DEV_AXIOM(count == 0 || ptr);
94 }
95
97 TfSpan(pointer first, pointer last)
98 : TfSpan(first, index_type(last-first))
99 {
101 }
102
106 template <class Container>
107 TfSpan(Container& cont,
108 typename std::enable_if<
109 !std::is_const<element_type>::value &&
110 std::is_same<typename Container::value_type, value_type
111 >::value, Container
112 >::type* = 0)
113 : _data(cont.data()), _size(cont.size())
114 {
115 TF_DEV_AXIOM(_size == 0 || _data);
116 }
117
121 template <class Container>
122 TfSpan(const Container& cont,
123 typename std::enable_if<
124 std::is_same<typename Container::value_type, value_type
125 >::value, Container
126 >::type* = 0)
127 : _data(cont.data()), _size(cont.size())
128 {
129 TF_DEV_AXIOM(_size == 0 || _data);
130 }
131
133 pointer data() const noexcept { return _data; }
134
136 index_type size() const noexcept { return _size; }
137
139 bool empty() const noexcept { return _size == 0; }
140
144 reference operator[](index_type idx) const {
145 TF_DEV_AXIOM(idx < _size);
146 return _data[idx];
147 }
148
150 reference front() const {
152 return *begin();
153 }
154
156 reference back() const {
158 return *(end() - 1);
159 }
160
162 iterator begin() const noexcept { return _data; }
163
165 const_iterator cbegin() const noexcept { return _data; }
166
168 iterator end() const noexcept { return _data + _size; }
169
171 const_iterator cend() const noexcept { return _data + _size; }
172
174 reverse_iterator rbegin() const noexcept
175 { return reverse_iterator(end()); }
176
178 const_reverse_iterator crbegin() const noexcept
179 { return const_reverse_iterator(cend()); }
180
182 reverse_iterator rend() const noexcept
183 { return reverse_iterator(begin()); }
184
186 const_reverse_iterator crend() const noexcept
187 { return const_reverse_iterator(cbegin()); }
188
193 TfSpan<T> subspan(difference_type offset, difference_type count=-1) const {
194 TF_DEV_AXIOM(offset >= 0 && (index_type)offset < _size);
195 if (count == -1) {
196 return TfSpan<T>(_data + offset, _size - offset);
197 } else {
198 TF_DEV_AXIOM(count >= 0);
199 TF_DEV_AXIOM(((index_type)offset+(index_type)count) <= _size);
200 return TfSpan<T>(_data + offset, count);
201 }
202 }
203
205 TfSpan<T> first(size_t count) const {
206 return subspan(0, count);
207 }
208
210 TfSpan<T> last(size_t count) const {
211 TF_DEV_AXIOM(_size >= count);
212 return TfSpan<T>(end() - count, count);
213 }
214
215private:
216 pointer _data = nullptr;
217 index_type _size = 0;
218};
219
220
222template <typename Container>
224TfMakeSpan(Container& cont)
225{
227}
228
229
231template <typename Container>
233TfMakeConstSpan(const Container& cont)
234{
236}
237
238PXR_NAMESPACE_CLOSE_SCOPE
239
240#endif // PXR_BASE_TF_SPAN_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
Represents a range of contiguous elements.
Definition: span.h:71
reference front() const
Return a reference to the first element in the span.
Definition: span.h:150
TfSpan(pointer first, pointer last)
Construct a span over the range [first, last).
Definition: span.h:97
reverse_iterator rbegin() const noexcept
Returns a non-const reverse iterator the start of the span.
Definition: span.h:174
bool empty() const noexcept
Returns true if this span contains no elements, false otherwise.
Definition: span.h:139
TfSpan< T > first(size_t count) const
Return a subspan consisting of the first count elements of this span.
Definition: span.h:205
index_type size() const noexcept
Return the total number of elements in the span.
Definition: span.h:136
TfSpan(pointer ptr, index_type count)
Construct a span over the range of [ptr, ptr+count).
Definition: span.h:90
reverse_iterator rend() const noexcept
Returns a non-const reverse iterator to the end of the span.
Definition: span.h:182
TfSpan< T > subspan(difference_type offset, difference_type count=-1) const
Returns a new span referencing a sub-range of this span.
Definition: span.h:193
reference operator[](index_type idx) const
Returns a reference to the idx'th element of the span.
Definition: span.h:144
const_iterator cend() const noexcept
Returns a const iterator to the end of the span.
Definition: span.h:171
reference back() const
Return a reference to the last element in the span.
Definition: span.h:156
pointer data() const noexcept
Return a pointer to the first element of the span.
Definition: span.h:133
TfSpan< T > last(size_t count) const
Return a subspan consisting of the last count elements of this span.
Definition: span.h:210
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.
Definition: span.h:122
iterator end() const noexcept
Returns a non-const iterator to the end of the span.
Definition: span.h:168
const_reverse_iterator crbegin() const noexcept
Returns a cons reverse iterator to the start of the span.
Definition: span.h:178
const_iterator cbegin() const noexcept
Returns a cons iterator to the start of the span.
Definition: span.h:165
const_reverse_iterator crend() const noexcept
Returns a const reverse iterator to the end of the span.
Definition: span.h:186
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.
Definition: span.h:107
iterator begin() const noexcept
Returns a non-const iterator the start of the span.
Definition: span.h:162
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:205
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