SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
enforce_random_access.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <iterator>
13#include <ranges>
14#include <type_traits>
15
19
20namespace seqan3::detail
21{
22
23// ============================================================================
24// view_pseudo_random_access
25// ============================================================================
26
38template <std::ranges::view urng_t>
40class view_enforce_random_access : public std::ranges::view_interface<view_enforce_random_access<urng_t>>
41{
42private:
43 // Iterator declaration.
44 template <typename underlying_iter_t>
45 class basic_iterator;
46
47public:
51 constexpr view_enforce_random_access() = default;
52 constexpr view_enforce_random_access(view_enforce_random_access const &) = default;
53 constexpr view_enforce_random_access(view_enforce_random_access &&) = default;
54 constexpr view_enforce_random_access & operator=(view_enforce_random_access const &) = default;
55 constexpr view_enforce_random_access & operator=(view_enforce_random_access &&) = default;
56 ~view_enforce_random_access() = default;
57
59 explicit constexpr view_enforce_random_access(urng_t && range) : urng{std::move(range)}
60 {}
61
63 template <typename viewable_rng_t>
64 requires (!std::same_as<std::remove_cvref_t<viewable_rng_t>, view_enforce_random_access>)
65 && std::ranges::viewable_range<viewable_rng_t>
66 && std::constructible_from<urng_t, std::ranges::ref_view<std::remove_reference_t<viewable_rng_t>>>
67 explicit constexpr view_enforce_random_access(viewable_rng_t && range) :
68 view_enforce_random_access{std::views::all(std::forward<viewable_rng_t>(range))}
69 {}
71
76 constexpr auto begin() noexcept
77 {
78 return basic_iterator<decltype(std::ranges::begin(urng))>{std::ranges::begin(urng)};
79 }
80
82 constexpr auto begin() const noexcept
83 requires const_iterable_range<urng_t>
84 {
85 return basic_iterator<decltype(std::ranges::begin(urng))>{std::ranges::begin(urng)};
86 }
87
96 constexpr auto end() noexcept
97 {
98 if constexpr (std::ranges::common_range<urng_t>)
99 return basic_iterator<decltype(std::ranges::end(urng))>{std::ranges::end(urng)};
100 else
101 return urng.end();
102 }
103
105 constexpr auto end() const noexcept
106 requires const_iterable_range<urng_t>
107 {
108 if constexpr (std::ranges::common_range<urng_t>)
109 return basic_iterator<decltype(std::ranges::end(urng))>{std::ranges::end(urng)};
110 else
111 return std::ranges::cend(urng);
112 }
114
115 urng_t urng;
116};
117
126template <std::ranges::view urng_t>
128template <typename underlying_iter_t>
129class view_enforce_random_access<urng_t>::basic_iterator :
130 public inherited_iterator_base<basic_iterator<underlying_iter_t>, underlying_iter_t>
131{
132private:
134 using base_t = inherited_iterator_base<basic_iterator<underlying_iter_t>, underlying_iter_t>;
135
136public:
138 using iterator_category = std::random_access_iterator_tag;
140 using iterator_concept = iterator_category;
141
145 // Importing base's constructors.
146 using base_t::base_t;
148 constexpr basic_iterator() = default;
150 constexpr basic_iterator(basic_iterator const &) = default;
152 constexpr basic_iterator(basic_iterator &&) = default;
154 constexpr basic_iterator & operator=(basic_iterator const &) = default;
156 constexpr basic_iterator & operator=(basic_iterator &&) = default;
158 ~basic_iterator() = default;
160
165 // Importing base's equality operators
166 using base_t::operator==;
167 using base_t::operator!=;
169 friend constexpr bool operator==(basic_iterator const & lhs, std::ranges::sentinel_t<urng_t> const & rhs)
170 noexcept(noexcept(std::declval<underlying_iter_t const &>()
171 == std::declval<std::ranges::sentinel_t<urng_t> const &>()))
172 {
173 return lhs.base() == rhs;
174 }
175
177 friend constexpr bool operator==(std::ranges::sentinel_t<urng_t> const & lhs, basic_iterator const & rhs)
178 noexcept(noexcept(std::declval<underlying_iter_t const &>()
179 == std::declval<std::ranges::sentinel_t<urng_t> const &>()))
180 {
181 return rhs == lhs;
182 }
183
185 friend constexpr bool operator!=(basic_iterator const & lhs, std::ranges::sentinel_t<urng_t> const & rhs)
186 noexcept(noexcept(std::declval<underlying_iter_t const &>()
187 != std::declval<std::ranges::sentinel_t<urng_t> const &>()))
188 {
189 return !(lhs == rhs);
190 }
191
193 friend constexpr bool operator!=(std::ranges::sentinel_t<urng_t> const & lhs, basic_iterator const & rhs)
194 noexcept(noexcept(std::declval<underlying_iter_t const &>()
195 != std::declval<std::ranges::sentinel_t<urng_t> const &>()))
196 {
197 return rhs != lhs;
198 }
200
204 // Import operator from base.
205 using base_t::operator-;
206
208 constexpr typename base_t::difference_type operator-(std::ranges::sentinel_t<urng_t> const & rhs) const
209 noexcept(noexcept(std::declval<underlying_iter_t const &>()
210 - std::declval<std::ranges::sentinel_t<urng_t> const &>()))
211 requires std::sized_sentinel_for<std::ranges::sentinel_t<urng_t>, underlying_iter_t>
212 {
213 return this->base() - rhs;
214 }
215
217 constexpr friend typename base_t::difference_type operator-(std::ranges::sentinel_t<urng_t> const & lhs,
218 basic_iterator const & rhs)
219 noexcept(noexcept(std::declval<std::ranges::sentinel_t<urng_t> const &>()
220 - std::declval<underlying_iter_t const &>()))
221 requires std::sized_sentinel_for<std::ranges::sentinel_t<urng_t>, underlying_iter_t>
222 {
223 return lhs - rhs.base();
224 }
226};
227
233template <std::ranges::viewable_range rng_t>
234view_enforce_random_access(rng_t &&) -> view_enforce_random_access<std::views::all_t<rng_t>>;
236
237// ============================================================================
238// pseudo_random_access_fn (adaptor definition)
239// ============================================================================
240
242struct pseudo_random_access_fn : public adaptor_base<pseudo_random_access_fn>
243{
244private:
246 using base_t = adaptor_base<pseudo_random_access_fn>;
247
248public:
250 using base_t::base_t;
251
252private:
254 friend base_t;
255
259 template <std::ranges::viewable_range urng_t>
260 static constexpr auto impl(urng_t && urange)
261 {
262 static_assert(std::ranges::random_access_range<urng_t> || pseudo_random_access_range<urng_t>,
263 "The adapted range must either model std::ranges::random_access_range or must be "
264 "a specific SeqAn range type that supports pseudo random access.");
265 static_assert(std::ranges::forward_range<urng_t>,
266 "The underlying range must model std::ranges::forward_range.");
267
268 if constexpr (std::ranges::random_access_range<urng_t>)
269 { // Nothing to do, just return as ref_view or original view.
270 return std::views::all(std::forward<urng_t>(urange));
271 }
272 else
273 { // Get a subrange using the random access iterators of the container.
274 return view_enforce_random_access{std::forward<urng_t>(urange)};
275 }
276 }
277};
278
279} // namespace seqan3::detail
280
281namespace seqan3::views
282{
351inline constexpr auto enforce_random_access = detail::pseudo_random_access_fn{};
352
353} // namespace seqan3::views
Provides seqan3::detail::adaptor_base and seqan3::detail::combined_adaptor.
T begin(T... args)
T end(T... args)
Provides the seqan3::detail::inherited_iterator_base template.
Specifies requirements of an input range type for which the const version of that type satisfies the ...
This concept checks if a type models a pseudo random access range.
T move(T... args)
The SeqAn namespace for views.
Definition char_strictly_to.hpp:19
SeqAn specific customisations in the standard namespace.
T operator!=(T... args)
Additional non-standard concepts for ranges.
Hide me