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// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2023, 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 range_type, template <typename...> typename derived_t_template, typename... args_t>
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
251 constexpr bool operator==(basic_iterator const & rhs) const noexcept
252 {
253 return base_t::operator==(rhs);
254 }
255
256 constexpr bool operator!=(basic_iterator const & rhs) const noexcept
257 {
258 return !(*this == rhs);
259 }
260
262 constexpr bool operator==(std::default_sentinel_t const &) const noexcept
263 {
264 return false;
265 }
266
268 constexpr bool operator!=(std::default_sentinel_t const &) const noexcept
269 {
270 return true;
271 }
272
274 friend constexpr bool operator==(std::default_sentinel_t const &, basic_iterator const &) noexcept
275 {
276 return false;
277 }
278
280 friend constexpr bool operator!=(std::default_sentinel_t const &, basic_iterator const &) noexcept
281 {
282 return true;
283 }
285};
286
287// ---------------------------------------------------------------------------------------------------------------------
288// repeat (factory)
289// ---------------------------------------------------------------------------------------------------------------------
290
292struct repeat_fn
293{
295 template <std::copy_constructible value_type>
296 constexpr auto operator()(value_type && value) const
297 {
298 return detail::repeat_view{std::forward<value_type>(value)};
299 }
300};
301
302} // namespace seqan3::detail
303
304namespace seqan3::views
305{
347inline constexpr detail::repeat_fn repeat{};
348
349} // 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:347
T is_same_v
Provides various transformation traits for use on iterators.
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.