MySQL 8.0.40
Source Code Documentation
ranges.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_STDX_RANGES_ENUMERATE_H
27#define MYSQL_HARNESS_STDX_RANGES_ENUMERATE_H
28
29//
30// From C++23's P2164
31//
32// http://wg21.link/p2164
33
34#include <iterator>
35#include <tuple>
36#include <utility> // declval, forward
37
38#include "mysql/harness/stdx/iterator.h" // iter_reference, iter_value
39
40namespace stdx::ranges {
41
42template <class T>
43using iterator_t = decltype(std::begin(std::declval<T &>()));
44
45template <class R>
47
48template <class R>
50
51/**
52 * enumerate_view over a range.
53 *
54 * @note only implements the const-iterator parts.
55 *
56 * @tparam V a range to enumerate
57 */
58template <class V>
60 private:
61 using Base = V;
62
63 Base base_ = {};
64
65 template <bool>
66 class iterator;
67
68 public:
70
71 constexpr enumerate_view() = default;
72
73 constexpr enumerate_view(V base) : base_(std::forward<Base>(base)) {}
74
75 constexpr auto begin() const { return iterator<true>{std::begin(base_), 0}; }
76
77 constexpr auto end() const { return iterator<true>{std::end(base_), 0}; }
78};
79
80template <class R>
82
83template <class V>
84template <bool Const>
86 private:
87 using Base = std::conditional_t<Const, const V, V>;
88
89 public:
90 using iterator_category = std::input_iterator_tag;
91
92 using index_type = size_t;
93 using reference = std::tuple<index_type, range_reference_t<Base>>;
94 using value_type = std::tuple<index_type, range_value_t<Base>>;
95
96 constexpr explicit iterator(iterator_t<Base> current, index_type pos)
97 : pos_{pos}, current_{std::move(current)} {}
98
99 constexpr bool operator!=(const iterator &other) const {
100 return current_ != other.current_;
101 }
102
103 constexpr iterator &operator++() {
104 ++pos_;
105 ++current_;
106
107 return *this;
108 }
109
110 constexpr decltype(auto) operator*() const {
111 return reference{pos_, *current_};
112 }
113
114 private:
116
118};
119
120namespace views {
121/*
122 * an iterator that wraps an iterable and returns a counter and the
123 * deref'ed wrapped iterable.
124 *
125 * @tparam T a iterable
126 *
127 * @code
128 * for (auto [ndx, vc]: enumerate(std::vector<int>{1, 23, 42})) {
129 * std::cerr << "[" << ndx << "] " << v << "\n";
130 * }
131 *
132 * // [0] 1
133 * // [1] 23
134 * // [2] 42
135 * @endcode
136 *
137 * modelled after P2164 from C++23, but implemented for C++17 (aka without
138 * ranges and views)
139 */
140template <class T, class TIter = decltype(std::begin(std::declval<T>())),
141 class = decltype(std::end(std::declval<T>()))>
142constexpr auto enumerate(T &&iterable) {
143 return enumerate_view{std::forward<T>(iterable)};
144}
145} // namespace views
146} // namespace stdx::ranges
147
148namespace stdx {
149namespace views = ranges::views;
150}
151#endif
std::input_iterator_tag iterator_category
Definition: ranges.h:90
size_t index_type
Definition: ranges.h:92
constexpr iterator & operator++()
Definition: ranges.h:103
std::tuple< index_type, range_reference_t< Base > > reference
Definition: ranges.h:93
iterator_t< Base > current_
Definition: ranges.h:117
index_type pos_
Definition: ranges.h:115
std::tuple< index_type, range_value_t< Base > > value_type
Definition: ranges.h:94
constexpr bool operator!=(const iterator &other) const
Definition: ranges.h:99
std::conditional_t< Const, const V, V > Base
Definition: ranges.h:87
constexpr iterator(iterator_t< Base > current, index_type pos)
Definition: ranges.h:96
enumerate_view over a range.
Definition: ranges.h:59
constexpr auto end() const
Definition: ranges.h:77
V Base
Definition: ranges.h:61
Base base_
Definition: ranges.h:63
constexpr enumerate_view(V base)
Definition: ranges.h:73
constexpr auto begin() const
Definition: ranges.h:75
stdx::iter_value_t< iterator< true > > value_type
Definition: ranges.h:69
constexpr enumerate_view()=default
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
Definition: gcs_xcom_synode.h:64
constexpr auto enumerate(T &&iterable)
Definition: ranges.h:142
Definition: ranges.h:40
stdx::iter_value_t< ranges::iterator_t< R > > range_value_t
Definition: ranges.h:46
enumerate_view(R &&) -> enumerate_view< R >
decltype(std::begin(std::declval< T & >())) iterator_t
Definition: ranges.h:43
stdx::iter_reference_t< ranges::iterator_t< R > > range_reference_t
Definition: ranges.h:49
Definition: bit.h:34
typename indirectly_readable_traits< stdx::remove_cvref_t< T > >::value_type iter_value_t
Definition: iterator.h:132
typename impl::iter_reference< stdx::remove_cvref_t< T > >::reference iter_reference_t
Definition: iterator.h:136