1 // Components for manipulating non-owning sequences of objects -*- C++ -*-
3 // Copyright (C) 2019-2020 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * This is a Standard C++ Library header.
31 // Contributed by ThePhD
35 #define _GLIBCXX_SPAN 1
37 #pragma GCC system_header
39 #if __cplusplus > 201703L
41 #include <type_traits>
43 #include <bits/stl_iterator.h>
44 #include <bits/range_access.h>
46 #if __cpp_lib_concepts
47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 #define __cpp_lib_span 202002L
53 inline constexpr size_t dynamic_extent = static_cast<size_t>(-1);
55 template<typename _Type, size_t _Extent>
60 template<typename _Tp>
61 struct __is_std_span : false_type { };
63 template<typename _Tp, size_t _Num>
64 struct __is_std_span<span<_Tp, _Num>> : true_type { };
66 template<typename _Tp>
67 struct __is_std_array : false_type { };
69 template<typename _Tp, size_t _Num>
70 struct __is_std_array<_GLIBCXX_STD_C::array<_Tp, _Num>> : true_type { };
73 template<typename _Tp, size_t _Num>
74 struct __is_std_array<__debug::array<_Tp, _Num>> : true_type { };
77 template<size_t _Extent>
78 class __extent_storage
82 __extent_storage(size_t) noexcept
85 static constexpr size_t
91 class __extent_storage<dynamic_extent>
95 __extent_storage(size_t __extent) noexcept
96 : _M_extent_value(__extent)
100 _M_extent() const noexcept
101 { return this->_M_extent_value; }
104 size_t _M_extent_value;
106 } // namespace __detail
108 template<typename _Type, size_t _Extent = dynamic_extent>
111 template<size_t _Offset, size_t _Count>
112 static constexpr size_t
115 if constexpr (_Count != dynamic_extent)
117 else if constexpr (extent != dynamic_extent)
118 return _Extent - _Offset;
120 return dynamic_extent;
123 // _GLIBCXX_RESOLVE_LIB_DEFECTS
124 // 3255. span's array constructor is too strict
125 template<typename _Tp, size_t _ArrayExtent>
126 requires (_Extent == dynamic_extent || _ArrayExtent == _Extent)
127 using __is_compatible_array = __is_array_convertible<_Type, _Tp>;
129 template<typename _Ref>
130 using __is_compatible_ref
131 = __is_array_convertible<_Type, remove_reference_t<_Ref>>;
135 using element_type = _Type;
136 using value_type = remove_cv_t<_Type>;
137 using size_type = size_t;
138 using difference_type = ptrdiff_t;
139 using pointer = _Type*;
140 using const_pointer = const _Type*;
141 using reference = element_type&;
142 using const_reference = const element_type&;
143 using iterator = __gnu_cxx::__normal_iterator<pointer, span>;
144 using reverse_iterator = std::reverse_iterator<iterator>;
147 static constexpr size_t extent = _Extent;
149 // constructors, copy and assignment
153 requires ((_Extent + 1u) <= 1u)
154 : _M_extent(0), _M_ptr(nullptr)
157 template<contiguous_iterator _It>
158 requires __is_compatible_ref<iter_reference_t<_It>>::value
159 constexpr explicit(extent != dynamic_extent)
160 span(_It __first, size_type __count)
162 : _M_extent(__count), _M_ptr(std::to_address(__first))
164 if constexpr (_Extent != dynamic_extent)
166 __glibcxx_assert(__count == _Extent);
170 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
171 requires __is_compatible_ref<iter_reference_t<_It>>::value
172 && (!is_convertible_v<_End, size_type>)
173 constexpr explicit(extent != dynamic_extent)
174 span(_It __first, _End __last)
175 noexcept(noexcept(__last - __first))
176 : _M_extent(static_cast<size_type>(__last - __first)),
177 _M_ptr(std::to_address(__first))
179 if constexpr (_Extent != dynamic_extent)
181 __glibcxx_assert((__last - __first) == _Extent);
185 template<size_t _ArrayExtent>
186 requires (_Extent == dynamic_extent || _ArrayExtent == _Extent)
188 span(type_identity_t<element_type> (&__arr)[_ArrayExtent]) noexcept
189 : span(static_cast<pointer>(__arr), _ArrayExtent)
192 template<typename _Tp, size_t _ArrayExtent>
193 requires __is_compatible_array<_Tp, _ArrayExtent>::value
195 span(array<_Tp, _ArrayExtent>& __arr) noexcept
196 : span(static_cast<pointer>(__arr.data()), _ArrayExtent)
199 template<typename _Tp, size_t _ArrayExtent>
200 requires __is_compatible_array<const _Tp, _ArrayExtent>::value
202 span(const array<_Tp, _ArrayExtent>& __arr) noexcept
203 : span(static_cast<pointer>(__arr.data()), _ArrayExtent)
206 template<typename _Range>
207 requires (!__detail::__is_std_span<remove_cvref_t<_Range>>::value)
208 && (!__detail::__is_std_array<remove_cvref_t<_Range>>::value)
209 && (!is_array_v<remove_cvref_t<_Range>>)
210 && ranges::contiguous_range<_Range> && ranges::sized_range<_Range>
211 && (ranges::borrowed_range<_Range> || is_const_v<element_type>)
212 && __is_compatible_ref<ranges::range_reference_t<_Range>>::value
213 constexpr explicit(extent != dynamic_extent)
214 span(_Range&& __range)
215 noexcept(noexcept(ranges::data(__range))
216 && noexcept(ranges::size(__range)))
217 : span(ranges::data(__range), ranges::size(__range))
219 if constexpr (extent != dynamic_extent)
221 __glibcxx_assert(ranges::size(__range) == extent);
226 span(const span&) noexcept = default;
228 template<typename _OType, size_t _OExtent>
229 requires (_Extent == dynamic_extent || _OExtent == dynamic_extent
230 || _Extent == _OExtent)
231 && (__is_array_convertible<_Type, _OType>::value)
233 explicit(extent != dynamic_extent && _OExtent == dynamic_extent)
234 span(const span<_OType, _OExtent>& __s) noexcept
235 : _M_extent(__s.size()), _M_ptr(__s.data())
237 if constexpr (extent != dynamic_extent)
239 __glibcxx_assert(__s.size() == extent);
243 ~span() noexcept = default;
246 operator=(const span&) noexcept = default;
251 size() const noexcept
252 { return this->_M_extent._M_extent(); }
255 size_bytes() const noexcept
256 { return this->_M_extent._M_extent() * sizeof(element_type); }
258 [[nodiscard]] constexpr bool
259 empty() const noexcept
260 { return size() == 0; }
265 front() const noexcept
267 __glibcxx_assert(!empty());
268 return *this->_M_ptr;
272 back() const noexcept
274 __glibcxx_assert(!empty());
275 return *(this->_M_ptr + (size() - 1));
279 operator[](size_type __idx) const noexcept
281 __glibcxx_assert(__idx < size());
282 return *(this->_M_ptr + __idx);
286 data() const noexcept
287 { return this->_M_ptr; }
292 begin() const noexcept
293 { return iterator(this->_M_ptr); }
297 { return iterator(this->_M_ptr + this->size()); }
299 constexpr reverse_iterator
300 rbegin() const noexcept
301 { return reverse_iterator(this->end()); }
303 constexpr reverse_iterator
304 rend() const noexcept
305 { return reverse_iterator(this->begin()); }
309 template<size_t _Count>
310 constexpr span<element_type, _Count>
311 first() const noexcept
313 if constexpr (_Extent == dynamic_extent)
314 __glibcxx_assert(_Count <= size());
316 static_assert(_Count <= extent);
317 using _Sp = span<element_type, _Count>;
318 return _Sp{ this->data(), _Count };
321 constexpr span<element_type, dynamic_extent>
322 first(size_type __count) const noexcept
324 __glibcxx_assert(__count <= size());
325 return { this->data(), __count };
328 template<size_t _Count>
329 constexpr span<element_type, _Count>
330 last() const noexcept
332 if constexpr (_Extent == dynamic_extent)
333 __glibcxx_assert(_Count <= size());
335 static_assert(_Count <= extent);
336 using _Sp = span<element_type, _Count>;
337 return _Sp{ this->data() + (this->size() - _Count), _Count };
340 constexpr span<element_type, dynamic_extent>
341 last(size_type __count) const noexcept
343 __glibcxx_assert(__count <= size());
344 return { this->data() + (this->size() - __count), __count };
347 template<size_t _Offset, size_t _Count = dynamic_extent>
349 subspan() const noexcept
350 -> span<element_type, _S_subspan_extent<_Offset, _Count>()>
352 if constexpr (_Extent == dynamic_extent)
354 __glibcxx_assert(_Offset <= size());
357 static_assert(_Offset <= extent);
359 using _Sp = span<element_type, _S_subspan_extent<_Offset, _Count>()>;
361 if constexpr (_Count == dynamic_extent)
362 return _Sp{ this->data() + _Offset, this->size() - _Offset };
365 if constexpr (_Extent == dynamic_extent)
367 __glibcxx_assert(_Count <= size());
368 __glibcxx_assert(_Count <= (size() - _Offset));
372 static_assert(_Count <= extent);
373 static_assert(_Count <= (extent - _Offset));
375 return _Sp{ this->data() + _Offset, _Count };
379 constexpr span<element_type, dynamic_extent>
380 subspan(size_type __offset, size_type __count = dynamic_extent) const
383 __glibcxx_assert(__offset <= size());
384 if (__count == dynamic_extent)
385 __count = this->size() - __offset;
388 __glibcxx_assert(__count <= size());
389 __glibcxx_assert(__offset + __count <= size());
391 return {this->data() + __offset, __count};
395 [[no_unique_address]] __detail::__extent_storage<extent> _M_extent;
401 template<typename _Type, size_t _ArrayExtent>
402 span(_Type(&)[_ArrayExtent]) -> span<_Type, _ArrayExtent>;
404 template<typename _Type, size_t _ArrayExtent>
405 span(array<_Type, _ArrayExtent>&) -> span<_Type, _ArrayExtent>;
407 template<typename _Type, size_t _ArrayExtent>
408 span(const array<_Type, _ArrayExtent>&)
409 -> span<const _Type, _ArrayExtent>;
411 template<contiguous_iterator _Iter, typename _End>
413 -> span<remove_reference_t<iter_reference_t<_Iter>>>;
415 template<ranges::contiguous_range _Range>
417 -> span<remove_reference_t<ranges::range_reference_t<_Range&>>>;
419 template<typename _Type, size_t _Extent>
421 span<const byte, _Extent == dynamic_extent
422 ? dynamic_extent : _Extent * sizeof(_Type)>
423 as_bytes(span<_Type, _Extent> __sp) noexcept
425 auto data = reinterpret_cast<const byte*>(__sp.data());
426 auto size = __sp.size_bytes();
427 constexpr auto extent = _Extent == dynamic_extent
428 ? dynamic_extent : _Extent * sizeof(_Type);
429 return span<const byte, extent>{data, size};
432 template<typename _Type, size_t _Extent>
433 requires (!is_const_v<_Type>)
435 span<byte, _Extent == dynamic_extent
436 ? dynamic_extent : _Extent * sizeof(_Type)>
437 as_writable_bytes(span<_Type, _Extent> __sp) noexcept
439 auto data = reinterpret_cast<byte*>(__sp.data());
440 auto size = __sp.size_bytes();
441 constexpr auto extent = _Extent == dynamic_extent
442 ? dynamic_extent : _Extent * sizeof(_Type);
443 return span<byte, extent>{data, size};
448 // Opt-in to borrowed_range concept
449 template<typename _ElementType, size_t _Extent>
450 inline constexpr bool
451 enable_borrowed_range<span<_ElementType, _Extent>> = true;
453 // Opt-in to view concept
454 template<typename _ElementType, size_t _Extent>
455 inline constexpr bool
456 enable_view<span<_ElementType, _Extent>> = true;
458 _GLIBCXX_END_NAMESPACE_VERSION
462 #endif // _GLIBCXX_SPAN