MySQL 8.4.1
Source Code Documentation
generic.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 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 *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 *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 Value = typename Container::value_type>
106int index_of(Container &c, Value &&val) {
107 auto it = std::find(c.begin(), c.end(), std::forward<Value>(val));
108 if (c.end() == it) return -1;
109 return std::distance(c.begin(), it);
110}
111
112template <typename Container, typename Find_if>
113void copy_if(const Container &input, Find_if &&find_if, Container &output) {
114 for (auto &e : input) {
115 if (find_if(e)) output.push_back(e);
116 }
117}
118
119template <typename Container, typename Value = typename Container::value_type>
120std::vector<Value> as_vector(const Container &v) {
121 return std::vector<Value>(v.begin(), v.end());
122}
123
124template <typename Value = uint8_t, typename Container>
125std::vector<Value> as_vector_t(const Container &v) {
126 return as_vector<Container, Value>(v);
127}
128
129template <typename Container, typename Value = typename Container::value_type>
130std::set<Value> as_set(const Container &v) {
131 return std::set<Value>(v.begin(), v.end());
132}
133
134template <typename Value = uint8_t, typename Container>
135std::set<Value> as_set_t(const Container &v) {
136 return as_set<Container, Value>(v);
137}
138
139} // namespace container
140} // namespace helper
141
142#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:120
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:130
int index_of(Container &c, Value &&val)
Definition: generic.h:106
std::vector< Value > as_vector_t(const Container &v)
Definition: generic.h:125
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:113
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:135
bool has(const Container &c, Value &&val)
Definition: generic.h:97
bool remove(Container &c, Value &&value)
Definition: generic.h:44
Definition: generic.h:35
const char * begin(const char *const c)
Definition: base64.h:44