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) noexcept(
170 noexcept(std::declval<underlying_iter_t const &>() == std::declval<std::ranges::sentinel_t<urng_t> const &>()))
171 {
172 return lhs.base() == rhs;
173 }
174
176 friend constexpr bool operator==(std::ranges::sentinel_t<urng_t> const & lhs, basic_iterator const & rhs) noexcept(
177 noexcept(std::declval<underlying_iter_t const &>() == std::declval<std::ranges::sentinel_t<urng_t> const &>()))
178 {
179 return rhs == lhs;
180 }
181
183 friend constexpr bool operator!=(basic_iterator const & lhs, std::ranges::sentinel_t<urng_t> const & rhs) noexcept(
184 noexcept(std::declval<underlying_iter_t const &>() != std::declval<std::ranges::sentinel_t<urng_t> const &>()))
185 {
186 return !(lhs == rhs);
187 }
188
190 friend constexpr bool operator!=(std::ranges::sentinel_t<urng_t> const & lhs, basic_iterator const & rhs) noexcept(
191 noexcept(std::declval<underlying_iter_t const &>() != std::declval<std::ranges::sentinel_t<urng_t> const &>()))
192 {
193 return rhs != lhs;
194 }
196
200 // Import operator from base.
201 using base_t::operator-;
202
204 constexpr typename base_t::difference_type operator-(std::ranges::sentinel_t<urng_t> const & rhs) const
205 noexcept(noexcept(std::declval<underlying_iter_t const &>()
206 - std::declval<std::ranges::sentinel_t<urng_t> const &>()))
207 requires std::sized_sentinel_for<std::ranges::sentinel_t<urng_t>, underlying_iter_t>
208 {
209 return this->base() - rhs;
210 }
211
213 constexpr friend typename base_t::difference_type
214 operator-(std::ranges::sentinel_t<urng_t> const & lhs,
215 basic_iterator const & rhs) noexcept(noexcept(std::declval<std::ranges::sentinel_t<urng_t> const &>()
216 - std::declval<underlying_iter_t const &>()))
217 requires std::sized_sentinel_for<std::ranges::sentinel_t<urng_t>, underlying_iter_t>
218 {
219 return lhs - rhs.base();
220 }
222};
223
229template <std::ranges::viewable_range rng_t>
230view_enforce_random_access(rng_t &&) -> view_enforce_random_access<std::views::all_t<rng_t>>;
232
233// ============================================================================
234// pseudo_random_access_fn (adaptor definition)
235// ============================================================================
236
238struct pseudo_random_access_fn : public adaptor_base<pseudo_random_access_fn>
239{
240private:
242 using base_t = adaptor_base<pseudo_random_access_fn>;
243
244public:
246 using base_t::base_t;
247
248private:
250 friend base_t;
251
255 template <std::ranges::viewable_range urng_t>
256 static constexpr auto impl(urng_t && urange)
257 {
258 static_assert(std::ranges::random_access_range<urng_t> || pseudo_random_access_range<urng_t>,
259 "The adapted range must either model std::ranges::random_access_range or must be "
260 "a specific SeqAn range type that supports pseudo random access.");
261 static_assert(std::ranges::forward_range<urng_t>,
262 "The underlying range must model std::ranges::forward_range.");
263
264 if constexpr (std::ranges::random_access_range<urng_t>)
265 { // Nothing to do, just return as ref_view or original view.
266 return std::views::all(std::forward<urng_t>(urange));
267 }
268 else
269 { // Get a subrange using the random access iterators of the container.
270 return view_enforce_random_access{std::forward<urng_t>(urange)};
271 }
272 }
273};
274
275} // namespace seqan3::detail
276
277namespace seqan3::views
278{
347inline constexpr auto enforce_random_access = detail::pseudo_random_access_fn{};
348
349} // 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