SeqAn3 3.4.0-rc.4
The Modern C++ library for sequence analysis.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
slice.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <concepts>
13#include <iterator>
14#include <ranges>
15#include <span>
16#include <stdexcept>
17#include <type_traits>
18
22
23namespace seqan3::detail
24{
25
26// ============================================================================
27// slice_fn (adaptor definition)
28// ============================================================================
29
31struct slice_fn
32{
34 constexpr auto operator()(ptrdiff_t begin_pos, ptrdiff_t end_pos) const noexcept
35 {
36 return detail::adaptor_from_functor{*this, begin_pos, end_pos};
37 }
38
42 template <std::ranges::viewable_range urng_t>
43 constexpr auto operator()(urng_t && urange,
44 std::ranges::range_difference_t<urng_t> begin_pos,
45 std::ranges::range_difference_t<urng_t> end_pos) const
46 {
47 using position_t = std::ranges::range_difference_t<urng_t>;
48 if constexpr (std::ranges::sized_range<urng_t>)
49 {
50 position_t urange_size = static_cast<position_t>(std::ranges::size(urange));
51
52 begin_pos = std::min(begin_pos, urange_size);
53 end_pos = std::min(end_pos, urange_size);
54 }
55 position_t target_size = end_pos - begin_pos;
56
57 if (end_pos < begin_pos)
58 throw std::invalid_argument{"end_pos argument to seqan3::views::slice must be >= the begin_pos argument."};
59
60 // urange | type_reduce | drop | take
61 return std::views::take(std::views::drop(seqan3::views::type_reduce(std::forward<urng_t>(urange)), begin_pos),
62 target_size);
63 }
64};
65
66} // namespace seqan3::detail
67
68// ============================================================================
69// views::slice (adaptor instance definition)
70// ============================================================================
71
72namespace seqan3::views
73{
137inline constexpr auto slice = detail::slice_fn{};
138
139} // namespace seqan3::views
Provides seqan3::detail::adaptor_from_functor.
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition slice.hpp:137
Provides exceptions used in the I/O module.
T min(T... args)
The SeqAn namespace for views.
Definition char_strictly_to.hpp:19
Provides seqan3::views::type_reduce.
Hide me