MySQL 9.7.0
Source Code Documentation
generic.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2026, 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_UTILITY_CONTAINER_GENERIC_H_
27#define MYSQL_HARNESS_UTILITY_CONTAINER_GENERIC_H_
28
29#include <algorithm>
30#include <cstdint>
31#include <set>
32#include <utility>
33#include <vector>
34
35namespace mysql_harness {
36namespace utility {
37namespace container {
38
39template <typename Container, typename Value = typename Container::value_type>
40typename Container::const_iterator find(const Container &c, Value &&value) {
41 return std::find(c.begin(), c.end(), std::forward<Value>(value));
42}
43
44template <typename Container, typename Value = typename Container::value_type>
45bool remove(Container &c, Value &&value) {
46 auto it = find(c, std::forward<Value>(value));
47
48 if (it == c.end()) return false;
49
50 c.erase(it);
51 return true;
52}
53
54template <typename Container, typename Find_if>
55typename Container::const_iterator find_if(const Container &c,
56 Find_if &&find_if) {
57 return std::find_if(c.begin(), c.end(), std::forward<Find_if>(find_if));
58}
59
60template <typename Container, typename Find_if>
61bool remove_if(Container &c, Find_if &&value) {
62 auto it = find_if<Container, Find_if>(c, std::forward<Find_if>(value));
63
64 if (it == c.end()) return false;
65
66 c.erase(it);
67 return true;
68}
69
70template <typename Container, typename Find_if>
71bool get_ptr_if(const Container &c, Find_if &&find_if,
72 const typename Container::value_type **out) {
73 auto it = std::find_if(c.begin(), c.end(), std::forward<Find_if>(find_if));
74 if (c.end() == it) return false;
75 *out = &(*it);
76 return true;
77}
78
79template <typename Container, typename Find_if>
80bool get_if(Container &c, Find_if &&find_if,
81 typename Container::value_type *out) {
82 auto it = std::find_if(c.begin(), c.end(), std::forward<Find_if>(find_if));
83 if (c.end() == it) return false;
84 if (out) *out = (*it);
85 return true;
86}
87
88template <typename Container, typename Value = typename Container::value_type>
89bool has(const Container &c, Value &&val) {
90 auto b = std::begin(c);
91 auto e = std::end(c);
92 auto it = std::find(b, e, std::forward<Value>(val));
93 if (e == it) return false;
94 return true;
95}
96
97template <typename Container, typename Find_if>
98bool has_if(const Container &c, Find_if &&find_if) {
99 auto it = std::find_if(c.begin(), c.end(), std::forward<Find_if>(find_if));
100 if (c.end() == it) return false;
101 return true;
102}
103
104template <typename Container, typename Value = typename Container::value_type>
105int index_of(Container &c, Value &&val) {
106 auto it = std::find(c.begin(), c.end(), std::forward<Value>(val));
107 if (c.end() == it) return -1;
108 return std::distance(c.begin(), it);
109}
110
111template <typename Container, typename Find_if>
112void copy_if(const Container &input, Find_if &&find_if, Container &output) {
113 for (auto &e : input) {
114 if (find_if(e)) output.push_back(e);
115 }
116}
117
118template <typename Container, typename Value = typename Container::value_type>
119std::vector<Value> as_vector(const Container &v) {
120 return std::vector<Value>(v.begin(), v.end());
121}
122
123template <typename Value = uint8_t, typename Container>
124std::vector<Value> as_vector_t(const Container &v) {
125 return as_vector<Container, Value>(v);
126}
127
128template <typename Container, typename Value = typename Container::value_type>
129std::set<Value> as_set(const Container &v) {
130 return std::set<Value>(v.begin(), v.end());
131}
132
133template <typename Value = uint8_t, typename Container>
134std::set<Value> as_set_t(const Container &v) {
135 return as_set<Container, Value>(v);
136}
137
138} // namespace container
139} // namespace utility
140} // namespace mysql_harness
141
142#endif // MYSQL_HARNESS_UTILITY_CONTAINER_GENERIC_H_
uint16_t value_type
Definition: vt100.h:184
Definition: atomics_array.h:39
bool distance(const dd::Spatial_reference_system *srs, const Geometry *g1, const Geometry *g2, double *distance, bool *is_null) noexcept
Computes the distance between two geometries.
Definition: distance.cc:40
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
bool remove_if(Container &c, Find_if &&value)
Definition: generic.h:61
std::vector< Value > as_vector_t(const Container &v)
Definition: generic.h:124
Container::const_iterator find(const Container &c, Value &&value)
Definition: generic.h:40
std::vector< Value > as_vector(const Container &v)
Definition: generic.h:119
std::set< Value > as_set(const Container &v)
Definition: generic.h:129
bool has(const Container &c, Value &&val)
Definition: generic.h:89
bool remove(Container &c, Value &&value)
Definition: generic.h:45
Container::const_iterator find_if(const Container &c, Find_if &&find_if)
Definition: generic.h:55
std::set< Value > as_set_t(const Container &v)
Definition: generic.h:134
bool get_if(Container &c, Find_if &&find_if, typename Container::value_type *out)
Definition: generic.h:80
bool get_ptr_if(const Container &c, Find_if &&find_if, const typename Container::value_type **out)
Definition: generic.h:71
int index_of(Container &c, Value &&val)
Definition: generic.h:105
bool has_if(const Container &c, Find_if &&find_if)
Definition: generic.h:98
void copy_if(const Container &input, Find_if &&find_if, Container &output)
Definition: generic.h:112
Definition: common.h:44
const char * begin(const char *const c)
Definition: base64.h:44