SeqAn3  3.0.2
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
seqan3::views
The SeqAn namespace for views.
Definition: view_iota_simd.hpp:218
span
Provides std::span from the C++20 standard library.
drop.hpp
Provides seqan3::views::drop.
type_traits
Provides C++20 additions to the type_traits header.
iterator
Provides C++20 additions to the <iterator> header.
concepts
The Concepts library.
concept.hpp
Additional non-standard concepts for ranges.
stdexcept
exception.hpp
Provides exceptions used in the I/O module.
std::invalid_argument
seqan3::views::take
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
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:116
std::min
T min(T... args)
take.hpp
Provides seqan3::views::take.
ranges
Adaptations of concepts from the Ranges TS.
seqan3::views::drop
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:169
seqan3::views::slice
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition: slice.hpp:141
detail.hpp
Auxiliary header for the views submodule .