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
62 using const_reference = value_type const &;
64 using difference_type = ptrdiff_t;
66
68 template <typename parent_type>
69 class basic_iterator;
70
79
81 template <typename range_type, template <typename...> typename derived_t_template, typename... args_t>
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;
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:
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{
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;
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>
240 && (!std::is_const_v<parent_type2>) && std::is_same_v<std::remove_const_t<parent_type>, 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
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
A CRTP base template for creating random access iterators.
Definition random_access_iterator.hpp:39
value_type * pointer
Pointer type is pointer of container element type.
Definition random_access_iterator.hpp:67
typename range_type::value_type value_type
Value type of container elements.
Definition random_access_iterator.hpp:59
typename range_type::difference_type difference_type
Type for distances between iterators.
Definition random_access_iterator.hpp:57
The forward declared iterator type for views::repeat (a random access iterator).
Definition repeat.hpp:200
friend constexpr bool operator==(std::default_sentinel_t const &, basic_iterator const &) noexcept
Equality comparison to the sentinel always returns false on an infinite view.
Definition repeat.hpp:271
basic_iterator & operator=(basic_iterator &&)=default
Defaulted.
constexpr bool operator!=(basic_iterator const &rhs) const noexcept
Equality comparison to the sentinel always returns false on an infinite view.
Definition repeat.hpp:253
basic_iterator(basic_iterator &&)=default
Defaulted.
constexpr bool operator==(basic_iterator const &rhs) const noexcept
Equality comparison to the sentinel always returns false on an infinite view.
Definition repeat.hpp:248
basic_iterator & operator=(basic_iterator const &)=default
Defaulted.
constexpr basic_iterator(parent_type &host) noexcept
Construct by host range.
Definition repeat.hpp:232
constexpr bool operator!=(std::default_sentinel_t const &) const noexcept
Inequality comparison to the sentinel always returns true on an infinite view.
Definition repeat.hpp:265
constexpr bool operator==(std::default_sentinel_t const &) const noexcept
Equality comparison to the sentinel always returns false on an infinite view.
Definition repeat.hpp:259
constexpr basic_iterator(basic_iterator< parent_type2 > const &rhs) noexcept
Constructor for const version from non-const version.
Definition repeat.hpp:241
friend constexpr bool operator!=(std::default_sentinel_t const &, basic_iterator const &) noexcept
Inequality comparison to the sentinel always returns true on an infinite view.
Definition repeat.hpp:277
basic_iterator(basic_iterator const &)=default
Defaulted.
The type returned by seqan3::views::repeat.
Definition repeat.hpp:42
constexpr sentinel_type end() noexcept
Returns an iterator to the element following the last element of the range.
Definition repeat.hpp:148
~repeat_view()=default
Defaulted.
repeat_view & operator=(repeat_view const &)=default
Defaulted.
repeat_view & operator=(repeat_view &&)=default
Defaulted.
repeat_view(repeat_view &&)=default
Defaulted.
std::ranges::view_interface< repeat_view< value_t > > base_t
/brief the base type.
Definition repeat.hpp:45
ptrdiff_t difference_type
The type to store the difference of two iterators.
Definition repeat.hpp:64
std::default_sentinel_t sentinel_type
The sentinel type is set to std::default_sentinel_t.
Definition repeat.hpp:48
constexpr repeat_view(value_t const &value)
Construct from any type (Note: the value will be copied into views::single).
Definition repeat.hpp:96
repeat_view()=default
Defaulted.
constexpr const_iterator begin() const noexcept
Returns an iterator to the first element of the range.
Definition repeat.hpp:128
value_type const & const_reference
The const reference type.
Definition repeat.hpp:62
decltype(std::views::single(std::declval< value_t >())) single_value_t
The view which wraps the single value to repeat.
Definition repeat.hpp:51
single_value_t single_value
}
Definition repeat.hpp:193
constexpr const_reference operator[](difference_type const n) const noexcept
Returns the n-th element.
Definition repeat.hpp:179
repeat_view(repeat_view const &)=default
Defaulted.
constexpr iterator begin() noexcept
Returns an iterator to the first element of the range.
Definition repeat.hpp:122
constexpr repeat_view(value_t &&value)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition repeat.hpp:100
constexpr reference operator[](difference_type const n) noexcept
Returns the n-th element.
Definition repeat.hpp:185
constexpr sentinel_type end() const noexcept
Returns an iterator to the element following the last element of the range.
Definition repeat.hpp:154
Provides various transformation traits used by the range module.
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.
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
The SeqAn namespace for views.
Definition char_strictly_to.hpp:19
SeqAn specific customisations in the standard namespace.
Provides the seqan3::detail::random_access_iterator class.
View factory definition for views::repeat.
Definition repeat.hpp:290
constexpr auto operator()(value_type &&value) const
Returns an instance of seqan3::detail::repeat_view constructed with value.
Definition repeat.hpp:293
Hide me