repeat_view.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_REPEAT_VIEW_H
  10. #define _LIBCPP___RANGES_REPEAT_VIEW_H
  11. #include <__concepts/constructible.h>
  12. #include <__concepts/same_as.h>
  13. #include <__concepts/semiregular.h>
  14. #include <__config>
  15. #include <__iterator/concepts.h>
  16. #include <__iterator/iterator_traits.h>
  17. #include <__iterator/unreachable_sentinel.h>
  18. #include <__memory/addressof.h>
  19. #include <__ranges/iota_view.h>
  20. #include <__ranges/movable_box.h>
  21. #include <__ranges/view_interface.h>
  22. #include <__type_traits/is_object.h>
  23. #include <__type_traits/make_unsigned.h>
  24. #include <__type_traits/remove_cv.h>
  25. #include <__utility/forward.h>
  26. #include <__utility/in_place.h>
  27. #include <__utility/move.h>
  28. #include <__utility/piecewise_construct.h>
  29. #include <tuple>
  30. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  31. # pragma GCC system_header
  32. #endif
  33. _LIBCPP_BEGIN_NAMESPACE_STD
  34. #if _LIBCPP_STD_VER >= 23
  35. namespace ranges {
  36. template <class _Tp>
  37. concept __integer_like_with_usable_difference_type =
  38. __signed_integer_like<_Tp> || (__integer_like<_Tp> && weakly_incrementable<_Tp>);
  39. template <class _Tp>
  40. struct __repeat_view_iterator_difference {
  41. using type = _IotaDiffT<_Tp>;
  42. };
  43. template <__signed_integer_like _Tp>
  44. struct __repeat_view_iterator_difference<_Tp> {
  45. using type = _Tp;
  46. };
  47. template <class _Tp>
  48. using __repeat_view_iterator_difference_t = typename __repeat_view_iterator_difference<_Tp>::type;
  49. namespace views::__drop {
  50. struct __fn;
  51. } // namespace views::__drop
  52. namespace views::__take {
  53. struct __fn;
  54. } // namespace views::__take
  55. template <move_constructible _Tp, semiregular _Bound = unreachable_sentinel_t>
  56. requires(is_object_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>> &&
  57. (__integer_like_with_usable_difference_type<_Bound> || same_as<_Bound, unreachable_sentinel_t>))
  58. class repeat_view : public view_interface<repeat_view<_Tp, _Bound>> {
  59. friend struct views::__take::__fn;
  60. friend struct views::__drop::__fn;
  61. class __iterator;
  62. public:
  63. _LIBCPP_HIDE_FROM_ABI repeat_view()
  64. requires default_initializable<_Tp>
  65. = default;
  66. _LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(const _Tp& __value, _Bound __bound_sentinel = _Bound())
  67. requires copy_constructible<_Tp>
  68. : __value_(in_place, __value), __bound_(__bound_sentinel) {
  69. if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
  70. _LIBCPP_ASSERT_UNCATEGORIZED(__bound_ >= 0, "The value of bound must be greater than or equal to 0");
  71. }
  72. _LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(_Tp&& __value, _Bound __bound_sentinel = _Bound())
  73. : __value_(in_place, std::move(__value)), __bound_(__bound_sentinel) {
  74. if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
  75. _LIBCPP_ASSERT_UNCATEGORIZED(__bound_ >= 0, "The value of bound must be greater than or equal to 0");
  76. }
  77. template <class... _TpArgs, class... _BoundArgs>
  78. requires(constructible_from<_Tp, _TpArgs...> && constructible_from<_Bound, _BoundArgs...>)
  79. _LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(
  80. piecewise_construct_t, tuple<_TpArgs...> __value_args, tuple<_BoundArgs...> __bound_args = tuple<>{})
  81. : __value_(in_place, std::make_from_tuple<_Tp>(std::move(__value_args))),
  82. __bound_(std::make_from_tuple<_Bound>(std::move(__bound_args))) {
  83. if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
  84. _LIBCPP_ASSERT_UNCATEGORIZED(
  85. __bound_ >= 0, "The behavior is undefined if Bound is not unreachable_sentinel_t and bound is negative");
  86. }
  87. _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() const { return __iterator(std::addressof(*__value_)); }
  88. _LIBCPP_HIDE_FROM_ABI constexpr __iterator end() const
  89. requires(!same_as<_Bound, unreachable_sentinel_t>)
  90. {
  91. return __iterator(std::addressof(*__value_), __bound_);
  92. }
  93. _LIBCPP_HIDE_FROM_ABI constexpr unreachable_sentinel_t end() const noexcept { return unreachable_sentinel; }
  94. _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
  95. requires(!same_as<_Bound, unreachable_sentinel_t>)
  96. {
  97. return std::__to_unsigned_like(__bound_);
  98. }
  99. private:
  100. __movable_box<_Tp> __value_;
  101. _LIBCPP_NO_UNIQUE_ADDRESS _Bound __bound_ = _Bound();
  102. };
  103. template <class _Tp, class _Bound>
  104. repeat_view(_Tp, _Bound) -> repeat_view<_Tp, _Bound>;
  105. // [range.repeat.iterator]
  106. template <move_constructible _Tp, semiregular _Bound>
  107. requires(is_object_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>> &&
  108. (__integer_like_with_usable_difference_type<_Bound> || same_as<_Bound, unreachable_sentinel_t>))
  109. class repeat_view<_Tp, _Bound>::__iterator {
  110. friend class repeat_view;
  111. using _IndexT = conditional_t<same_as<_Bound, unreachable_sentinel_t>, ptrdiff_t, _Bound>;
  112. _LIBCPP_HIDE_FROM_ABI constexpr explicit __iterator(const _Tp* __value, _IndexT __bound_sentinel = _IndexT())
  113. : __value_(__value), __current_(__bound_sentinel) {}
  114. public:
  115. using iterator_concept = random_access_iterator_tag;
  116. using iterator_category = random_access_iterator_tag;
  117. using value_type = _Tp;
  118. using difference_type = __repeat_view_iterator_difference_t<_IndexT>;
  119. _LIBCPP_HIDE_FROM_ABI __iterator() = default;
  120. _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const noexcept { return *__value_; }
  121. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
  122. ++__current_;
  123. return *this;
  124. }
  125. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) {
  126. auto __tmp = *this;
  127. ++*this;
  128. return __tmp;
  129. }
  130. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--() {
  131. if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
  132. _LIBCPP_ASSERT_UNCATEGORIZED(__current_ > 0, "The value of bound must be greater than or equal to 0");
  133. --__current_;
  134. return *this;
  135. }
  136. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int) {
  137. auto __tmp = *this;
  138. --*this;
  139. return __tmp;
  140. }
  141. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n) {
  142. if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
  143. _LIBCPP_ASSERT_UNCATEGORIZED(__current_ + __n >= 0, "The value of bound must be greater than or equal to 0");
  144. __current_ += __n;
  145. return *this;
  146. }
  147. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n) {
  148. if constexpr (!same_as<_Bound, unreachable_sentinel_t>)
  149. _LIBCPP_ASSERT_UNCATEGORIZED(__current_ - __n >= 0, "The value of bound must be greater than or equal to 0");
  150. __current_ -= __n;
  151. return *this;
  152. }
  153. _LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](difference_type __n) const noexcept { return *(*this + __n); }
  154. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {
  155. return __x.__current_ == __y.__current_;
  156. }
  157. _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y) {
  158. return __x.__current_ <=> __y.__current_;
  159. }
  160. _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(__iterator __i, difference_type __n) {
  161. __i += __n;
  162. return __i;
  163. }
  164. _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, __iterator __i) {
  165. __i += __n;
  166. return __i;
  167. }
  168. _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(__iterator __i, difference_type __n) {
  169. __i -= __n;
  170. return __i;
  171. }
  172. _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y) {
  173. return static_cast<difference_type>(__x.__current_) - static_cast<difference_type>(__y.__current_);
  174. }
  175. private:
  176. const _Tp* __value_ = nullptr;
  177. _IndexT __current_ = _IndexT();
  178. };
  179. // clang-format off
  180. namespace views {
  181. namespace __repeat {
  182. struct __fn {
  183. template <class _Tp>
  184. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __value) const
  185. noexcept(noexcept(ranges::repeat_view(std::forward<_Tp>(__value))))
  186. -> decltype( ranges::repeat_view(std::forward<_Tp>(__value)))
  187. { return ranges::repeat_view(std::forward<_Tp>(__value)); }
  188. template <class _Tp, class _Bound>
  189. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __value, _Bound&& __bound_sentinel) const
  190. noexcept(noexcept(ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel))))
  191. -> decltype( ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel)))
  192. { return ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel)); }
  193. };
  194. } // namespace __repeat
  195. // clang-format on
  196. inline namespace __cpo {
  197. inline constexpr auto repeat = __repeat::__fn{};
  198. } // namespace __cpo
  199. } // namespace views
  200. template <class _Tp>
  201. inline constexpr bool __is_repeat_specialization = false;
  202. template <class _Tp, class _Bound>
  203. inline constexpr bool __is_repeat_specialization<repeat_view<_Tp, _Bound>> = true;
  204. } // namespace ranges
  205. #endif // _LIBCPP_STD_VER >= 23
  206. _LIBCPP_END_NAMESPACE_STD
  207. #endif // _LIBCPP___RANGES_REPEAT_VIEW_H