Debian Codegraph @

3rd_party/range-v3/range/v3/view/delimit.hpp

raw
1 /// \file
2 // Range v3 library
3 //
4 // Copyright Eric Niebler 2013-present
5 //
6 // Use, modification and distribution is subject to the
7 // Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Project home: https://github.com/ericniebler/range-v3
12 //
13
14 #ifndef RANGES_V3_VIEW_DELIMIT_HPP
15 #define RANGES_V3_VIEW_DELIMIT_HPP
16
17 #include <meta/meta.hpp>
18
19 #include <range/v3/range_fwd.hpp>
20
21 #include <range/v3/functional/bind_back.hpp>
22 #include <range/v3/iterator/concepts.hpp>
23 #include <range/v3/iterator/unreachable_sentinel.hpp>
24 #include <range/v3/range/concepts.hpp>
25 #include <range/v3/utility/static_const.hpp>
26 #include <range/v3/view/adaptor.hpp>
27 #include <range/v3/view/all.hpp>
28 #include <range/v3/view/subrange.hpp>
29 #include <range/v3/view/view.hpp>
30
31 #include <range/v3/detail/prologue.hpp>
32
33 namespace ranges
34 {
35 /// \addtogroup group-views
36 /// @{
37 template<typename Rng, typename Val>
38 struct delimit_view
39 : view_adaptor<delimit_view<Rng, Val>, Rng,
40 is_finite<Rng>::value ? finite : unknown>
41 {
42 private:
43 friend range_access;
44 Val value_;
45
46 struct sentinel_adaptor : adaptor_base
47 {
48 sentinel_adaptor() = default;
49 sentinel_adaptor(Val value)
50 : value_(std::move(value))
51 {}
52 template<class I, class S>
53 bool empty(I const & it, S const & last) const
54 {
55 return it == last || *it == value_;
56 }
57 Val value_;
58 };
59
60 sentinel_adaptor end_adaptor() const
61 {
62 return {value_};
63 }
64
65 public:
66 delimit_view() = default;
67 constexpr delimit_view(Rng rng, Val value)
68 : delimit_view::view_adaptor{std::move(rng)}
69 , value_(std::move(value))
70 {}
71 };
72
73 // the begin iterator will be an iterator into the underlying view (conditionally
74 // borrowed) and the end iterator owns the value to be compared against (borrowed)
75 template<typename Rng, typename Val>
76 RANGES_INLINE_VAR constexpr bool enable_borrowed_range<delimit_view<Rng, Val>> = //
77 enable_borrowed_range<Rng>;
78
79 #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
80 template(typename Rng, typename Val)(
81 requires copy_constructible<Val>)
82 delimit_view(Rng &&, Val)
83 -> delimit_view<views::all_t<Rng>, Val>;
84 #endif
85
86 namespace views
87 {
88 struct delimit_base_fn
89 {
90 template(typename I_, typename Val, typename I = detail::decay_t<I_>)(
91 requires (!range<I_>) AND convertible_to<I_, I> AND input_iterator<I> AND
92 semiregular<Val> AND
93 equality_comparable_with<Val, iter_reference_t<I>>)
94 constexpr auto operator()(I_ && begin_, Val value) const
95 -> delimit_view<subrange<I, unreachable_sentinel_t>, Val>
96 {
97 return {{static_cast<I_ &&>(begin_), {}}, std::move(value)};
98 }
99
100 template(typename Rng, typename Val)(
101 requires viewable_range<Rng> AND input_range<Rng> AND semiregular<
102 Val> AND equality_comparable_with<Val, range_reference_t<Rng>>)
103 constexpr auto operator()(Rng && rng, Val value) const //
104 -> delimit_view<all_t<Rng>, Val>
105 {
106 return {all(static_cast<Rng &&>(rng)), std::move(value)};
107 }
108 };
109
110 struct delimit_fn : delimit_base_fn
111 {
112 using delimit_base_fn::operator();
113
114 template<typename Val>
115 constexpr auto operator()(Val value) const
116 {
117 return make_view_closure(bind_back(delimit_base_fn{}, std::move(value)));
118 }
119 };
120
121 /// \relates delimit_fn
122 /// \ingroup group-views
123 RANGES_INLINE_VARIABLE(delimit_fn, delimit)
124 } // namespace views
125 /// @}
126 } // namespace ranges
127
128 #include <range/v3/detail/epilogue.hpp>
129 #include <range/v3/detail/satisfy_boost_range.hpp>
130 RANGES_SATISFY_BOOST_RANGE(::ranges::delimit_view)
131
132 #endif