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