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