MySQL 9.6.0
Source Code Documentation
nested_set_subtraction_iterator.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_SUBTRACTION_ITERATOR_H
25#define MYSQL_SETS_NESTED_SET_SUBTRACTION_ITERATOR_H
26
27/// @file
28/// Experimental API header
29
30#include "mysql/sets/binary_operation.h" // Binary_operation
31#include "mysql/sets/nested_set_binary_operation_iterator_base.h" // Nested_set_binary_operation_iterator_base
32#include "mysql/sets/nested_set_meta.h" // Is_nested_set
33#include "mysql/sets/set_categories_and_traits.h" // Is_compatible_set
34
35/// @addtogroup GroupLibsMysqlSets
36/// @{
37
38namespace mysql::sets::detail {
39
40/// Iterator over the difference of two sets.
41///
42/// @tparam Source1_tp The first source.
43///
44/// @tparam Source2_tp The second source.
45template <Is_nested_set Source1_tp, Is_nested_set Source2_tp>
46 requires Is_compatible_set<Source1_tp, Source2_tp>
49 Nested_set_subtraction_iterator<Source1_tp, Source2_tp>, Source1_tp,
50 Source2_tp, Binary_operation::op_subtraction> {
53 Self_t, Source1_tp, Source2_tp, Binary_operation::op_subtraction>;
54
55 public:
56 // This iterator holds iterators into each source. Each source iterator has to
57 // skip keys that don't exist in the other set, and keys for which the
58 // intersections of mapped sets is empty. Therefore, advancing the iterator
59 // has two phases:
60 //
61 // 1. Advance both source iterators one step.
62 //
63 // 2. While the iterators point to different keys, or the mapped sets
64 // intersect, step the iterators forward (according the scheme in
65 // advance_if_needed).
66 //
67 // This iterator performs only phase 1 at the time the increment is requested,
68 // and performs phase 2 "lazily", i.e., only when it is needed, which is at
69 // the next dereference, advancement, or comparison operation.
70
71 using typename Base_t::Iterator1_t;
72 using typename Base_t::Iterator2_t;
73 using typename Base_t::Source1_t;
74 using typename Base_t::Source2_t;
75 using typename Base_t::Value_t;
76
78
79 // Construct from two sources and one iterator into each of them.
81 const Source2_t *source2,
84 : Base_t(source1, source2, iterator1, iterator2) {}
85
86 /// @return The value that this iterator currently points to.
87 [[nodiscard]] auto get() const {
88 clean();
89 if (this->m_iterator2 == this->m_source2.end()) return this->make_value1();
90 return this->make_value();
91 }
92
93 /// Move to the next iterator position.
94 void next() {
95 clean();
96 ++this->m_iterator1;
97 m_is_dirty = true;
98 }
99
100 /// @return true if this iterator equals other.
101 [[nodiscard]] bool is_equal(
102 const Nested_set_subtraction_iterator &other) const {
103 clean();
104 return this->m_iterator1 == other.m_iterator1;
105 }
106
107 private:
108 /// True when the first phase of advancing the position but not the second one
109 /// has completed.
110 mutable bool m_is_dirty{true};
111
112 /// Perform the second phase of advancing the position, unless it has already
113 /// been done.
114 void clean() const {
115 if (m_is_dirty) {
117 m_is_dirty = false;
118 }
119 }
120
121 /// Perform the second phase of advancing the position.
122 ///
123 /// Algorithm: try set the second iterator to point to the same key as the
124 /// first iterator:
125 ///
126 /// - If no such key exists in the second set, leave the second iterator at
127 /// the end position and return.
128 ///
129 /// - If the key exists in the second set, and the subtraction of the second
130 /// mapped set from the first mapped set does not result in empty set (i.e.,
131 /// the first mapped set is not a subset of the second mapped set), leave the
132 /// second iterator at this position and continue.
133 ///
134 /// - Otherwise, step iterator1 and start over.
136 if (!this->m_source2.has_object()) return;
137 while (true) {
138 if (this->m_iterator1 == this->m_source1.end()) {
139 this->m_iterator2 = this->m_source2.end();
140 return;
141 }
142 this->m_iterator2 = this->m_source2->find(this->m_iterator1->first);
143 if (this->m_iterator2 == this->m_source2.end()) return;
144 if (!is_subset(this->m_iterator1->second, this->m_iterator2->second))
145 return;
146 ++this->m_iterator1;
147 }
148 }
149}; // class Nested_set_subtraction_iterator
150
151} // namespace mysql::sets::detail
152
153// addtogroup GroupLibsMysqlSets
154/// @}
155
156#endif // ifndef MYSQL_SETS_NESTED_SET_SUBTRACTION_ITERATOR_H
Experimental API header.
Self_tp Self_t
Definition: iterator_interface.h:372
auto end() const
Return a valid end iterator, even if !has_object().
Definition: view_sources.h:273
bool has_object() const
Return true if this object holds a source.
Definition: view_sources.h:229
auto find(Args_t &&...args) const
If the source is present, invoke find on it.
Definition: optional_view_source_set.h:70
Common base class for the forward iterators over union view, intersection view, and subtraction view ...
Definition: nested_set_binary_operation_iterator_base.h:54
Source2_tp Source2_t
Definition: nested_set_binary_operation_iterator_base.h:57
typename Source2_t::Const_iterator_t Iterator2_t
Definition: nested_set_binary_operation_iterator_base.h:63
std::pair< const Key_t, Mapped_t > Value_t
Definition: nested_set_binary_operation_iterator_base.h:69
Value_t make_value1() const
Return the current value, computed as operation(m_iterator1, nullptr).
Definition: nested_set_binary_operation_iterator_base.h:119
Source1_tp Source1_t
Definition: nested_set_binary_operation_iterator_base.h:56
const auto & iterator2() const
Return const reference to the current iterator into the second set.
Definition: nested_set_binary_operation_iterator_base.h:106
typename Source1_t::Const_iterator_t Iterator1_t
Definition: nested_set_binary_operation_iterator_base.h:62
Value_t make_value() const
Return the current value, computed as operation(m_iterator1, m_iterator2).
Definition: nested_set_binary_operation_iterator_base.h:113
const auto & iterator1() const
Return const reference to the current iterator into the first set.
Definition: nested_set_binary_operation_iterator_base.h:100
Iterator over the difference of two sets.
Definition: nested_set_subtraction_iterator.h:50
Source2_tp Source2_t
Definition: nested_set_binary_operation_iterator_base.h:57
void clean() const
Perform the second phase of advancing the position, unless it has already been done.
Definition: nested_set_subtraction_iterator.h:114
void next()
Move to the next iterator position.
Definition: nested_set_subtraction_iterator.h:94
auto get() const
Definition: nested_set_subtraction_iterator.h:87
typename Source2_t::Const_iterator_t Iterator2_t
Definition: nested_set_binary_operation_iterator_base.h:63
Nested_set_subtraction_iterator< Source1_tp, Source2_tp > Self_t
Definition: nested_set_subtraction_iterator.h:51
bool m_is_dirty
True when the first phase of advancing the position but not the second one has completed.
Definition: nested_set_subtraction_iterator.h:110
Source1_tp Source1_t
Definition: nested_set_binary_operation_iterator_base.h:56
Nested_set_subtraction_iterator(const Source1_t *source1, const Source2_t *source2, const Iterator1_t &iterator1, const Iterator2_t &iterator2)
Definition: nested_set_subtraction_iterator.h:80
void set_iterator2_and_advance_if_needed() const
Perform the second phase of advancing the position.
Definition: nested_set_subtraction_iterator.h:135
typename Source1_t::Const_iterator_t Iterator1_t
Definition: nested_set_binary_operation_iterator_base.h:62
bool is_equal(const Nested_set_subtraction_iterator &other) const
Definition: nested_set_subtraction_iterator.h:101
Definition: aliases.h:97
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
Experimental API header.
Experimental API header.