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 <my_bitmap.h>
26
template <uint default_width> class Bitmap
29
uint32 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(ulonglong map2buff)
52
bitmap_init(&map2, (uint32 *)&map2buff, sizeof(ulonglong)*8, 0);
53
bitmap_intersect(&map, &map2);
55
/* Use highest bit for all bits above sizeof(ulonglong)*8. */
56
void intersect_extended(ulonglong map2buff)
59
if (map.n_bits > sizeof(ulonglong) * 8)
60
bitmap_set_above(&map, sizeof(ulonglong),
61
test(map2buff & (LL(1) << (sizeof(ulonglong) * 8 - 1))));
63
void subtract(Bitmap& map2) { bitmap_subtract(&map, &map2.map); }
64
void merge(Bitmap& map2) { bitmap_union(&map, &map2.map); }
65
my_bool is_set(uint n) const { return bitmap_is_set(&map, n); }
66
my_bool is_set() const { return !bitmap_is_clear_all(&map); }
67
my_bool is_prefix(uint n) const { return bitmap_is_prefix(&map, n); }
68
my_bool is_clear_all() const { return bitmap_is_clear_all(&map); }
69
my_bool is_set_all() const { return bitmap_is_set_all(&map); }
70
my_bool is_subset(const Bitmap& map2) const { return bitmap_is_subset(&map, &map2.map); }
71
my_bool is_overlapping(const Bitmap& map2) const { return bitmap_is_overlapping(&map, &map2.map); }
72
my_bool operator==(const Bitmap& map2) const { return bitmap_cmp(&map, &map2.map); }
73
my_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
ulonglong to_ulonglong() const
148
if (sizeof(buffer) >= 8)
149
return uint8korr(buffer);
150
DBUG_ASSERT(sizeof(buffer) >= 4);
151
return (ulonglong) uint4korr(buffer);
155
template <> class Bitmap<64>
159
Bitmap<64>() { map= 0; }
160
#if defined(__NETWARE__) || defined(__MWERKS__)
162
Metwork compiler gives error on Bitmap<64>
163
Changed to Bitmap, since in this case also it will proper construct
166
explicit Bitmap(uint prefix_to_set) { set_prefix(prefix_to_set); }
168
explicit Bitmap<64>(uint prefix_to_set) { set_prefix(prefix_to_set); }
171
void init(uint prefix_to_set) { set_prefix(prefix_to_set); }
172
uint length() const { return 64; }
173
void set_bit(uint n) { map|= ((ulonglong)1) << n; }
174
void clear_bit(uint n) { map&= ~(((ulonglong)1) << n); }
175
void set_prefix(uint n)
180
map= (((ulonglong)1) << n)-1;
182
void set_all() { map=~(ulonglong)0; }
183
void clear_all() { map=(ulonglong)0; }
184
void intersect(Bitmap<64>& map2) { map&= map2.map; }
185
void intersect(ulonglong map2) { map&= map2; }
186
void intersect_extended(ulonglong map2) { map&= map2; }
187
void subtract(Bitmap<64>& map2) { map&= ~map2.map; }
188
void merge(Bitmap<64>& map2) { map|= map2.map; }
189
my_bool is_set(uint n) const { return test(map & (((ulonglong)1) << n)); }
190
my_bool is_prefix(uint n) const { return map == (((ulonglong)1) << n)-1; }
191
my_bool is_clear_all() const { return map == (ulonglong)0; }
192
my_bool is_set_all() const { return map == ~(ulonglong)0; }
193
my_bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); }
194
my_bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; }
195
my_bool operator==(const Bitmap<64>& map2) const { return map == map2.map; }
196
char *print(char *buf) const { longlong2str(map,buf,16); return buf; }
197
ulonglong to_ulonglong() const { return map; }
201
/* An iterator to quickly walk over bits in unlonglong bitmap. */
202
class Table_map_iterator
207
Table_map_iterator(ulonglong t) : bmp(t), no(0) {}
210
static const char last_bit[16]= {32, 0, 1, 0,
215
while ((bit= last_bit[bmp & 0xF]) == 32)
222
bmp &= ~(1LL << bit);
225
enum { BITMAP_END= 64 };
230
void print_bits(table_map bmp)
232
Table_map_iterator it(bmp);
234
fprintf(stderr, "0x%llx = ", bmp);
235
while ((i= it.next_bit()) != Table_map_iterator::BITMAP_END)
237
fprintf(stderr, " %s 2^%d", (first?"":"+"), i);
241
fprintf(stderr, "\n");
257
#endif /* _SQL_BITMAP_H_ */