iota_view.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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_PUSH_MACROS
  41. #include <__undef_macros>
  42. _LIBCPP_BEGIN_NAMESPACE_STD
  43. #if _LIBCPP_STD_VER >= 20
  44. namespace ranges {
  45. template <class _Int>
  46. struct __get_wider_signed {
  47. consteval static auto __call() {
  48. if constexpr (sizeof(_Int) < sizeof(short))
  49. return type_identity<short>{};
  50. else if constexpr (sizeof(_Int) < sizeof(int))
  51. return type_identity<int>{};
  52. else if constexpr (sizeof(_Int) < sizeof(long))
  53. return type_identity<long>{};
  54. else
  55. return type_identity<long long>{};
  56. static_assert(
  57. sizeof(_Int) <= sizeof(long long), "Found integer-like type that is bigger than largest integer like type.");
  58. }
  59. using type = typename decltype(__call())::type;
  60. };
  61. template <class _Start>
  62. using _IotaDiffT =
  63. typename _If< (!integral<_Start> || sizeof(iter_difference_t<_Start>) > sizeof(_Start)),
  64. type_identity<iter_difference_t<_Start>>,
  65. __get_wider_signed<_Start> >::type;
  66. template <class _Iter>
  67. concept __decrementable = incrementable<_Iter> && requires(_Iter __i) {
  68. { --__i } -> same_as<_Iter&>;
  69. { __i-- } -> same_as<_Iter>;
  70. };
  71. template <class _Iter>
  72. concept __advanceable =
  73. __decrementable<_Iter> && totally_ordered<_Iter> &&
  74. requires(_Iter __i, const _Iter __j, const _IotaDiffT<_Iter> __n) {
  75. { __i += __n } -> same_as<_Iter&>;
  76. { __i -= __n } -> same_as<_Iter&>;
  77. _Iter(__j + __n);
  78. _Iter(__n + __j);
  79. _Iter(__j - __n);
  80. { __j - __j } -> convertible_to<_IotaDiffT<_Iter>>;
  81. };
  82. template <class>
  83. struct __iota_iterator_category {};
  84. template <incrementable _Tp>
  85. struct __iota_iterator_category<_Tp> {
  86. using iterator_category = input_iterator_tag;
  87. };
  88. template <weakly_incrementable _Start, semiregular _BoundSentinel = unreachable_sentinel_t>
  89. requires __weakly_equality_comparable_with<_Start, _BoundSentinel> && copyable<_Start>
  90. class iota_view : public view_interface<iota_view<_Start, _BoundSentinel>> {
  91. struct __iterator : public __iota_iterator_category<_Start> {
  92. friend class iota_view;
  93. using iterator_concept =
  94. _If<__advanceable<_Start>,
  95. random_access_iterator_tag,
  96. _If<__decrementable<_Start>,
  97. bidirectional_iterator_tag,
  98. _If<incrementable<_Start>,
  99. forward_iterator_tag,
  100. /*Else*/ input_iterator_tag>>>;
  101. using value_type = _Start;
  102. using difference_type = _IotaDiffT<_Start>;
  103. _Start __value_ = _Start();
  104. _LIBCPP_HIDE_FROM_ABI __iterator()
  105. requires default_initializable<_Start>
  106. = default;
  107. _LIBCPP_HIDE_FROM_ABI constexpr explicit __iterator(_Start __value) : __value_(std::move(__value)) {}
  108. _LIBCPP_HIDE_FROM_ABI constexpr _Start operator*() const noexcept(is_nothrow_copy_constructible_v<_Start>) {
  109. return __value_;
  110. }
  111. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
  112. ++__value_;
  113. return *this;
  114. }
  115. _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++*this; }
  116. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int)
  117. requires incrementable<_Start>
  118. {
  119. auto __tmp = *this;
  120. ++*this;
  121. return __tmp;
  122. }
  123. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--()
  124. requires __decrementable<_Start>
  125. {
  126. --__value_;
  127. return *this;
  128. }
  129. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int)
  130. requires __decrementable<_Start>
  131. {
  132. auto __tmp = *this;
  133. --*this;
  134. return __tmp;
  135. }
  136. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n)
  137. requires __advanceable<_Start>
  138. {
  139. if constexpr (__integer_like<_Start> && !__signed_integer_like<_Start>) {
  140. if (__n >= difference_type(0)) {
  141. __value_ += static_cast<_Start>(__n);
  142. } else {
  143. __value_ -= static_cast<_Start>(-__n);
  144. }
  145. } else {
  146. __value_ += __n;
  147. }
  148. return *this;
  149. }
  150. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n)
  151. requires __advanceable<_Start>
  152. {
  153. if constexpr (__integer_like<_Start> && !__signed_integer_like<_Start>) {
  154. if (__n >= difference_type(0)) {
  155. __value_ -= static_cast<_Start>(__n);
  156. } else {
  157. __value_ += static_cast<_Start>(-__n);
  158. }
  159. } else {
  160. __value_ -= __n;
  161. }
  162. return *this;
  163. }
  164. _LIBCPP_HIDE_FROM_ABI constexpr _Start operator[](difference_type __n) const
  165. requires __advanceable<_Start>
  166. {
  167. return _Start(__value_ + __n);
  168. }
  169. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
  170. requires equality_comparable<_Start>
  171. {
  172. return __x.__value_ == __y.__value_;
  173. }
  174. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
  175. requires totally_ordered<_Start>
  176. {
  177. return __x.__value_ < __y.__value_;
  178. }
  179. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
  180. requires totally_ordered<_Start>
  181. {
  182. return __y < __x;
  183. }
  184. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
  185. requires totally_ordered<_Start>
  186. {
  187. return !(__y < __x);
  188. }
  189. _LIBCPP_HIDE_FROM_ABI 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 friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
  195. requires totally_ordered<_Start> && three_way_comparable<_Start>
  196. {
  197. return __x.__value_ <=> __y.__value_;
  198. }
  199. _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(__iterator __i, difference_type __n)
  200. requires __advanceable<_Start>
  201. {
  202. __i += __n;
  203. return __i;
  204. }
  205. _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, __iterator __i)
  206. requires __advanceable<_Start>
  207. {
  208. return __i + __n;
  209. }
  210. _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(__iterator __i, difference_type __n)
  211. requires __advanceable<_Start>
  212. {
  213. __i -= __n;
  214. return __i;
  215. }
  216. _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
  217. requires __advanceable<_Start>
  218. {
  219. if constexpr (__integer_like<_Start>) {
  220. if constexpr (__signed_integer_like<_Start>) {
  221. return difference_type(difference_type(__x.__value_) - difference_type(__y.__value_));
  222. }
  223. if (__y.__value_ > __x.__value_) {
  224. return difference_type(-difference_type(__y.__value_ - __x.__value_));
  225. }
  226. return difference_type(__x.__value_ - __y.__value_);
  227. }
  228. return __x.__value_ - __y.__value_;
  229. }
  230. };
  231. struct __sentinel {
  232. friend class iota_view;
  233. private:
  234. _BoundSentinel __bound_sentinel_ = _BoundSentinel();
  235. public:
  236. _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
  237. _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(_BoundSentinel __bound_sentinel)
  238. : __bound_sentinel_(std::move(__bound_sentinel)) {}
  239. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __sentinel& __y) {
  240. return __x.__value_ == __y.__bound_sentinel_;
  241. }
  242. _LIBCPP_HIDE_FROM_ABI friend constexpr iter_difference_t<_Start>
  243. operator-(const __iterator& __x, const __sentinel& __y)
  244. requires sized_sentinel_for<_BoundSentinel, _Start>
  245. {
  246. return __x.__value_ - __y.__bound_sentinel_;
  247. }
  248. _LIBCPP_HIDE_FROM_ABI friend constexpr iter_difference_t<_Start>
  249. operator-(const __sentinel& __x, const __iterator& __y)
  250. requires sized_sentinel_for<_BoundSentinel, _Start>
  251. {
  252. return -(__y - __x);
  253. }
  254. };
  255. _Start __value_ = _Start();
  256. _BoundSentinel __bound_sentinel_ = _BoundSentinel();
  257. public:
  258. _LIBCPP_HIDE_FROM_ABI iota_view()
  259. requires default_initializable<_Start>
  260. = default;
  261. _LIBCPP_HIDE_FROM_ABI constexpr explicit iota_view(_Start __value) : __value_(std::move(__value)) {}
  262. _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23
  263. iota_view(type_identity_t<_Start> __value, type_identity_t<_BoundSentinel> __bound_sentinel)
  264. : __value_(std::move(__value)), __bound_sentinel_(std::move(__bound_sentinel)) {
  265. // Validate the precondition if possible.
  266. if constexpr (totally_ordered_with<_Start, _BoundSentinel>) {
  267. _LIBCPP_ASSERT_UNCATEGORIZED(
  268. ranges::less_equal()(__value_, __bound_sentinel_), "Precondition violated: value is greater than bound.");
  269. }
  270. }
  271. _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(__iterator __first, __iterator __last)
  272. requires same_as<_Start, _BoundSentinel>
  273. : iota_view(std::move(__first.__value_), std::move(__last.__value_)) {}
  274. _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(__iterator __first, _BoundSentinel __last)
  275. requires same_as<_BoundSentinel, unreachable_sentinel_t>
  276. : iota_view(std::move(__first.__value_), std::move(__last)) {}
  277. _LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 iota_view(__iterator __first, __sentinel __last)
  278. requires(!same_as<_Start, _BoundSentinel> && !same_as<_BoundSentinel, unreachable_sentinel_t>)
  279. : iota_view(std::move(__first.__value_), std::move(__last.__bound_sentinel_)) {}
  280. _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() const { return __iterator{__value_}; }
  281. _LIBCPP_HIDE_FROM_ABI constexpr auto end() const {
  282. if constexpr (same_as<_BoundSentinel, unreachable_sentinel_t>)
  283. return unreachable_sentinel;
  284. else
  285. return __sentinel{__bound_sentinel_};
  286. }
  287. _LIBCPP_HIDE_FROM_ABI constexpr __iterator end() const
  288. requires same_as<_Start, _BoundSentinel>
  289. {
  290. return __iterator{__bound_sentinel_};
  291. }
  292. _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
  293. requires(same_as<_Start, _BoundSentinel> && __advanceable<_Start>) ||
  294. (integral<_Start> && integral<_BoundSentinel>) || sized_sentinel_for<_BoundSentinel, _Start>
  295. {
  296. if constexpr (__integer_like<_Start> && __integer_like<_BoundSentinel>) {
  297. return (__value_ < 0)
  298. ? ((__bound_sentinel_ < 0)
  299. ? std::__to_unsigned_like(-__value_) - std::__to_unsigned_like(-__bound_sentinel_)
  300. : std::__to_unsigned_like(__bound_sentinel_) + std::__to_unsigned_like(-__value_))
  301. : std::__to_unsigned_like(__bound_sentinel_) - std::__to_unsigned_like(__value_);
  302. } else {
  303. return std::__to_unsigned_like(__bound_sentinel_ - __value_);
  304. }
  305. }
  306. };
  307. template <class _Start, class _BoundSentinel>
  308. requires(!__integer_like<_Start> || !__integer_like<_BoundSentinel> ||
  309. (__signed_integer_like<_Start> == __signed_integer_like<_BoundSentinel>))
  310. iota_view(_Start, _BoundSentinel) -> iota_view<_Start, _BoundSentinel>;
  311. template <class _Start, class _BoundSentinel>
  312. inline constexpr bool enable_borrowed_range<iota_view<_Start, _BoundSentinel>> = true;
  313. namespace views {
  314. namespace __iota {
  315. struct __fn {
  316. template <class _Start>
  317. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Start&& __start) const
  318. noexcept(noexcept(ranges::iota_view(std::forward<_Start>(__start))))
  319. -> decltype(ranges::iota_view(std::forward<_Start>(__start))) {
  320. return ranges::iota_view(std::forward<_Start>(__start));
  321. }
  322. template <class _Start, class _BoundSentinel>
  323. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Start&& __start, _BoundSentinel&& __bound_sentinel) const noexcept(
  324. noexcept(ranges::iota_view(std::forward<_Start>(__start), std::forward<_BoundSentinel>(__bound_sentinel))))
  325. -> decltype(ranges::iota_view(std::forward<_Start>(__start), std::forward<_BoundSentinel>(__bound_sentinel))) {
  326. return ranges::iota_view(std::forward<_Start>(__start), std::forward<_BoundSentinel>(__bound_sentinel));
  327. }
  328. };
  329. } // namespace __iota
  330. inline namespace __cpo {
  331. inline constexpr auto iota = __iota::__fn{};
  332. } // namespace __cpo
  333. } // namespace views
  334. } // namespace ranges
  335. #endif // _LIBCPP_STD_VER >= 20
  336. _LIBCPP_END_NAMESPACE_STD
  337. _LIBCPP_POP_MACROS
  338. #endif // _LIBCPP___RANGES_IOTA_VIEW_H