1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/* Copyright (c) 2003, 2013, Oracle and/or its affiliates
Copyright (c) 2009, 2013, Monty Program Ab.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
/*
Implementation of a bitmap type.
The idea with this is to be able to handle any constant number of bits but
also be able to use 32 or 64 bits bitmaps very efficiently
*/
#ifndef SQL_BITMAP_INCLUDED
#define SQL_BITMAP_INCLUDED
#include <my_sys.h>
#include <my_bitmap.h>
#include <my_bit.h>
/* An iterator to quickly walk over bits in ulonglong bitmap. */
class Table_map_iterator
{
ulonglong bmp;
public:
Table_map_iterator(ulonglong t): bmp(t){}
uint next_bit()
{
if (!bmp)
return BITMAP_END;
uint bit= my_find_first_bit(bmp);
bmp &= ~(1ULL << bit);
return bit;
}
int operator++(int) { return next_bit(); }
enum { BITMAP_END= 64 };
};
template <uint width> class Bitmap
{
/*
Workaround GCC optimizer bug (generating SSE instuctions on unaligned data)
*/
#if defined (__GNUC__) && defined(__x86_64__) && (__GNUC__ < 6) && !defined(__clang__)
#define NEED_GCC_NO_SSE_WORKAROUND
#endif
#ifdef NEED_GCC_NO_SSE_WORKAROUND
#pragma GCC push_options
#pragma GCC target ("no-sse")
#endif
private:
static const int BITS_PER_ELEMENT= sizeof(ulonglong) * 8;
static const int ARRAY_ELEMENTS= (width + BITS_PER_ELEMENT - 1) / BITS_PER_ELEMENT;
static const ulonglong ALL_BITS_SET= ULLONG_MAX;
ulonglong buffer[ARRAY_ELEMENTS];
uint bit_index(uint n) const
{
DBUG_ASSERT(n < width);
return ARRAY_ELEMENTS == 1 ? 0 : n / BITS_PER_ELEMENT;
}
ulonglong bit_mask(uint n) const
{
DBUG_ASSERT(n < width);
return ARRAY_ELEMENTS == 1 ? 1ULL << n : 1ULL << (n % BITS_PER_ELEMENT);
}
ulonglong last_element_mask(int n) const
{
DBUG_ASSERT(n % BITS_PER_ELEMENT != 0);
return bit_mask(n) - 1;
}
public:
/*
The default constructor does nothing.
The caller is supposed to either zero the memory
or to call set_all()/clear_all()/set_prefix()
to initialize bitmap.
*/
Bitmap() = default;
explicit Bitmap(uint prefix)
{
set_prefix(prefix);
}
void init(uint prefix)
{
set_prefix(prefix);
}
uint length() const
{
return width;
}
void set_bit(uint n)
{
buffer[bit_index(n)] |= bit_mask(n);
}
void clear_bit(uint n)
{
buffer[bit_index(n)] &= ~bit_mask(n);
}
bool is_set(uint n) const
{
return buffer[bit_index(n)] & bit_mask(n);
}
void set_prefix(uint prefix_size)
{
set_if_smaller(prefix_size, width);
size_t idx= prefix_size / BITS_PER_ELEMENT;
for (size_t i= 0; i < idx; i++)
buffer[i]= ALL_BITS_SET;
if (prefix_size % BITS_PER_ELEMENT)
buffer[idx++]= last_element_mask(prefix_size);
for (size_t i= idx; i < ARRAY_ELEMENTS; i++)
buffer[i]= 0;
}
bool is_prefix(uint prefix_size) const
{
DBUG_ASSERT(prefix_size <= width);
size_t idx= prefix_size / BITS_PER_ELEMENT;
for (size_t i= 0; i < idx; i++)
if (buffer[i] != ALL_BITS_SET)
return false;
if (prefix_size % BITS_PER_ELEMENT)
if (buffer[idx++] != last_element_mask(prefix_size))
return false;
for (size_t i= idx; i < ARRAY_ELEMENTS; i++)
if (buffer[i] != 0)
return false;
return true;
}
void set_all()
{
if (width % BITS_PER_ELEMENT)
set_prefix(width);
else if (ARRAY_ELEMENTS > 1)
memset(buffer, 0xff, sizeof(buffer));
else
buffer[0] = ALL_BITS_SET;
}
void clear_all()
{
if (ARRAY_ELEMENTS > 1)
memset(buffer, 0, sizeof(buffer));
else
buffer[0]= 0;
}
void intersect(const Bitmap& map2)
{
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
buffer[i] &= map2.buffer[i];
}
private:
/*
Intersect with a bitmap represented as as longlong.
In addition, pad the rest of the bitmap with 0 or 1 bits
depending on pad_with_ones parameter.
*/
void intersect_and_pad(ulonglong map2buff, bool pad_with_ones)
{
buffer[0] &= map2buff;
for (size_t i= 1; i < ARRAY_ELEMENTS; i++)
buffer[i]= pad_with_ones ? ALL_BITS_SET : 0;
if (ARRAY_ELEMENTS > 1 && (width % BITS_PER_ELEMENT) && pad_with_ones)
buffer[ARRAY_ELEMENTS - 1]= last_element_mask(width);
}
public:
void intersect(ulonglong map2buff)
{
intersect_and_pad(map2buff, 0);
}
/* Use highest bit for all bits above first element. */
void intersect_extended(ulonglong map2buff)
{
intersect_and_pad(map2buff, (map2buff & (1ULL << 63)));
}
void subtract(const Bitmap& map2)
{
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
buffer[i] &= ~(map2.buffer[i]);
}
void merge(const Bitmap& map2)
{
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
buffer[i] |= map2.buffer[i];
}
bool is_clear_all() const
{
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
if (buffer[i])
return false;
return true;
}
bool is_subset(const Bitmap& map2) const
{
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
if (buffer[i] & ~(map2.buffer[i]))
return false;
return true;
}
bool is_overlapping(const Bitmap& map2) const
{
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
if (buffer[i] & map2.buffer[i])
return true;
return false;
}
bool operator==(const Bitmap& map2) const
{
if (ARRAY_ELEMENTS > 1)
return !memcmp(buffer,map2.buffer,sizeof(buffer));
return buffer[0] == map2.buffer[0];
}
bool operator!=(const Bitmap& map2) const
{
return !(*this == map2);
}
/*
Print hexadecimal representation of bitmap.
Truncate trailing zeros.
*/
char *print(char *buf) const
{
size_t last; /*index of the last non-zero element, or 0. */
for (last= ARRAY_ELEMENTS - 1; last && !buffer[last]; last--){}
const int HEX_DIGITS_PER_ELEMENT= BITS_PER_ELEMENT / 4;
for (size_t i= 0; i < last; i++)
{
ulonglong num = buffer[i];
uint shift = BITS_PER_ELEMENT - 4;
size_t pos= i * HEX_DIGITS_PER_ELEMENT;
for (size_t j= 0; j < HEX_DIGITS_PER_ELEMENT; j++)
{
buf[pos + j]= _dig_vec_upper[(num >> shift) & 0xf];
shift += 4;
}
}
longlong2str(buffer[last], buf, 16);
return buf;
}
ulonglong to_ulonglong() const
{
return buffer[0];
}
uint bits_set() const
{
uint res= 0;
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
if (buffer[i])
res+= my_count_bits(buffer[i]);
return res;
}
uint find_first_bit() const
{
for (size_t i= 0; i < ARRAY_ELEMENTS; i++)
if (buffer[i])
return (uint)i*BITS_PER_ELEMENT + my_find_first_bit(buffer[i]);
return width;
}
class Iterator
{
const Bitmap& map;
uint offset;
Table_map_iterator tmi;
public:
Iterator(const Bitmap<width>& map2) : map(map2), offset(0), tmi(map2.buffer[0]) {}
int operator++(int)
{
for (;;)
{
int nextbit= tmi++;
if (nextbit != Table_map_iterator::BITMAP_END)
return offset + nextbit;
if (offset + BITS_PER_ELEMENT >= map.length())
return BITMAP_END;
offset += BITS_PER_ELEMENT;
tmi= Table_map_iterator(map.buffer[offset / BITS_PER_ELEMENT]);
}
}
enum { BITMAP_END = width };
};
#ifdef NEED_GCC_NO_SSE_WORKAROUND
#pragma GCC pop_options
#undef NEED_GCC_NO_SSE_WORKAROUND
#endif
};
typedef Bitmap<MAX_INDEXES> key_map; /* Used for finding keys */
#endif /* SQL_BITMAP_INCLUDED */
|