390.1.2
by Monty Taylor
Fixed copyright headers in drizzled/ |
1 |
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Sun Microsystems
|
|
5 |
*
|
|
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.
|
|
9 |
*
|
|
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.
|
|
14 |
*
|
|
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
|
|
18 |
*/
|
|
19 |
||
1
by brian
clean slate |
20 |
#ifndef _SQL_BITMAP_H_
|
21 |
#define _SQL_BITMAP_H_
|
|
22 |
||
23 |
/*
|
|
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
|
|
27 |
*/
|
|
28 |
||
212.5.18
by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype. |
29 |
#include <mysys/my_bitmap.h> |
1
by brian
clean slate |
30 |
|
482
by Brian Aker
Remove uint. |
31 |
template <uint32_t default_width> class Bitmap |
1
by brian
clean slate |
32 |
{
|
33 |
MY_BITMAP map; |
|
205
by Brian Aker
uint32 -> uin32_t |
34 |
uint32_t buffer[(default_width+31)/32]; |
1
by brian
clean slate |
35 |
public: |
36 |
Bitmap() { init(); } |
|
37 |
Bitmap(const Bitmap& from) { *this=from; } |
|
482
by Brian Aker
Remove uint. |
38 |
explicit Bitmap(uint32_t prefix_to_set) { init(prefix_to_set); } |
1
by brian
clean slate |
39 |
void init() { bitmap_init(&map, buffer, default_width, 0); } |
482
by Brian Aker
Remove uint. |
40 |
void init(uint32_t prefix_to_set) { init(); set_prefix(prefix_to_set); } |
41 |
uint32_t length() const { return default_width; } |
|
1
by brian
clean slate |
42 |
Bitmap& operator=(const Bitmap& map2) |
43 |
{
|
|
44 |
init(); |
|
45 |
memcpy(buffer, map2.buffer, sizeof(buffer)); |
|
46 |
return *this; |
|
47 |
}
|
|
482
by Brian Aker
Remove uint. |
48 |
void set_bit(uint32_t n) { bitmap_set_bit(&map, n); } |
49 |
void clear_bit(uint32_t n) { bitmap_clear_bit(&map, n); } |
|
50 |
void set_prefix(uint32_t n) { bitmap_set_prefix(&map, n); } |
|
1
by brian
clean slate |
51 |
void set_all() { bitmap_set_all(&map); } |
52 |
void clear_all() { bitmap_clear_all(&map); } |
|
53 |
void intersect(Bitmap& map2) { bitmap_intersect(&map, &map2.map); } |
|
151
by Brian Aker
Ulonglong to uint64_t |
54 |
void intersect(uint64_t map2buff) |
1
by brian
clean slate |
55 |
{
|
56 |
MY_BITMAP map2; |
|
205
by Brian Aker
uint32 -> uin32_t |
57 |
bitmap_init(&map2, (uint32_t *)&map2buff, sizeof(uint64_t)*8, 0); |
1
by brian
clean slate |
58 |
bitmap_intersect(&map, &map2); |
59 |
}
|
|
151
by Brian Aker
Ulonglong to uint64_t |
60 |
/* Use highest bit for all bits above sizeof(uint64_t)*8. */
|
61 |
void intersect_extended(uint64_t map2buff) |
|
1
by brian
clean slate |
62 |
{
|
63 |
intersect(map2buff); |
|
59
by Brian Aker
Removed preload from parser. |
64 |
if (map.n_bits > sizeof(uint64_t) * 8) |
65 |
bitmap_set_above(&map, sizeof(uint64_t), |
|
53.2.31
by Monty Taylor
Removed HAVE_LONG_LONG, as this is now assumed. |
66 |
test(map2buff & (1 << (sizeof(uint64_t) * 8 - 1)))); |
1
by brian
clean slate |
67 |
}
|
68 |
void subtract(Bitmap& map2) { bitmap_subtract(&map, &map2.map); } |
|
69 |
void merge(Bitmap& map2) { bitmap_union(&map, &map2.map); } |
|
482
by Brian Aker
Remove uint. |
70 |
bool is_set(uint32_t n) const { return bitmap_is_set(&map, n); } |
196
by Brian Aker
Converted bitmap to use bool over my_bool |
71 |
bool is_set() const { return !bitmap_is_clear_all(&map); } |
482
by Brian Aker
Remove uint. |
72 |
bool is_prefix(uint32_t n) const { return bitmap_is_prefix(&map, n); } |
196
by Brian Aker
Converted bitmap to use bool over my_bool |
73 |
bool is_clear_all() const { return bitmap_is_clear_all(&map); } |
74 |
bool is_set_all() const { return bitmap_is_set_all(&map); } |
|
75 |
bool is_subset(const Bitmap& map2) const { return bitmap_is_subset(&map, &map2.map); } |
|
76 |
bool is_overlapping(const Bitmap& map2) const { return bitmap_is_overlapping(&map, &map2.map); } |
|
77 |
bool operator==(const Bitmap& map2) const { return bitmap_cmp(&map, &map2.map); } |
|
78 |
bool operator!=(const Bitmap& map2) const { return !bitmap_cmp(&map, &map2.map); } |
|
482
by Brian Aker
Remove uint. |
79 |
Bitmap operator&=(uint32_t n) |
1
by brian
clean slate |
80 |
{
|
81 |
if (bitmap_is_set(&map, n)) |
|
82 |
{
|
|
83 |
bitmap_clear_all(&map); |
|
84 |
bitmap_set_bit(&map, n); |
|
85 |
}
|
|
86 |
else
|
|
87 |
bitmap_clear_all(&map); |
|
88 |
return *this; |
|
89 |
}
|
|
90 |
Bitmap operator&=(const Bitmap& map2) |
|
91 |
{
|
|
92 |
bitmap_intersect(&map, &map2.map); |
|
93 |
return *this; |
|
94 |
}
|
|
482
by Brian Aker
Remove uint. |
95 |
Bitmap operator&(uint32_t n) |
1
by brian
clean slate |
96 |
{
|
97 |
Bitmap bm(*this); |
|
98 |
bm&= n; |
|
99 |
return bm; |
|
100 |
}
|
|
101 |
Bitmap operator&(const Bitmap& map2) |
|
102 |
{
|
|
103 |
Bitmap bm(*this); |
|
104 |
bm&= map2; |
|
105 |
return bm; |
|
106 |
}
|
|
482
by Brian Aker
Remove uint. |
107 |
Bitmap operator|=(uint32_t n) |
1
by brian
clean slate |
108 |
{
|
109 |
bitmap_set_bit(&map, n); |
|
110 |
return *this; |
|
111 |
}
|
|
112 |
Bitmap operator|=(const Bitmap& map2) |
|
113 |
{
|
|
114 |
bitmap_union(&map, &map2.map); |
|
115 |
}
|
|
482
by Brian Aker
Remove uint. |
116 |
Bitmap operator|(uint32_t n) |
1
by brian
clean slate |
117 |
{
|
118 |
Bitmap bm(*this); |
|
119 |
bm|= n; |
|
120 |
return bm; |
|
121 |
}
|
|
122 |
Bitmap operator|(const Bitmap& map2) |
|
123 |
{
|
|
124 |
Bitmap bm(*this); |
|
125 |
bm|= map2; |
|
126 |
return bm; |
|
127 |
}
|
|
128 |
Bitmap operator~() |
|
129 |
{
|
|
130 |
Bitmap bm(*this); |
|
131 |
bitmap_invert(&bm.map); |
|
132 |
return bm; |
|
133 |
}
|
|
134 |
char *print(char *buf) const |
|
135 |
{
|
|
136 |
char *s=buf; |
|
481
by Brian Aker
Remove all of uchar. |
137 |
const unsigned char *e=(unsigned char *)buffer, *b=e+sizeof(buffer)-1; |
1
by brian
clean slate |
138 |
while (!*b && b>e) |
139 |
b--; |
|
140 |
if ((*s=_dig_vec_upper[*b >> 4]) != '0') |
|
141 |
s++; |
|
142 |
*s++=_dig_vec_upper[*b & 15]; |
|
143 |
while (--b>=e) |
|
144 |
{
|
|
145 |
*s++=_dig_vec_upper[*b >> 4]; |
|
146 |
*s++=_dig_vec_upper[*b & 15]; |
|
147 |
}
|
|
148 |
*s=0; |
|
149 |
return buf; |
|
150 |
}
|
|
151
by Brian Aker
Ulonglong to uint64_t |
151 |
uint64_t to_uint64_t() const |
1
by brian
clean slate |
152 |
{
|
153 |
if (sizeof(buffer) >= 8) |
|
154 |
return uint8korr(buffer); |
|
51.1.49
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
155 |
assert(sizeof(buffer) >= 4); |
151
by Brian Aker
Ulonglong to uint64_t |
156 |
return (uint64_t) uint4korr(buffer); |
1
by brian
clean slate |
157 |
}
|
158 |
};
|
|
159 |
||
160 |
template <> class Bitmap<64> |
|
161 |
{
|
|
151
by Brian Aker
Ulonglong to uint64_t |
162 |
uint64_t map; |
1
by brian
clean slate |
163 |
public: |
164 |
Bitmap<64>() { map= 0; } |
|
482
by Brian Aker
Remove uint. |
165 |
explicit Bitmap<64>(uint32_t prefix_to_set) { set_prefix(prefix_to_set); } |
1
by brian
clean slate |
166 |
void init() { } |
482
by Brian Aker
Remove uint. |
167 |
void init(uint32_t prefix_to_set) { set_prefix(prefix_to_set); } |
168 |
uint32_t length() const { return 64; } |
|
169 |
void set_bit(uint32_t n) { map|= ((uint64_t)1) << n; } |
|
170 |
void clear_bit(uint32_t n) { map&= ~(((uint64_t)1) << n); } |
|
171 |
void set_prefix(uint32_t n) |
|
1
by brian
clean slate |
172 |
{
|
173 |
if (n >= length()) |
|
174 |
set_all(); |
|
175 |
else
|
|
151
by Brian Aker
Ulonglong to uint64_t |
176 |
map= (((uint64_t)1) << n)-1; |
1
by brian
clean slate |
177 |
}
|
151
by Brian Aker
Ulonglong to uint64_t |
178 |
void set_all() { map=~(uint64_t)0; } |
179 |
void clear_all() { map=(uint64_t)0; } |
|
1
by brian
clean slate |
180 |
void intersect(Bitmap<64>& map2) { map&= map2.map; } |
151
by Brian Aker
Ulonglong to uint64_t |
181 |
void intersect(uint64_t map2) { map&= map2; } |
182 |
void intersect_extended(uint64_t map2) { map&= map2; } |
|
1
by brian
clean slate |
183 |
void subtract(Bitmap<64>& map2) { map&= ~map2.map; } |
184 |
void merge(Bitmap<64>& map2) { map|= map2.map; } |
|
482
by Brian Aker
Remove uint. |
185 |
bool is_set(uint32_t n) const { return test(map & (((uint64_t)1) << n)); } |
186 |
bool is_prefix(uint32_t n) const { return map == (((uint64_t)1) << n)-1; } |
|
196
by Brian Aker
Converted bitmap to use bool over my_bool |
187 |
bool is_clear_all() const { return map == (uint64_t)0; } |
188 |
bool is_set_all() const { return map == ~(uint64_t)0; } |
|
189 |
bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); } |
|
190 |
bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; } |
|
191 |
bool operator==(const Bitmap<64>& map2) const { return map == map2.map; } |
|
152
by Brian Aker
longlong replacement |
192 |
char *print(char *buf) const { int64_t2str(map,buf,16); return buf; } |
151
by Brian Aker
Ulonglong to uint64_t |
193 |
uint64_t to_uint64_t() const { return map; } |
1
by brian
clean slate |
194 |
};
|
195 |
||
196 |
||
152
by Brian Aker
longlong replacement |
197 |
/* An iterator to quickly walk over bits in unint64_t bitmap. */
|
1
by brian
clean slate |
198 |
class Table_map_iterator |
199 |
{
|
|
151
by Brian Aker
Ulonglong to uint64_t |
200 |
uint64_t bmp; |
482
by Brian Aker
Remove uint. |
201 |
uint32_t no; |
1
by brian
clean slate |
202 |
public: |
151
by Brian Aker
Ulonglong to uint64_t |
203 |
Table_map_iterator(uint64_t t) : bmp(t), no(0) {} |
1
by brian
clean slate |
204 |
int next_bit() |
205 |
{
|
|
206 |
static const char last_bit[16]= {32, 0, 1, 0, |
|
207 |
2, 0, 1, 0, |
|
208 |
3, 0, 1, 0, |
|
209 |
2, 0, 1, 0}; |
|
482
by Brian Aker
Remove uint. |
210 |
uint32_t bit; |
1
by brian
clean slate |
211 |
while ((bit= last_bit[bmp & 0xF]) == 32) |
212 |
{
|
|
213 |
no += 4; |
|
214 |
bmp= bmp >> 4; |
|
215 |
if (!bmp) |
|
216 |
return BITMAP_END; |
|
217 |
}
|
|
53.2.31
by Monty Taylor
Removed HAVE_LONG_LONG, as this is now assumed. |
218 |
bmp &= ~(1 << bit); |
1
by brian
clean slate |
219 |
return no + bit; |
220 |
}
|
|
221 |
enum { BITMAP_END= 64 }; |
|
222 |
};
|
|
223 |
||
224 |
||
225 |
#if 0
|
|
226 |
void print_bits(table_map bmp)
|
|
227 |
{
|
|
228 |
Table_map_iterator it(bmp);
|
|
229 |
int i, first= 1;
|
|
230 |
fprintf(stderr, "0x%llx = ", bmp);
|
|
231 |
while ((i= it.next_bit()) != Table_map_iterator::BITMAP_END)
|
|
232 |
{
|
|
233 |
fprintf(stderr, " %s 2^%d", (first?"":"+"), i);
|
|
234 |
if (first)
|
|
235 |
first= 0;
|
|
236 |
}
|
|
237 |
fprintf(stderr, "\n");
|
|
238 |
}
|
|
239 |
||
240 |
int main()
|
|
241 |
{
|
|
242 |
print_bits(1024);
|
|
243 |
print_bits(3);
|
|
244 |
print_bits(0xF);
|
|
245 |
print_bits(0xF0);
|
|
246 |
print_bits(35);
|
|
247 |
print_bits(1LL<<63);
|
|
248 |
print_bits(0);
|
|
249 |
print_bits(-1LL);
|
|
250 |
}
|
|
251 |
#endif
|
|
252 |
||
253 |
#endif /* _SQL_BITMAP_H_ */ |