MySQL 9.6.0
Source Code Documentation
nested_set_volume.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_VOLUME_H
25#define MYSQL_SETS_NESTED_SET_VOLUME_H
26
27/// @file
28/// Experimental API header
29
30#include <numeric> // accumulate
31#include <ranges> // range
32#include "mysql/math/summation.h" // sequence_sum_difference
33#include "mysql/ranges/flat_view.h" // make_flat_view
34#include "mysql/ranges/projection_views.h" // make_mapped_view
35#include "mysql/ranges/transform_view.h" // Transform_view
36#include "mysql/sets/nested_set_meta.h" // Is_nested_set
37#include "mysql/sets/set_categories_and_traits.h" // Is_compatible_set
38
39/// @addtogroup GroupLibsMysqlSets
40/// @{
41
42namespace mysql::sets::detail {
43
44/// Given a set, returns its volume.
46 static auto transform(const auto &set) { return volume(set); }
47};
48
49/// Given a nested set, returns a range over its inner sets.
50struct Unfold_set {
51 [[nodiscard]] static auto unfold(const Is_nested_set auto &nested_set) {
52 return mysql::ranges::make_mapped_view(nested_set);
53 }
54};
55
56/// Given a Nested set, returns a range over the volumes of the inner sets.
57template <Is_nested_set Nested_set_t>
58[[nodiscard]] auto make_volume_view(const Nested_set_t &nested_set) {
59 auto flat_view =
60 mysql::ranges::make_flat_view<detail::Unfold_set>(nested_set);
61 return mysql::ranges::make_transform_view<Volume_transform>(flat_view);
62}
63
64} // namespace mysql::sets::detail
65
66namespace mysql::sets {
67
68/// Return the volume of a nested set.
69///
70/// @tparam Result_t Type of return value. Default is double. The result will
71/// be exact if it is at most `mysql::math::max_exact_int<double>`.
72///
73/// @tparam Nested_set_t Type of nested set set.
74///
75/// @param set Set.
76template <class Result_t = double, Is_nested_set Nested_set_t>
77[[nodiscard]] Result_t volume(const Nested_set_t &set) {
78 auto volume_view = make_volume_view(set);
79 auto ret =
80 std::accumulate(volume_view.begin(), volume_view.end(), Result_t{});
81 return ret;
82}
83
84/// Return the volume of the first nested set minus the volume of the second
85/// nested set.
86///
87/// @tparam Result_t Type of return value. Default is double. The result will
88/// be exact if it is at most `mysql::math::max_exact_int<double>`.
89///
90/// @tparam Set1_t Type of first set.
91///
92/// @tparam Set2_t Type of second set.
93///
94/// param set1 First set.
95///
96/// param set2 Second set.
97//
98// Doxygen complains that the parameters are duplicated (maybe mixing up
99// overloads with different constraints). We work around that by using param
100// instead of @param.
101template <class Result_t = double, Is_nested_set Set1_t, Is_nested_set Set2_t>
102 requires Is_compatible_set<Set1_t, Set2_t>
103[[nodiscard]] Result_t volume_difference(const Set1_t &set1,
104 const Set2_t &set2) {
105 auto volume_view1 = make_volume_view(set1);
106 auto volume_view2 = make_volume_view(set2);
107 return mysql::math::sequence_sum_difference<Result_t>(
108 volume_view1.begin(), volume_view1.end(), volume_view2.begin(),
109 volume_view2.end());
110}
111
112} // namespace mysql::sets
113
114// addtogroup GroupLibsMysqlSets
115/// @}
116
117#endif // ifndef MYSQL_SETS_NESTED_SET_VOLUME_H
True if Test is a nested set.
Definition: nested_set_meta.h:110
Experimental API header.
auto make_mapped_view(const auto &source)
Factory function to create a new view over the mapped values in a range of pairs.
Definition: projection_views.h:175
Definition: aliases.h:97
auto make_volume_view(const Nested_set_t &nested_set)
Given a Nested set, returns a range over the volumes of the inner sets.
Definition: nested_set_volume.h:58
Definition: gtid_set.h:183
constexpr auto volume(const Boundary_set_t &set)
Return the sum of the lengths of all intervals in the given Boundary set.
Definition: boundary_set_volume.h:43
Result_t volume_difference(const Set1_t &set1, const Set2_t &set2)
Return the volume of the first set minus the volume of the second set.
Definition: base_volume.h:57
std::set< Key, Compare, ut::allocator< Key > > set
Specialization of set which uses ut_allocator.
Definition: ut0new.h:2888
Experimental API header.
Experimental API header.
Experimental API header.
Given a nested set, returns a range over its inner sets.
Definition: nested_set_volume.h:50
static auto unfold(const Is_nested_set auto &nested_set)
Definition: nested_set_volume.h:51
Given a set, returns its volume.
Definition: nested_set_volume.h:45
static auto transform(const auto &set)
Definition: nested_set_volume.h:46
Experimental API header.
Experimental API header.