TRIQS/itertools 1.3.0
C++ range library
Loading...
Searching...
No Matches
enumerate.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_ENUMERATE_HPP
23#define _ITERTOOLS_ENUMERATE_HPP
24
25#include "./iterator_facade.hpp"
26#include "./sentinel.hpp"
27
28#include <iterator>
29#include <tuple>
30#include <utility>
31
32namespace itertools {
33
46 template <typename Iter> struct enum_iter : iterator_facade<enum_iter<Iter>, std::pair<long, typename std::iterator_traits<Iter>::value_type>> {
48 Iter it;
49
51 long i = 0;
52
54 enum_iter() = default;
55
60 enum_iter(Iter it) : it(std::move(it)) {}
61
63 void increment() {
64 ++it;
65 ++i;
66 }
67
74 [[nodiscard]] bool operator==(enum_iter const &other) const { return it == other.it; }
75
83 template <typename SentinelIter> [[nodiscard]] bool operator==(sentinel_t<SentinelIter> const &s) const { return it == s.it; }
84
89 [[nodiscard]] decltype(auto) dereference() const { return std::tuple<long, decltype(*it)>{i, *it}; }
90 };
91
100 template <typename R> struct enumerated {
102 R rg;
103
105 using iterator = enum_iter<decltype(std::begin(rg))>;
106
108 using const_iterator = enum_iter<decltype(std::cbegin(rg))>;
109
111 [[nodiscard]] bool operator==(enumerated const &) const = default;
112
117 [[nodiscard]] iterator begin() noexcept { return std::begin(rg); }
118
120 [[nodiscard]] const_iterator cbegin() const noexcept { return std::cbegin(rg); }
121
123 [[nodiscard]] const_iterator begin() const noexcept { return cbegin(); }
124
129 [[nodiscard]] auto end() noexcept { return make_sentinel(std::end(rg)); }
130
132 [[nodiscard]] auto cend() const noexcept { return make_sentinel(std::cend(rg)); }
133
135 [[nodiscard]] auto end() const noexcept { return cend(); }
136 };
137
168 template <typename R> [[nodiscard]] enumerated<R> enumerate(R &&rg) { return {std::forward<R>(rg)}; }
169
170} // namespace itertools
171
172#endif // _ITERTOOLS_ENUMERATE_HPP
enumerated< R > enumerate(R &&rg)
Lazy-enumerate a given range (similar to Python's enumerate).
sentinel_t< Iter > make_sentinel(Iter it)
Create an itertools::sentinel_t from an iterator using template type deduction.
Definition sentinel.hpp:50
Provides a CRTP base class for various iterator types in itertools.
Provides a generic sentinel type for various iterator types in itertools.
Iterator for an itertools::enumerated range.
Definition enumerate.hpp:46
bool operator==(sentinel_t< SentinelIter > const &s) const
Equal-to operator for a itertools::enum_iter and an itertools::sentinel_t.
Definition enumerate.hpp:83
enum_iter(Iter it)
Construct an enumerated iterator from a given iterator and set the index to zero.
Definition enumerate.hpp:60
decltype(auto) dereference() const
Dereference the iterator.
Definition enumerate.hpp:89
long i
Index for enumerating.
Definition enumerate.hpp:51
Iter it
Iterator of the original range.
Definition enumerate.hpp:48
bool operator==(enum_iter const &other) const
Equal-to operator for two itertools::enum_iter objects.
Definition enumerate.hpp:74
void increment()
Increment the iterator by incrementing the original iterator and the index.
Definition enumerate.hpp:63
enum_iter()=default
Default constructor sets the index to zero and default constructs the original iterator.
Represents an enumerated range.
bool operator==(enumerated const &) const =default
Default equal-to operator.
const_iterator begin() const noexcept
Const overload of begin().
iterator begin() noexcept
Beginning of the enumerated range.
const_iterator cbegin() const noexcept
Const version of begin().
auto end() noexcept
End of the enumerated range.
auto cend() const noexcept
Const version of end().
R rg
Original range.
auto end() const noexcept
Const overload of end().
Generic sentinel type that can be used to mark the end of a range.
Definition sentinel.hpp:38
Iter it
End iterator of some range.
Definition sentinel.hpp:40