iota_view.h 13 KB

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