MySQL 9.6.0
Source Code Documentation
common_predicates.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_COMMON_PREDICATES_H
25#define MYSQL_SETS_COMMON_PREDICATES_H
26
27/// @file
28/// Experimental API header
29
30#include "mysql/sets/meta.h" // Has_fast_size
31#include "mysql/sets/set_categories.h" // Is_set_category
33
34/// @addtogroup GroupLibsMysqlSets
35/// @{
36
37namespace mysql::sets {
38
39/// Return true if two sets are equal, which must be of the same Set category
40/// and Set traits and be *iterator-defined*. See Is_iterator_defined_set for
41/// details.
42///
43/// Complexity: the number of value comparisons is linear in the size of the
44/// smallest set.
45template <Is_set Set1_t, Is_set Set2_t>
46 requires Is_compatible_set<Set1_t, Set2_t> && Is_iterator_defined_set<Set1_t>
47[[nodiscard]] constexpr bool operator==(const Set1_t &set1,
48 const Set2_t &set2) {
49 if constexpr (std::same_as<Set1_t, Set2_t>) {
50 // Self-comparison
51 if (&set1 == &set2) return true;
52 }
54 // If it is fast to compute size, do that first, and compare elements only
55 // if the sizes are equal. This case typically occurs when the operands are
56 // containers.
57 auto size1 = set1.size();
58 auto size2 = set2.size();
59 if (size1 != size2) return false;
60
61 auto it2 = set2.begin();
62 for (auto &&value1 : set1) {
63 if (value1 != *it2) return false;
64 assert(it2 != set2.end());
65 ++it2;
66 }
67 } else {
68 // If it is not fast to compute size, compare elements, and check if one
69 // sequence ends before the other. This case typically occurs when the
70 // operands are views.
71 auto it2 = set2.begin();
72 for (auto &&value1 : set1) {
73 if (it2 == set2.end()) return false;
74 if (value1 != *it2) return false;
75 ++it2;
76 }
77 if (it2 != set2.end()) return false;
78 }
79 return true;
80}
81
82/// Return true if the two sets are not equal.
83template <Is_set Set1_t, Is_set Set2_t>
84 requires Is_compatible_set<Set1_t, Set2_t>
85[[nodiscard]] constexpr bool operator!=(const Set1_t &set1,
86 const Set2_t &set2) {
87 return !(set1 == set2);
88}
89
90/// Return true if the two sets are equal, which must be of the same Set
91/// category and Set traits.
92///
93/// This is alternative syntax to operator==. If both the operands and the
94/// calling context are outside the mysql::sets namespace, name lookup will not
95/// find the operator. It can be qualified by the namespace using the syntax
96/// mysql::sets::operator==(a, b). We provide this equivalent alternative in
97/// case anyone thinks that looks funny, and also because the syntax is
98/// analogous to other set predicates such as is_subset etc.
99template <Is_set Set1_t, Is_set Set2_t>
100 requires Is_compatible_set<Set1_t, Set2_t>
101[[nodiscard]] constexpr bool is_equal(const Set1_t &set1, const Set2_t &set2) {
102 return set1 == set2;
103}
104
105/// Return true if the left object is a superset of or equal to the right
106/// object.
107///
108/// This just delegates work to is_subset, and is defined for any types for
109/// which is_subset is defined.
110[[nodiscard]] constexpr bool is_superset(const auto &lhs, const auto &rhs) {
111 return is_subset(rhs, lhs);
112}
113
114/// Return true if the two objects are disjoint (have nothing in common). The
115/// objects may be intervals, boundary sets, or interval sets.
116///
117/// This just delegates work to is_intersecting, and is defined for any types
118/// for which is_intersecting is defined.
119[[nodiscard]] constexpr bool is_disjoint(const auto &lhs, const auto &rhs) {
120 return !is_intersecting(lhs, rhs);
121}
122
123} // namespace mysql::sets
124
125// addtogroup GroupLibsMysqlSets
126/// @}
127
128#endif // ifndef MYSQL_SETS_COMMON_PREDICATES_H
Determines if the given type has "fast" size computations.
Definition: meta.h:61
Experimental API header.
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
constexpr bool is_equal(const Set1_t &set1, const Set2_t &set2)
Return true if the two sets are equal, which must be of the same Set category and Set traits.
Definition: common_predicates.h:101
constexpr bool operator==(const Set1_t &set1, const Set2_t &set2)
Return true if two sets are equal, which must be of the same Set category and Set traits and be itera...
Definition: common_predicates.h:47
constexpr bool is_disjoint(const auto &lhs, const auto &rhs)
Return true if the two objects are disjoint (have nothing in common).
Definition: common_predicates.h:119
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
constexpr bool is_superset(const auto &lhs, const auto &rhs)
Return true if the left object is a superset of or equal to the right object.
Definition: common_predicates.h:110
constexpr bool operator!=(const Set1_t &set1, const Set2_t &set2)
Return true if the two sets are not equal.
Definition: common_predicates.h:85
Experimental API header.
Experimental API header.