SeqAn3 3.2.0
The Modern C++ library for sequence analysis.
repeat.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6// -----------------------------------------------------------------------------------------------------
7
13#pragma once
14
15#include <algorithm>
16#include <ranges>
17
21
22namespace seqan3::detail
23{
24
25// ---------------------------------------------------------------------------------------------------------------------
26// repeat_view class
27// ---------------------------------------------------------------------------------------------------------------------
28
43template <std::copy_constructible value_t>
44class repeat_view : public std::ranges::view_interface<repeat_view<value_t>>
45{
46private:
48 using base_t = std::ranges::view_interface<repeat_view<value_t>>;
49
51 using sentinel_type = std::default_sentinel_t;
52
54 using single_value_t = decltype(std::views::single(std::declval<value_t>()));
55
61 using value_type = std::remove_reference_t<value_t>;
63 using reference = value_type &;
65 using const_reference = value_type const &;
67 using difference_type = ptrdiff_t;
69
71 template <typename parent_type>
72 class basic_iterator;
73
78 using iterator = basic_iterator<repeat_view>;
80 using const_iterator = basic_iterator<repeat_view const>;
82
84 template <typename parent_type, typename crtp_base>
85 friend class detail::random_access_iterator_base;
86
87public:
91 repeat_view() = default;
92 repeat_view(repeat_view const &) = default;
93 repeat_view & operator=(repeat_view const &) = default;
94 repeat_view(repeat_view &&) = default;
95 repeat_view & operator=(repeat_view &&) = default;
96 ~repeat_view() = default;
97
99 constexpr explicit repeat_view(value_t const & value) : single_value{value}
100 {}
101
103 constexpr explicit repeat_view(value_t && value) : single_value{std::move(value)}
104 {}
106
125 constexpr iterator begin() noexcept
126 {
127 return iterator{*this};
128 }
129
131 constexpr const_iterator begin() const noexcept
132 {
133 return const_iterator{*this};
134 }
135
151 constexpr sentinel_type end() noexcept
152 {
153 return {};
154 }
155
157 constexpr sentinel_type end() const noexcept
158 {
159 return {};
160 }
162
182 constexpr const_reference operator[](difference_type const SEQAN3_DOXYGEN_ONLY(n)) const noexcept
183 {
184 return *single_value.begin();
185 }
186
188 constexpr reference operator[](difference_type const SEQAN3_DOXYGEN_ONLY(n)) noexcept
189 {
190 return *single_value.begin();
191 }
193
194private:
196 single_value_t single_value;
197};
198
200template <std::copy_constructible value_t>
201template <typename parent_type>
202class repeat_view<value_t>::basic_iterator : public detail::random_access_iterator_base<parent_type, basic_iterator>
203{
205 using base_t = detail::random_access_iterator_base<parent_type, basic_iterator>;
206
208 using typename base_t::position_type;
209
210public:
212 using typename base_t::difference_type;
214 using typename base_t::value_type;
216 using typename base_t::reference;
218 using typename base_t::pointer;
220 using typename base_t::iterator_category;
221
225 basic_iterator() = default;
226 basic_iterator(basic_iterator const &) = default;
227 basic_iterator & operator=(basic_iterator const &) = default;
228 basic_iterator(basic_iterator &&) = default;
229 basic_iterator & operator=(basic_iterator &&) = default;
230 ~basic_iterator() = default;
231
235 explicit constexpr basic_iterator(parent_type & host) noexcept : base_t{host}
236 {}
237
241 template <typename parent_type2>
242 requires std::is_const_v<parent_type>
243 && (!std::is_const_v<parent_type2>) && std::is_same_v<std::remove_const_t<parent_type>, parent_type2>
244 constexpr basic_iterator(basic_iterator<parent_type2> const & rhs) noexcept : base_t{rhs}
245 {}
247
252 using base_t::operator==;
254 using base_t::operator!=;
255
257 constexpr bool operator==(std::default_sentinel_t const &) const noexcept
258 {
259 return false;
260 }
261
263 constexpr bool operator!=(std::default_sentinel_t const &) const noexcept
264 {
265 return true;
266 }
267
269 friend constexpr bool operator==(std::default_sentinel_t const &, basic_iterator const &) noexcept
270 {
271 return false;
272 }
273
275 friend constexpr bool operator!=(std::default_sentinel_t const &, basic_iterator const &) noexcept
276 {
277 return true;
278 }
280};
281
282// ---------------------------------------------------------------------------------------------------------------------
283// repeat (factory)
284// ---------------------------------------------------------------------------------------------------------------------
285
287struct repeat_fn
288{
290 template <std::copy_constructible value_type>
291 constexpr auto operator()(value_type && value) const
292 {
293 return detail::repeat_view{std::forward<value_type>(value)};
294 }
295};
296
297} // namespace seqan3::detail
298
299namespace seqan3::views
300{
342inline constexpr detail::repeat_fn repeat{};
343
344} // namespace seqan3::views
T begin(T... args)
Provides various transformation traits used by the range module.
T end(T... args)
@ single
The text is a single range.
Definition: concept.hpp:93
constexpr detail::repeat_fn repeat
A view factory that repeats a given value infinitely.
Definition: repeat.hpp:342
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:22
SeqAn specific customisations in the standard namespace.
T operator!=(T... args)
Provides the seqan3::detail::random_access_iterator class.