MySQL  8.0.27
Source Code Documentation
tables_contained_in.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2019, 2021, 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 also distributed 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 included with MySQL.
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 TABLES_CONTAINED_IN_H_
25 #define TABLES_CONTAINED_IN_H_
26 
27 #include <string.h>
28 
29 #include "my_inttypes.h"
30 #include "sql/sql_optimizer.h"
31 
32 #ifdef _MSC_VER
33 #include <intrin.h>
34 #pragma intrinsic(_BitScanForward64)
35 #endif
36 
37 // A utility class to loop over all set bits in the given map.
38 // Use as:
39 //
40 // qep_tab_map = ...;
41 // for (QEP_TAB *qep_tab : TablesContainedIn(join, qep_tab_map)) {
42 // ...
43 // }
45  public:
46  class iterator {
47  private:
48  const JOIN *const m_join;
50 
51  public:
53  : m_join(join), m_bits_left(map) {}
54  bool operator==(const iterator &other) const {
55  assert(m_join == other.m_join);
56  return m_bits_left == other.m_bits_left;
57  }
58  bool operator!=(const iterator &other) const {
59  assert(m_join == other.m_join);
60  return m_bits_left != other.m_bits_left;
61  }
62  QEP_TAB *operator*() const {
63  // Find the QEP_TAB that corresponds to the lowest set bit.
64  assert(m_bits_left != 0);
65 #ifdef _MSC_VER
66  unsigned long idx;
67  _BitScanForward64(&idx, m_bits_left);
68 #else
69  size_t idx = ffsll(m_bits_left) - 1;
70 #endif
71  assert(idx < m_join->tables);
72  return &m_join->qep_tab[idx];
73  }
75  // Clear the lowest set bit.
76  assert(m_bits_left != 0);
77  m_bits_left &= (m_bits_left - 1);
78  return *this;
79  }
80  };
81 
83  : m_join(join), m_initial_map(map) {}
84 
85  iterator begin() const { return {m_join, m_initial_map}; }
86  iterator end() const { return {m_join, 0}; }
87 
88  private:
89  const JOIN *const m_join;
91 };
92 
93 #endif // TABLES_CONTAINED_IN_H_
Definition: sql_optimizer.h:125
QEP_TAB * qep_tab
Array of QEP_TABs.
Definition: sql_optimizer.h:148
Definition: sql_executor.h:256
Definition: tables_contained_in.h:46
iterator(const JOIN *join, qep_tab_map map)
Definition: tables_contained_in.h:52
iterator & operator++()
Definition: tables_contained_in.h:74
QEP_TAB * operator*() const
Definition: tables_contained_in.h:62
bool operator==(const iterator &other) const
Definition: tables_contained_in.h:54
bool operator!=(const iterator &other) const
Definition: tables_contained_in.h:58
qep_tab_map m_bits_left
Definition: tables_contained_in.h:49
const JOIN *const m_join
Definition: tables_contained_in.h:48
Definition: tables_contained_in.h:44
iterator end() const
Definition: tables_contained_in.h:86
const qep_tab_map m_initial_map
Definition: tables_contained_in.h:90
iterator begin() const
Definition: tables_contained_in.h:85
const JOIN *const m_join
Definition: tables_contained_in.h:89
TablesContainedIn(const JOIN *join, qep_tab_map map)
Definition: tables_contained_in.h:82
Some integer typedefs for easier portability.
uint64_t qep_tab_map
Definition: my_table_map.h:31
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:144
Classes used for query optimizations.