20
20
#ifndef _SQL_BITMAP_H_
21
21
#define _SQL_BITMAP_H_
23
#include <drizzled/global.h>
24
Implementation of a bitmap type.
25
The idea with this is to be able to handle any constant number of bits but
26
also be able to use 32 or 64 bits bitmaps very efficiently
29
/// TODO: OMG FIX THIS
31
#include <mysys/my_bitmap.h>
24
32
#include <drizzled/definitions.h>
25
33
#include <drizzled/util/test.h>
29
#define BIT_NONE (~(uint32_t) 0)
37
template <uint32_t default_width> class Bitmap
40
uint32_t buffer[(default_width+31)/32];
42
Bitmap() : map() { init(); }
43
Bitmap(const Bitmap& from) : map() { *this=from; }
44
explicit Bitmap(uint32_t prefix_to_set) : map(0) { init(prefix_to_set); }
45
void init() { bitmap_init(&map, buffer, default_width, 0); }
46
void init(uint32_t prefix_to_set) { init(); set_prefix(prefix_to_set); }
47
uint32_t length() const { return default_width; }
48
Bitmap& operator=(const Bitmap& map2)
51
memcpy(buffer, map2.buffer, sizeof(buffer));
54
void set_bit(uint32_t n) { bitmap_set_bit(&map, n); }
55
void clear_bit(uint32_t n) { bitmap_clear_bit(&map, n); }
56
void set_prefix(uint32_t n) { bitmap_set_prefix(&map, n); }
57
void set_all() { bitmap_set_all(&map); }
58
void clear_all() { bitmap_clear_all(&map); }
59
void intersect(Bitmap& map2) { bitmap_intersect(&map, &map2.map); }
60
void intersect(uint64_t map2buff)
63
bitmap_init(&map2, (uint32_t *)&map2buff, sizeof(uint64_t)*8, 0);
64
bitmap_intersect(&map, &map2);
66
/* Use highest bit for all bits above sizeof(uint64_t)*8. */
67
void intersect_extended(uint64_t map2buff)
70
if (map.n_bits > sizeof(uint64_t) * 8)
71
bitmap_set_above(&map, sizeof(uint64_t),
72
test(map2buff & (1 << (sizeof(uint64_t) * 8 - 1))));
74
void subtract(Bitmap& map2) { bitmap_subtract(&map, &map2.map); }
75
void merge(Bitmap& map2) { bitmap_union(&map, &map2.map); }
76
bool is_set(uint32_t n) const { return bitmap_is_set(&map, n); }
77
bool is_set() const { return !bitmap_is_clear_all(&map); }
78
bool is_prefix(uint32_t n) const { return bitmap_is_prefix(&map, n); }
79
bool is_clear_all() const { return bitmap_is_clear_all(&map); }
80
bool is_set_all() const { return bitmap_is_set_all(&map); }
81
bool is_subset(const Bitmap& map2) const { return bitmap_is_subset(&map, &map2.map); }
82
bool operator==(const Bitmap& map2) const { return bitmap_cmp(&map, &map2.map); }
83
bool operator!=(const Bitmap& map2) const { return !bitmap_cmp(&map, &map2.map); }
84
Bitmap operator&=(uint32_t n)
86
if (bitmap_is_set(&map, n))
88
bitmap_clear_all(&map);
89
bitmap_set_bit(&map, n);
92
bitmap_clear_all(&map);
95
Bitmap operator&=(const Bitmap& map2)
97
bitmap_intersect(&map, &map2.map);
100
Bitmap operator&(uint32_t n)
106
Bitmap operator&(const Bitmap& map2)
112
Bitmap operator|=(uint32_t n)
114
bitmap_set_bit(&map, n);
117
Bitmap operator|=(const Bitmap& map2)
119
bitmap_union(&map, &map2.map);
121
Bitmap operator|(uint32_t n)
127
Bitmap operator|(const Bitmap& map2)
136
bitmap_invert(&bm.map);
139
char *print(char *buf) const
142
const unsigned char *e=(unsigned char *)buffer, *b=e+sizeof(buffer)-1;
145
if ((*s=_dig_vec_upper[*b >> 4]) != '0')
147
*s++=_dig_vec_upper[*b & 15];
150
*s++=_dig_vec_upper[*b >> 4];
151
*s++=_dig_vec_upper[*b & 15];
156
uint64_t to_uint64_t() const
158
if (sizeof(buffer) >= 8)
159
return uint8korr(buffer);
160
assert(sizeof(buffer) >= 4);
161
return (uint64_t) uint4korr(buffer);
165
template <> class Bitmap<64>
169
Bitmap<64>() : map(0) { }
170
explicit Bitmap<64>(uint32_t prefix_to_set) : map(0) { set_prefix(prefix_to_set); }
172
void init(uint32_t prefix_to_set) { set_prefix(prefix_to_set); }
173
uint32_t length() const { return 64; }
174
void set_bit(uint32_t n) { map|= ((uint64_t)1) << n; }
175
void clear_bit(uint32_t n) { map&= ~(((uint64_t)1) << n); }
176
void set_prefix(uint32_t n)
181
map= (((uint64_t)1) << n)-1;
183
void set_all() { map=~(uint64_t)0; }
184
void clear_all() { map=(uint64_t)0; }
185
void intersect(Bitmap<64>& map2) { map&= map2.map; }
186
void intersect(uint64_t map2) { map&= map2; }
187
void intersect_extended(uint64_t map2) { map&= map2; }
188
void subtract(Bitmap<64>& map2) { map&= ~map2.map; }
189
void merge(Bitmap<64>& map2) { map|= map2.map; }
190
bool is_set(uint32_t n) const { return test(map & (((uint64_t)1) << n)); }
191
bool is_prefix(uint32_t n) const { return map == (((uint64_t)1) << n)-1; }
192
bool is_clear_all() const { return map == (uint64_t)0; }
193
bool is_set_all() const { return map == ~(uint64_t)0; }
194
bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); }
195
bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; }
196
bool operator==(const Bitmap<64>& map2) const { return map == map2.map; }
197
char *print(char *buf) const { int64_t2str(map,buf,16); return buf; }
198
uint64_t to_uint64_t() const { return map; }
31
202
typedef uint64_t table_map; /* Used for table bits in join */
203
#if MAX_INDEXES <= 64
204
typedef Bitmap<64> key_map; /* Used for finding keys */
206
typedef Bitmap<((MAX_INDEXES+7)/8*8)> key_map; /* Used for finding keys */
32
208
typedef uint32_t nesting_map; /* Used for flags of nesting constructs */