SeqAn3  3.0.1
The Modern C++ library for sequence analysis.
repeat.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 <range/v3/view/single.hpp>
16 
20 #include <seqan3/std/ranges>
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::ranges::default_sentinel_t;
52 
54  template <typename parent_type>
55  class repeat_view_iterator : public detail::random_access_iterator_base<parent_type, repeat_view_iterator>
56  {
58  using base_t = detail::random_access_iterator_base<parent_type, repeat_view_iterator>;
59 
61  using typename base_t::position_type;
62 
63  public:
65  using typename base_t::difference_type;
67  using typename base_t::value_type;
69  using typename base_t::reference;
71  using typename base_t::pointer;
73  using typename base_t::iterator_category;
74 
78  constexpr repeat_view_iterator() = default;
79  constexpr repeat_view_iterator(repeat_view_iterator const &) = default;
80  constexpr repeat_view_iterator & operator=(repeat_view_iterator const &) = default;
81  constexpr repeat_view_iterator (repeat_view_iterator &&) = default;
82  constexpr repeat_view_iterator & operator=(repeat_view_iterator &&) = default;
83  ~repeat_view_iterator() = default;
84 
88  explicit constexpr repeat_view_iterator(parent_type & host) noexcept : base_t{host} {}
89 
93  template <typename parent_type2>
95  requires std::is_const_v<parent_type> && !std::is_const_v<parent_type2> &&
96  std::is_same_v<std::remove_const_t<parent_type>, parent_type2>
98  constexpr repeat_view_iterator(repeat_view_iterator<parent_type2> const & rhs) noexcept :
99  base_t{rhs}
100  {}
102 
106  using base_t::operator==;
109  using base_t::operator!=;
110 
112  constexpr bool operator==(std::ranges::default_sentinel_t const &) const noexcept
113  {
114  return false;
115  }
116 
118  constexpr bool operator!=(std::ranges::default_sentinel_t const &) const noexcept
119  {
120  return true;
121  }
122 
124  friend constexpr bool operator==(std::ranges::default_sentinel_t const &,
125  repeat_view_iterator const &) noexcept
126  {
127  return false;
128  }
129 
131  friend constexpr bool operator!=(std::ranges::default_sentinel_t const &,
132  repeat_view_iterator const &) noexcept
133  {
134  return true;
135  }
137  };
138 
139 public:
143  using value_type = std::remove_reference_t<value_t>;
146  using reference = value_type &;
148  using const_reference = value_type const &;
150  using size_type = void;
152  using difference_type = ptrdiff_t;
154  using iterator = repeat_view_iterator<repeat_view>;
156  using const_iterator = repeat_view_iterator<repeat_view const>;
158 
162  constexpr repeat_view() = default;
163  constexpr repeat_view(repeat_view const &) = default;
164  constexpr repeat_view & operator=(repeat_view const &) = default;
165  constexpr repeat_view(repeat_view &&) = default;
166  constexpr repeat_view & operator=(repeat_view &&) = default;
167  ~repeat_view() = default;
168 
170  constexpr explicit repeat_view(value_t const & value) : single_value{ranges::single_view{value}}
171  {}
172 
174  constexpr explicit repeat_view(value_t && value) : single_value{ranges::single_view{std::move(value)}}
175  {}
177 
196  constexpr iterator begin() noexcept
197  {
198  return iterator{*this};
199  }
200 
202  constexpr const_iterator begin() const noexcept
203  {
204  return const_iterator{*this};
205  }
206 
208  constexpr const_iterator cbegin() const noexcept
209  {
210  return const_iterator{*this};
211  }
212 
228  constexpr sentinel_type end() noexcept
229  {
230  return {};
231  }
232 
234  constexpr sentinel_type end() const noexcept
235  {
236  return {};
237  }
238 
240  constexpr sentinel_type cend() const noexcept
241  {
242  return {};
243  }
245 
265  constexpr const_reference operator[](difference_type const SEQAN3_DOXYGEN_ONLY(n)) const noexcept
266  {
267  return *single_value.begin();
268  }
269 
271  constexpr reference operator[](difference_type const SEQAN3_DOXYGEN_ONLY(n)) noexcept
272  {
273  return *single_value.begin();
274  }
276 
277 private:
279  decltype(std::views::single(std::declval<value_t &>())) single_value;
280 };
281 
282 // ---------------------------------------------------------------------------------------------------------------------
283 // repeat (factory)
284 // ---------------------------------------------------------------------------------------------------------------------
285 
287 struct 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 
299 namespace seqan3::views
300 {
301 
302 // ---------------------------------------------------------------------------------------------------------------------
303 // views::repeat (factory instance)
304 // ---------------------------------------------------------------------------------------------------------------------
305 
350 constexpr inline detail::repeat_fn repeat{};
352 
353 } // namespace seqan3::views
seqan3::single
The text is a single range.
Definition: concept.hpp:84
seqan3::views
The SeqAn namespace for views.
Definition: view_to_simd.hpp:672
std::rel_ops::operator!=
T operator!=(T... args)
seqan3::views::move
const auto move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
random_access_iterator.hpp
Provides the seqan3::detail::random_access_iterator class.
range.hpp
Provides various transformation traits used by the range module.
iterator.hpp
Provides various transformation traits for use on iterators.
ranges
Adaptations of concepts from the Ranges TS.
std::remove_reference_t
std::begin
T begin(T... args)
seqan3::views::repeat
constexpr detail::repeat_fn repeat
A view factory that repeats a given value infinitely.
Definition: repeat.hpp:350
std::end
T end(T... args)