SeqAn3  3.0.0
The Modern C++ library for sequence analysis.
alignment_range.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, 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/concepts>
16 #include <seqan3/std/ranges>
17 
18 namespace seqan3
19 {
20 
31 template <typename alignment_executor_type>
32 //TODO requires alignment_executor_concept<alignment_executor_type>
34 {
35  static_assert(!std::is_const_v<alignment_executor_type>,
36  "Cannot create an alignment stream over a const buffer.");
37 
39  class iterator_type
40  {
41  public:
45  using value_type = typename alignment_range::value_type;
47  using reference = typename alignment_range::reference;
49  using pointer = std::add_pointer_t<value_type>;
51  using iterator_category = std::input_iterator_tag;
52 
56  constexpr iterator_type() noexcept = default;
57  constexpr iterator_type(iterator_type const &) noexcept = default;
58  constexpr iterator_type(iterator_type &&) noexcept = default;
59  constexpr iterator_type & operator=(iterator_type const &) noexcept = default;
60  constexpr iterator_type & operator=(iterator_type &&) noexcept = default;
61  ~iterator_type() = default;
62 
64  constexpr iterator_type(alignment_range & range) noexcept : range_ptr(&range)
65  {}
67 
75  reference operator*() const noexcept
76  {
77  return range_ptr->cache;
78  }
80 
85  iterator_type & operator++(/*pre*/) noexcept
87  {
88  range_ptr->next();
89  return *this;
90  }
91 
93  void operator++(int /*post*/) noexcept
94  {
95  ++(*this);
96  }
98 
103  constexpr bool operator==(std::ranges::default_sentinel_t const &) const noexcept
105  {
106  return range_ptr->eof();
107  }
108 
110  friend constexpr bool operator==(std::ranges::default_sentinel_t const & lhs,
111  iterator_type const & rhs) noexcept
112  {
113  return rhs == lhs;
114  }
115 
117  constexpr bool operator!=(std::ranges::default_sentinel_t const & rhs) const noexcept
118  {
119  return !(*this == rhs);
120  }
121 
123  friend constexpr bool operator!=(std::ranges::default_sentinel_t const & lhs,
124  iterator_type const & rhs) noexcept
125  {
126  return rhs != lhs;
127  }
129  private:
131  alignment_range * range_ptr{};
132  };
133 
134  // Befriend the iterator with this class.
135  // TODO Check if this is necessary.
136  friend class iterator_type;
137 
138 public:
139 
141  using difference_type = typename alignment_executor_type::difference_type;
143  using value_type = typename alignment_executor_type::value_type;
145  using reference = typename alignment_executor_type::reference;
147  using iterator = iterator_type;
149  using const_iterator = void;
152 
156  alignment_range() = default;
157  alignment_range(alignment_range const &) = delete;
158  alignment_range(alignment_range &&) = default;
159  alignment_range & operator=(alignment_range const &) = delete;
160  alignment_range & operator=(alignment_range &&) = default;
161  ~alignment_range() = default;
162 
164  explicit alignment_range(alignment_executor_type const & _alignment_executor) = delete;
165 
175  explicit alignment_range(alignment_executor_type && _alignment_executor) :
176  alignment_executor{new alignment_executor_type{std::move(_alignment_executor)}},
177  eof_flag(false)
178  {}
180 
191  constexpr iterator begin()
192  {
193  if (!eof_flag)
194  next();
195  return iterator{*this};
196  }
197 
198  const_iterator begin() const = delete;
199  const_iterator cbegin() const = delete;
200 
208  constexpr sentinel end() noexcept
209  {
210  return {};
211  }
212 
213  constexpr sentinel end() const = delete;
214  constexpr sentinel cend() const = delete;
216 
217 protected:
218 
220  void next()
221  {
222  assert(!eof());
223 
224  if (!alignment_executor)
225  throw std::runtime_error{"No alignment execution buffer available."};
226 
227  if (auto opt = alignment_executor->bump(); opt.has_value())
228  cache = std::move(*opt);
229  else
230  eof_flag = true;
231  }
232 
234  constexpr bool eof() const noexcept
235  {
236  return eof_flag;
237  }
238 
239 private:
241  std::unique_ptr<alignment_executor_type> alignment_executor{};
243  value_type cache{};
245  bool eof_flag{true};
246 };
247 
253 template <typename alignment_executor_type>
255 alignment_range(alignment_executor_type &&) -> alignment_range<std::remove_reference_t<alignment_executor_type>>;
257 
258 } // namespace seqan3
void next()
Receives the next alignment result from the executor buffer.
Definition: alignment_range.hpp:220
::ranges::cbegin cbegin
Alias for ranges::cbegin. Returns an iterator to the beginning of a range.
Definition: ranges:209
alignment_range()=default
Defaulted.
typename alignment_executor_type::reference reference
The reference type.
Definition: alignment_range.hpp:145
alignment_range & operator=(alignment_range const &)=delete
This is a move-only type.
constexpr iterator begin()
Returns an iterator to the first element of the alignment range.
Definition: alignment_range.hpp:191
typename alignment_executor_type::difference_type difference_type
The offset type.
Definition: alignment_range.hpp:141
The main SeqAn3 namespace.
typename alignment_executor_type::value_type value_type
The alignment result type.
Definition: alignment_range.hpp:143
alignment_range(alignment_executor_type &&_alignment_executor)
Constructs a new alignment range by taking ownership over the passed alignment buffer.
Definition: alignment_range.hpp:175
void const_iterator
This range is never const-iterable. The const_iterator is always void.
Definition: alignment_range.hpp:149
The Concepts library.
Adaptations of concepts from the Ranges TS.
std::ranges::default_sentinel_t sentinel
The sentinel type.
Definition: alignment_range.hpp:151
constexpr sentinel end() noexcept
Returns a sentinel signaling the end of the alignment range.
Definition: alignment_range.hpp:208
constexpr bool eof() const noexcept
Returns whether the executor buffer reached is end.
Definition: alignment_range.hpp:234
::ranges::default_sentinel_t default_sentinel_t
Alias for ranges::default_sentinel_t. Type of ranges::default_sentinel.
Definition: iterator:351
::ranges::cend cend
Alias for ranges::cend. Returns an iterator to the end of a range.
Definition: ranges:214
~alignment_range()=default
Defaulted.
The alignment.
Definition: alignment_range.hpp:33
iterator_type iterator
The iterator type.
Definition: alignment_range.hpp:147