TRIQS/itertools
2.0.0
C++ range library
Toggle main menu visibility
Loading...
Searching...
No Matches
stride.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_STRIDE_HPP
23
#define _ITERTOOLS_STRIDE_HPP
24
25
#include "
./iterator_facade.hpp
"
26
#include "
./utils.hpp
"
27
28
#include <cstddef>
29
#include <iterator>
30
#include <stdexcept>
31
#include <utility>
32
33
namespace
itertools {
34
46
template
<
typename
Iter>
struct
stride_iter
: iterator_facade<stride_iter<Iter>, typename std::iterator_traits<Iter>::value_type> {
48
Iter
it
;
49
51
std::ptrdiff_t
stride
{1};
52
54
stride_iter
() =
default
;
55
62
stride_iter
(Iter
it
, std::ptrdiff_t
stride
) :
it
(
it
),
stride
(
stride
) {
63
if
(
stride
<= 0)
throw
std::runtime_error(
"The itertools::strided range requires a positive stride"
);
64
}
65
67
void
increment
() { std::advance(
it
,
stride
); }
68
75
[[nodiscard]]
bool
operator==
(
stride_iter
const
&other)
const
{
return
it
== other.
it
; }
76
81
[[nodiscard]]
decltype
(
auto
)
dereference
()
const
{
return
*
it
; }
82
};
83
92
template
<
typename
R>
struct
strided
{
94
R
rg
;
95
97
std::ptrdiff_t
stride
;
98
100
using
iterator
=
stride_iter
<
decltype
(std::begin(
rg
))>;
101
103
using
const_iterator
=
stride_iter
<
decltype
(std::cbegin(
rg
))>;
104
106
[[nodiscard]]
bool
operator==
(
strided
const
&)
const
=
default
;
107
108
private
:
109
// Helper function to calculate the index of the end iterator.
110
[[nodiscard]] std::ptrdiff_t end_offset()
const
{
111
auto
size =
distance
(std::cbegin(
rg
), std::cend(
rg
));
112
return
(size == 0) ? 0 : ((size - 1) /
stride
+ 1) *
stride
;
113
}
114
115
public
:
120
[[nodiscard]]
iterator
begin
() noexcept {
return
{std::begin(
rg
),
stride
}; }
121
123
[[nodiscard]]
const_iterator
cbegin
() const noexcept {
return
{std::cbegin(
rg
),
stride
}; }
124
126
[[nodiscard]]
const_iterator
begin
() const noexcept {
return
cbegin
(); }
127
133
[[nodiscard]]
iterator
end
() noexcept {
return
{std::next(std::begin(
rg
), end_offset()),
stride
}; }
134
136
[[nodiscard]]
const_iterator
cend
() const noexcept {
return
{std::next(std::cbegin(
rg
), end_offset()),
stride
}; }
137
139
[[nodiscard]]
const_iterator
end
() const noexcept {
return
cend
(); }
140
};
141
167
template
<
typename
R> [[nodiscard]]
strided<R>
stride
(R &&rg, std::ptrdiff_t
stride
) {
return
{std::forward<R>(rg),
stride
}; }
168
169
}
// namespace itertools
170
171
#endif
// _ITERTOOLS_STRIDE_HPP
itertools::stride
strided< R > stride(R &&rg, std::ptrdiff_t stride)
Lazy-stride through a given range.
Definition
stride.hpp:167
itertools::distance
std::iterator_traits< Iter1 >::difference_type distance(Iter1 first, Iter2 last)
Calculate the distance between two iterators.
Definition
utils.hpp:51
iterator_facade.hpp
Provides a CRTP base class for various iterator types in itertools.
itertools::stride_iter
Iterator for an itertools::strided range.
Definition
stride.hpp:46
itertools::stride_iter< decltype(std::begin(rg))>::stride
std::ptrdiff_t stride
Definition
stride.hpp:51
itertools::stride_iter::stride_iter
stride_iter()=default
Default constructor.
itertools::stride_iter::stride_iter
stride_iter(Iter it, std::ptrdiff_t stride)
Construct a strided iterator from a given iterator and a given stride.
Definition
stride.hpp:62
itertools::stride_iter::dereference
decltype(auto) dereference() const
Dereference the iterator.
Definition
stride.hpp:81
itertools::stride_iter::increment
void increment()
Increment the iterator by advancing the original iterator by the stride.
Definition
stride.hpp:67
itertools::stride_iter::operator==
bool operator==(stride_iter const &other) const
Equal-to operator for two itertools::stride_iter objects.
Definition
stride.hpp:75
itertools::stride_iter< decltype(std::begin(rg))>::it
decltype(std::begin(rg)) it
Definition
stride.hpp:48
itertools::strided
Represents a strided range.
Definition
stride.hpp:92
itertools::strided::const_iterator
stride_iter< decltype(std::cbegin(rg))> const_iterator
Const iterator type of the strided range.
Definition
stride.hpp:103
itertools::strided::begin
const_iterator begin() const noexcept
Const overload of begin().
Definition
stride.hpp:126
itertools::strided::end
const_iterator end() const noexcept
Const overload of end().
Definition
stride.hpp:139
itertools::strided::begin
iterator begin() noexcept
Beginning of the strided range.
Definition
stride.hpp:120
itertools::strided::cend
const_iterator cend() const noexcept
Const version of end().
Definition
stride.hpp:136
itertools::strided::cbegin
const_iterator cbegin() const noexcept
Const version of begin().
Definition
stride.hpp:123
itertools::strided::rg
R rg
Original range.
Definition
stride.hpp:94
itertools::strided::stride
std::ptrdiff_t stride
Number of elements in the original range to skip when incrementing the iterator.
Definition
stride.hpp:97
itertools::strided::end
iterator end() noexcept
End of the strided range.
Definition
stride.hpp:133
itertools::strided::operator==
bool operator==(strided const &) const =default
Default equal-to operator.
itertools::strided::iterator
stride_iter< decltype(std::begin(rg))> iterator
Iterator type of the strided range.
Definition
stride.hpp:100
utils.hpp
Provides some utility functions for itertools.
itertools
stride.hpp
Generated by
1.17.0