SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
repeat.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, 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 <seqan3/std/algorithm>
16 #include <seqan3/std/ranges>
17 
21 
22 namespace seqan3::detail
23 {
24 
25 // ---------------------------------------------------------------------------------------------------------------------
26 // repeat_view class
27 // ---------------------------------------------------------------------------------------------------------------------
28 
43 template <std::copy_constructible value_t>
44 class repeat_view : public std::ranges::view_interface<repeat_view<value_t>>
45 {
46 private:
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 
87 public:
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 
194 private:
196  single_value_t single_value;
197 };
198 
200 template <std::copy_constructible value_t>
201 template <typename parent_type>
202 class repeat_view<value_t>::basic_iterator :
203  public detail::random_access_iterator_base<parent_type, basic_iterator>
204 {
206  using base_t = detail::random_access_iterator_base<parent_type, basic_iterator>;
207 
209  using typename base_t::position_type;
210 
211 public:
213  using typename base_t::difference_type;
215  using typename base_t::value_type;
217  using typename base_t::reference;
219  using typename base_t::pointer;
221  using typename base_t::iterator_category;
222 
226  basic_iterator() = default;
227  basic_iterator(basic_iterator const &) = default;
228  basic_iterator & operator=(basic_iterator const &) = default;
229  basic_iterator (basic_iterator &&) = default;
230  basic_iterator & operator=(basic_iterator &&) = default;
231  ~basic_iterator() = default;
232 
236  explicit constexpr basic_iterator(parent_type & host) noexcept : base_t{host} {}
237 
241  template <typename parent_type2>
243  requires std::is_const_v<parent_type> && (!std::is_const_v<parent_type2>) &&
246  constexpr basic_iterator(basic_iterator<parent_type2> const & rhs) noexcept :
247  base_t{rhs}
248  {}
250 
255  using base_t::operator==;
257  using base_t::operator!=;
258 
260  constexpr bool operator==(std::default_sentinel_t const &) const noexcept
261  {
262  return false;
263  }
264 
266  constexpr bool operator!=(std::default_sentinel_t const &) const noexcept
267  {
268  return true;
269  }
270 
272  friend constexpr bool operator==(std::default_sentinel_t const &,
273  basic_iterator const &) noexcept
274  {
275  return false;
276  }
277 
279  friend constexpr bool operator!=(std::default_sentinel_t const &,
280  basic_iterator const &) noexcept
281  {
282  return true;
283  }
285 };
286 
287 // ---------------------------------------------------------------------------------------------------------------------
288 // repeat (factory)
289 // ---------------------------------------------------------------------------------------------------------------------
290 
292 struct 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 
304 namespace seqan3::views
305 {
306 
307 // ---------------------------------------------------------------------------------------------------------------------
308 // views::repeat (factory instance)
309 // ---------------------------------------------------------------------------------------------------------------------
310 
355 constexpr inline detail::repeat_fn repeat{};
357 
358 } // namespace seqan3::views
Adaptations of algorithms from the Ranges TS.
T begin(T... args)
Provides the seqan3::detail::random_access_iterator class.
Provides various transformation traits used by the range module.
T end(T... args)
@ single
The text is a single range.
Definition: concept.hpp:83
constexpr detail::repeat_fn repeat
A view factory that repeats a given value infinitely.
Definition: repeat.hpp:355
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:74
T is_same_v
Provides various transformation traits for use on iterators.
The SeqAn namespace for views.
Definition: char_to.hpp:22
SeqAn specific customisations in the standard namespace.
T operator!=(T... args)
Adaptations of concepts from the Ranges TS.