MySQL 9.6.0
Source Code Documentation
nested_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_NESTED_SET_PREDICATES_H
25#define MYSQL_SETS_NESTED_SET_PREDICATES_H
26
27/// @file
28/// Experimental API header
29
30#include <concepts> // same_as
31#include "mysql/sets/meta.h" // Has_fast_size
32#include "mysql/sets/nested_set_meta.h" // Is_nested_set_traits
33#include "mysql/sets/set_categories_and_traits.h" // Is_compatible_set
34#include "mysql/sets/set_traits.h" // Is_compatible_set
35
36/// @addtogroup GroupLibsMysqlSets
37/// @{
38
39namespace mysql::sets {
40
41/// Return true if the two pairs are equal, for pairs in which the second
42/// components have different types but the same set category and traits.
43///
44/// Thus, when two nested sets with the same traits (but possibly different
45/// types) are compared, it will run the algorithm in common_predicates.h
46/// in the outer set; in each iteration invoke this
47template <class Key_t, Is_set Mapped1_t, Is_set Mapped2_t>
48 requires(Is_compatible_set<Mapped1_t, Mapped2_t> &&
49 !std::same_as<Mapped1_t, Mapped2_t>)
50[[nodiscard]] constexpr bool operator==(
51 const std::pair<const Key_t, Mapped1_t> &left,
52 const std::pair<const Key_t, Mapped2_t> &right) {
53 return (left.first == right.first) && (left.second == right.second);
54}
55
56/// Return true if the value is contained in the Nested set.
57template <Is_nested_set Nested_set_t>
58[[nodiscard]] constexpr bool contains_element(
59 const Nested_set_t &set, const typename Nested_set_t::Key_t &key,
60 const auto &...mapped) {
61 auto it = set.find(key);
62 if (it == set.end()) return false;
63 return contains_element(it->second, mapped...);
64}
65
66/// Return true if the left Nested set is a subset of or equal to the
67/// right Nested_set.
68template <Is_nested_set Nested_set1_t, Is_nested_set Nested_set2_t>
69 requires Is_compatible_set<Nested_set1_t, Nested_set2_t>
70[[nodiscard]] constexpr bool is_subset(const Nested_set1_t &set1,
71 const Nested_set2_t &set2) {
73 if (set1.size() > set2.size()) return false;
74 }
75 for (auto &&[key, mapped1] : set1) {
76 auto it2 = set2.find(key);
77 if (it2 == set2.end()) return false;
78 if (!is_subset(mapped1, it2->second)) return false;
79 }
80 return true;
81}
82
83/// Return true if the two Nested sets intersect (overlap).
84template <Is_nested_set Nested_set1_t, Is_nested_set Nested_set2_t>
85 requires Is_compatible_set<Nested_set1_t, Nested_set2_t>
86[[nodiscard]] constexpr bool is_intersecting(const Nested_set1_t &set1,
87 const Nested_set2_t &set2) {
88 auto cursor1 = set1.begin();
89 auto cursor2 = set2.begin();
90 while (cursor1 != set1.end()) {
91 // Invariant: the intersection up to cursor1 is empty, and cursor1 is not
92 // end.
93 auto it2 = set2.find(cursor2, cursor1->first);
94 if (it2 != set2.end()) {
95 if (is_intersecting(cursor1->second, it2->second)) return true;
96 }
97 if (cursor2 == set2.end()) return false;
98 // Invariant: the intersection up to cursor2 is empty, and cursor2 is not
99 // end.
100 auto it1 = set1.find(cursor1, cursor2->first);
101 if (it1 != set1.end()) {
102 if (is_intersecting(it1->second, cursor2->second)) return true;
103 }
104 }
105 return false;
106}
107
108} // namespace mysql::sets
109
110// addtogroup GroupLibsMysqlSets
111/// @}
112
113#endif // ifndef MYSQL_SETS_NESTED_SET_PREDICATES_H
Determines if the given type has "fast" size computations.
Definition: meta.h:61
Experimental API header.
void right(std::string *to_trim)
Definition: trim.h:41
void left(std::string *to_trim)
Definition: trim.h:35
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.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Experimental API header.
Experimental API header.