MySQL 9.2.0
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 <ranges>
36#include <tuple>
37#include <utility> // declval, forward
38
39#include "mysql/harness/stdx/iterator.h" // iter_reference, iter_value
40
41namespace stdx::ranges {
42
43template <class T>
44using iterator_t = decltype(std::begin(std::declval<T &>()));
45
46template <class R>
48
49template <class R>
51
52/**
53 * enumerate_view over a range.
54 *
55 * @note only implements the const-iterator parts.
56 *
57 * @tparam V a range to enumerate
58 */
59template <class V>
61 private:
62 using Base = V;
63
64 Base base_ = {};
65
66 template <bool>
67 class iterator;
68
69 public:
71
72 constexpr enumerate_view() = default;
73
74 constexpr enumerate_view(V base) : base_(std::forward<Base>(base)) {}
75
76 constexpr auto begin() const { return iterator<true>{std::begin(base_), 0}; }
77
78 constexpr auto end() const { return iterator<true>{std::end(base_), 0}; }
79};
80
81template <class R>
83
84template <class V>
85template <bool Const>
87 private:
88 using Base = std::conditional_t<Const, const V, V>;
89
90 public:
91 using iterator_category = std::input_iterator_tag;
92
93 using index_type = size_t;
94 using reference = std::tuple<index_type, range_reference_t<Base>>;
95 using value_type = std::tuple<index_type, range_value_t<Base>>;
96
97 constexpr explicit iterator(iterator_t<Base> current, index_type pos)
98 : pos_{pos}, current_{std::move(current)} {}
99
100 constexpr bool operator!=(const iterator &other) const {
101 return current_ != other.current_;
102 }
103
104 constexpr iterator &operator++() {
105 ++pos_;
106 ++current_;
107
108 return *this;
109 }
110
111 constexpr decltype(auto) operator*() const {
112 return reference{pos_, *current_};
113 }
114
115 private:
117
119};
120
121namespace views {
122/*
123 * an iterator that wraps an iterable and returns a counter and the
124 * deref'ed wrapped iterable.
125 *
126 * @tparam T a iterable
127 *
128 * @code
129 * for (auto [ndx, vc]: enumerate(std::vector<int>{1, 23, 42})) {
130 * std::cerr << "[" << ndx << "] " << v << "\n";
131 * }
132 *
133 * // [0] 1
134 * // [1] 23
135 * // [2] 42
136 * @endcode
137 *
138 * modelled after P2164 from C++23, but implemented for C++17 (aka without
139 * ranges and views)
140 */
141template <class T, class TIter = decltype(std::begin(std::declval<T>())),
142 class = decltype(std::end(std::declval<T>()))>
143constexpr auto enumerate(T &&iterable) {
144 return enumerate_view{std::forward<T>(iterable)};
145}
146} // namespace views
147
148/**
149 * Checks if a container contains a specified element. Implements
150 * https://en.cppreference.com/w/cpp/algorithm/ranges/contains
151 *
152 * @param container The container in which to search for the needle.
153 * @param needle The element to search for in the container.
154 * @return `true` if the needle is found in the container, `false` otherwise.
155 */
156template <std::ranges::input_range Range, typename T>
157 requires std::convertible_to<T, typename Range::value_type> bool
158contains(const Range &container, const T &needle) {
159 return std::find(container.begin(), container.end(), needle) !=
160 container.end();
161}
162
163} // namespace stdx::ranges
164
165namespace stdx {
166namespace views = ranges::views;
167}
168#endif
std::input_iterator_tag iterator_category
Definition: ranges.h:91
size_t index_type
Definition: ranges.h:93
constexpr iterator & operator++()
Definition: ranges.h:104
std::tuple< index_type, range_reference_t< Base > > reference
Definition: ranges.h:94
iterator_t< Base > current_
Definition: ranges.h:118
index_type pos_
Definition: ranges.h:116
std::tuple< index_type, range_value_t< Base > > value_type
Definition: ranges.h:95
constexpr bool operator!=(const iterator &other) const
Definition: ranges.h:100
std::conditional_t< Const, const V, V > Base
Definition: ranges.h:88
constexpr iterator(iterator_t< Base > current, index_type pos)
Definition: ranges.h:97
enumerate_view over a range.
Definition: ranges.h:60
constexpr auto end() const
Definition: ranges.h:78
V Base
Definition: ranges.h:62
Base base_
Definition: ranges.h:64
constexpr enumerate_view(V base)
Definition: ranges.h:74
constexpr auto begin() const
Definition: ranges.h:76
stdx::iter_value_t< iterator< true > > value_type
Definition: ranges.h:70
constexpr enumerate_view()=default
Definition: atomics_array.h:39
std::pair< os_offset_t, os_offset_t > Range
Represents the chunk in bytes : first element represents the beginning offset of the chunk and,...
Definition: ddl0impl-file-reader.h:44
Container::const_iterator find(const Container &c, Value &&value)
Definition: generic.h:39
const char * begin(const char *const c)
Definition: base64.h:44
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:143
Definition: ranges.h:41
stdx::iter_value_t< ranges::iterator_t< R > > range_value_t
Definition: ranges.h:47
enumerate_view(R &&) -> enumerate_view< R >
bool contains(const Range &container, const T &needle)
Checks if a container contains a specified element.
Definition: ranges.h:158
decltype(std::begin(std::declval< T & >())) iterator_t
Definition: ranges.h:44
stdx::iter_reference_t< ranges::iterator_t< R > > range_reference_t
Definition: ranges.h:50
Definition: bit.h:32
typename indirectly_readable_traits< std::remove_cvref_t< T > >::value_type iter_value_t
Definition: iterator.h:130
typename impl::iter_reference< std::remove_cvref_t< T > >::reference iter_reference_t
Definition: iterator.h:134