File indexing completed on 2025-01-18 10:09:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef RANGES_V3_VIEW_COMMON_HPP
0014 #define RANGES_V3_VIEW_COMMON_HPP
0015
0016 #include <type_traits>
0017
0018 #include <range/v3/range_fwd.hpp>
0019
0020 #include <range/v3/iterator/common_iterator.hpp>
0021 #include <range/v3/iterator/concepts.hpp>
0022 #include <range/v3/range/access.hpp>
0023 #include <range/v3/range/concepts.hpp>
0024 #include <range/v3/range/primitives.hpp>
0025 #include <range/v3/range/traits.hpp>
0026 #include <range/v3/utility/static_const.hpp>
0027 #include <range/v3/view/all.hpp>
0028 #include <range/v3/view/interface.hpp>
0029 #include <range/v3/view/view.hpp>
0030
0031 #include <range/v3/detail/prologue.hpp>
0032
0033 namespace ranges
0034 {
0035
0036
0037
0038
0039 namespace detail
0040 {
0041
0042
0043
0044 template<typename R>
0045 CPP_concept random_access_and_sized_range =
0046 random_access_range<R> && sized_range<R>;
0047
0048
0049 template<typename R>
0050 using common_view_iterator_t =
0051 meta::if_c<random_access_and_sized_range<R>, iterator_t<R>,
0052 common_iterator_t<iterator_t<R>, sentinel_t<R>>>;
0053
0054 template<typename Rng>
0055 struct is_common_range : meta::bool_<common_range<Rng>>
0056 {};
0057 }
0058
0059
0060 template<typename Rng, bool = detail::is_common_range<Rng>::value>
0061 struct common_view : view_interface<common_view<Rng>, range_cardinality<Rng>::value>
0062 {
0063 private:
0064 CPP_assert(view_<Rng>);
0065 CPP_assert(!(common_range<Rng> && view_<Rng>));
0066 Rng rng_;
0067
0068 sentinel_t<Rng> end_(std::false_type)
0069 {
0070 return ranges::end(rng_);
0071 }
0072 iterator_t<Rng> end_(std::true_type)
0073 {
0074 return ranges::begin(rng_) + ranges::distance(rng_);
0075 }
0076 template(bool Const = true)(
0077 requires Const AND range<meta::const_if_c<Const, Rng>>)
0078 sentinel_t<meta::const_if_c<Const, Rng>> end_(std::false_type) const
0079 {
0080 return ranges::end(rng_);
0081 }
0082 template(bool Const = true)(
0083 requires Const AND range<meta::const_if_c<Const, Rng>>)
0084 iterator_t<meta::const_if_c<Const, Rng>> end_(std::true_type) const
0085 {
0086 return ranges::begin(rng_) + ranges::distance(rng_);
0087 }
0088
0089 public:
0090 common_view() = default;
0091 explicit common_view(Rng rng)
0092 : rng_(detail::move(rng))
0093 {}
0094 Rng base() const
0095 {
0096 return rng_;
0097 }
0098
0099 detail::common_view_iterator_t<Rng> begin()
0100 {
0101 return detail::common_view_iterator_t<Rng>{ranges::begin(rng_)};
0102 }
0103 detail::common_view_iterator_t<Rng> end()
0104 {
0105 return detail::common_view_iterator_t<Rng>{
0106 end_(meta::bool_<detail::random_access_and_sized_range<Rng>>{})};
0107 }
0108 CPP_auto_member
0109 auto CPP_fun(size)()(
0110 requires sized_range<Rng>)
0111 {
0112 return ranges::size(rng_);
0113 }
0114
0115 template(bool Const = true)(
0116 requires range<meta::const_if_c<Const, Rng>>)
0117 auto begin() const
0118 -> detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>
0119 {
0120 return detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>{
0121 ranges::begin(rng_)};
0122 }
0123 template(bool Const = true)(
0124 requires range<meta::const_if_c<Const, Rng>>)
0125 auto end() const
0126 -> detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>
0127 {
0128 return detail::common_view_iterator_t<meta::const_if_c<Const, Rng>>{
0129 end_(meta::bool_<detail::random_access_and_sized_range<
0130 meta::const_if_c<Const, Rng>>>{})};
0131 }
0132 CPP_auto_member
0133 auto CPP_fun(size)()(const
0134 requires sized_range<Rng const>)
0135 {
0136 return ranges::size(rng_);
0137 }
0138 };
0139
0140 template<typename Rng, bool B>
0141 RANGES_INLINE_VAR constexpr bool enable_borrowed_range<common_view<Rng, B>> =
0142 enable_borrowed_range<Rng>;
0143
0144 #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
0145 template(typename Rng)(
0146 requires (!common_range<Rng>))
0147 common_view(Rng &&)
0148 ->common_view<views::all_t<Rng>>;
0149 #endif
0150
0151 template<typename Rng>
0152 struct common_view<Rng, true> : identity_adaptor<Rng>
0153 {
0154 CPP_assert(common_range<Rng>);
0155 using identity_adaptor<Rng>::identity_adaptor;
0156 };
0157
0158 namespace views
0159 {
0160 struct cpp20_common_fn
0161 {
0162 template(typename Rng)(
0163 requires viewable_range<Rng> AND common_range<Rng>)
0164 all_t<Rng> operator()(Rng && rng) const
0165 {
0166 return all(static_cast<Rng &&>(rng));
0167 }
0168
0169 template(typename Rng)(
0170 requires viewable_range<Rng> AND (!common_range<Rng>))
0171 common_view<all_t<Rng>> operator()(Rng && rng) const
0172 {
0173 return common_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
0174 }
0175 };
0176
0177 struct common_fn
0178 {
0179 template(typename Rng)(
0180 requires viewable_range<Rng>)
0181 common_view<all_t<Rng>> operator()(Rng && rng) const
0182 {
0183 return common_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
0184 }
0185 };
0186
0187
0188
0189 RANGES_INLINE_VARIABLE(view_closure<common_fn>, common)
0190 }
0191
0192
0193
0194 template<typename Rng>
0195 using bounded_view RANGES_DEPRECATED(
0196 "The name bounded_view is deprecated. "
0197 "Please use common_view instead.") = common_view<Rng>;
0198
0199
0200 namespace views
0201 {
0202
0203 namespace
0204 {
0205 RANGES_DEPRECATED(
0206 "The name views::bounded is deprecated. "
0207 "Please use views::common instead.")
0208 RANGES_INLINE_VAR constexpr auto & bounded = common;
0209 }
0210
0211 template<typename Rng>
0212 using bounded_t RANGES_DEPRECATED("The name views::bounded_t is deprecated.") =
0213 decltype(common(std::declval<Rng>()));
0214
0215 }
0216
0217 namespace cpp20
0218 {
0219 namespace views
0220 {
0221 RANGES_INLINE_VARIABLE(
0222 ranges::views::view_closure<ranges::views::cpp20_common_fn>, common)
0223 }
0224 template(typename Rng)(
0225 requires view_<Rng> && (!common_range<Rng>))
0226 using common_view = ranges::common_view<Rng>;
0227 }
0228 }
0229
0230 #include <range/v3/detail/epilogue.hpp>
0231
0232 #include <range/v3/detail/satisfy_boost_range.hpp>
0233 RANGES_SATISFY_BOOST_RANGE(::ranges::common_view)
0234
0235 #endif