MySQL 9.6.0
Source Code Documentation
boundary_set_const_views.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_BOUNDARY_SET_CONST_VIEWS_H
25#define MYSQL_SETS_BOUNDARY_SET_CONST_VIEWS_H
26
27/// @file
28/// Experimental API header
29///
30/// This file defines *constant boundary set views*. These are boundary
31/// sets whose values are specified at compile time, including:
32///
33/// - The empty set, represented by the class Empty_boundary_view
34/// and produced by the factory function make_empty_boundary_view.
35///
36/// - The "full" set, containing all values in the Set traits, represented
37/// by the class Full_boundary_view and produced by the factory
38/// function make_full_boundary_view.
39///
40/// - Arbitrary sets, represented by the class
41/// Const_boundary_view. The boundary values are given as template
42/// arguments.
43
44#include <algorithm> // upper_bound
45#include <array> // array
46#include "mysql/iterators/iterator_interface.h" // Iterator_interface
47#include "mysql/meta/is_same_ignore_const.h" // Is_same_ignore_const
48#include "mysql/sets/base_const_views.h" // Empty_set_view
49#include "mysql/sets/boundary_set_category.h" // Boundary_set_category_tag
50#include "mysql/sets/boundary_set_interface.h" // Boundary_view_interface
51#include "mysql/sets/ordered_set_traits_interface.h" // Int_set_traits
52#include "mysql/sets/set_traits.h" // Is_bounded_set_traits
53
54/// @addtogroup GroupLibsMysqlSets
55/// @{
56
57namespace mysql::sets::detail {
58
59/// Contiguous iterator over a Const_boundary_view.
60///
61/// @tparam Set_traits_tp Bounded set traits for the set type.
62///
63/// @tparam boundaries Values of all boundaries. This must have an even number
64/// of elements.
65template <Is_bounded_set_traits Set_traits_tp,
66 typename Set_traits_tp::Element_t... boundaries>
69 Const_boundary_view_iterator<Set_traits_tp, boundaries...>> {
70 public:
71 using Set_traits_t = Set_traits_tp;
72 using Element_t = Set_traits_t::Element_t;
73 static constexpr auto size = sizeof...(boundaries);
74 using Array_t = std::array<Element_t, size>;
75 using Iterator_category_t = std::contiguous_iterator_tag;
76
77 static_assert(size % 2 == 0, "Const_boundary_view size must be even.");
78
79 /// Construct an iterator to the beginning.
81
82 /// Construct an iterator to the std::next(begin(), position).
83 explicit Const_boundary_view_iterator(int position) : m_position(position) {}
84
85 /// Return the boundary at the current position.
86 [[nodiscard]] constexpr const Element_t *get_pointer() const {
87 return array().data() + m_position;
88 }
89
90 /// Move the position forward by the given number of steps.
91 constexpr void advance(std::ptrdiff_t delta) { m_position += delta; }
92
93 /// Return the distance from the other iterator to this one.
94 [[nodiscard]] constexpr std::ptrdiff_t distance_from(
95 const Const_boundary_view_iterator &other) const {
96 return m_position - other.m_position;
97 }
98
99 /// Return true if the current position is an endpoint.
100 [[nodiscard]] constexpr bool is_endpoint() const {
101 return (m_position & 1) == 1;
102 }
103
104 /// Return an array of the values.
105 [[nodiscard]] static const Array_t &array() {
106 static const Array_t ret{boundaries...};
107 return ret;
108 }
109
110 private:
111 // Index to the current position within the array.
113}; // class Const_boundary_view_iterator
114
115} // namespace mysql::sets::detail
116
117namespace mysql::sets {
118
119/// Boundary set view for which the values are defined at compile-time.
120///
121/// @tparam Set_traits_tp Bounded set traits for the set type.
122///
123/// @tparam boundaries Values of all boundaries. This must have an even number
124/// of elements.
125template <Is_bounded_set_traits Set_traits_tp,
126 typename Set_traits_tp::Element_t... boundaries>
129 Const_boundary_view<Set_traits_tp, boundaries...>,
130 detail::Const_boundary_view_iterator<Set_traits_tp, boundaries...>,
131 detail::Const_boundary_view_iterator<Set_traits_tp, boundaries...>,
132 Set_traits_tp> {
133 using This_t = Const_boundary_view<Set_traits_tp, boundaries...>;
134
135 public:
136 using Set_traits_t = Set_traits_tp;
137 using Element_t = typename Set_traits_t::Element_t;
138 using Less_t = typename Set_traits_t::Less_t;
140 detail::Const_boundary_view_iterator<Set_traits_tp, boundaries...>;
142
143 /// @return Iterator to the beginning.
144 [[nodiscard]] auto begin() const { return Iterator_t(); }
145
146 /// @return Iterator to the end.
147 [[nodiscard]] auto end() const { return Iterator_t(Iterator_t::size); }
148
149 /// Only for internal use by the CRTP base class.
150 ///
151 /// Return the upper bound for the given element in this object.
152 template <class Iter_t>
153 [[nodiscard]] static constexpr Iter_t upper_bound_impl(
154 mysql::meta::Is_same_ignore_const<This_t> auto &self, const Iter_t &hint,
155 const Element_t &element) {
156 return std::upper_bound(hint, self.end(), element, Less_t());
157 }
158
159 /// Only for internal use by the CRTP base class.
160 ///
161 /// Return the lower bound for the given element in this object.
162 template <class Iter_t>
163 [[nodiscard]] static constexpr Iter_t lower_bound_impl(
164 mysql::meta::Is_same_ignore_const<This_t> auto &self, const Iter_t &hint,
165 const Element_t &element) {
166 return std::lower_bound(hint, self.end(), element, Less_t());
167 }
168}; // class Const_boundary_view
169
170/// View over the empty Boundary set for the given Set traits.
171template <Is_bounded_set_traits Set_traits_tp>
173 : public Const_boundary_view<Set_traits_tp> {};
174
175/// View over the Boundary set containing the full range of values for
176/// the given Set traits.
177template <Is_bounded_set_traits Set_traits_tp>
179 : public Const_boundary_view<Set_traits_tp, Set_traits_tp::min(),
180 Set_traits_tp::max_exclusive()> {};
181
182} // namespace mysql::sets
183
184// addtogroup GroupLibsMysqlSets
185/// @}
186
187#endif // ifndef MYSQL_SETS_BOUNDARY_SET_CONST_VIEWS_H
Experimental API header.
Experimental API header.
Experimental API header.
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, used to implement Boundary Sets that are views.
Definition: boundary_set_interface.h:106
Boundary set view for which the values are defined at compile-time.
Definition: boundary_set_const_views.h:132
typename Set_traits_t::Less_t Less_t
Definition: boundary_set_const_views.h:138
static constexpr Iter_t upper_bound_impl(mysql::meta::Is_same_ignore_const< This_t > auto &self, const Iter_t &hint, const Element_t &element)
Only for internal use by the CRTP base class.
Definition: boundary_set_const_views.h:153
auto end() const
Definition: boundary_set_const_views.h:147
typename Set_traits_t::Element_t Element_t
Definition: boundary_set_const_views.h:137
detail::Const_boundary_view_iterator< Set_traits_tp, boundaries... > Iterator_t
Definition: boundary_set_const_views.h:140
Set_traits_tp Set_traits_t
Definition: boundary_set_const_views.h:136
static constexpr Iter_t lower_bound_impl(mysql::meta::Is_same_ignore_const< This_t > auto &self, const Iter_t &hint, const Element_t &element)
Only for internal use by the CRTP base class.
Definition: boundary_set_const_views.h:163
auto begin() const
Definition: boundary_set_const_views.h:144
Forward declaration of primary template for views over empty sets.
Definition: base_const_views.h:46
Forward declaration of primary template for views over "full" sets, i.e., the complement of the empty...
Definition: base_const_views.h:72
Contiguous iterator over a Const_boundary_view.
Definition: boundary_set_const_views.h:69
Const_boundary_view_iterator() noexcept=default
Construct an iterator to the beginning.
int m_position
Definition: boundary_set_const_views.h:112
static constexpr auto size
Definition: boundary_set_const_views.h:73
std::contiguous_iterator_tag Iterator_category_t
Definition: boundary_set_const_views.h:75
Set_traits_t::Element_t Element_t
Definition: boundary_set_const_views.h:72
constexpr const Element_t * get_pointer() const
Return the boundary at the current position.
Definition: boundary_set_const_views.h:86
std::array< Element_t, size > Array_t
Definition: boundary_set_const_views.h:74
constexpr bool is_endpoint() const
Return true if the current position is an endpoint.
Definition: boundary_set_const_views.h:100
constexpr std::ptrdiff_t distance_from(const Const_boundary_view_iterator &other) const
Return the distance from the other iterator to this one.
Definition: boundary_set_const_views.h:94
constexpr void advance(std::ptrdiff_t delta)
Move the position forward by the given number of steps.
Definition: boundary_set_const_views.h:91
Set_traits_tp Set_traits_t
Definition: boundary_set_const_views.h:71
static const Array_t & array()
Return an array of the values.
Definition: boundary_set_const_views.h:105
true if Type1 and Type2 are equal, const-ness ignored
Definition: is_same_ignore_const.h:39
Experimental API header.
Experimental API header.
Definition: aliases.h:97
Definition: gtid_set.h:183
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
Experimental API header.
Experimental API header.
Tag to identify a class as a Boundary set.
Definition: boundary_set_category.h:41