MySQL 9.6.0
Source Code Documentation
boundary_set_predicates.h
Go to the documentation of this file.
1// Copyright (c) 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_PREDICATES_H
25#define MYSQL_SETS_BOUNDARY_SET_PREDICATES_H
26
27/// @file
28/// Experimental API header
29
30#include "mysql/sets/boundary_set_meta.h" // Is_boundary_set_over_traits
31#include "mysql/sets/interval.h" // Interval
32#include "mysql/sets/set_categories_and_traits.h" // Is_compatible_set
33
34/// @addtogroup GroupLibsMysqlSets
35/// @{
36
37namespace mysql::sets {
38
39// ==== contains_element ====
40
41/// Return true if the element is contained in the Boundary set.
42///
43/// Complexity: The time of one invocation of set.upper_bound.
44template <Is_boundary_set Boundary_set_t>
45[[nodiscard]] constexpr bool contains_element(
46 const Boundary_set_t &set,
47 const typename Boundary_set_t::Element_t &element) {
48 return set.upper_bound(element).is_endpoint();
49}
50
51// ==== is_subset ====
52
53/// Return true if the left Interval is a subset of or equal to the right
54/// Boundary_set.
55///
56/// Complexity: The time of one invocation of set2.upper_bound.
57template <Is_bounded_set_traits Set_traits_t>
58[[nodiscard]] constexpr bool is_subset(
59 const Interval<Set_traits_t> &interval1,
61 auto ub = set2.upper_bound(interval1.start());
62 return ub.is_endpoint() && Set_traits_t::le(interval1.exclusive_end(), *ub);
63}
64
65/// Return true if the left Boundary set is a subset of or equal to the
66/// right Interval.
67///
68/// Complexity: Constant.
69template <Is_bounded_set_traits Set_traits_t>
70[[nodiscard]] constexpr bool is_subset(
72 const Interval<Set_traits_t> &interval2) {
73 return set1.empty() ||
74 (Set_traits_t::le(interval2.start(), set1.front()) &&
75 Set_traits_t::ge(interval2.exclusive_end(), set1.back()));
76}
77
78/// Return true if the left Boundary set is a subset of or equal to the right
79/// Boundary_set.
80///
81/// Complexity: The number of iterations is linear in the size of the smallest
82/// set. Each iteration requires an invocation of upper_bound in both sets.
83template <Is_boundary_set Boundary_set1_t, Is_boundary_set Boundary_set2_t>
84 requires Is_compatible_set<Boundary_set1_t, Boundary_set2_t>
85[[nodiscard]] constexpr bool is_subset(const Boundary_set1_t &set1,
86 const Boundary_set2_t &set2) {
87 auto it1 = set1.begin();
88 auto end1 = set1.end();
89 auto it2 = set2.begin();
90 auto end2 = set2.end();
91 // Each iteration (except possibly the last one) visits a start-point in set2
92 // and an end-point in set1. So the maximum possible number of iterations
93 // can't exceed the number of elements in either set.
94 while (it1 != end1) {
95 // Invariant holding here: it1.start==true, and there are no elements in
96 // it1-it2 up to it1.
97 it2 = set2.upper_bound(it2, *it1);
98 // Is it1 past the end of set2?
99 if (it2 == end2) return false;
100 // Is it1 outside intervals in set2?
101 if (!it2.is_endpoint()) return false;
102
103 // Invariant holding here: it2.start==false, and there are no elements in
104 // it1-it2 up to it2.
105 it1 = set1.upper_bound(it1, *it2);
106 // Is it2 past the end of set1?
107 if (it1 == end1) return true;
108 // Is it1 ouside intervals in set2?
109 if (it1.is_endpoint()) return false;
110 }
111 return true;
112}
113
114// ==== is_intersecting ====
115
116/// Return true if the left Interval and the right Boundary set intersect
117/// (overlap).
118///
119/// Complexity: The time of one invocation of set.upper_bound.
120template <Is_bounded_set_traits Set_traits_t>
121[[nodiscard]] constexpr bool is_intersecting(
124 auto ub = set.upper_bound(interval.start());
125 return ub.is_endpoint() ||
126 (ub != set.end() && Set_traits_t::lt(*ub, interval.exclusive_end()));
127}
128
129/// Return true if the left Boundary set and the right Interval intersect
130/// (overlap).
131///
132/// Complexity: The time of one invocation of set.upper_bound.
133template <Is_bounded_set_traits Set_traits_t>
134[[nodiscard]] constexpr bool is_intersecting(
138}
139
140/// Return true if the two Boundary sets intersect (overlap).
141///
142/// Complexity: The number of iterations is linear in the size of the smallest
143/// set. Each iteration requires an invocation of upper_bound in both sets.
144template <Is_boundary_set Boundary_set1_t, Is_boundary_set Boundary_set2_t>
145 requires Is_compatible_set<Boundary_set1_t, Boundary_set2_t>
146[[nodiscard]] constexpr bool is_intersecting(const Boundary_set1_t &set1,
147 const Boundary_set2_t &set2) {
148 auto it1 = set1.begin();
149 auto end1 = set1.end();
150 auto it2 = set2.begin();
151 auto end2 = set2.end();
152 if (it1 == end1) return false;
153 // Each iteration (except possibly the last one) visits a start-point in each
154 // set. So the maximum possible number of iterations can't exceed the number
155 // of elements in either set.
156 while (true) {
157 // Invariant holding here: there are no overlaps up to it1, and
158 // it1.is_endpoint==false.
159 it2 = set2.upper_bound(it2, *it1);
160 // Is it1 past the end of set2?
161 if (it2 == end2) return false;
162 // Is it1 in the middle of an interval?
163 if (it2.is_endpoint()) return true;
164
165 // Invariant holding here: there are no overlaps up to it2, and
166 // it2.is_endpoint==false.
167 it1 = set1.upper_bound(it1, *it2);
168 // Is it2 past the end of set1?
169 if (it1 == end1) return false;
170 // Is it1 in the middle of an interval?
171 if (it1.is_endpoint()) return true;
172 }
173 return false;
174}
175
176} // namespace mysql::sets
177
178// addtogroup GroupLibsMysqlSets
179/// @}
180
181#endif // ifndef MYSQL_SETS_BOUNDARY_SET_PREDICATES_H
Experimental API header.
Holds the start boundary and endpoint boundary of an interval.
Definition: interval.h:178
constexpr const Element_t & exclusive_end() const
Return const reference to the exclusive endpoint of the interval.
Definition: interval.h:110
constexpr const Element_t & start() const
Return const reference to the starting point of the interval (inclusive).
Definition: interval.h:107
True if Test is an interval set over the given Set traits.
Definition: boundary_set_meta.h:217
Experimental API header.
static int interval
Definition: mysqladmin.cc:72
Definition: gtid_set.h:183
constexpr bool is_subset(const Interval< Set_traits_t > &interval1, const Is_boundary_set_over_traits< Set_traits_t > auto &set2)
Return true if the left Interval is a subset of or equal to the right Boundary_set.
Definition: boundary_set_predicates.h:58
bool contains_element(const mysql::gtids::Is_gtid_set auto &gtid_set, const mysql::gtids::Is_gtid auto &gtid) noexcept
contains_element for Gtid_sets, accepting a Gtid for the element.
Definition: gtid_set.h:186
constexpr bool is_intersecting(const Interval< Set_traits_t > &interval, const Is_boundary_set_over_traits< Set_traits_t > auto &set)
Return true if the left Interval and the right Boundary set intersect (overlap).
Definition: boundary_set_predicates.h:121
std::set< Key, Compare, ut::allocator< Key > > set
Specialization of set which uses ut_allocator.
Definition: ut0new.h:2888
Experimental API header.