MySQL 8.0.37
Source Code Documentation
tables_contained_in.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25#ifndef TABLES_CONTAINED_IN_H_
26#define TABLES_CONTAINED_IN_H_
27
28#include <string.h>
29
30#include "my_inttypes.h"
31#include "sql/sql_optimizer.h"
32
33#ifdef _MSC_VER
34#include <intrin.h>
35#pragma intrinsic(_BitScanForward64)
36#endif
37
38// A utility class to loop over all set bits in the given map.
39// Use as:
40//
41// qep_tab_map = ...;
42// for (QEP_TAB *qep_tab : TablesContainedIn(join, qep_tab_map)) {
43// ...
44// }
46 public:
47 class iterator {
48 private:
49 const JOIN *const m_join;
51
52 public:
55 bool operator==(const iterator &other) const {
56 assert(m_join == other.m_join);
57 return m_bits_left == other.m_bits_left;
58 }
59 bool operator!=(const iterator &other) const {
60 assert(m_join == other.m_join);
61 return m_bits_left != other.m_bits_left;
62 }
63 QEP_TAB *operator*() const {
64 // Find the QEP_TAB that corresponds to the lowest set bit.
65 assert(m_bits_left != 0);
66#ifdef _MSC_VER
67 unsigned long idx;
68 _BitScanForward64(&idx, m_bits_left);
69#else
70 size_t idx = ffsll(m_bits_left) - 1;
71#endif
72 assert(idx < m_join->tables);
73 return &m_join->qep_tab[idx];
74 }
76 // Clear the lowest set bit.
77 assert(m_bits_left != 0);
78 m_bits_left &= (m_bits_left - 1);
79 return *this;
80 }
81 };
82
85
86 iterator begin() const { return {m_join, m_initial_map}; }
87 iterator end() const { return {m_join, 0}; }
88
89 private:
90 const JOIN *const m_join;
92};
93
94#endif // TABLES_CONTAINED_IN_H_
Definition: sql_optimizer.h:133
QEP_TAB * qep_tab
Array of QEP_TABs.
Definition: sql_optimizer.h:156
Definition: sql_executor.h:260
Definition: tables_contained_in.h:47
iterator(const JOIN *join, qep_tab_map map)
Definition: tables_contained_in.h:53
QEP_TAB * operator*() const
Definition: tables_contained_in.h:63
bool operator==(const iterator &other) const
Definition: tables_contained_in.h:55
bool operator!=(const iterator &other) const
Definition: tables_contained_in.h:59
iterator & operator++()
Definition: tables_contained_in.h:75
qep_tab_map m_bits_left
Definition: tables_contained_in.h:50
const JOIN *const m_join
Definition: tables_contained_in.h:49
Definition: tables_contained_in.h:45
iterator end() const
Definition: tables_contained_in.h:87
const qep_tab_map m_initial_map
Definition: tables_contained_in.h:91
iterator begin() const
Definition: tables_contained_in.h:86
const JOIN *const m_join
Definition: tables_contained_in.h:90
TablesContainedIn(const JOIN *join, qep_tab_map map)
Definition: tables_contained_in.h:83
Some integer typedefs for easier portability.
uint64_t qep_tab_map
Definition: my_table_map.h:32
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:151
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2892
Classes used for query optimizations.