Loading...
Searching...
No Matches
slice.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <concepts>
#include <iterator>
#include <ranges>
#include <span>
#include <stdexcept>
#include <type_traits>
#include <seqan3/core/range/detail/adaptor_from_functor.hpp>
#include <seqan3/io/exception.hpp>
#include <seqan3/utility/views/type_reduce.hpp>
namespace seqan3::detail
{
// ============================================================================
// slice_fn (adaptor definition)
// ============================================================================
struct slice_fn
{
constexpr auto operator()(ptrdiff_t begin_pos, ptrdiff_t end_pos) const noexcept
{
return detail::adaptor_from_functor{*this, begin_pos, end_pos};
}
template <std::ranges::viewable_range urng_t>
constexpr auto operator()(urng_t && urange,
std::ranges::range_difference_t<urng_t> begin_pos,
std::ranges::range_difference_t<urng_t> end_pos) const
{
using position_t = std::ranges::range_difference_t<urng_t>;
if constexpr (std::ranges::sized_range<urng_t>)
{
position_t urange_size = static_cast<position_t>(std::ranges::size(urange));
begin_pos = std::min(begin_pos, urange_size);
end_pos = std::min(end_pos, urange_size);
}
position_t target_size = end_pos - begin_pos;
if (end_pos < begin_pos)
throw std::invalid_argument{"end_pos argument to seqan3::views::slice must be >= the begin_pos argument."};
// urange | type_reduce | drop | take
return std::views::take(std::views::drop(seqan3::views::type_reduce(std::forward<urng_t>(urange)), begin_pos),
target_size);
}
};
} // namespace seqan3::detail
// ============================================================================
// views::slice (adaptor instance definition)
// ============================================================================
namespace seqan3::views
{
inline constexpr auto slice = detail::slice_fn{};
} // namespace seqan3::views
adaptor_from_functor.hpp
Provides seqan3::detail::adaptor_from_functor.
std::forward
T forward(T... args)
seqan3::views::slice
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition slice.hpp:137
seqan3::views::type_reduce
constexpr auto type_reduce
A view adaptor that behaves like std::views::all, but type erases certain ranges.
Definition type_reduce.hpp:147
exception.hpp
Provides exceptions used in the I/O module.
iterator
std::min
T min(T... args)
seqan3::views
The SeqAn namespace for views.
Definition char_strictly_to.hpp:19
stdexcept
type_reduce.hpp
Provides seqan3::views::type_reduce.
type_traits
Hide me