SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
slice.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 <stdexcept>
16 
17 #include <seqan3/io/exception.hpp>
18 #include <seqan3/range/concept.hpp>
22 #include <seqan3/std/concepts>
23 #include <seqan3/std/iterator>
24 #include <seqan3/std/ranges>
25 #include <seqan3/std/span>
26 #include <seqan3/std/type_traits>
27 
28 namespace seqan3::detail
29 {
30 
31 // ============================================================================
32 // slice_fn (adaptor definition)
33 // ============================================================================
34 
36 struct slice_fn
37 {
39  constexpr auto operator()(ptrdiff_t begin_pos, ptrdiff_t end_pos) const noexcept
40  {
41  return detail::adaptor_from_functor{*this, begin_pos, end_pos};
42  }
43 
47  template <std::ranges::viewable_range urng_t>
48  constexpr auto operator()(urng_t && urange, ptrdiff_t begin_pos, ptrdiff_t end_pos) const
49  {
50  if constexpr (std::ranges::sized_range<urng_t>)
51  {
52  begin_pos = std::min(begin_pos, static_cast<ptrdiff_t>(std::ranges::size(urange)));
53  end_pos = std::min(end_pos, static_cast<ptrdiff_t>(std::ranges::size(urange)));
54  }
55 
56  if (end_pos < begin_pos)
57  throw std::invalid_argument{"end_pos argument to seqan3::views::slice must be >= the begin_pos argument."};
58 
59  return std::forward<urng_t>(urange) | views::drop(begin_pos) | views::take(end_pos - begin_pos);
60  }
61 
62  // does not require special overloads, because views::drop and views::take handle the flattening.
63 };
64 
65 } // namespace seqan3::detail
66 
67 // ============================================================================
68 // views::slice (adaptor instance definition)
69 // ============================================================================
70 
71 namespace seqan3::views
72 {
73 
141 inline constexpr auto slice = detail::slice_fn{};
142 
144 
145 } // namespace seqan3::views
Template for range adaptor closure objects that store arguments and wrap a proto-adaptor.
Definition: detail.hpp:371
The Concepts library.
Provides seqan3::views::drop.
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:150
constexpr auto drop
A view adaptor that returns all elements after n from the underlying range (or an empty range if the ...
Definition: drop.hpp:170
constexpr auto take
A view adaptor that returns the first size elements from the underlying range (or less if the underly...
Definition: take.hpp:611
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition: slice.hpp:141
Provides exceptions used in the I/O module.
Provides C++20 additions to the <iterator> header.
T min(T... args)
The internal SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
The SeqAn namespace for views.
Additional non-standard concepts for ranges.
Auxiliary header for the views submodule .
Adaptations of concepts from the Ranges TS.
Provides std::span from the C++20 standard library.
View adaptor definition for views::slice.
Definition: slice.hpp:37
constexpr auto operator()(urng_t &&urange, ptrdiff_t begin_pos, ptrdiff_t end_pos) const
Call the view's constructor with the underlying view as argument.
Definition: slice.hpp:48
constexpr auto operator()(ptrdiff_t begin_pos, ptrdiff_t end_pos) const noexcept
Store the arguments and return a range adaptor closure object.
Definition: slice.hpp:39
Provides seqan3::views::take.
Provides C++20 additions to the type_traits header.