22 #ifndef SQL_BITMAP_INCLUDED
23 #define SQL_BITMAP_INCLUDED
26 #include <my_bitmap.h>
28 template <u
int default_w
idth>
class Bitmap
31 uint32 buffer[(default_width+31)/32];
35 explicit Bitmap(uint prefix_to_set) { init(prefix_to_set); }
36 void init() { bitmap_init(&map, buffer, default_width, 0); }
37 void init(uint prefix_to_set) { init(); set_prefix(prefix_to_set); }
38 uint length()
const {
return default_width; }
42 memcpy(buffer, map2.buffer,
sizeof(buffer));
45 void set_bit(uint
n) { bitmap_set_bit(&map, n); }
46 void clear_bit(uint n) { bitmap_clear_bit(&map, n); }
47 void set_prefix(uint n) { bitmap_set_prefix(&map, n); }
48 void set_all() { bitmap_set_all(&map); }
49 void clear_all() { bitmap_clear_all(&map); }
50 void intersect(
const Bitmap& map2) { bitmap_intersect(&map, &map2.map); }
51 void intersect(ulonglong map2buff)
54 bitmap_init(&map2, (uint32 *)&map2buff,
sizeof(ulonglong)*8, 0);
55 bitmap_intersect(&map, &map2);
58 void intersect_extended(ulonglong map2buff)
61 if (map.n_bits >
sizeof(ulonglong) * 8)
62 bitmap_set_above(&map,
sizeof(ulonglong),
63 test(map2buff & (LL(1) << (
sizeof(ulonglong) * 8 - 1))));
65 void subtract(
const Bitmap& map2) { bitmap_subtract(&map, &map2.map); }
66 void merge(
const Bitmap& map2) { bitmap_union(&map, &map2.map); }
67 my_bool is_set(uint n)
const {
return bitmap_is_set(&map, n); }
68 my_bool is_prefix(uint n)
const {
return bitmap_is_prefix(&map, n); }
69 my_bool is_clear_all()
const {
return bitmap_is_clear_all(&map); }
70 my_bool is_set_all()
const {
return bitmap_is_set_all(&map); }
71 my_bool is_subset(
const Bitmap& map2)
const {
return bitmap_is_subset(&map, &map2.map); }
72 my_bool is_overlapping(
const Bitmap& map2)
const {
return bitmap_is_overlapping(&map, &map2.map); }
73 my_bool operator==(
const Bitmap& map2)
const {
return bitmap_cmp(&map, &map2.map); }
74 my_bool operator!=(
const Bitmap& map2)
const {
return !(*
this == map2); }
75 char *print(
char *
buf)
const
78 const uchar *e=(uchar *)buffer, *b=e+
sizeof(buffer)-1;
81 if ((*s=_dig_vec_upper[*b >> 4]) !=
'0')
83 *s++=_dig_vec_upper[*b & 15];
86 *s++=_dig_vec_upper[*b >> 4];
87 *s++=_dig_vec_upper[*b & 15];
92 ulonglong to_ulonglong()
const
94 if (
sizeof(buffer) >= 8)
95 return uint8korr(buffer);
96 DBUG_ASSERT(
sizeof(buffer) >= 4);
97 return (ulonglong) uint4korr(buffer);
99 uint bits_set()
const {
return bitmap_bits_set(&map); }
107 enum { ALL_BITS = 64 };
109 #if defined(__NETWARE__) || defined(__MWERKS__)
115 explicit Bitmap(uint prefix_to_set) { set_prefix(prefix_to_set); }
117 explicit Bitmap<64>(uint prefix_to_set) { set_prefix(prefix_to_set); }
119 void init() { clear_all(); }
120 void init(uint prefix_to_set) { set_prefix(prefix_to_set); }
121 uint length()
const {
return 64; }
122 void set_bit(uint
n) { map|= ((ulonglong)1) <<
n; }
123 void clear_bit(uint n) { map&= ~(((ulonglong)1) <<
n); }
124 void set_prefix(uint n)
129 map= (((ulonglong)1) <<
n)-1;
131 void set_all() { map=~(ulonglong)0; }
132 void clear_all() { map=(ulonglong)0; }
133 void intersect(
const Bitmap<64>& map2) { map&= map2.map; }
134 void intersect(ulonglong map2) { map&= map2; }
135 void intersect_extended(ulonglong map2) { map&= map2; }
136 void subtract(
const Bitmap<64>& map2) { map&= ~map2.map; }
137 void merge(
const Bitmap<64>& map2) { map|= map2.map; }
138 my_bool is_set(uint n)
const {
return test(map & (((ulonglong)1) << n)); }
139 my_bool is_prefix(uint n)
const {
return map == (((ulonglong)1) <<
n)-1; }
140 my_bool is_clear_all()
const {
return map == (ulonglong)0; }
141 my_bool is_set_all()
const {
return map == ~(ulonglong)0; }
142 my_bool is_subset(
const Bitmap<64>& map2)
const {
return !(map & ~map2.map); }
143 my_bool is_overlapping(
const Bitmap<64>& map2)
const {
return (map & map2.map)!= 0; }
144 my_bool operator==(
const Bitmap<64>& map2)
const {
return map == map2.map; }
145 my_bool operator!=(
const Bitmap<64>& map2)
const {
return !(*
this == map2); }
146 char *print(
char *
buf)
const { longlong2str(map,buf,16);
return buf; }
147 ulonglong to_ulonglong()
const {
return map; }
160 static const char last_bit[16]= {32, 0, 1, 0,
165 while ((bit= last_bit[bmp & 0xF]) == 32)
172 bmp &= ~(1LL << bit);
175 enum { BITMAP_END= 64 };