3
/* Copyright (C) 2003 MySQL AB
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19
Implementation of a bitmap type.
20
The idea with this is to be able to handle any constant number of bits but
21
also be able to use 32 or 64 bits bitmaps very efficiently
24
#include <mysys/my_bitmap.h>
26
template <uint default_width> class Bitmap
29
uint32_t buffer[(default_width+31)/32];
32
Bitmap(const Bitmap& from) { *this=from; }
33
explicit Bitmap(uint prefix_to_set) { init(prefix_to_set); }
34
void init() { bitmap_init(&map, buffer, default_width, 0); }
35
void init(uint prefix_to_set) { init(); set_prefix(prefix_to_set); }
36
uint length() const { return default_width; }
37
Bitmap& operator=(const Bitmap& map2)
40
memcpy(buffer, map2.buffer, sizeof(buffer));
43
void set_bit(uint n) { bitmap_set_bit(&map, n); }
44
void clear_bit(uint n) { bitmap_clear_bit(&map, n); }
45
void set_prefix(uint n) { bitmap_set_prefix(&map, n); }
46
void set_all() { bitmap_set_all(&map); }
47
void clear_all() { bitmap_clear_all(&map); }
48
void intersect(Bitmap& map2) { bitmap_intersect(&map, &map2.map); }
49
void intersect(uint64_t map2buff)
52
bitmap_init(&map2, (uint32_t *)&map2buff, sizeof(uint64_t)*8, 0);
53
bitmap_intersect(&map, &map2);
55
/* Use highest bit for all bits above sizeof(uint64_t)*8. */
56
void intersect_extended(uint64_t map2buff)
59
if (map.n_bits > sizeof(uint64_t) * 8)
60
bitmap_set_above(&map, sizeof(uint64_t),
61
test(map2buff & (1 << (sizeof(uint64_t) * 8 - 1))));
63
void subtract(Bitmap& map2) { bitmap_subtract(&map, &map2.map); }
64
void merge(Bitmap& map2) { bitmap_union(&map, &map2.map); }
65
bool is_set(uint n) const { return bitmap_is_set(&map, n); }
66
bool is_set() const { return !bitmap_is_clear_all(&map); }
67
bool is_prefix(uint n) const { return bitmap_is_prefix(&map, n); }
68
bool is_clear_all() const { return bitmap_is_clear_all(&map); }
69
bool is_set_all() const { return bitmap_is_set_all(&map); }
70
bool is_subset(const Bitmap& map2) const { return bitmap_is_subset(&map, &map2.map); }
71
bool is_overlapping(const Bitmap& map2) const { return bitmap_is_overlapping(&map, &map2.map); }
72
bool operator==(const Bitmap& map2) const { return bitmap_cmp(&map, &map2.map); }
73
bool operator!=(const Bitmap& map2) const { return !bitmap_cmp(&map, &map2.map); }
74
Bitmap operator&=(uint n)
76
if (bitmap_is_set(&map, n))
78
bitmap_clear_all(&map);
79
bitmap_set_bit(&map, n);
82
bitmap_clear_all(&map);
85
Bitmap operator&=(const Bitmap& map2)
87
bitmap_intersect(&map, &map2.map);
90
Bitmap operator&(uint n)
96
Bitmap operator&(const Bitmap& map2)
102
Bitmap operator|=(uint n)
104
bitmap_set_bit(&map, n);
107
Bitmap operator|=(const Bitmap& map2)
109
bitmap_union(&map, &map2.map);
111
Bitmap operator|(uint n)
117
Bitmap operator|(const Bitmap& map2)
126
bitmap_invert(&bm.map);
129
char *print(char *buf) const
132
const uchar *e=(uchar *)buffer, *b=e+sizeof(buffer)-1;
135
if ((*s=_dig_vec_upper[*b >> 4]) != '0')
137
*s++=_dig_vec_upper[*b & 15];
140
*s++=_dig_vec_upper[*b >> 4];
141
*s++=_dig_vec_upper[*b & 15];
146
uint64_t to_uint64_t() const
148
if (sizeof(buffer) >= 8)
149
return uint8korr(buffer);
150
assert(sizeof(buffer) >= 4);
151
return (uint64_t) uint4korr(buffer);
155
template <> class Bitmap<64>
159
Bitmap<64>() { map= 0; }
160
explicit Bitmap<64>(uint prefix_to_set) { set_prefix(prefix_to_set); }
162
void init(uint prefix_to_set) { set_prefix(prefix_to_set); }
163
uint length() const { return 64; }
164
void set_bit(uint n) { map|= ((uint64_t)1) << n; }
165
void clear_bit(uint n) { map&= ~(((uint64_t)1) << n); }
166
void set_prefix(uint n)
171
map= (((uint64_t)1) << n)-1;
173
void set_all() { map=~(uint64_t)0; }
174
void clear_all() { map=(uint64_t)0; }
175
void intersect(Bitmap<64>& map2) { map&= map2.map; }
176
void intersect(uint64_t map2) { map&= map2; }
177
void intersect_extended(uint64_t map2) { map&= map2; }
178
void subtract(Bitmap<64>& map2) { map&= ~map2.map; }
179
void merge(Bitmap<64>& map2) { map|= map2.map; }
180
bool is_set(uint n) const { return test(map & (((uint64_t)1) << n)); }
181
bool is_prefix(uint n) const { return map == (((uint64_t)1) << n)-1; }
182
bool is_clear_all() const { return map == (uint64_t)0; }
183
bool is_set_all() const { return map == ~(uint64_t)0; }
184
bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); }
185
bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; }
186
bool operator==(const Bitmap<64>& map2) const { return map == map2.map; }
187
char *print(char *buf) const { int64_t2str(map,buf,16); return buf; }
188
uint64_t to_uint64_t() const { return map; }
192
/* An iterator to quickly walk over bits in unint64_t bitmap. */
193
class Table_map_iterator
198
Table_map_iterator(uint64_t t) : bmp(t), no(0) {}
201
static const char last_bit[16]= {32, 0, 1, 0,
206
while ((bit= last_bit[bmp & 0xF]) == 32)
216
enum { BITMAP_END= 64 };
221
void print_bits(table_map bmp)
223
Table_map_iterator it(bmp);
225
fprintf(stderr, "0x%llx = ", bmp);
226
while ((i= it.next_bit()) != Table_map_iterator::BITMAP_END)
228
fprintf(stderr, " %s 2^%d", (first?"":"+"), i);
232
fprintf(stderr, "\n");
248
#endif /* _SQL_BITMAP_H_ */