MySQL 8.0.40
Source Code Documentation
sql_bitmap.h
Go to the documentation of this file.
1/* Copyright (c) 2003, 2024, 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/*
25 Implementation of a bitmap type.
26 The idea with this is to be able to handle any constant number of bits but
27 also be able to use 32 or 64 bits bitmaps very efficiently
28*/
29
30#ifndef SQL_BITMAP_INCLUDED
31#define SQL_BITMAP_INCLUDED
32
33#include <assert.h>
34#include "m_string.h" // longlong2str
35#include "my_bitmap.h" // MY_BITMAP
36#include "my_byteorder.h" // int8store
37
38#include "template_utils.h"
39
40template <uint default_width>
41class Bitmap {
43 uint32 buffer[(default_width + 31) / 32];
44
45 public:
46 enum { ALL_BITS = default_width };
47 Bitmap() { init(); }
48 Bitmap(const Bitmap &from) { *this = from; }
49 explicit Bitmap(uint prefix_to_set) { init(prefix_to_set); }
50 void init() { bitmap_init(&map, buffer, default_width); }
51 void init(uint prefix_to_set) {
52 init();
53 set_prefix(prefix_to_set);
54 }
55 uint length() const { return default_width; }
56 Bitmap &operator=(const Bitmap &map2) {
57 init();
58 memcpy(buffer, map2.buffer, sizeof(buffer));
59 return *this;
60 }
66 void intersect(const Bitmap &map2) { bitmap_intersect(&map, &map2.map); }
67 void intersect(ulonglong map2buff) {
68 // Use a spearate temporary buffer, as bitmap_init() clears all the bits.
69 ulonglong buf2;
70 MY_BITMAP map2;
71
72 bitmap_init(&map2, (uint32 *)&buf2, sizeof(ulonglong) * 8);
73
74 // Store the original bits.
75 if (sizeof(ulonglong) >= 8) {
77 const_cast<uchar *>(static_cast<uchar *>(static_cast<void *>(&buf2))),
78 map2buff);
79 } else {
80 assert(sizeof(buffer) >= 4);
82 const_cast<uchar *>(static_cast<uchar *>(static_cast<void *>(&buf2))),
83 static_cast<uint32>(map2buff));
84 }
85
86 bitmap_intersect(&map, &map2);
87 }
88 /* Use highest bit for all bits above sizeof(ulonglong)*8. */
90 intersect(map2buff);
91 if (map.n_bits > sizeof(ulonglong) * 8)
93 (map2buff & (1LL << (sizeof(ulonglong) * 8 - 1))));
94 }
95 void subtract(const Bitmap &map2) { bitmap_subtract(&map, &map2.map); }
96 void merge(const Bitmap &map2) { bitmap_union(&map, &map2.map); }
97 bool is_set(uint n) const { return bitmap_is_set(&map, n); }
98 bool is_prefix(uint n) const { return bitmap_is_prefix(&map, n); }
99 bool is_clear_all() const { return bitmap_is_clear_all(&map); }
100 bool is_set_all() const { return bitmap_is_set_all(&map); }
101 bool is_subset(const Bitmap &map2) const {
102 return bitmap_is_subset(&map, &map2.map);
103 }
104 bool is_overlapping(const Bitmap &map2) const {
105 return bitmap_is_overlapping(&map, &map2.map);
106 }
107 bool operator==(const Bitmap &map2) const {
108 return bitmap_cmp(&map, &map2.map);
109 }
110 bool operator!=(const Bitmap &map2) const { return !(*this == map2); }
111 char *print(char *buf) const {
112 char *s = buf;
113 const uchar *e = pointer_cast<const uchar *>(&buffer[0]),
114 *b = e + sizeof(buffer) - 1;
115 while (!*b && b > e) b--;
116 if ((*s = _dig_vec_upper[*b >> 4]) != '0') s++;
117 *s++ = _dig_vec_upper[*b & 15];
118 while (--b >= e) {
119 *s++ = _dig_vec_upper[*b >> 4];
120 *s++ = _dig_vec_upper[*b & 15];
121 }
122 *s = 0;
123 return buf;
124 }
126 if (sizeof(buffer) >= 8)
127 return uint8korr(
128 static_cast<const uchar *>(static_cast<const void *>(buffer)));
129 assert(sizeof(buffer) >= 4);
130 return (ulonglong)uint4korr(
131 static_cast<const uchar *>(static_cast<const void *>(buffer)));
132 }
133 uint bits_set() const { return bitmap_bits_set(&map); }
135};
136
137template <>
138class Bitmap<64> {
140
141 public:
142 Bitmap() { init(); }
143 enum { ALL_BITS = 64 };
144
145 explicit Bitmap(uint prefix_to_set) { set_prefix(prefix_to_set); }
146 void init() { clear_all(); }
147 void init(uint prefix_to_set) { set_prefix(prefix_to_set); }
148 uint length() const { return 64; }
149 void set_bit(uint n) {
150 assert(n < 64);
151 map |= ((ulonglong)1) << n;
152 }
154 assert(n < 64);
155 map &= ~(((ulonglong)1) << n);
156 }
158 if (n >= length())
159 set_all();
160 else
161 map = (((ulonglong)1) << n) - 1;
162 }
163 void set_all() { map = ~(ulonglong)0; }
164 void clear_all() { map = (ulonglong)0; }
165 void intersect(const Bitmap<64> &map2) { map &= map2.map; }
166 void intersect(ulonglong map2) { map &= map2; }
167 void intersect_extended(ulonglong map2) { map &= map2; }
168 void subtract(const Bitmap<64> &map2) { map &= ~map2.map; }
169 void merge(const Bitmap<64> &map2) { map |= map2.map; }
170 bool is_set(uint n) const {
171 assert(n < 64);
172 return (map & (((ulonglong)1) << n));
173 }
174 bool is_prefix(uint n) const {
175 assert(n <= 64);
176 if (n < 64)
177 return map == (((ulonglong)1) << n) - 1;
178 else
179 return map == ~(ulonglong)1;
180 }
181 bool is_clear_all() const { return map == (ulonglong)0; }
182 bool is_set_all() const { return map == ~(ulonglong)0; }
183 bool is_subset(const Bitmap<64> &map2) const { return !(map & ~map2.map); }
184 bool is_overlapping(const Bitmap<64> &map2) const {
185 return (map & map2.map) != 0;
186 }
187 bool operator==(const Bitmap<64> &map2) const { return map == map2.map; }
188 bool operator!=(const Bitmap<64> &map2) const { return !(*this == map2); }
189 char *print(char *buf) const {
190 longlong2str(map, buf, 16);
191 return buf;
192 }
193 ulonglong to_ulonglong() const { return map; }
194 uint bits_set() const {
195 uint res = 0;
196 for (uint i = 0; i < ALL_BITS; i++)
197 if (is_set(i)) res++;
198 return res;
199 }
201 for (uint i = 0; i < ALL_BITS; i++)
202 if (map & (1ULL << i)) return i;
203 return MY_BIT_NONE;
204 }
205};
206
207/* An iterator to quickly walk over bits in unlonglong bitmap. */
211
212 public:
214 int next_bit() {
215 static const char last_bit[16] = {32, 0, 1, 0, 2, 0, 1, 0,
216 3, 0, 1, 0, 2, 0, 1, 0};
217 uint bit;
218 while ((bit = last_bit[bmp & 0xF]) == 32) {
219 no += 4;
220 bmp = bmp >> 4;
221 if (!bmp) return BITMAP_END;
222 }
223 bmp &= ~(1LL << bit);
224 return no + bit;
225 }
226 enum { BITMAP_END = 64 };
227};
228
229#if MAX_INDEXES <= 64
230typedef Bitmap<64> Key_map; /* Used for finding keys */
231#elif MAX_INDEXES > 255
232#error "MAX_INDEXES values greater than 255 is not supported."
233#else
234typedef Bitmap<((MAX_INDEXES + 7) / 8 * 8)> Key_map; /* Used for finding keys */
235#endif
236
237#endif /* SQL_BITMAP_INCLUDED */
Definition: sql_bitmap.h:138
char * print(char *buf) const
Definition: sql_bitmap.h:189
Bitmap(uint prefix_to_set)
Definition: sql_bitmap.h:145
bool is_set(uint n) const
Definition: sql_bitmap.h:170
void set_all()
Definition: sql_bitmap.h:163
bool operator==(const Bitmap< 64 > &map2) const
Definition: sql_bitmap.h:187
Bitmap()
Definition: sql_bitmap.h:142
void init(uint prefix_to_set)
Definition: sql_bitmap.h:147
void intersect(const Bitmap< 64 > &map2)
Definition: sql_bitmap.h:165
void clear_all()
Definition: sql_bitmap.h:164
void set_bit(uint n)
Definition: sql_bitmap.h:149
uint bits_set() const
Definition: sql_bitmap.h:194
ulonglong to_ulonglong() const
Definition: sql_bitmap.h:193
ulonglong map
Definition: sql_bitmap.h:139
bool is_subset(const Bitmap< 64 > &map2) const
Definition: sql_bitmap.h:183
void intersect(ulonglong map2)
Definition: sql_bitmap.h:166
uint length() const
Definition: sql_bitmap.h:148
void set_prefix(uint n)
Definition: sql_bitmap.h:157
bool is_overlapping(const Bitmap< 64 > &map2) const
Definition: sql_bitmap.h:184
bool is_clear_all() const
Definition: sql_bitmap.h:181
bool is_set_all() const
Definition: sql_bitmap.h:182
uint get_first_set() const
Definition: sql_bitmap.h:200
void intersect_extended(ulonglong map2)
Definition: sql_bitmap.h:167
bool operator!=(const Bitmap< 64 > &map2) const
Definition: sql_bitmap.h:188
void merge(const Bitmap< 64 > &map2)
Definition: sql_bitmap.h:169
void init()
Definition: sql_bitmap.h:146
void subtract(const Bitmap< 64 > &map2)
Definition: sql_bitmap.h:168
bool is_prefix(uint n) const
Definition: sql_bitmap.h:174
void clear_bit(uint n)
Definition: sql_bitmap.h:153
Definition: sql_bitmap.h:41
void intersect(const Bitmap &map2)
Definition: sql_bitmap.h:66
char * print(char *buf) const
Definition: sql_bitmap.h:111
void set_all()
Definition: sql_bitmap.h:64
Bitmap(uint prefix_to_set)
Definition: sql_bitmap.h:49
void intersect_extended(ulonglong map2buff)
Definition: sql_bitmap.h:89
void set_prefix(uint n)
Definition: sql_bitmap.h:63
void clear_all()
Definition: sql_bitmap.h:65
MY_BITMAP map
Definition: sql_bitmap.h:42
bool is_subset(const Bitmap &map2) const
Definition: sql_bitmap.h:101
void clear_bit(uint n)
Definition: sql_bitmap.h:62
void merge(const Bitmap &map2)
Definition: sql_bitmap.h:96
ulonglong to_ulonglong() const
Definition: sql_bitmap.h:125
bool is_set_all() const
Definition: sql_bitmap.h:100
void subtract(const Bitmap &map2)
Definition: sql_bitmap.h:95
bool is_prefix(uint n) const
Definition: sql_bitmap.h:98
Bitmap & operator=(const Bitmap &map2)
Definition: sql_bitmap.h:56
bool is_clear_all() const
Definition: sql_bitmap.h:99
uint length() const
Definition: sql_bitmap.h:55
uint get_first_set() const
Definition: sql_bitmap.h:134
@ ALL_BITS
Definition: sql_bitmap.h:46
uint32 buffer[(default_width+31)/32]
Definition: sql_bitmap.h:43
bool operator!=(const Bitmap &map2) const
Definition: sql_bitmap.h:110
void init()
Definition: sql_bitmap.h:50
void init(uint prefix_to_set)
Definition: sql_bitmap.h:51
bool operator==(const Bitmap &map2) const
Definition: sql_bitmap.h:107
void intersect(ulonglong map2buff)
Definition: sql_bitmap.h:67
Bitmap()
Definition: sql_bitmap.h:47
bool is_set(uint n) const
Definition: sql_bitmap.h:97
bool is_overlapping(const Bitmap &map2) const
Definition: sql_bitmap.h:104
void set_bit(uint n)
Definition: sql_bitmap.h:61
uint bits_set() const
Definition: sql_bitmap.h:133
Bitmap(const Bitmap &from)
Definition: sql_bitmap.h:48
Definition: sql_bitmap.h:208
int next_bit()
Definition: sql_bitmap.h:214
Table_map_iterator(ulonglong t)
Definition: sql_bitmap.h:213
uint no
Definition: sql_bitmap.h:210
ulonglong bmp
Definition: sql_bitmap.h:209
@ BITMAP_END
Definition: sql_bitmap.h:226
char * longlong2str(int64_t val, char *dst, int radix)
Definition: m_string.h:297
#define MY_BIT_NONE
Definition: my_bitmap.h:32
static bool bitmap_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2)
Quite unlike other C comparison functions ending with 'cmp', e.g.
Definition: my_bitmap.h:108
bool bitmap_is_overlapping(const MY_BITMAP *map1, const MY_BITMAP *map2)
Definition: my_bitmap.cc:301
static void bitmap_set_all(MY_BITMAP *map)
Definition: my_bitmap.h:128
bool bitmap_is_clear_all(const MY_BITMAP *map)
Definition: my_bitmap.cc:268
static bool bitmap_is_set(const MY_BITMAP *map, uint bit)
Definition: my_bitmap.h:95
bool bitmap_is_set_all(const MY_BITMAP *map)
Definition: my_bitmap.cc:257
static void bitmap_clear_all(MY_BITMAP *map)
Definition: my_bitmap.h:121
void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2)
Definition: my_bitmap.cc:380
uint bitmap_bits_set(const MY_BITMAP *map)
Definition: my_bitmap.cc:416
void bitmap_intersect(MY_BITMAP *to, const MY_BITMAP *from)
Definition: my_bitmap.cc:335
void bitmap_set_above(MY_BITMAP *map, uint from_byte, bool use_bit)
Definition: my_bitmap.cc:372
uint bitmap_get_first_set(const MY_BITMAP *map)
Definition: my_bitmap.cc:439
bool bitmap_init(MY_BITMAP *map, my_bitmap_map *buf, uint n_bits)
Definition: my_bitmap.cc:140
void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2)
Definition: my_bitmap.cc:389
static void bitmap_clear_bit(MY_BITMAP *map, uint bit)
Definition: my_bitmap.h:90
void bitmap_set_prefix(MY_BITMAP *map, uint prefix_size)
Set the specified number of bits in the bitmap buffer.
Definition: my_bitmap.cc:201
bool bitmap_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2)
Definition: my_bitmap.cc:281
static void bitmap_set_bit(MY_BITMAP *map, uint bit)
Definition: my_bitmap.h:80
bool bitmap_is_prefix(const MY_BITMAP *map, uint prefix_size)
Definition: my_bitmap.cc:218
Functions for reading and storing in machine-independent format.
void int4store(char *pT, uint32 A)
Definition: my_byteorder.h:173
ulonglong uint8korr(const char *pT)
Definition: my_byteorder.h:157
void int8store(char *pT, ulonglong A)
Definition: my_byteorder.h:185
uint32 uint4korr(const char *pT)
Definition: my_byteorder.h:145
#define MAX_INDEXES
Definition: my_config.h:210
unsigned long long int ulonglong
Definition: my_inttypes.h:56
unsigned char uchar
Definition: my_inttypes.h:52
uint32_t uint32
Definition: my_inttypes.h:67
const char _dig_vec_upper[]
Definition: my_hex_tools.cc:89
Definition: buf0block_hint.cc:30
Bitmap< 64 > Key_map
Definition: sql_bitmap.h:230
Definition: my_bitmap.h:43
uint n_bits
Definition: my_bitmap.h:45
unsigned int uint
Definition: uca9-dump.cc:75
int n
Definition: xcom_base.cc:509