|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /// \file
- // Range v3 library
- //
- // Copyright Eric Niebler 2014-present
- //
- // Use, modification and distribution is subject to the
- // Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- //
- // Project home: https://github.com/ericniebler/range-v3
- //
- #ifndef RANGES_V3_VIEW_COMMON_HPP
- #define RANGES_V3_VIEW_COMMON_HPP
-
- #include <type_traits>
-
- #include <range/v3/range_fwd.hpp>
-
- #include <range/v3/iterator/common_iterator.hpp>
- #include <range/v3/iterator/concepts.hpp>
- #include <range/v3/range/access.hpp>
- #include <range/v3/range/concepts.hpp>
- #include <range/v3/range/primitives.hpp>
- #include <range/v3/range/traits.hpp>
- #include <range/v3/utility/static_const.hpp>
- #include <range/v3/view/all.hpp>
- #include <range/v3/view/interface.hpp>
- #include <range/v3/view/view.hpp>
-
- namespace ranges
- {
- /// \addtogroup group-views
- /// @{
-
- namespace detail
- {
- /// \cond
- // clang-format off
- CPP_def
- (
- template(typename R)
- concept random_access_and_sized_range,
- random_access_range<R> && sized_range<R>
- );
- // clang-format on
-
- template<typename R>
- using common_view_iterator_t =
- meta::if_c<random_access_and_sized_range<R>, iterator_t<R>,
- common_iterator_t<iterator_t<R>, sentinel_t<R>>>;
-
- template<typename Rng>
- struct is_common_range : meta::bool_<common_range<Rng>>
- {};
- /// \endcond
- } // namespace detail
-
- template<typename Rng, bool = detail::is_common_range<Rng>::value>
- struct common_view : view_interface<common_view<Rng>, range_cardinality<Rng>::value>
- {
- private:
- CPP_assert(view_<Rng>);
- CPP_assert(!(common_range<Rng> && view_<Rng>));
- Rng rng_;
-
- sentinel_t<Rng> end_(std::false_type)
- {
- return ranges::end(rng_);
- }
- iterator_t<Rng> end_(std::true_type)
- {
- return ranges::begin(rng_) + ranges::distance(rng_);
- }
- template<bool Const = true>
- auto end_(std::false_type) const
- -> CPP_ret(sentinel_t<meta::const_if_c<Const, Rng>>)( //
- requires Const && range<meta::const_if_c<Const, Rng>>)
- {
- return ranges::end(rng_);
- }
- template<bool Const = true>
- auto end_(std::true_type) const
- -> CPP_ret(iterator_t<meta::const_if_c<Const, Rng>>)( //
- requires Const && range<meta::const_if_c<Const, Rng>>)
- {
- return ranges::begin(rng_) + ranges::distance(rng_);
- }
-
- public:
- common_view() = default;
- explicit common_view(Rng rng)
- : rng_(detail::move(rng))
- {}
- Rng base() const
- {
- return rng_;
- }
-
- detail::common_view_iterator_t<Rng> begin()
- {
- return detail::common_view_iterator_t<Rng>{ranges::begin(rng_)};
- }
- detail::common_view_iterator_t<Rng> end()
- {
- return detail::common_view_iterator_t<Rng>{
- end_(meta::bool_<detail::random_access_and_sized_range<Rng>>{})};
- }
- CPP_member
- auto CPP_fun(size)()(requires sized_range<Rng>)
- {
- return ranges::size(rng_);
- }
-
- template<bool Const = true>
- auto begin() const
- -> CPP_ret(detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>)( //
- requires range<meta::const_if_c<Const, Rng>>)
- {
- return detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>{
- ranges::begin(rng_)};
- }
- template<bool Const = true>
- auto end() const
- -> CPP_ret(detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>)( //
- requires range<meta::const_if_c<Const, Rng>>)
- {
- return detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>{
- end_(meta::bool_<detail::random_access_and_sized_range<
- meta::const_if_c<Const, Rng>>>{})};
- }
- CPP_member
- auto CPP_fun(size)()(const requires sized_range<Rng const>)
- {
- return ranges::size(rng_);
- }
- };
-
- #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
- CPP_template(typename Rng)(requires !common_range<Rng>) common_view(Rng &&)
- ->common_view<views::all_t<Rng>>;
- #endif
-
- template<typename Rng>
- struct common_view<Rng, true> : identity_adaptor<Rng>
- {
- CPP_assert(common_range<Rng>);
- using identity_adaptor<Rng>::identity_adaptor;
- };
-
- namespace views
- {
- struct cpp20_common_fn
- {
- template<typename Rng>
- auto operator()(Rng && rng) const -> CPP_ret(common_view<all_t<Rng>>)( //
- requires viewable_range<Rng> && (!common_range<Rng>))
- {
- return common_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
- }
- };
-
- struct common_fn
- {
- template<typename Rng>
- auto operator()(Rng && rng) const -> CPP_ret(common_view<all_t<Rng>>)( //
- requires viewable_range<Rng>)
- {
- return common_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
- }
- };
-
- /// \relates common_fn
- /// \ingroup group-views
- RANGES_INLINE_VARIABLE(view<common_fn>, common)
- } // namespace views
- /// @}
-
- /// \cond
- template<typename Rng>
- using bounded_view RANGES_DEPRECATED(
- "The name bounded_view is deprecated. "
- "Please use common_view instead.") = common_view<Rng>;
- /// \endcond
-
- namespace views
- {
- /// \cond
- namespace
- {
- RANGES_DEPRECATED(
- "The name views::bounded is deprecated. "
- "Please use views::common instead.")
- RANGES_INLINE_VAR constexpr auto & bounded = common;
- } // namespace
-
- template<typename Rng>
- using bounded_t RANGES_DEPRECATED("The name views::bounded_t is deprecated.") =
- decltype(common(std::declval<Rng>()));
- /// \endcond
- } // namespace views
-
- namespace cpp20
- {
- namespace views
- {
- RANGES_INLINE_VARIABLE(ranges::views::view<ranges::views::cpp20_common_fn>,
- common)
- }
- CPP_template(typename Rng)( //
- requires view_<Rng> && (!common_range<Rng>)) //
- using common_view = ranges::common_view<Rng>;
- } // namespace cpp20
- } // namespace ranges
-
- #include <range/v3/detail/satisfy_boost_range.hpp>
- RANGES_SATISFY_BOOST_RANGE(::ranges::common_view)
-
- #endif
|