SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
single_pass_input.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 
16 #include <seqan3/std/concepts>
17 #include <seqan3/std/iterator>
18 #include <seqan3/std/ranges>
19 
20 //-----------------------------------------------------------------------------
21 // Implementation of single pass input view.
22 //-----------------------------------------------------------------------------
23 
24 namespace seqan3::detail
25 {
26 
28 template <typename view_t>
30 
37 template <std::ranges::view urng_t>
38 class single_pass_input_view : public std::ranges::view_interface<single_pass_input_view<urng_t>>
39 {
41 private:
42 
44  using urng_iterator_type = std::ranges::iterator_t<urng_t>;
45 
47  template <typename view_t>
48  friend class basic_iterator;
49 
51  struct state
52  {
54  urng_t urng;
56  urng_iterator_type cached_urng_iter = std::ranges::begin(urng);
57  };
58 
61 
68  using sentinel = std::ranges::sentinel_t<urng_t>;
69  //\}
70 
71 public:
77  constexpr single_pass_input_view() = default;
79  constexpr single_pass_input_view(single_pass_input_view const &) = default;
88 
90  explicit single_pass_input_view(urng_t _urng) :
91  state_ptr{new state{std::move(_urng)}}
92  {}
93 
95  template <typename other_urng_t>
98  std::ranges::viewable_range<other_urng_t> && // Must come after self type check to avoid conflicts with the move constructor.
99  std::constructible_from<urng_t, std::ranges::ref_view<std::remove_reference_t<other_urng_t>>>)
101  explicit single_pass_input_view(other_urng_t && _urng) :
102  single_pass_input_view{std::views::all(_urng)}
103  {}
105 
117  {
118  return {*this};
119  }
120 
122  iterator begin() const = delete;
123 
126  {
127  return {std::ranges::end(state_ptr->urng)};
128  }
129 
131  sentinel end() const = delete;
133 };
134 
141 template <std::ranges::viewable_range urng_t>
145 } // seqan3::detail
146 
147 //-----------------------------------------------------------------------------
148 // Iterator for single pass input view.
149 //-----------------------------------------------------------------------------
150 
151 namespace seqan3::detail
152 {
160 template <typename view_type>
162 {
167 
170 
172  template <typename input_view_type>
173  friend class basic_iterator;
174 
176  static_assert(std::sentinel_for<sentinel_type, base_iterator_type>);
177 
178 public:
179 
194 
199  basic_iterator() = default;
201  constexpr basic_iterator(basic_iterator const & rhs) = default;
203  constexpr basic_iterator(basic_iterator && rhs) = default;
205  constexpr basic_iterator & operator=(basic_iterator const & rhs) = default;
207  constexpr basic_iterator & operator=(basic_iterator && rhs) = default;
209  ~basic_iterator() = default;
210 
212  basic_iterator(single_pass_input_view<view_type> & view) noexcept : view_ptr{&view}
213  {}
215 
220  reference operator*() const noexcept
221  {
222  return *cached();
223  }
224 
226  pointer operator->() const noexcept
228  requires (!std::is_void_v<pointer>)
230  {
231  return std::addressof(*cached());
232  }
234 
240  {
241  ++cached();
242  return *this;
243  }
244 
246  auto operator++(int) noexcept
247  {
248  if constexpr (std::output_iterator<base_iterator_type, reference> &&
249  std::copy_constructible<base_iterator_type>)
250  {
251  basic_iterator tmp{*this};
252  ++(*this);
253  return tmp;
254  }
255  else
256  {
257  ++(*this);
258  }
259  }
261 
266  constexpr bool operator==(sentinel_type const & s) const noexcept
267  {
268  return cached() == s;
269  }
270 
272  friend constexpr bool
273  operator==(sentinel_type const & s, basic_iterator const & rhs) noexcept
274  {
275  return rhs == s;
276  }
277 
279  constexpr bool operator!=(sentinel_type const & rhs) const noexcept
280  {
281  return !(*this == rhs);
282  }
283 
285  friend constexpr bool
286  operator!=(sentinel_type const & s, basic_iterator const & rhs) noexcept
287  {
288  return rhs != s;
289  }
291 
292 protected:
295  base_iterator_type & cached() const noexcept
296  {
297  assert(view_ptr != nullptr);
298  assert(view_ptr->state_ptr != nullptr);
299  return view_ptr->state_ptr->cached_urng_iter;
300  }
301 };
302 } // seqan3::detail
303 
304 //-----------------------------------------------------------------------------
305 // View shortcut for functor.
306 //-----------------------------------------------------------------------------
307 
309 namespace seqan3::views
310 {
363 
365 } // namespace seqan3::views
T addressof(T... args)
Template for range adaptor closure objects that store no arguments and always delegate to the view co...
Definition: detail.hpp:292
auto operator++(int) noexcept
Post-increment.
Definition: single_pass_input.hpp:246
typename single_pass_input_view< view_type >::sentinel sentinel_type
The sentinel type to compare to.
Definition: single_pass_input.hpp:166
base_iterator_type & cached() const noexcept
Gives access to the cached iterator.
Definition: single_pass_input.hpp:295
basic_iterator(single_pass_input_view< view_type > &view) noexcept
Constructing from the underlying seqan3::single_pass_input_view.
Definition: single_pass_input.hpp:212
constexpr bool operator==(sentinel_type const &s) const noexcept
Compares for equality with sentinel.
Definition: single_pass_input.hpp:266
constexpr bool operator!=(sentinel_type const &rhs) const noexcept
Compares for inequality with sentinel.
Definition: single_pass_input.hpp:279
constexpr basic_iterator(basic_iterator &&rhs)=default
Move construction.
basic_iterator & operator++() noexcept
Pre-increment.
Definition: single_pass_input.hpp:239
constexpr basic_iterator(basic_iterator const &rhs)=default
Copy construction.
pointer operator->() const noexcept
Returns pointer to the pointed-to object.
Definition: single_pass_input.hpp:226
typename single_pass_input_view< view_type >::urng_iterator_type base_iterator_type
The pointer to the associated view.
Definition: single_pass_input.hpp:164
constexpr basic_iterator & operator=(basic_iterator const &rhs)=default
Copy assignment.
constexpr friend bool operator!=(sentinel_type const &s, basic_iterator const &rhs) noexcept
Compares for inequality with sentinel.
Definition: single_pass_input.hpp:286
constexpr basic_iterator & operator=(basic_iterator &&rhs)=default
Move assignment.
reference operator*() const noexcept
Dereferences the cached iterator.
Definition: single_pass_input.hpp:220
detail::iter_pointer_t< base_iterator_type > pointer
Pointer type.
Definition: single_pass_input.hpp:188
constexpr friend bool operator==(sentinel_type const &s, basic_iterator const &rhs) noexcept
Compares for equality with sentinel.
Definition: single_pass_input.hpp:273
Forward declaration.
Definition: single_pass_input.hpp:29
Adds single_pass_input behavior to the underlying range.
Definition: single_pass_input.hpp:39
sentinel end() const =delete
Const version of end is deleted, since the underlying view_state must be mutable.
constexpr single_pass_input_view(single_pass_input_view &&)=default
Default move-constructor.
std::ranges::iterator_t< urng_t > urng_iterator_type
[view_def]
Definition: single_pass_input.hpp:44
constexpr single_pass_input_view & operator=(single_pass_input_view const &)=default
Default copy-assignment.
iterator begin()
Returns an iterator to the current begin of the underlying range.
Definition: single_pass_input.hpp:116
std::shared_ptr< state > state_ptr
Manages the internal state.
Definition: single_pass_input.hpp:60
~single_pass_input_view()=default
Default destructor.
single_pass_input_view(urng_t &&) -> single_pass_input_view< std::views::all_t< urng_t >>
Deduces the single_pass_input_view from the underlying range if it is a std::ranges::viewable_range.
constexpr single_pass_input_view(single_pass_input_view const &)=default
Default copy-constructor.
single_pass_input_view(urng_t _urng)
Construction from the underlying view.
Definition: single_pass_input.hpp:90
sentinel end()
Returns a sentinel.
Definition: single_pass_input.hpp:125
iterator begin() const =delete
Const version of begin is deleted, since the underlying view_state must be mutable.
constexpr single_pass_input_view & operator=(single_pass_input_view &&)=default
Default move-assignment.
std::ranges::sentinel_t< urng_t > sentinel
The sentinel type.
Definition: single_pass_input.hpp:68
constexpr single_pass_input_view()=default
Default default-constructor.
The Concepts library.
constexpr auto single_pass_input
A view adapter that decays most of the range properties and adds single pass behavior.
Definition: single_pass_input.hpp:362
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:70
Provides C++20 additions to the <iterator> header.
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
typename iter_pointer< it_t >::type iter_pointer_t
Return the pointer type of the input type (transformation_trait shortcut).
Definition: iterator_traits.hpp:270
The SeqAn namespace for views.
SeqAn specific customisations in the standard namespace.
Auxiliary header for the views submodule .
Adaptations of concepts from the Ranges TS.
An internal state to capture the underlying range and a cached iterator.
Definition: single_pass_input.hpp:52
urng_iterator_type cached_urng_iter
The cached iterator of the underlying range.
Definition: single_pass_input.hpp:56
urng_t urng
The underlying range.
Definition: single_pass_input.hpp:54