SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
deep.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/ranges>
17 
18 namespace seqan3::views
19 {
20 
100 template <typename underlying_adaptor_t>
101 class deep : public detail::adaptor_base<deep<underlying_adaptor_t>, underlying_adaptor_t>
102 {
103 private:
106 
108  friend base_type;
109 
110 public:
114  constexpr deep() noexcept = default;
115  constexpr deep(deep const &) noexcept = default;
116  constexpr deep(deep &&) noexcept = default;
117  constexpr deep & operator=(deep const &) noexcept = default;
118  constexpr deep & operator=(deep &&) noexcept = default;
119  ~deep() noexcept = default;
120 
121  using base_type::base_type;
123 
125 
127  using base_type::operator();
128 
136  template <std::ranges::input_range urng_t, typename underlying_adaptor_t_>
137  static constexpr auto impl(urng_t && urange, underlying_adaptor_t_ && deep_adaptor)
138  {
139  static_assert(std::same_as<underlying_adaptor_t_, underlying_adaptor_t>,
140  "Internally stored deep-adaptor does not match!");
141 
142  constexpr size_t range_dimension = range_dimension_v<urng_t>;
143 
144  // note: if range_dimension == 1, the expression will actually be
145  // `std::forward<underlying_adaptor_t_>(adap)(urange)`, thus allowing the stateful adaptor to move its arguments
146  // into the view that will be constructed.
147  return recursive_adaptor<range_dimension>(std::forward<underlying_adaptor_t_>(deep_adaptor))(urange);
148  }
149 
171  template <std::size_t range_dimension>
172  static constexpr decltype(auto) recursive_adaptor(underlying_adaptor_t deep_adaptor)
173  {
174  if constexpr (range_dimension > 1u)
175  {
176  auto transform
177  = [adaptor = recursive_adaptor<range_dimension - 1u>(std::forward<underlying_adaptor_t>(deep_adaptor))]
178  (auto && inner_range)
179  {
180  // We don't want to move a stateful adaptor here, as this adaptor will be called on any element of this
181  // std::views::transform range.
182  return adaptor(std::forward<decltype(inner_range)>(inner_range));
183  };
185  }
186  else
187  {
188  // recursion anchor: only the innermost std::views::transform will store the deep_adaptor.
189  // In an earlier version of seqan3 we recursively passed the deep_adaptor through all std::view::transform
190  // instances and each depth stored the same deep_adaptor. The current approach can save memory as it only
191  // stores the deep_adaptor once in the innermost std::views::transform. It will also produce slightly better
192  // stack-traces by defining the recursion over the range_dimension.
193 
194  // NOTE: to allow an lvalue to be returned we need to have the return type decltype(auto).
195  return deep_adaptor;
196  }
197  }
198 
207  template <typename first_arg_t, typename ...stored_arg_types>
209  requires (!std::ranges::input_range<first_arg_t>)
211  constexpr auto operator()(first_arg_t && first, stored_arg_types && ...args) const
212  {
213  // The adaptor currently wrapped is a proto-adaptor and this function has the arguments to "complete" it.
214  // We extract the adaptor that is stored and invoke it with the given arguments.
215  // This returns an adaptor closure object.
216  auto adaptor_closure = std::get<0>(this->arguments)(std::forward<first_arg_t>(first),
217  std::forward<stored_arg_types>(args)...);
218  // Now we wrap this closure object back into a views::deep to get the deep behaviour.
219  return deep<decltype(adaptor_closure)>{std::move(adaptor_closure)};
220  }
221 
223  constexpr auto operator()() const
224  {
225  // Proto-adaptors require arguments by definition, but some support defaulting those (e.g. views::translate).
226  // This extracts the proto adaptor and invokes it without args which yields a different object, the closure
227  // with default arguments.
228  auto adaptor_closure = std::get<0>(this->arguments)();
229  // Now we wrap this closure object back into a views::deep to get the deep behaviour.
230  return deep<decltype(adaptor_closure)>{std::move(adaptor_closure)};
231  }
232 
245  template <std::ranges::input_range urng_t, typename ...stored_arg_types>
247  requires (sizeof...(stored_arg_types) > 0)
249  constexpr auto operator()(urng_t && urange, stored_arg_types && ...args) const
250  {
251  auto adaptor_closure = std::get<0>(this->arguments)(std::forward<stored_arg_types>(args)...);
252  deep<decltype(adaptor_closure)> deep_adaptor{std::move(adaptor_closure)};
253  return deep_adaptor(std::forward<urng_t>(urange));
254  }
255 };
256 
262 template <typename underlying_adaptor_t>
263 deep(underlying_adaptor_t && inner) -> deep<underlying_adaptor_t>;
264 
266 
267 } // namespace seqan3::views
CRTP-base to simplify the definition of range adaptor closure objects and similar types.
Definition: detail.hpp:79
std::tuple< stored_args_ts... > arguments
Stores the arguments.
Definition: detail.hpp:82
A wrapper type around an existing view adaptor that enables "deep view" behaviour for that view.
Definition: deep.hpp:102
deep(underlying_adaptor_t &&inner) -> deep< underlying_adaptor_t >
Template argument deduction helper that preserves lvalue references and turns rvalue references into ...
static constexpr auto impl(urng_t &&urange, underlying_adaptor_t_ &&deep_adaptor)
Unwrap the internal adaptor closure object and pipe the range into it.
Definition: deep.hpp:137
constexpr auto operator()() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: deep.hpp:223
friend base_type
Befriend the base class so it can call impl().
Definition: deep.hpp:108
static constexpr decltype(auto) recursive_adaptor(underlying_adaptor_t deep_adaptor)
recursively construct the deep adaptor
Definition: deep.hpp:172
constexpr deep() noexcept=default
Defaulted.
T forward(T... args)
decltype(detail::transform< trait_t >(list_t{})) transform
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: traits.hpp:434
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:70
The SeqAn namespace for views.
Definition: async_input_buffer.hpp:343
SeqAn specific customisations in the standard namespace.
Auxiliary header for the views submodule .
Adaptations of concepts from the Ranges TS.