You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

463 lines
13KB

  1. // Components for manipulating non-owning sequences of objects -*- C++ -*-
  2. // Copyright (C) 2019-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file span
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // P0122 span library
  25. // Contributed by ThePhD
  26. //
  27. #ifndef _GLIBCXX_SPAN
  28. #define _GLIBCXX_SPAN 1
  29. #pragma GCC system_header
  30. #if __cplusplus > 201703L
  31. #include <type_traits>
  32. #include <array>
  33. #include <bits/stl_iterator.h>
  34. #include <bits/range_access.h>
  35. #if __cpp_lib_concepts
  36. namespace std _GLIBCXX_VISIBILITY(default)
  37. {
  38. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  39. #define __cpp_lib_span 202002L
  40. inline constexpr size_t dynamic_extent = static_cast<size_t>(-1);
  41. template<typename _Type, size_t _Extent>
  42. class span;
  43. namespace __detail
  44. {
  45. template<typename _Tp>
  46. struct __is_std_span : false_type { };
  47. template<typename _Tp, size_t _Num>
  48. struct __is_std_span<span<_Tp, _Num>> : true_type { };
  49. template<typename _Tp>
  50. struct __is_std_array : false_type { };
  51. template<typename _Tp, size_t _Num>
  52. struct __is_std_array<_GLIBCXX_STD_C::array<_Tp, _Num>> : true_type { };
  53. #ifdef _GLIBCXX_DEBUG
  54. template<typename _Tp, size_t _Num>
  55. struct __is_std_array<__debug::array<_Tp, _Num>> : true_type { };
  56. #endif
  57. template<size_t _Extent>
  58. class __extent_storage
  59. {
  60. public:
  61. constexpr
  62. __extent_storage(size_t) noexcept
  63. { }
  64. static constexpr size_t
  65. _M_extent() noexcept
  66. { return _Extent; }
  67. };
  68. template<>
  69. class __extent_storage<dynamic_extent>
  70. {
  71. public:
  72. constexpr
  73. __extent_storage(size_t __extent) noexcept
  74. : _M_extent_value(__extent)
  75. { }
  76. constexpr size_t
  77. _M_extent() const noexcept
  78. { return this->_M_extent_value; }
  79. private:
  80. size_t _M_extent_value;
  81. };
  82. } // namespace __detail
  83. template<typename _Type, size_t _Extent = dynamic_extent>
  84. class span
  85. {
  86. template<size_t _Offset, size_t _Count>
  87. static constexpr size_t
  88. _S_subspan_extent()
  89. {
  90. if constexpr (_Count != dynamic_extent)
  91. return _Count;
  92. else if constexpr (extent != dynamic_extent)
  93. return _Extent - _Offset;
  94. else
  95. return dynamic_extent;
  96. }
  97. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  98. // 3255. span's array constructor is too strict
  99. template<typename _Tp, size_t _ArrayExtent>
  100. requires (_Extent == dynamic_extent || _ArrayExtent == _Extent)
  101. using __is_compatible_array = __is_array_convertible<_Type, _Tp>;
  102. template<typename _Ref>
  103. using __is_compatible_ref
  104. = __is_array_convertible<_Type, remove_reference_t<_Ref>>;
  105. public:
  106. // member types
  107. using element_type = _Type;
  108. using value_type = remove_cv_t<_Type>;
  109. using size_type = size_t;
  110. using difference_type = ptrdiff_t;
  111. using pointer = _Type*;
  112. using const_pointer = const _Type*;
  113. using reference = element_type&;
  114. using const_reference = const element_type&;
  115. using iterator = __gnu_cxx::__normal_iterator<pointer, span>;
  116. using reverse_iterator = std::reverse_iterator<iterator>;
  117. // member constants
  118. static constexpr size_t extent = _Extent;
  119. // constructors, copy and assignment
  120. constexpr
  121. span() noexcept
  122. requires ((_Extent + 1u) <= 1u)
  123. : _M_extent(0), _M_ptr(nullptr)
  124. { }
  125. template<contiguous_iterator _It>
  126. requires __is_compatible_ref<iter_reference_t<_It>>::value
  127. constexpr explicit(extent != dynamic_extent)
  128. span(_It __first, size_type __count)
  129. noexcept
  130. : _M_extent(__count), _M_ptr(std::to_address(__first))
  131. {
  132. if constexpr (_Extent != dynamic_extent)
  133. {
  134. __glibcxx_assert(__count == _Extent);
  135. }
  136. }
  137. template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
  138. requires __is_compatible_ref<iter_reference_t<_It>>::value
  139. && (!is_convertible_v<_End, size_type>)
  140. constexpr explicit(extent != dynamic_extent)
  141. span(_It __first, _End __last)
  142. noexcept(noexcept(__last - __first))
  143. : _M_extent(static_cast<size_type>(__last - __first)),
  144. _M_ptr(std::to_address(__first))
  145. {
  146. if constexpr (_Extent != dynamic_extent)
  147. {
  148. __glibcxx_assert((__last - __first) == _Extent);
  149. }
  150. }
  151. template<size_t _ArrayExtent>
  152. requires (_Extent == dynamic_extent || _ArrayExtent == _Extent)
  153. constexpr
  154. span(type_identity_t<element_type> (&__arr)[_ArrayExtent]) noexcept
  155. : span(static_cast<pointer>(__arr), _ArrayExtent)
  156. { }
  157. template<typename _Tp, size_t _ArrayExtent>
  158. requires __is_compatible_array<_Tp, _ArrayExtent>::value
  159. constexpr
  160. span(array<_Tp, _ArrayExtent>& __arr) noexcept
  161. : span(static_cast<pointer>(__arr.data()), _ArrayExtent)
  162. { }
  163. template<typename _Tp, size_t _ArrayExtent>
  164. requires __is_compatible_array<const _Tp, _ArrayExtent>::value
  165. constexpr
  166. span(const array<_Tp, _ArrayExtent>& __arr) noexcept
  167. : span(static_cast<pointer>(__arr.data()), _ArrayExtent)
  168. { }
  169. template<typename _Range>
  170. requires ranges::contiguous_range<_Range> && ranges::sized_range<_Range>
  171. && (ranges::borrowed_range<_Range> || is_const_v<element_type>)
  172. && (!__detail::__is_std_span<remove_cvref_t<_Range>>::value)
  173. && (!__detail::__is_std_array<remove_cvref_t<_Range>>::value)
  174. && (!is_array_v<remove_cvref_t<_Range>>)
  175. && __is_compatible_ref<ranges::range_reference_t<_Range>>::value
  176. constexpr explicit(extent != dynamic_extent)
  177. span(_Range&& __range)
  178. noexcept(noexcept(ranges::data(__range))
  179. && noexcept(ranges::size(__range)))
  180. : span(ranges::data(__range), ranges::size(__range))
  181. {
  182. if constexpr (extent != dynamic_extent)
  183. {
  184. __glibcxx_assert(ranges::size(__range) == extent);
  185. }
  186. }
  187. constexpr
  188. span(const span&) noexcept = default;
  189. template<typename _OType, size_t _OExtent>
  190. requires (_Extent == dynamic_extent || _OExtent == dynamic_extent
  191. || _Extent == _OExtent)
  192. && (__is_array_convertible<_Type, _OType>::value)
  193. constexpr
  194. explicit(extent != dynamic_extent && _OExtent == dynamic_extent)
  195. span(const span<_OType, _OExtent>& __s) noexcept
  196. : _M_extent(__s.size()), _M_ptr(__s.data())
  197. {
  198. if constexpr (extent != dynamic_extent)
  199. {
  200. __glibcxx_assert(__s.size() == extent);
  201. }
  202. }
  203. ~span() noexcept = default;
  204. constexpr span&
  205. operator=(const span&) noexcept = default;
  206. // observers
  207. constexpr size_type
  208. size() const noexcept
  209. { return this->_M_extent._M_extent(); }
  210. constexpr size_type
  211. size_bytes() const noexcept
  212. { return this->_M_extent._M_extent() * sizeof(element_type); }
  213. [[nodiscard]] constexpr bool
  214. empty() const noexcept
  215. { return size() == 0; }
  216. // element access
  217. constexpr reference
  218. front() const noexcept
  219. {
  220. __glibcxx_assert(!empty());
  221. return *this->_M_ptr;
  222. }
  223. constexpr reference
  224. back() const noexcept
  225. {
  226. __glibcxx_assert(!empty());
  227. return *(this->_M_ptr + (size() - 1));
  228. }
  229. constexpr reference
  230. operator[](size_type __idx) const noexcept
  231. {
  232. __glibcxx_assert(__idx < size());
  233. return *(this->_M_ptr + __idx);
  234. }
  235. constexpr pointer
  236. data() const noexcept
  237. { return this->_M_ptr; }
  238. // iterator support
  239. constexpr iterator
  240. begin() const noexcept
  241. { return iterator(this->_M_ptr); }
  242. constexpr iterator
  243. end() const noexcept
  244. { return iterator(this->_M_ptr + this->size()); }
  245. constexpr reverse_iterator
  246. rbegin() const noexcept
  247. { return reverse_iterator(this->end()); }
  248. constexpr reverse_iterator
  249. rend() const noexcept
  250. { return reverse_iterator(this->begin()); }
  251. // subviews
  252. template<size_t _Count>
  253. constexpr span<element_type, _Count>
  254. first() const noexcept
  255. {
  256. if constexpr (_Extent == dynamic_extent)
  257. __glibcxx_assert(_Count <= size());
  258. else
  259. static_assert(_Count <= extent);
  260. using _Sp = span<element_type, _Count>;
  261. return _Sp{ this->data(), _Count };
  262. }
  263. constexpr span<element_type, dynamic_extent>
  264. first(size_type __count) const noexcept
  265. {
  266. __glibcxx_assert(__count <= size());
  267. return { this->data(), __count };
  268. }
  269. template<size_t _Count>
  270. constexpr span<element_type, _Count>
  271. last() const noexcept
  272. {
  273. if constexpr (_Extent == dynamic_extent)
  274. __glibcxx_assert(_Count <= size());
  275. else
  276. static_assert(_Count <= extent);
  277. using _Sp = span<element_type, _Count>;
  278. return _Sp{ this->data() + (this->size() - _Count), _Count };
  279. }
  280. constexpr span<element_type, dynamic_extent>
  281. last(size_type __count) const noexcept
  282. {
  283. __glibcxx_assert(__count <= size());
  284. return { this->data() + (this->size() - __count), __count };
  285. }
  286. template<size_t _Offset, size_t _Count = dynamic_extent>
  287. constexpr auto
  288. subspan() const noexcept
  289. -> span<element_type, _S_subspan_extent<_Offset, _Count>()>
  290. {
  291. if constexpr (_Extent == dynamic_extent)
  292. {
  293. __glibcxx_assert(_Offset <= size());
  294. }
  295. else
  296. static_assert(_Offset <= extent);
  297. using _Sp = span<element_type, _S_subspan_extent<_Offset, _Count>()>;
  298. if constexpr (_Count == dynamic_extent)
  299. return _Sp{ this->data() + _Offset, this->size() - _Offset };
  300. else
  301. {
  302. if constexpr (_Extent == dynamic_extent)
  303. {
  304. __glibcxx_assert(_Count <= size());
  305. __glibcxx_assert(_Count <= (size() - _Offset));
  306. }
  307. else
  308. {
  309. static_assert(_Count <= extent);
  310. static_assert(_Count <= (extent - _Offset));
  311. }
  312. return _Sp{ this->data() + _Offset, _Count };
  313. }
  314. }
  315. constexpr span<element_type, dynamic_extent>
  316. subspan(size_type __offset, size_type __count = dynamic_extent) const
  317. noexcept
  318. {
  319. __glibcxx_assert(__offset <= size());
  320. if (__count == dynamic_extent)
  321. __count = this->size() - __offset;
  322. else
  323. {
  324. __glibcxx_assert(__count <= size());
  325. __glibcxx_assert(__offset + __count <= size());
  326. }
  327. return {this->data() + __offset, __count};
  328. }
  329. private:
  330. [[no_unique_address]] __detail::__extent_storage<extent> _M_extent;
  331. pointer _M_ptr;
  332. };
  333. // deduction guides
  334. template<typename _Type, size_t _ArrayExtent>
  335. span(_Type(&)[_ArrayExtent]) -> span<_Type, _ArrayExtent>;
  336. template<typename _Type, size_t _ArrayExtent>
  337. span(array<_Type, _ArrayExtent>&) -> span<_Type, _ArrayExtent>;
  338. template<typename _Type, size_t _ArrayExtent>
  339. span(const array<_Type, _ArrayExtent>&)
  340. -> span<const _Type, _ArrayExtent>;
  341. template<contiguous_iterator _Iter, typename _End>
  342. span(_Iter, _End)
  343. -> span<remove_reference_t<iter_reference_t<_Iter>>>;
  344. template<typename _Range>
  345. span(_Range &&)
  346. -> span<remove_reference_t<ranges::range_reference_t<_Range&>>>;
  347. template<typename _Type, size_t _Extent>
  348. inline
  349. span<const byte, _Extent == dynamic_extent
  350. ? dynamic_extent : _Extent * sizeof(_Type)>
  351. as_bytes(span<_Type, _Extent> __sp) noexcept
  352. {
  353. auto data = reinterpret_cast<const byte*>(__sp.data());
  354. auto size = __sp.size_bytes();
  355. constexpr auto extent = _Extent == dynamic_extent
  356. ? dynamic_extent : _Extent * sizeof(_Type);
  357. return span<const byte, extent>{data, size};
  358. }
  359. template<typename _Type, size_t _Extent>
  360. inline
  361. span<byte, _Extent == dynamic_extent
  362. ? dynamic_extent : _Extent * sizeof(_Type)>
  363. as_writable_bytes(span<_Type, _Extent> __sp) noexcept
  364. {
  365. auto data = reinterpret_cast<byte*>(__sp.data());
  366. auto size = __sp.size_bytes();
  367. constexpr auto extent = _Extent == dynamic_extent
  368. ? dynamic_extent : _Extent * sizeof(_Type);
  369. return span<byte, extent>{data, size};
  370. }
  371. namespace ranges
  372. {
  373. // Opt-in to borrowed_range concept
  374. template<typename _ElementType, size_t _Extent>
  375. inline constexpr bool
  376. enable_borrowed_range<span<_ElementType, _Extent>> = true;
  377. // Opt-in to view concept
  378. template<typename _ElementType, size_t _Extent>
  379. inline constexpr bool
  380. enable_view<span<_ElementType, _Extent>>
  381. = _Extent == 0 || _Extent == dynamic_extent;
  382. }
  383. _GLIBCXX_END_NAMESPACE_VERSION
  384. } // namespace std
  385. #endif // concepts
  386. #endif // C++20
  387. #endif // _GLIBCXX_SPAN