SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
repeat.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 <algorithm>
13#include <ranges>
14
18
19namespace seqan3::detail
20{
21
22// ---------------------------------------------------------------------------------------------------------------------
23// repeat_view class
24// ---------------------------------------------------------------------------------------------------------------------
25
40template <std::copy_constructible value_t>
41class repeat_view : public std::ranges::view_interface<repeat_view<value_t>>
42{
43private:
45 using base_t = std::ranges::view_interface<repeat_view<value_t>>;
46
48 using sentinel_type = std::default_sentinel_t;
49
51 using single_value_t = decltype(std::views::single(std::declval<value_t>()));
52
58 using value_type = std::remove_reference_t<value_t>;
60 using reference = value_type &;
62 using const_reference = value_type const &;
64 using difference_type = ptrdiff_t;
66
68 template <typename parent_type>
69 class basic_iterator;
70
75 using iterator = basic_iterator<repeat_view>;
77 using const_iterator = basic_iterator<repeat_view const>;
79
81 template <typename range_type, template <typename...> typename derived_t_template, typename... args_t>
82 friend class detail::random_access_iterator_base;
83
84public:
88 repeat_view() = default;
89 repeat_view(repeat_view const &) = default;
90 repeat_view & operator=(repeat_view const &) = default;
91 repeat_view(repeat_view &&) = default;
92 repeat_view & operator=(repeat_view &&) = default;
93 ~repeat_view() = default;
94
96 constexpr explicit repeat_view(value_t const & value) : single_value{value}
97 {}
98
100 constexpr explicit repeat_view(value_t && value) : single_value{std::move(value)}
101 {}
103
122 constexpr iterator begin() noexcept
123 {
124 return iterator{*this};
125 }
126
128 constexpr const_iterator begin() const noexcept
129 {
130 return const_iterator{*this};
131 }
132
148 constexpr sentinel_type end() noexcept
149 {
150 return {};
151 }
152
154 constexpr sentinel_type end() const noexcept
155 {
156 return {};
157 }
159
179 constexpr const_reference operator[](difference_type const SEQAN3_DOXYGEN_ONLY(n)) const noexcept
180 {
181 return *single_value.begin();
182 }
183
185 constexpr reference operator[](difference_type const SEQAN3_DOXYGEN_ONLY(n)) noexcept
186 {
187 return *single_value.begin();
188 }
190
191private:
193 single_value_t single_value;
194};
195
197template <std::copy_constructible value_t>
198template <typename parent_type>
199class repeat_view<value_t>::basic_iterator : public detail::random_access_iterator_base<parent_type, basic_iterator>
200{
202 using base_t = detail::random_access_iterator_base<parent_type, basic_iterator>;
203
205 using typename base_t::position_type;
206
207public:
209 using typename base_t::difference_type;
211 using typename base_t::value_type;
213 using typename base_t::reference;
215 using typename base_t::pointer;
217 using typename base_t::iterator_category;
218
222 basic_iterator() = default;
223 basic_iterator(basic_iterator const &) = default;
224 basic_iterator & operator=(basic_iterator const &) = default;
225 basic_iterator(basic_iterator &&) = default;
226 basic_iterator & operator=(basic_iterator &&) = default;
227 ~basic_iterator() = default;
228
232 explicit constexpr basic_iterator(parent_type & host) noexcept : base_t{host}
233 {}
234
238 template <typename parent_type2>
239 requires std::is_const_v<parent_type> && (!std::is_const_v<parent_type2>)
241 constexpr basic_iterator(basic_iterator<parent_type2> const & rhs) noexcept : base_t{rhs}
242 {}
244
248 constexpr bool operator==(basic_iterator const & rhs) const noexcept
249 {
250 return base_t::operator==(rhs);
251 }
252
253 constexpr bool operator!=(basic_iterator const & rhs) const noexcept
254 {
255 return !(*this == rhs);
256 }
257
259 constexpr bool operator==(std::default_sentinel_t const &) const noexcept
260 {
261 return false;
262 }
263
265 constexpr bool operator!=(std::default_sentinel_t const &) const noexcept
266 {
267 return true;
268 }
269
271 friend constexpr bool operator==(std::default_sentinel_t const &, basic_iterator const &) noexcept
272 {
273 return false;
274 }
275
277 friend constexpr bool operator!=(std::default_sentinel_t const &, basic_iterator const &) noexcept
278 {
279 return true;
280 }
282};
283
284// ---------------------------------------------------------------------------------------------------------------------
285// repeat (factory)
286// ---------------------------------------------------------------------------------------------------------------------
287
289struct repeat_fn
290{
292 template <std::copy_constructible value_type>
293 constexpr auto operator()(value_type && value) const
294 {
295 return detail::repeat_view{std::forward<value_type>(value)};
296 }
297};
298
299} // namespace seqan3::detail
300
301namespace seqan3::views
302{
344inline constexpr detail::repeat_fn repeat{};
345
346} // namespace seqan3::views
T begin(T... args)
Provides various transformation traits used by the range module.
T end(T... args)
constexpr detail::repeat_fn repeat
A view factory that repeats a given value infinitely.
Definition repeat.hpp:344
T is_same_v
Provides various transformation traits for use on iterators.
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)
Provides the seqan3::detail::random_access_iterator class.
Hide me