MySQL 9.6.0
Source Code Documentation
vector_boundary_storage.h
Go to the documentation of this file.
1// Copyright (c) 2024, 2025, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
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, version 2.0, 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#ifndef MYSQL_SETS_THROWING_VECTOR_BOUNDARY_STORAGE_H
25#define MYSQL_SETS_THROWING_VECTOR_BOUNDARY_STORAGE_H
26
27/// @file
28/// Experimental API header
29///
30/// This file contains the class Vector_boundary_storage, which provides storage
31/// for a boundary container backed by a std::vector.
32
33#include <algorithm> // upper_bound
34#include <vector> // vector
35#include "mysql/allocators/allocator.h" // Allocator
36#include "mysql/allocators/memory_resource.h" // Memory_resource
37#include "mysql/containers/basic_container_wrapper.h" // Basic_container_wrapper
38#include "mysql/iterators/iterator_interface.h" // Iterator_interface
39#include "mysql/iterators/meta.h" // Is_declared_legacy_bidirectional_iterator
40#include "mysql/meta/is_same_ignore_const.h" // Is_same_ignore_const
41#include "mysql/ranges/collection_interface.h" // Collection_interface
42#include "mysql/sets/boundary_set_meta.h" // Is_boundary_storage
43
44/// @addtogroup GroupLibsMysqlSets
45/// @{
46
48
49/// Boundary_iterator based on a std::vector iterator or const iterator.
50template <class Vector_iterator_tp, class Mutable_vector_iterator_tp = void>
52 : public mysql::iterators::Iterator_interface<Vector_boundary_iterator<
53 Vector_iterator_tp, Mutable_vector_iterator_tp>> {
54 using Vector_iterator_t = Vector_iterator_tp;
55 using Mutable_vector_iterator_t = Mutable_vector_iterator_tp;
56
57 public:
58 /// Default constructor. The resulting iterator is in a "pointless" state,
59 /// where the only allowed operation is to assign to it.
61
62 /// Construct an iterator at the given position, and at the given endpoint
63 /// state.
65 : m_position(position), m_is_endpoint(is_endpoint) {}
66
67 /// Converting constructor from the mutable iterator type, enabled if this is
68 /// a const iterator.
71 requires(!std::same_as<Mutable_vector_iterator_t, void>)
72 : m_position(other.vector_iterator()),
73 m_is_endpoint(other.is_endpoint()) {}
74
75 /// @return pointer to the current boundary.
76 [[nodiscard]] auto *get_pointer() const {
77 return std::to_address(m_position);
78 }
79
80 /// Move forward the given number of steps.
81 void advance(std::ptrdiff_t delta) {
82 m_position += delta;
83 if ((delta & 1) != 0) {
85 }
86 }
87
88 /// @return the distance from this iterator to the given one.
89 [[nodiscard]] std::ptrdiff_t distance_from(
90 const Vector_boundary_iterator &other) const {
91 return m_position - other.m_position;
92 }
93
94 /// @return true if this is an endpoint.
95 [[nodiscard]] bool is_endpoint() const { return m_is_endpoint; }
96
97 /// @return the underlying std::vector iterator.
98 [[nodiscard]] const Vector_iterator_t &vector_iterator() const {
99 return m_position;
100 }
101
102 private:
103 /// Iterator to the underlying vector.
105
106 /// True if the current boundary is an endpoint.
107 bool m_is_endpoint{false};
108}; // class Vector_boundary_iterator
109
110template <Is_bounded_set_traits Set_traits_t>
112 std::vector<typename Set_traits_t::Element_t,
114
115} // namespace mysql::sets::throwing::detail
116
117namespace mysql::sets::throwing {
118
119/// Storage for boundary points, backed by std::vector.
120///
121/// The vector is maintained sorted. Insertion and deletion is linear in the
122/// number of elements to the right of the first inserted/deleted element.
123/// upper_bound/lower_bound is logarithmic. The iterator category is
124/// contiguous_iterator.
125///
126/// @tparam Set_traits_tp Bounded set traits describing properties of the
127/// element type.
128template <Is_bounded_set_traits Set_traits_tp>
131 Vector_boundary_storage<Set_traits_tp>,
132 detail::Vector_for_set_traits<Set_traits_tp>>,
134 Vector_boundary_storage<Set_traits_tp>, Set_traits_tp,
135 mysql::ranges::Range_iterator_type<
136 detail::Vector_for_set_traits<Set_traits_tp>>,
137 mysql::ranges::Range_const_iterator_type<
138 detail::Vector_for_set_traits<Set_traits_tp>>,
139 Iterator_get_value> {
141
142 public:
143 using Set_traits_t = Set_traits_tp;
144
145 using Element_t = Set_traits_t::Element_t;
146 using Less_t = Set_traits_t::Less_t;
147
150
151 using Vector_t = std::vector<Element_t, Allocator_t>;
152 using Vector_iterator_t = Vector_t::iterator;
153 using Vector_const_iterator_t = Vector_t::const_iterator;
155
156 static_assert(std::contiguous_iterator<Iterator_t>);
157 static_assert(
161
164
165 static_assert(
166 std::same_as<Vector_t, detail::Vector_for_set_traits<Set_traits_t>>);
167
168 /// Declare that insertion is "slow", i.e., O(N).
169 static constexpr bool has_fast_insertion = false;
170
171 /// Construct a new, empty object, with the given Memory_resource.
173 const Memory_resource_t &memory_resource = Memory_resource_t()) noexcept
174 : Basic_container_wrapper_t(Allocator_t(memory_resource)) {
175 static_assert(Is_boundary_storage<This_t>);
176 }
177
178 /// Constructor copying both values and Memory_resource from another storage.
183
184 /// Constructor copying from another type, using the given Memory_resource.
187 const Memory_resource_t &memory_resource)
189 Allocator_t(memory_resource)) {}
190
191 /// Constructor copying a range defined by the given iterators, using
192 /// memory_resource if given, or the default Memory_resource() otherwise.
193 template <std::input_iterator First_iterator_t>
195 const First_iterator_t &first,
196 const std::sentinel_for<First_iterator_t> auto &last,
197 const Memory_resource_t &memory_resource = Memory_resource_t())
198 : Basic_container_wrapper_t(first, last, Allocator_t(memory_resource)) {}
199
200 // use defaults for copy/move/destructor
205 default;
207
208 /// Assignment operator copying from another type, preserving the existing
209 /// Memory_resource.
212 &source) {
213 this->assign(source);
214 return *this;
215 }
216
217 [[nodiscard]] Vector_t &vector() { return this->wrapped(); }
218 [[nodiscard]] const Vector_t &vector() const { return this->wrapped(); }
219
220 /// @return const iterator to the beginning.
221 [[nodiscard]] auto begin() const {
222 return Const_iterator_t(vector().begin(), false);
223 }
224
225 /// @return const iterator to the end.
226 [[nodiscard]] auto end() const {
227 return Const_iterator_t(vector().end(), false);
228 }
229
230 /// @return mutable iterator to the beginning.
231 [[nodiscard]] auto begin() { return Iterator_t(vector().begin(), false); }
232
233 /// @return mutable iterator to the end.
234 [[nodiscard]] auto end() { return Iterator_t(vector().end(), false); }
235
236 /// @return the number of boundary points.
237 [[nodiscard]] std::size_t size() const { return vector().size(); }
238
239 /// @return true if this object is empty.
240 [[nodiscard]] bool empty() const { return vector().empty(); }
241
242 /// @return true if this object is nonempty.
243 [[nodiscard]] explicit operator bool() const { return (bool)vector(); }
244
245 /// @return the upper bound for the given element in the given storage.
246 template <class Iter_t>
247 [[nodiscard]] static Iter_t upper_bound_impl(
248 mysql::meta::Is_same_ignore_const<This_t> auto &self, const Iter_t &hint,
249 const Element_t &element) {
250 return std::upper_bound(hint, self.end(), element, Less_t());
251 }
252
253 /// Return the lower bound for the given element in the given storage.
254 template <class Iter_t>
255 [[nodiscard]] static Iter_t lower_bound_impl(
256 mysql::meta::Is_same_ignore_const<This_t> auto &self, const Iter_t &hint,
257 const Element_t &element) {
258 return std::lower_bound(hint, self.end(), element, Less_t());
259 }
260
261 /// Erase an even-length range of boundary points.
262 ///
263 /// This invalidates all iterators to elements from left (inclusive) until the
264 /// end.
265 ///
266 /// @param left The first boundary point to remove, inclusive.
267 ///
268 /// @param right The last boundary point to remove, exclusive.
269 ///
270 /// @return Iterator to the next point after the last removed point.
271 ///
272 /// @note It is required that left.is_endpoint() == right.is_endpoint().
273 [[nodiscard]] Iterator_t erase(const Iterator_t &left,
274 const Iterator_t &right) {
275 assert(std::distance(left, right) % 2 == 0);
276 bool is_endpoint = left.is_endpoint();
277 return Iterator_t(
278 vector().erase(left.vector_iterator(), right.vector_iterator()),
279 is_endpoint);
280 }
281
282 /// Insert two boundary points.
283 ///
284 /// This invalidates all iterators.
285 ///
286 /// @param position Iterator to where the interval shall be inserted.
287 ///
288 /// @param v1 The first boundary point to insert.
289 ///
290 /// @param v2 The second boundary point to insert.
291 ///
292 /// @return Iterator to the next point after the inserted point.
293 [[nodiscard]] Iterator_t insert(const Iterator_t &position,
294 const Element_t &v1, const Element_t &v2) {
295 assert(position == begin() || lt(*std::prev(position), v1));
296 assert(lt(v1, v2));
297 assert(position == end() || lt(v2, *position));
298 bool is_endpoint = position.is_endpoint();
299 return Iterator_t(vector().insert(position.vector_iterator(), {v1, v2}) + 2,
300 is_endpoint);
301 }
302
303 /// Modify the element that the iterator points to.
304 ///
305 /// @return iterator to next element.
306 [[nodiscard]] Iterator_t update_point(const Iterator_t &position,
307 const Element_t &element) {
308 *position = element;
309 return std::next(position);
310 }
311
312 private:
313 /// Return true if "left < right", according to the order defined by
314 /// Set_traits_t.
315 [[nodiscard]] static constexpr bool lt(const Element_t &left,
316 const Element_t &right) {
317 return Set_traits_t::lt(left, right);
318 }
319
320 /// Return true if "left <= right", according to the order defined by
321 /// Set_traits_t.
322 [[nodiscard]] static constexpr bool le(const Element_t &left,
323 const Element_t &right) {
324 return Set_traits_t::le(left, right);
325 }
326}; // class Vector_boundary_storage
327
328} // namespace mysql::sets::throwing
329
330// addtogroup GroupLibsMysqlSets
331/// @}
332
333#endif // ifndef MYSQL_SETS_THROWING_VECTOR_BOUNDARY_STORAGE_H
Experimental API header.
Experimental API header.
Allocator using a Memory_resource to do the allocation.
Definition: allocator.h:52
Polymorphism-free memory resource class with custom allocator and deallocator functions.
Definition: memory_resource.h:88
CRTP base class (mixin) to define a wrapper around a container.
Definition: basic_container_wrapper.h:59
auto get_memory_resource() const noexcept
Return the memory resource used by the wrapped object.
Definition: basic_container_wrapper.h:137
auto assign(const First_iterator_t &first, const Sentinel_t &last) noexcept(mysql::utils::Shall_catch::no==mysql::utils::Shall_catch::yes||noexcept(std::declval< Wrapped_t >().assign(first, last)))
Assign a range defined by the two iterators to the wrapped object.
Definition: basic_container_wrapper.h:83
CRTP base class (mixin) that makes your class a standard-compliant iterator, given only a minimal set...
Definition: iterator_interface.h:370
CRTP base class (mixin) to define a set that has upper_bound and lower_bound members.
Definition: upper_lower_bound_interface.h:193
Storage for boundary points, backed by std::vector.
Definition: vector_boundary_storage.h:139
Vector_boundary_storage(const First_iterator_t &first, const std::sentinel_for< First_iterator_t > auto &last, const Memory_resource_t &memory_resource=Memory_resource_t())
Constructor copying a range defined by the given iterators, using memory_resource if given,...
Definition: vector_boundary_storage.h:194
auto begin() const
Definition: vector_boundary_storage.h:221
Set_traits_t::Element_t Element_t
Definition: vector_boundary_storage.h:145
auto begin()
Definition: vector_boundary_storage.h:231
static Iter_t upper_bound_impl(mysql::meta::Is_same_ignore_const< This_t > auto &self, const Iter_t &hint, const Element_t &element)
Definition: vector_boundary_storage.h:247
Vector_boundary_storage(const Is_readable_boundary_storage_over_traits< Set_traits_t > auto &source, const Memory_resource_t &memory_resource)
Constructor copying from another type, using the given Memory_resource.
Definition: vector_boundary_storage.h:185
Vector_t::const_iterator Vector_const_iterator_t
Definition: vector_boundary_storage.h:153
auto end()
Definition: vector_boundary_storage.h:234
Vector_boundary_storage(const Is_readable_boundary_storage_over_traits< Set_traits_t > auto &source)
Constructor copying both values and Memory_resource from another storage.
Definition: vector_boundary_storage.h:179
const Vector_t & vector() const
Definition: vector_boundary_storage.h:218
static Iter_t lower_bound_impl(mysql::meta::Is_same_ignore_const< This_t > auto &self, const Iter_t &hint, const Element_t &element)
Return the lower bound for the given element in the given storage.
Definition: vector_boundary_storage.h:255
Vector_boundary_storage(Vector_boundary_storage &&) noexcept=default
Vector_t & vector()
Definition: vector_boundary_storage.h:217
detail::Vector_boundary_iterator< Vector_const_iterator_t > Const_iterator_t
Definition: vector_boundary_storage.h:160
auto end() const
Definition: vector_boundary_storage.h:226
bool empty() const
Definition: vector_boundary_storage.h:240
Vector_boundary_storage(const Memory_resource_t &memory_resource=Memory_resource_t()) noexcept
Construct a new, empty object, with the given Memory_resource.
Definition: vector_boundary_storage.h:172
Iterator_t erase(const Iterator_t &left, const Iterator_t &right)
Erase an even-length range of boundary points.
Definition: vector_boundary_storage.h:273
mysql::allocators::Memory_resource Memory_resource_t
Definition: vector_boundary_storage.h:148
static constexpr bool lt(const Element_t &left, const Element_t &right)
Return true if "left < right", according to the order defined by Set_traits_t.
Definition: vector_boundary_storage.h:315
Iterator_t update_point(const Iterator_t &position, const Element_t &element)
Modify the element that the iterator points to.
Definition: vector_boundary_storage.h:306
Set_traits_tp Set_traits_t
Definition: vector_boundary_storage.h:143
static constexpr bool has_fast_insertion
Declare that insertion is "slow", i.e., O(N).
Definition: vector_boundary_storage.h:169
Set_traits_t::Less_t Less_t
Definition: vector_boundary_storage.h:146
Iterator_t insert(const Iterator_t &position, const Element_t &v1, const Element_t &v2)
Insert two boundary points.
Definition: vector_boundary_storage.h:293
std::vector< Element_t, Allocator_t > Vector_t
Definition: vector_boundary_storage.h:151
std::size_t size() const
Definition: vector_boundary_storage.h:237
static constexpr bool le(const Element_t &left, const Element_t &right)
Return true if "left <= right", according to the order defined by Set_traits_t.
Definition: vector_boundary_storage.h:322
Vector_boundary_storage(const Vector_boundary_storage &)=default
detail::Vector_boundary_iterator< Vector_iterator_t > Iterator_t
Definition: vector_boundary_storage.h:154
Vector_t::iterator Vector_iterator_t
Definition: vector_boundary_storage.h:152
Boundary_iterator based on a std::vector iterator or const iterator.
Definition: vector_boundary_storage.h:53
Vector_iterator_tp Vector_iterator_t
Definition: vector_boundary_storage.h:54
Vector_boundary_iterator(const Vector_iterator_t &position, bool is_endpoint)
Construct an iterator at the given position, and at the given endpoint state.
Definition: vector_boundary_storage.h:64
Vector_iterator_t m_position
Iterator to the underlying vector.
Definition: vector_boundary_storage.h:104
auto * get_pointer() const
Definition: vector_boundary_storage.h:76
Vector_boundary_iterator()=default
Default constructor.
Mutable_vector_iterator_tp Mutable_vector_iterator_t
Definition: vector_boundary_storage.h:55
Vector_boundary_iterator(const Vector_boundary_iterator< Mutable_vector_iterator_t > &other)
Converting constructor from the mutable iterator type, enabled if this is a const iterator.
Definition: vector_boundary_storage.h:69
std::ptrdiff_t distance_from(const Vector_boundary_iterator &other) const
Definition: vector_boundary_storage.h:89
bool is_endpoint() const
Definition: vector_boundary_storage.h:95
bool m_is_endpoint
True if the current boundary is an endpoint.
Definition: vector_boundary_storage.h:107
const Vector_iterator_t & vector_iterator() const
Definition: vector_boundary_storage.h:98
void advance(std::ptrdiff_t delta)
Move forward the given number of steps.
Definition: vector_boundary_storage.h:81
Experimental API header.
True if the iterator is declared to meet LegacyBidirectionalIterator requirements.
Definition: meta.h:83
true if Type1 and Type2 are equal, const-ness ignored
Definition: is_same_ignore_const.h:39
True if Test is a readable boundary storage, i.e., an object that can be used as the back-end storage...
Definition: boundary_set_meta.h:342
True if Test is a boundary storage over the given Set traits.
Definition: boundary_set_meta.h:313
Experimental API header.
Experimental API header.
Allocator class that uses a polymorphic Memory_resource to allocate memory.
Experimental API header.
Class that wraps resources in a polymorphic manner.
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
void right(std::string *to_trim)
Definition: trim.h:41
void left(std::string *to_trim)
Definition: trim.h:35
Definition: aliases.h:73
std::vector< typename Set_traits_t::Element_t, mysql::allocators::Allocator< typename Set_traits_t::Element_t > > Vector_for_set_traits
Definition: vector_boundary_storage.h:113
Definition: aliases.h:73
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
std::vector< T, ut::allocator< T > > vector
Specialization of vector which uses allocator.
Definition: ut0new.h:2880
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42