SeqAn3 3.3.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-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
65 using const_reference = value_type const &;
67 using difference_type = ptrdiff_t;
69
71 template <typename parent_type>
72 class basic_iterator;
73
82
84 template <typename parent_type, typename crtp_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;
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:
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{
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;
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
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
A CRTP base template for creating random access iterators.
Definition: random_access_iterator.hpp:42
value_type * pointer
Pointer type is pointer of container element type.
Definition: random_access_iterator.hpp:70
typename range_type::value_type value_type
Value type of container elements.
Definition: random_access_iterator.hpp:62
typename range_type::difference_type difference_type
Type for distances between iterators.
Definition: random_access_iterator.hpp:60
The forward declared iterator type for views::repeat (a random access iterator).
Definition: repeat.hpp:203
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:269
basic_iterator & operator=(basic_iterator &&)=default
Defaulted.
basic_iterator(basic_iterator &&)=default
Defaulted.
basic_iterator & operator=(basic_iterator const &)=default
Defaulted.
constexpr basic_iterator(parent_type &host) noexcept
Construct by host range.
Definition: repeat.hpp:235
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:263
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:257
constexpr basic_iterator(basic_iterator< parent_type2 > const &rhs) noexcept
Constructor for const version from non-const version.
Definition: repeat.hpp:244
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:275
basic_iterator(basic_iterator const &)=default
Defaulted.
The type returned by seqan3::views::repeat.
Definition: repeat.hpp:45
constexpr sentinel_type end() noexcept
Returns an iterator to the element following the last element of the range.
Definition: repeat.hpp:151
~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:48
ptrdiff_t difference_type
The type to store the difference of two iterators.
Definition: repeat.hpp:67
std::default_sentinel_t sentinel_type
The sentinel type is set to std::default_sentinel_t.
Definition: repeat.hpp:51
constexpr repeat_view(value_t const &value)
Construct from any type (Note: the value will be copied into views::single).
Definition: repeat.hpp:99
repeat_view()=default
Defaulted.
constexpr const_iterator begin() const noexcept
Returns an iterator to the first element of the range.
Definition: repeat.hpp:131
value_type const & const_reference
The const reference type.
Definition: repeat.hpp:65
decltype(std::views::single(std::declval< value_t >())) single_value_t
The view which wraps the single value to repeat.
Definition: repeat.hpp:54
single_value_t single_value
}
Definition: repeat.hpp:196
constexpr const_reference operator[](difference_type const n) const noexcept
Returns the n-th element.
Definition: repeat.hpp:182
repeat_view(repeat_view const &)=default
Defaulted.
constexpr iterator begin() noexcept
Returns an iterator to the first element of the range.
Definition: repeat.hpp:125
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:103
constexpr reference operator[](difference_type const n) noexcept
Returns the n-th element.
Definition: repeat.hpp:188
constexpr sentinel_type end() const noexcept
Returns an iterator to the element following the last element of the range.
Definition: repeat.hpp:157
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:342
T is_same_v
Provides various transformation traits for use on iterators.
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
The SeqAn namespace for views.
Definition: char_strictly_to.hpp:22
SeqAn specific customisations in the standard namespace.
Provides the seqan3::detail::random_access_iterator class.
View factory definition for views::repeat.
Definition: repeat.hpp:288
constexpr auto operator()(value_type &&value) const
Returns an instance of seqan3::detail::repeat_view constructed with value.
Definition: repeat.hpp:291