TRIQS/itertools 1.3.0
C++ range library
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
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
33namespace 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
178 template <typename R> [[nodiscard]] strided<R> stride(R &&rg, std::ptrdiff_t stride) { return {std::forward<R>(rg), stride}; }
179
180} // namespace itertools
181
182#endif // _ITERTOOLS_STRIDE_HPP
strided< R > stride(R &&rg, std::ptrdiff_t stride)
Lazy-stride through a given range.
Definition stride.hpp:178
std::iterator_traits< Iter1 >::difference_type distance(Iter1 first, Iter2 last)
Calculate the distance between two iterators.
Definition utils.hpp:51
Provides a CRTP base class for various iterator types in itertools.
Iterator for a itertools::strided range.
Definition stride.hpp:46
std::ptrdiff_t stride
Number of elements in the original range to skip when incrementing the iterator.
Definition stride.hpp:51
stride_iter()=default
Default constructor.
stride_iter(Iter it, std::ptrdiff_t stride)
Construct a strided iterator from a given iterator and a given stride.
Definition stride.hpp:62
decltype(auto) dereference() const
Dereference the iterator.
Definition stride.hpp:81
void increment()
Increment the iterator by advancing the original iterator by the stride.
Definition stride.hpp:67
bool operator==(stride_iter const &other) const
Equal-to operator for two itertools::stride_iter objects.
Definition stride.hpp:75
Iter it
Iterator of the original range.
Definition stride.hpp:48
Represents a strided range.
Definition stride.hpp:92
const_iterator begin() const noexcept
Const overload of begin().
Definition stride.hpp:126
const_iterator end() const noexcept
Const overload of end().
Definition stride.hpp:139
iterator begin() noexcept
Beginning of the strided range.
Definition stride.hpp:120
const_iterator cend() const noexcept
Const version of end().
Definition stride.hpp:136
const_iterator cbegin() const noexcept
Const version of begin().
Definition stride.hpp:123
R rg
Original range.
Definition stride.hpp:94
std::ptrdiff_t stride
Number of elements in the original range to skip when incrementing the iterator.
Definition stride.hpp:97
iterator end() noexcept
End of the strided range.
Definition stride.hpp:133
bool operator==(strided const &) const =default
Default equal-to operator.
Provides some utility functions for itertools.