1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#ifndef DRIZZLED_SQL_BITMAP_H
17
#define DRIZZLED_SQL_BITMAP_H
20
Implementation of a bitmap type.
21
The idea with this is to be able to handle any constant number of bits but
22
also be able to use 32 or 64 bits bitmaps very efficiently
25
#include <drizzled/definitions.h>
26
#include <drizzled/util/test.h>
27
#include <drizzled/key_map.h>
35
typedef uint64_t table_map; /* Used for table bits in join */
36
typedef uint32_t nesting_map; /* Used for flags of nesting constructs */
39
static const uint32_t MY_BIT_NONE= UINT32_MAX;
41
typedef uint32_t my_bitmap_map;
45
* @brief Represents a dynamically sized bitmap.
47
* Handling of unsigned char arrays as large bitmaps.
49
* API limitations (or, rather asserted safety assumptions,
50
* to encourage correct programming)
51
* - the internal size is a set of 32 bit words
52
* - the number of bits specified in creation can be any number
55
* Original version created by Sergei Golubchik 2001 - 2004.
56
* New version written and test program added and some changes to the
57
* interface was made by Mikael Ronström 2005, with assistance of Tomas
58
* Ulin and Mats Kindahl.
71
MyBitmap(const MyBitmap& rhs);
76
* We are not de-allocating memory here correctly at the moment!!!!
77
* Placing a delete [] statement here causes bad bad things to
79
* Placing this comment here in the hope that someone can help me
80
* resolve this issue....
85
MyBitmap& operator=(const MyBitmap& rhs);
87
bool init(my_bitmap_map *buf, uint32_t num_bits);
90
* Compare the given bitmap with this bitmap.
92
* @param[in] map bitmap to perform a comparison against
93
* @return true if bitmaps are equal; false otherwise
95
bool operator==(const MyBitmap *map)
97
*last_word_ptr|= last_word_mask;
98
*(map)->last_word_ptr|= (map)->last_word_mask;
99
return memcmp(bitmap, map->getBitmap(), 4*numOfWordsInMap())==0;
103
* Test whether the specified bit is set in the bitmap
104
* already and set it if it was not already set.
106
* @param[in] bitPos position to test and set
107
* @return false if the bit was not set; true otherwise
109
bool testAndSet(const uint32_t bitPos);
112
* Test whether the specified bit is set in the bitmap
113
* and clear it if it was set.
115
* @param[in] bitPos position to test and clear
116
* @return false if the bit was not set; true otherwise
118
bool testAndClear(const uint32_t bitPos);
121
* Set the next bit position that is not yet set starting
122
* from the beginning of the bitmap.
124
* @return the bit position that was set
131
* @return the position of the first bit set.
133
uint32_t getFirstSet();
136
* @return the bits set in this bitmap.
138
uint32_t getBitsSet();
141
* Set/clear all bits above a bit.
142
* Only full bytes can be set/cleared.
143
* The function is meant for the situation that you can copy
144
* a smaller bitmap to a bigger bitmap. Bitmap lengths are
145
* always a multiple of 8 (byte size). Using 'from_byte' saves
146
* multiplication and division by 8 during parameter passing.
148
* @param[in] from_byte bitmap buffer byte offset to start with
149
* @param[in] use_bit bit value (1/0) to use for all upper bits
151
void setAbove(const uint32_t from_byte, const uint32_t use_bit);
155
* Test whether all the bits in the bitmap are clear or not.
157
* @return true if all bits in bitmap are cleared; false otherwise
159
bool isClearAll() const;
161
bool isPrefix(const uint32_t prefix_size) const;
163
void setPrefix(uint32_t prefix_size);
166
* Test whether all the bits in the bitmap are set or not.
168
* @return true if all bits in bitmap are set; false otherwise
170
bool isSetAll() const;
174
* Test if the specified bit is set or not.
176
* @param[in] bit position of bit to test
177
* @return true if the bit is set; false otherwise
179
bool isBitSet(const uint32_t bit) const
181
return (bool)((reinterpret_cast<unsigned char *>(bitmap))[bit / 8] & (1 << ((bit) & 7)));
185
* Set the specified bit.
187
* @param[in] bit position of bit to set in bitmap
189
void setBit(const uint32_t bit)
191
reinterpret_cast<unsigned char *>(bitmap)[bit / 8]=
192
static_cast<unsigned char>(
193
(reinterpret_cast<unsigned char *>(bitmap))[bit / 8] |
198
* Flip the specified bit.
200
* @param[in] bit position of bit to flip in bitmap
202
void flipBit(const uint32_t bit)
204
reinterpret_cast<unsigned char *>(bitmap)[bit / 8]=
205
static_cast<unsigned char>(
206
(reinterpret_cast<unsigned char *>(bitmap))[bit / 8] ^
211
* Clear the specified bit.
213
* @param[in] bit position of bit to clear in bitmap
215
void clearBit(const uint32_t bit)
217
reinterpret_cast<unsigned char *>(bitmap)[bit / 8]=
218
static_cast<unsigned char>(
219
(reinterpret_cast<unsigned char *>(bitmap))[bit / 8] &
220
~ (1 << ((bit) & 7)));
224
* Clear all the bits in the bitmap i.e. set every
229
memset(bitmap, 0, 4*numOfWordsInMap());
233
* Set all the bits in the bitmap i.e. set every
238
memset(bitmap, 0xFF, 4*numOfWordsInMap());
242
* @return the number of words in this bitmap.
244
uint32_t numOfWordsInMap() const
246
return ((n_bits + 31)/32);
250
* @return the number of bytes in this bitmap.
252
uint32_t numOfBytesInMap() const
254
return ((n_bits + 7)/8);
258
* Get the size of the bitmap in bits.
260
* @return the size of the bitmap in bits.
262
uint32_t numOfBitsInMap() const
268
* @return the current bitmap
270
my_bitmap_map *getBitmap() const
276
* Set the bitmap to the specified bitmap.
278
* @param[in] new_bitmap bitmap to use
280
void setBitmap(my_bitmap_map *new_bitmap)
286
* Obtains the number of used bits (1..8) in the last
287
* byte and creates a mask with the upper 'unused' bits
288
* set and the lower 'used' bits clear.
290
void createLastWordMask();
293
* @return the last word pointer for this bitmap
295
my_bitmap_map *getLastWordPtr() const
297
return last_word_ptr;
300
void addMaskToLastWord() const
302
*last_word_ptr|= last_word_mask;
306
* This resets the last bits in the bitmap to 0.
308
void subtractMaskFromLastWord() const
310
*last_word_ptr&= ~last_word_mask;
316
* The bitmap is stored in this variable.
318
my_bitmap_map *bitmap;
321
* Number of bits occupied by the bitmap.
326
* A mask used for the last word in the bitmap.
328
my_bitmap_map last_word_mask;
331
* A pointer to the last word in the bitmap.
333
my_bitmap_map *last_word_ptr;
337
bool bitmap_is_subset(const MyBitmap *map1, const MyBitmap *map2);
338
bool bitmap_is_overlapping(const MyBitmap *map1,
339
const MyBitmap *map2);
341
void bitmap_intersect(MyBitmap *map, const MyBitmap *map2);
342
void bitmap_subtract(MyBitmap *map, const MyBitmap *map2);
343
void bitmap_union(MyBitmap *map, const MyBitmap *map2);
344
void bitmap_xor(MyBitmap *map, const MyBitmap *map2);
345
void bitmap_invert(MyBitmap *map);
347
/* Fast, not thread safe, bitmap functions */
348
/* This one is a define because it gets used in an array decl */
349
#define bitmap_buffer_size(bits) ((bits+31)/32)*4
352
inline T bytes_word_aligned(T bytes)
354
return (4*((bytes + 3)/4));
357
static inline bool bitmap_cmp(const MyBitmap *map1, const MyBitmap *map2)
359
map1->addMaskToLastWord();
360
map2->addMaskToLastWord();
361
return memcmp((map1)->getBitmap(),
363
4*map1->numOfWordsInMap()) == 0;
366
} /* namespace drizzled */
368
#endif /* DRIZZLED_SQL_BITMAP_H */