1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#ifndef _SQL_BITMAP_H_
21
#define _SQL_BITMAP_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>
32
#include <drizzled/definitions.h>
33
#include <drizzled/util/test.h>
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; }
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 */
208
typedef uint32_t nesting_map; /* Used for flags of nesting constructs */
211
Used to identify NESTED_JOIN structures within a join (applicable only to
212
structures that have not been simplified away and embed more the one
215
typedef uint64_t nested_join_map; /* Needed by sql_select.h and table.h */
217
/* useful constants */#
218
extern const key_map key_map_empty;
219
extern key_map key_map_full; /* Should be threaded as const */
222
* Finds the first bit that is not set and sets
225
* @param the bitmap to work with
227
uint32_t setNextBit(std::bitset<MAX_FIELDS> &bitmap);
230
* Returns the position of the first bit in the
231
* given bitmap which is not set. If every bit is set
232
* in the bitmap, return MY_BIT_NONE.
234
* @param the bitmap to work with
236
uint32_t getFirstBitPos(const std::bitset<MAX_FIELDS> &bitmap);
239
* Returns true if there is any overlapping bits between
240
* the 2 given bitmaps.
242
* @param the first bitmap to work with
243
* @param the second bitmap to work with
245
bool isBitmapOverlapping(const std::bitset<MAX_FIELDS> *map1, const std::bitset<MAX_FIELDS> *map2);
247
#endif /* _SQL_BITMAP_H_ */