BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
slice.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
16#include <concepts>
17#include <iterator>
18#include <ranges>
19#include <span>
20#include <stdexcept>
21#include <type_traits>
22
26
27namespace bio::ranges::detail
28{
29
30// ============================================================================
31// slice_fn (adaptor definition)
32// ============================================================================
33
35struct slice_fn
36{
38 constexpr auto operator()(ptrdiff_t begin_pos, ptrdiff_t end_pos) const noexcept
39 {
40 return detail::adaptor_from_functor{*this, begin_pos, end_pos};
41 }
42
46 template <std::ranges::viewable_range urng_t>
47 constexpr auto operator()(urng_t && urange, ptrdiff_t begin_pos, ptrdiff_t end_pos) const
48 {
49 if constexpr (std::ranges::sized_range<urng_t>)
50 {
51 begin_pos = std::min(begin_pos, static_cast<ptrdiff_t>(std::ranges::size(urange)));
52 end_pos = std::min(end_pos, static_cast<ptrdiff_t>(std::ranges::size(urange)));
53 }
54
55 if (end_pos < begin_pos)
56 throw std::invalid_argument{"end_pos argument to bio::views::slice must be >= the begin_pos argument."};
57
58 return std::forward<urng_t>(urange) | std::views::drop(begin_pos) | std::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 bio::ranges::detail
66
67// ============================================================================
68// views::slice (adaptor instance definition)
69// ============================================================================
70
71namespace bio::ranges::views
72{
73
141inline constexpr auto slice = detail::slice_fn{};
142
144
145} // namespace bio::ranges::views
constexpr auto type_reduce
A view adaptor that behaves like std::views::all, but type erases certain ranges.
Definition: type_reduce.hpp:152
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition: slice.hpp:141
T min(T... args)
The BioC++ namespace for views.
Additional non-standard concepts for ranges.
Auxiliary header for the views submodule .
Provides bio::views::type_reduce.