TRIQS/itertools 1.3.0
C++ range library
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
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
32namespace 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
137 template <typename R> [[nodiscard]] sliced<R> slice(R &&rg, std::ptrdiff_t start_idx, std::ptrdiff_t end_idx) {
138 return {std::forward<R>(rg), start_idx, std::max(start_idx, end_idx)};
139 }
140
141} // namespace itertools
142
143#endif // _ITERTOOLS_SLICE_HPP
sliced< R > slice(R &&rg, std::ptrdiff_t start_idx, std::ptrdiff_t end_idx)
Lazy-slice a given range.
Definition slice.hpp:137
std::iterator_traits< Iter1 >::difference_type distance(Iter1 first, Iter2 last)
Calculate the distance between two iterators.
Definition utils.hpp:51
Represents a sliced range.
Definition slice.hpp:42
iterator begin() noexcept
Beginning of the sliced range.
Definition slice.hpp:74
std::ptrdiff_t size() const
Helper function to calculate the size of the sliced range.
Definition slice.hpp:65
bool operator==(sliced const &) const =default
Default equal-to operator.
std::ptrdiff_t end_idx
Index at which the sliced range ends.
Definition slice.hpp:50
decltype(std::begin(rg)) iterator
Iterator type of the sliced range.
Definition slice.hpp:53
decltype(std::cbegin(rg)) const_iterator
Const iterator type of the sliced range.
Definition slice.hpp:56
const_iterator cbegin() const noexcept
Const version of begin().
Definition slice.hpp:77
R rg
Original range.
Definition slice.hpp:44
const_iterator end() const noexcept
Const overload of end().
Definition slice.hpp:92
iterator end() noexcept
End of the sliced range.
Definition slice.hpp:86
const_iterator cend() const noexcept
Const version of end().
Definition slice.hpp:89
std::ptrdiff_t start_idx
Index at which the sliced range starts.
Definition slice.hpp:47
const_iterator begin() const noexcept
Const overload of begin().
Definition slice.hpp:80
Provides some utility functions for itertools.