Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/repeat_view.h
35236 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___RANGES_REPEAT_VIEW_H10#define _LIBCPP___RANGES_REPEAT_VIEW_H1112#include <__assert>13#include <__concepts/constructible.h>14#include <__concepts/same_as.h>15#include <__concepts/semiregular.h>16#include <__config>17#include <__iterator/concepts.h>18#include <__iterator/iterator_traits.h>19#include <__iterator/unreachable_sentinel.h>20#include <__memory/addressof.h>21#include <__ranges/iota_view.h>22#include <__ranges/movable_box.h>23#include <__ranges/view_interface.h>24#include <__type_traits/decay.h>25#include <__type_traits/is_object.h>26#include <__type_traits/make_unsigned.h>27#include <__type_traits/remove_cv.h>28#include <__utility/forward.h>29#include <__utility/in_place.h>30#include <__utility/move.h>31#include <__utility/piecewise_construct.h>32#include <tuple>3334#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)35# pragma GCC system_header36#endif3738_LIBCPP_PUSH_MACROS39#include <__undef_macros>4041_LIBCPP_BEGIN_NAMESPACE_STD4243#if _LIBCPP_STD_VER >= 234445namespace ranges {4647template <class _Tp>48concept __integer_like_with_usable_difference_type =49__signed_integer_like<_Tp> || (__integer_like<_Tp> && weakly_incrementable<_Tp>);5051template <class _Tp>52struct __repeat_view_iterator_difference {53using type = _IotaDiffT<_Tp>;54};5556template <__signed_integer_like _Tp>57struct __repeat_view_iterator_difference<_Tp> {58using type = _Tp;59};6061template <class _Tp>62using __repeat_view_iterator_difference_t = typename __repeat_view_iterator_difference<_Tp>::type;6364namespace views::__drop {65struct __fn;66} // namespace views::__drop6768namespace views::__take {69struct __fn;70} // namespace views::__take7172template <move_constructible _Tp, semiregular _Bound = unreachable_sentinel_t>73requires(is_object_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>> &&74(__integer_like_with_usable_difference_type<_Bound> || same_as<_Bound, unreachable_sentinel_t>))75class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS repeat_view : public view_interface<repeat_view<_Tp, _Bound>> {76friend struct views::__take::__fn;77friend struct views::__drop::__fn;78class __iterator;7980public:81_LIBCPP_HIDE_FROM_ABI repeat_view()82requires default_initializable<_Tp>83= default;8485_LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(const _Tp& __value, _Bound __bound_sentinel = _Bound())86requires copy_constructible<_Tp>87: __value_(in_place, __value), __bound_(__bound_sentinel) {88if constexpr (!same_as<_Bound, unreachable_sentinel_t>)89_LIBCPP_ASSERT_UNCATEGORIZED(__bound_ >= 0, "The value of bound must be greater than or equal to 0");90}9192_LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(_Tp&& __value, _Bound __bound_sentinel = _Bound())93: __value_(in_place, std::move(__value)), __bound_(__bound_sentinel) {94if constexpr (!same_as<_Bound, unreachable_sentinel_t>)95_LIBCPP_ASSERT_UNCATEGORIZED(__bound_ >= 0, "The value of bound must be greater than or equal to 0");96}9798template <class... _TpArgs, class... _BoundArgs>99requires(constructible_from<_Tp, _TpArgs...> && constructible_from<_Bound, _BoundArgs...>)100_LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(101piecewise_construct_t, tuple<_TpArgs...> __value_args, tuple<_BoundArgs...> __bound_args = tuple<>{})102: __value_(in_place, std::make_from_tuple<_Tp>(std::move(__value_args))),103__bound_(std::make_from_tuple<_Bound>(std::move(__bound_args))) {104if constexpr (!same_as<_Bound, unreachable_sentinel_t>)105_LIBCPP_ASSERT_UNCATEGORIZED(106__bound_ >= 0, "The behavior is undefined if Bound is not unreachable_sentinel_t and bound is negative");107}108109_LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() const { return __iterator(std::addressof(*__value_)); }110111_LIBCPP_HIDE_FROM_ABI constexpr __iterator end() const112requires(!same_as<_Bound, unreachable_sentinel_t>)113{114return __iterator(std::addressof(*__value_), __bound_);115}116117_LIBCPP_HIDE_FROM_ABI constexpr unreachable_sentinel_t end() const noexcept { return unreachable_sentinel; }118119_LIBCPP_HIDE_FROM_ABI constexpr auto size() const120requires(!same_as<_Bound, unreachable_sentinel_t>)121{122return std::__to_unsigned_like(__bound_);123}124125private:126_LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Tp> __value_;127_LIBCPP_NO_UNIQUE_ADDRESS _Bound __bound_ = _Bound();128};129130template <class _Tp, class _Bound = unreachable_sentinel_t>131repeat_view(_Tp, _Bound = _Bound()) -> repeat_view<_Tp, _Bound>;132133// [range.repeat.iterator]134template <move_constructible _Tp, semiregular _Bound>135requires(is_object_v<_Tp> && same_as<_Tp, remove_cv_t<_Tp>> &&136(__integer_like_with_usable_difference_type<_Bound> || same_as<_Bound, unreachable_sentinel_t>))137class repeat_view<_Tp, _Bound>::__iterator {138friend class repeat_view;139140using _IndexT = conditional_t<same_as<_Bound, unreachable_sentinel_t>, ptrdiff_t, _Bound>;141142_LIBCPP_HIDE_FROM_ABI constexpr explicit __iterator(const _Tp* __value, _IndexT __bound_sentinel = _IndexT())143: __value_(__value), __current_(__bound_sentinel) {}144145public:146using iterator_concept = random_access_iterator_tag;147using iterator_category = random_access_iterator_tag;148using value_type = _Tp;149using difference_type = __repeat_view_iterator_difference_t<_IndexT>;150151_LIBCPP_HIDE_FROM_ABI __iterator() = default;152153_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator*() const noexcept { return *__value_; }154155_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {156++__current_;157return *this;158}159160_LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) {161auto __tmp = *this;162++*this;163return __tmp;164}165166_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--() {167if constexpr (!same_as<_Bound, unreachable_sentinel_t>)168_LIBCPP_ASSERT_UNCATEGORIZED(__current_ > 0, "The value of bound must be greater than or equal to 0");169--__current_;170return *this;171}172173_LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int) {174auto __tmp = *this;175--*this;176return __tmp;177}178179_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n) {180if constexpr (!same_as<_Bound, unreachable_sentinel_t>)181_LIBCPP_ASSERT_UNCATEGORIZED(__current_ + __n >= 0, "The value of bound must be greater than or equal to 0");182__current_ += __n;183return *this;184}185186_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n) {187if constexpr (!same_as<_Bound, unreachable_sentinel_t>)188_LIBCPP_ASSERT_UNCATEGORIZED(__current_ - __n >= 0, "The value of bound must be greater than or equal to 0");189__current_ -= __n;190return *this;191}192193_LIBCPP_HIDE_FROM_ABI constexpr const _Tp& operator[](difference_type __n) const noexcept { return *(*this + __n); }194195_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {196return __x.__current_ == __y.__current_;197}198199_LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y) {200return __x.__current_ <=> __y.__current_;201}202203_LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(__iterator __i, difference_type __n) {204__i += __n;205return __i;206}207208_LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, __iterator __i) {209__i += __n;210return __i;211}212213_LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(__iterator __i, difference_type __n) {214__i -= __n;215return __i;216}217218_LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y) {219return static_cast<difference_type>(__x.__current_) - static_cast<difference_type>(__y.__current_);220}221222private:223const _Tp* __value_ = nullptr;224_IndexT __current_ = _IndexT();225};226227// clang-format off228namespace views {229namespace __repeat {230struct __fn {231template <class _Tp>232[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Tp&& __value)233noexcept(noexcept(ranges::repeat_view<decay_t<_Tp>>(std::forward<_Tp>(__value))))234-> decltype( ranges::repeat_view<decay_t<_Tp>>(std::forward<_Tp>(__value)))235{ return ranges::repeat_view<decay_t<_Tp>>(std::forward<_Tp>(__value)); }236237template <class _Tp, class _Bound>238[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Tp&& __value, _Bound&& __bound_sentinel)239noexcept(noexcept(ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel))))240-> decltype( ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel)))241{ return ranges::repeat_view(std::forward<_Tp>(__value), std::forward<_Bound>(__bound_sentinel)); }242};243} // namespace __repeat244// clang-format on245246inline namespace __cpo {247inline constexpr auto repeat = __repeat::__fn{};248} // namespace __cpo249} // namespace views250251template <class _Tp>252inline constexpr bool __is_repeat_specialization = false;253254template <class _Tp, class _Bound>255inline constexpr bool __is_repeat_specialization<repeat_view<_Tp, _Bound>> = true;256257} // namespace ranges258259#endif // _LIBCPP_STD_VER >= 23260261_LIBCPP_END_NAMESPACE_STD262263_LIBCPP_POP_MACROS264265#endif // _LIBCPP___RANGES_REPEAT_VIEW_H266267268