TRIQS/itertools
2.0.0
C++ range library
Toggle main menu visibility
Loading...
Searching...
No Matches
slice.hpp
Go to the documentation of this file.
1
// Copyright (c) 2024 Simons Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0.txt
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell, chuffa
16
21
22
#ifndef _ITERTOOLS_SLICE_HPP
23
#define _ITERTOOLS_SLICE_HPP
24
25
#include "
./utils.hpp
"
26
27
#include <algorithm>
28
#include <cstddef>
29
#include <iterator>
30
#include <utility>
31
32
namespace
itertools {
33
42
template
<
typename
R>
struct
sliced
{
44
R
rg
;
45
47
std::ptrdiff_t
start_idx
;
48
50
std::ptrdiff_t
end_idx
;
51
53
using
iterator
=
decltype
(std::begin(
rg
));
54
56
using
const_iterator
=
decltype
(std::cbegin(
rg
));
57
59
[[nodiscard]]
bool
operator==
(
sliced
const
&)
const
=
default
;
60
65
[[nodiscard]] std::ptrdiff_t
size
()
const
{
66
std::ptrdiff_t total_size =
distance
(std::cbegin(
rg
), std::cend(
rg
));
67
return
std::min(total_size,
end_idx
) -
start_idx
;
68
}
69
74
[[nodiscard]]
iterator
begin
() noexcept {
return
std::next(std::begin(
rg
),
start_idx
); }
75
77
[[nodiscard]]
const_iterator
cbegin
() const noexcept {
return
std::next(std::cbegin(
rg
),
start_idx
); }
78
80
[[nodiscard]]
const_iterator
begin
() const noexcept {
return
cbegin
(); }
81
86
[[nodiscard]]
iterator
end
() noexcept {
return
std::next(
begin
(),
size
()); }
87
89
[[nodiscard]]
const_iterator
cend
() const noexcept {
return
std::next(
cbegin
(),
size
()); }
90
92
[[nodiscard]]
const_iterator
end
() const noexcept {
return
cend
(); }
93
};
94
123
template
<
typename
R> [[nodiscard]]
sliced<R>
slice
(R &&rg, std::ptrdiff_t start_idx, std::ptrdiff_t end_idx) {
124
return
{std::forward<R>(rg), start_idx, std::max(start_idx, end_idx)};
125
}
126
127
}
// namespace itertools
128
129
#endif
// _ITERTOOLS_SLICE_HPP
itertools::slice
sliced< R > slice(R &&rg, std::ptrdiff_t start_idx, std::ptrdiff_t end_idx)
Lazy-slice a given range.
Definition
slice.hpp:123
itertools::distance
std::iterator_traits< Iter1 >::difference_type distance(Iter1 first, Iter2 last)
Calculate the distance between two iterators.
Definition
utils.hpp:51
itertools::sliced
Represents a sliced range.
Definition
slice.hpp:42
itertools::sliced::begin
iterator begin() noexcept
Beginning of the sliced range.
Definition
slice.hpp:74
itertools::sliced::size
std::ptrdiff_t size() const
Helper function to calculate the size of the sliced range.
Definition
slice.hpp:65
itertools::sliced::operator==
bool operator==(sliced const &) const =default
Default equal-to operator.
itertools::sliced::end_idx
std::ptrdiff_t end_idx
Index at which the sliced range ends.
Definition
slice.hpp:50
itertools::sliced::iterator
decltype(std::begin(rg)) iterator
Iterator type of the sliced range.
Definition
slice.hpp:53
itertools::sliced::const_iterator
decltype(std::cbegin(rg)) const_iterator
Const iterator type of the sliced range.
Definition
slice.hpp:56
itertools::sliced::cbegin
const_iterator cbegin() const noexcept
Const version of begin().
Definition
slice.hpp:77
itertools::sliced::rg
R rg
Original range.
Definition
slice.hpp:44
itertools::sliced::end
const_iterator end() const noexcept
Const overload of end().
Definition
slice.hpp:92
itertools::sliced::end
iterator end() noexcept
End of the sliced range.
Definition
slice.hpp:86
itertools::sliced::cend
const_iterator cend() const noexcept
Const version of end().
Definition
slice.hpp:89
itertools::sliced::start_idx
std::ptrdiff_t start_idx
Index at which the sliced range starts.
Definition
slice.hpp:47
itertools::sliced::begin
const_iterator begin() const noexcept
Const overload of begin().
Definition
slice.hpp:80
utils.hpp
Provides some utility functions for itertools.
itertools
slice.hpp
Generated by
1.17.0