iota_view.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___RANGES_IOTA_VIEW_H
  10. #define _LIBCPP___RANGES_IOTA_VIEW_H
  11. #include <__assert>
  12. #include <__compare/three_way_comparable.h>
  13. #include <__concepts/arithmetic.h>
  14. #include <__concepts/constructible.h>
  15. #include <__concepts/convertible_to.h>
  16. #include <__concepts/copyable.h>
  17. #include <__concepts/equality_comparable.h>
  18. #include <__concepts/invocable.h>
  19. #include <__concepts/same_as.h>
  20. #include <__concepts/semiregular.h>
  21. #include <__concepts/totally_ordered.h>
  22. #include <__config>
  23. #include <__functional/ranges_operations.h>
  24. #include <__iterator/concepts.h>
  25. #include <__iterator/incrementable_traits.h>
  26. #include <__iterator/iterator_traits.h>
  27. #include <__iterator/unreachable_sentinel.h>
  28. #include <__ranges/enable_borrowed_range.h>
  29. #include <__ranges/movable_box.h>
  30. #include <__ranges/view_interface.h>
  31. #include <__type_traits/conditional.h>
  32. #include <__type_traits/is_nothrow_copy_constructible.h>
  33. #include <__type_traits/make_unsigned.h>
  34. #include <__type_traits/type_identity.h>
  35. #include <__utility/forward.h>
  36. #include <__utility/move.h>
  37. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  38. # pragma GCC system_header
  39. #endif
  40. _LIBCPP_BEGIN_NAMESPACE_STD
  41. #if _LIBCPP_STD_VER >= 20
  42. namespace ranges {
  43. template<class _Int>
  44. struct __get_wider_signed {
  45. consteval static auto __call() {
  46. if constexpr (sizeof(_Int) < sizeof(short)) return type_identity<short>{};
  47. else if constexpr (sizeof(_Int) < sizeof(int)) return type_identity<int>{};
  48. else if constexpr (sizeof(_Int) < sizeof(long)) return type_identity<long>{};
  49. else return type_identity<long long>{};
  50. static_assert(sizeof(_Int) <= sizeof(long long),
  51. "Found integer-like type that is bigger than largest integer like type.");
  52. }
  53. using type = typename decltype(__call())::type;
  54. };
  55. template<class _Start>
  56. using _IotaDiffT = typename _If<
  57. (!integral<_Start> || sizeof(iter_difference_t<_Start>) > sizeof(_Start)),
  58. type_identity<iter_difference_t<_Start>>,
  59. __get_wider_signed<_Start>
  60. >::type;
  61. template<class _Iter>
  62. concept __decrementable = incrementable<_Iter> && requires(_Iter __i) {
  63. { --__i } -> same_as<_Iter&>;
  64. { __i-- } -> same_as<_Iter>;
  65. };
  66. template<class _Iter>
  67. concept __advanceable =
  68. __decrementable<_Iter> && totally_ordered<_Iter> &&
  69. requires(_Iter __i, const _Iter __j, const _IotaDiffT<_Iter> __n) {
  70. { __i += __n } -> same_as<_Iter&>;
  71. { __i -= __n } -> same_as<_Iter&>;
  72. _Iter(__j + __n);
  73. _Iter(__n + __j);
  74. _Iter(__j - __n);
  75. { __j - __j } -> convertible_to<_IotaDiffT<_Iter>>;
  76. };
  77. template<class>
  78. struct __iota_iterator_category {};
  79. template<incrementable _Tp>
  80. struct __iota_iterator_category<_Tp> {
  81. using iterator_category = input_iterator_tag;
  82. };
  83. template <weakly_incrementable _Start, semiregular _BoundSentinel = unreachable_sentinel_t>
  84. requires __weakly_equality_comparable_with<_Start, _BoundSentinel> && copyable<_Start>
  85. class iota_view : public view_interface<iota_view<_Start, _BoundSentinel>> {
  86. struct __iterator : public __iota_iterator_category<_Start> {
  87. friend class iota_view;
  88. using iterator_concept =
  89. _If<__advanceable<_Start>, random_access_iterator_tag,
  90. _If<__decrementable<_Start>, bidirectional_iterator_tag,
  91. _If<incrementable<_Start>, forward_iterator_tag,
  92. /*Else*/ input_iterator_tag>>>;
  93. using value_type = _Start;
  94. using difference_type = _IotaDiffT<_Start>;
  95. _Start __value_ = _Start();
  96. _LIBCPP_HIDE_FROM_ABI
  97. __iterator() requires default_initializable<_Start> = default;
  98. _LIBCPP_HIDE_FROM_ABI
  99. constexpr explicit __iterator(_Start __value) : __value_(std::move(__value)) {}
  100. _LIBCPP_HIDE_FROM_ABI
  101. constexpr _Start operator*() const noexcept(is_nothrow_copy_constructible_v<_Start>) {
  102. return __value_;
  103. }
  104. _LIBCPP_HIDE_FROM_ABI
  105. constexpr __iterator& operator++() {
  106. ++__value_;
  107. return *this;
  108. }
  109. _LIBCPP_HIDE_FROM_ABI
  110. constexpr void operator++(int) { ++*this; }
  111. _LIBCPP_HIDE_FROM_ABI
  112. constexpr __iterator operator++(int) requires incrementable<_Start> {
  113. auto __tmp = *this;
  114. ++*this;
  115. return __tmp;
  116. }
  117. _LIBCPP_HIDE_FROM_ABI
  118. constexpr __iterator& operator--() requires __decrementable<_Start> {
  119. --__value_;
  120. return *this;
  121. }
  122. _LIBCPP_HIDE_FROM_ABI
  123. constexpr __iterator operator--(int) requires __decrementable<_Start> {
  124. auto __tmp = *this;
  125. --*this;
  126. return __tmp;
  127. }
  128. _LIBCPP_HIDE_FROM_ABI
  129. constexpr __iterator& operator+=(difference_type __n)
  130. requires __advanceable<_Start>
  131. {
  132. if constexpr (__integer_like<_Start> && !__signed_integer_like<_Start>) {
  133. if (__n >= difference_type(0)) {
  134. __value_ += static_cast<_Start>(__n);
  135. } else {
  136. __value_ -= static_cast<_Start>(-__n);
  137. }
  138. } else {
  139. __value_ += __n;
  140. }
  141. return *this;
  142. }
  143. _LIBCPP_HIDE_FROM_ABI
  144. constexpr __iterator& operator-=(difference_type __n)
  145. requires __advanceable<_Start>
  146. {
  147. if constexpr (__integer_like<_Start> && !__signed_integer_like<_Start>) {
  148. if (__n >= difference_type(0)) {
  149. __value_ -= static_cast<_Start>(__n);
  150. } else {
  151. __value_ += static_cast<_Start>(-__n);
  152. }
  153. } else {
  154. __value_ -= __n;
  155. }
  156. return *this;
  157. }
  158. _LIBCPP_HIDE_FROM_ABI
  159. constexpr _Start operator[](difference_type __n) const
  160. requires __advanceable<_Start>
  161. {
  162. return _Start(__value_ + __n);
  163. }
  164. _LIBCPP_HIDE_FROM_ABI
  165. friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
  166. requires equality_comparable<_Start>
  167. {
  168. return __x.__value_ == __y.__value_;
  169. }
  170. _LIBCPP_HIDE_FROM_ABI
  171. friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
  172. requires totally_ordered<_Start>
  173. {
  174. return __x.__value_ < __y.__value_;
  175. }
  176. _LIBCPP_HIDE_FROM_ABI
  177. friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
  178. requires totally_ordered<_Start>
  179. {
  180. return __y < __x;
  181. }
  182. _LIBCPP_HIDE_FROM_ABI
  183. friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
  184. requires totally_ordered<_Start>
  185. {
  186. return !(__y < __x);
  187. }
  188. _LIBCPP_HIDE_FROM_ABI
  189. friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y)
  190. requires totally_ordered<_Start>
  191. {
  192. return !(__x < __y);
  193. }
  194. _LIBCPP_HIDE_FROM_ABI
  195. friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
  196. requires totally_ordered<_Start> && three_way_comparable<_Start>
  197. {
  198. return __x.__value_ <=> __y.__value_;
  199. }
  200. _LIBCPP_HIDE_FROM_ABI
  201. friend constexpr __iterator operator+(__iterator __i, difference_type __n)
  202. requires __advanceable<_Start>
  203. {
  204. __i += __n;
  205. return __i;
  206. }
  207. _LIBCPP_HIDE_FROM_ABI
  208. friend constexpr __iterator operator+(difference_type __n, __iterator __i)
  209. requires __advanceable<_Start>
  210. {
  211. return __i + __n;
  212. }
  213. _LIBCPP_HIDE_FROM_ABI
  214. friend constexpr __iterator operator-(__iterator __i, difference_type __n)
  215. requires __advanceable<_Start>
  216. {
  217. __i -= __n;
  218. return __i;
  219. }
  220. _LIBCPP_HIDE_FROM_ABI
  221. friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
  222. requires __advanceable<_Start>
  223. {
  224. if constexpr (__integer_like<_Start>) {
  225. if constexpr (__signed_integer_like<_Start>) {
  226. return difference_type(difference_type(__x.__value_) - difference_type(__y.__value_));
  227. }
  228. if (__y.__value_ > __x.__value_) {
  229. return difference_type(-difference_type(__y.__value_ - __x.__value_));
  230. }
  231. return difference_type(__x.__value_ - __y.__value_);
  232. }
  233. return __x.__value_ - __y.__value_;
  234. }
  235. };
  236. struct __sentinel {
  237. friend class iota_view;
  238. private:
  239. _BoundSentinel __bound_sentinel_ = _BoundSentinel();
  240. public:
  241. _LIBCPP_HIDE_FROM_ABI
  242. __sentinel() = default;
  243. _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(_BoundSentinel __bound_sentinel)
  244. : __bound_sentinel_(std::move(__bound_sentinel)) {}
  245. _LIBCPP_HIDE_FROM_ABI
  246. friend constexpr bool operator==(const __iterator& __x, const __sentinel& __y) {
  247. return __x.__value_ == __y.__bound_sentinel_;
  248. }
  249. _LIBCPP_HIDE_FROM_ABI
  250. friend constexpr iter_difference_t<_Start> operator-(const __iterator& __x, const __sentinel& __y)
  251. requires sized_sentinel_for<_BoundSentinel, _Start>
  252. {
  253. return __x.__value_ - __y.__bound_sentinel_;
  254. }
  255. _LIBCPP_HIDE_FROM_ABI
  256. friend constexpr iter_difference_t<_Start> operator-(const __sentinel& __x, const __iterator& __y)
  257. requires sized_sentinel_for<_BoundSentinel, _Start>
  258. {
  259. return -(__y - __x);
  260. }
  261. };
  262. _Start __value_ = _Start();
  263. _BoundSentinel __bound_sentinel_ = _BoundSentinel();
  264. public:
  265. _LIBCPP_HIDE_FROM_ABI
  266. iota_view() requires default_initializable<_Start> = default;
  267. _LIBCPP_HIDE_FROM_ABI
  268. constexpr explicit iota_view(_Start __value) : __value_(std::move(__value)) { }
  269. _LIBCPP_HIDE_FROM_ABI
  270. constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(type_identity_t<_Start> __value, type_identity_t<_BoundSentinel> __bound_sentinel)
  271. : __value_(std::move(__value)), __bound_sentinel_(std::move(__bound_sentinel)) {
  272. // Validate the precondition if possible.
  273. if constexpr (totally_ordered_with<_Start, _BoundSentinel>) {
  274. _LIBCPP_ASSERT_UNCATEGORIZED(ranges::less_equal()(__value_, __bound_sentinel_),
  275. "Precondition violated: value is greater than bound.");
  276. }
  277. }
  278. _LIBCPP_HIDE_FROM_ABI
  279. constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(__iterator __first, __iterator __last)
  280. requires same_as<_Start, _BoundSentinel>
  281. : iota_view(std::move(__first.__value_), std::move(__last.__value_)) {}
  282. _LIBCPP_HIDE_FROM_ABI
  283. constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(__iterator __first, _BoundSentinel __last)
  284. requires same_as<_BoundSentinel, unreachable_sentinel_t>
  285. : iota_view(std::move(__first.__value_), std::move(__last)) {}
  286. _LIBCPP_HIDE_FROM_ABI
  287. constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(__iterator __first, __sentinel __last)
  288. requires(!same_as<_Start, _BoundSentinel> && !same_as<_BoundSentinel, unreachable_sentinel_t>)
  289. : iota_view(std::move(__first.__value_), std::move(__last.__bound_sentinel_)) {}
  290. _LIBCPP_HIDE_FROM_ABI
  291. constexpr __iterator begin() const { return __iterator{__value_}; }
  292. _LIBCPP_HIDE_FROM_ABI
  293. constexpr auto end() const {
  294. if constexpr (same_as<_BoundSentinel, unreachable_sentinel_t>)
  295. return unreachable_sentinel;
  296. else
  297. return __sentinel{__bound_sentinel_};
  298. }
  299. _LIBCPP_HIDE_FROM_ABI
  300. constexpr __iterator end() const
  301. requires same_as<_Start, _BoundSentinel>
  302. {
  303. return __iterator{__bound_sentinel_};
  304. }
  305. _LIBCPP_HIDE_FROM_ABI
  306. constexpr auto size() const
  307. requires(same_as<_Start, _BoundSentinel> && __advanceable<_Start>) ||
  308. (integral<_Start> && integral<_BoundSentinel>) || sized_sentinel_for<_BoundSentinel, _Start>
  309. {
  310. if constexpr (__integer_like<_Start> && __integer_like<_BoundSentinel>) {
  311. return (__value_ < 0)
  312. ? ((__bound_sentinel_ < 0)
  313. ? std::__to_unsigned_like(-__value_) - std::__to_unsigned_like(-__bound_sentinel_)
  314. : std::__to_unsigned_like(__bound_sentinel_) + std::__to_unsigned_like(-__value_))
  315. : std::__to_unsigned_like(__bound_sentinel_) - std::__to_unsigned_like(__value_);
  316. } else {
  317. return std::__to_unsigned_like(__bound_sentinel_ - __value_);
  318. }
  319. }
  320. };
  321. template <class _Start, class _BoundSentinel>
  322. requires(!__integer_like<_Start> || !__integer_like<_BoundSentinel> ||
  323. (__signed_integer_like<_Start> == __signed_integer_like<_BoundSentinel>))
  324. iota_view(_Start, _BoundSentinel) -> iota_view<_Start, _BoundSentinel>;
  325. template <class _Start, class _BoundSentinel>
  326. inline constexpr bool enable_borrowed_range<iota_view<_Start, _BoundSentinel>> = true;
  327. namespace views {
  328. namespace __iota {
  329. struct __fn {
  330. template<class _Start>
  331. _LIBCPP_HIDE_FROM_ABI
  332. constexpr auto operator()(_Start&& __start) const
  333. noexcept(noexcept(ranges::iota_view(std::forward<_Start>(__start))))
  334. -> decltype( ranges::iota_view(std::forward<_Start>(__start)))
  335. { return ranges::iota_view(std::forward<_Start>(__start)); }
  336. template <class _Start, class _BoundSentinel>
  337. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Start&& __start, _BoundSentinel&& __bound_sentinel) const
  338. noexcept(noexcept(ranges::iota_view(std::forward<_Start>(__start), std::forward<_BoundSentinel>(__bound_sentinel))))
  339. -> decltype( ranges::iota_view(std::forward<_Start>(__start), std::forward<_BoundSentinel>(__bound_sentinel)))
  340. { return ranges::iota_view(std::forward<_Start>(__start), std::forward<_BoundSentinel>(__bound_sentinel)); }
  341. };
  342. } // namespace __iota
  343. inline namespace __cpo {
  344. inline constexpr auto iota = __iota::__fn{};
  345. } // namespace __cpo
  346. } // namespace views
  347. } // namespace ranges
  348. #endif // _LIBCPP_STD_VER >= 20
  349. _LIBCPP_END_NAMESPACE_STD
  350. #endif // _LIBCPP___RANGES_IOTA_VIEW_H