SeqAn3  3.0.0
The Modern C++ library for sequence analysis.
slice.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 <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::ViewableRange urng_t>
48  constexpr auto operator()(urng_t && urange, ptrdiff_t begin_pos, ptrdiff_t end_pos) const
49  {
50  if constexpr (std::ranges::SizedRange<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::view::slice must be >= the begin_pos argument."};
58 
59  return std::forward<urng_t>(urange) | view::drop(begin_pos) | view::take(end_pos - begin_pos);
60  }
61 
62  // does not require special overloads, because view::drop and view::take handle the flattening.
63 };
64 
65 } // namespace seqan3::detail
66 
67 // ============================================================================
68 // view::slice (adaptor instance definition)
69 // ============================================================================
70 
71 namespace seqan3::view
72 {
73 
144 inline constexpr auto slice = detail::slice_fn{};
145 
147 
148 } // namespace seqan3::view
Provides exceptions used in the I/O module.
Provides C++20 additions to the <iterator> header.
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:171
Provides seqan3::view::take.
::ranges::size size
Alias for ranges::size. Obtains the size of a range whose size can be calculated in constant time...
Definition: ranges:189
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition: slice.hpp:144
T min(T... args)
Specifies the requirements of a Range type that knows its size in constant time with the size functio...
Additional non-standard concepts for ranges.
The Concepts library.
Auxiliary header for the view submodule .
Adaptations of concepts from the Ranges TS.
The SeqAn3 namespace for views.
Provides std::span from the C++20 standard library.
Definition: aligned_sequence_concept.hpp:35
Provides C++20 additions to the type_traits header.
Provides seqan3::view::drop.
auto constexpr take
A view adaptor that returns the first size elements from the underlying range (or less if the underly...
Definition: take.hpp:596