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
1
#ifndef _SQL_BITMAP_H_
21
2
#define _SQL_BITMAP_H_
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 */
24
19
Implementation of a bitmap type.
26
21
also be able to use 32 or 64 bits bitmaps very efficiently
29
#include <mysys/my_bitmap.h>
24
#include <my_bitmap.h>
31
template <uint32_t default_width> class Bitmap
26
template <uint default_width> class Bitmap
34
uint32_t buffer[(default_width+31)/32];
29
uint32 buffer[(default_width+31)/32];
36
31
Bitmap() { init(); }
37
32
Bitmap(const Bitmap& from) { *this=from; }
38
explicit Bitmap(uint32_t prefix_to_set) { init(prefix_to_set); }
33
explicit Bitmap(uint prefix_to_set) { init(prefix_to_set); }
39
34
void init() { bitmap_init(&map, buffer, default_width, 0); }
40
void init(uint32_t prefix_to_set) { init(); set_prefix(prefix_to_set); }
41
uint32_t length() const { return default_width; }
35
void init(uint prefix_to_set) { init(); set_prefix(prefix_to_set); }
36
uint length() const { return default_width; }
42
37
Bitmap& operator=(const Bitmap& map2)
45
40
memcpy(buffer, map2.buffer, sizeof(buffer));
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); }
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); }
51
46
void set_all() { bitmap_set_all(&map); }
52
47
void clear_all() { bitmap_clear_all(&map); }
53
48
void intersect(Bitmap& map2) { bitmap_intersect(&map, &map2.map); }
54
49
void intersect(uint64_t map2buff)
57
bitmap_init(&map2, (uint32_t *)&map2buff, sizeof(uint64_t)*8, 0);
52
bitmap_init(&map2, (uint32 *)&map2buff, sizeof(uint64_t)*8, 0);
58
53
bitmap_intersect(&map, &map2);
60
55
/* Use highest bit for all bits above sizeof(uint64_t)*8. */
68
63
void subtract(Bitmap& map2) { bitmap_subtract(&map, &map2.map); }
69
64
void merge(Bitmap& map2) { bitmap_union(&map, &map2.map); }
70
bool is_set(uint32_t n) const { return bitmap_is_set(&map, n); }
71
bool is_set() const { return !bitmap_is_clear_all(&map); }
72
bool is_prefix(uint32_t n) const { return bitmap_is_prefix(&map, n); }
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); }
79
Bitmap operator&=(uint32_t n)
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)
81
76
if (bitmap_is_set(&map, n))
134
129
char *print(char *buf) const
137
const unsigned char *e=(unsigned char *)buffer, *b=e+sizeof(buffer)-1;
132
const uchar *e=(uchar *)buffer, *b=e+sizeof(buffer)-1;
138
133
while (!*b && b>e)
140
135
if ((*s=_dig_vec_upper[*b >> 4]) != '0')
164
159
Bitmap<64>() { map= 0; }
165
explicit Bitmap<64>(uint32_t prefix_to_set) { set_prefix(prefix_to_set); }
160
explicit Bitmap<64>(uint prefix_to_set) { set_prefix(prefix_to_set); }
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)
162
void init(uint prefix_to_set) { set_prefix(prefix_to_set); }
163
uint length() const { return 64; }
164
void set_bit(uint n) { map|= ((uint64_t)1) << n; }
165
void clear_bit(uint n) { map&= ~(((uint64_t)1) << n); }
166
void set_prefix(uint n)
173
168
if (n >= length())
182
177
void intersect_extended(uint64_t map2) { map&= map2; }
183
178
void subtract(Bitmap<64>& map2) { map&= ~map2.map; }
184
179
void merge(Bitmap<64>& map2) { map|= map2.map; }
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; }
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; }
180
my_bool is_set(uint n) const { return test(map & (((uint64_t)1) << n)); }
181
my_bool is_prefix(uint n) const { return map == (((uint64_t)1) << n)-1; }
182
my_bool is_clear_all() const { return map == (uint64_t)0; }
183
my_bool is_set_all() const { return map == ~(uint64_t)0; }
184
my_bool is_subset(const Bitmap<64>& map2) const { return !(map & ~map2.map); }
185
my_bool is_overlapping(const Bitmap<64>& map2) const { return (map & map2.map)!= 0; }
186
my_bool operator==(const Bitmap<64>& map2) const { return map == map2.map; }
192
187
char *print(char *buf) const { int64_t2str(map,buf,16); return buf; }
193
188
uint64_t to_uint64_t() const { return map; }