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 MYSYS_MY_BITMAP_H
17
#define MYSYS_MY_BITMAP_H
22
static const uint32_t MY_BIT_NONE= UINT32_MAX;
24
typedef uint32_t my_bitmap_map;
28
* @brief Represents a dynamically sized bitmap.
30
* Handling of unsigned char arrays as large bitmaps.
32
* API limitations (or, rather asserted safety assumptions,
33
* to encourage correct programming)
34
* - the internal size is a set of 32 bit words
35
* - the number of bits specified in creation can be any number
38
* Original version created by Sergei Golubchik 2001 - 2004.
39
* New version written and test program added and some changes to the
40
* interface was made by Mikael Ronström 2005, with assistance of Tomas
41
* Ulin and Mats Kindahl.
54
MyBitmap(const MyBitmap& rhs);
59
* We are not de-allocating memory here correctly at the moment!!!!
60
* Placing a delete [] statement here causes bad bad things to
62
* Placing this comment here in the hope that someone can help me
63
* resolve this issue....
68
MyBitmap& operator=(const MyBitmap& rhs);
70
bool init(my_bitmap_map *buf, uint32_t num_bits);
73
* Compare the given bitmap with this bitmap.
75
* @param[in] map bitmap to perform a comparison against
76
* @return true if bitmaps are equal; false otherwise
78
bool operator==(const MyBitmap *map)
80
*last_word_ptr|= last_word_mask;
81
*(map)->last_word_ptr|= (map)->last_word_mask;
82
return memcmp(bitmap, map->getBitmap(), 4*numOfWordsInMap())==0;
86
* Test whether the specified bit is set in the bitmap
87
* already and set it if it was not already set.
89
* @param[in] bitPos position to test and set
90
* @return false if the bit was not set; true otherwise
92
bool testAndSet(const uint32_t bitPos);
95
* Test whether the specified bit is set in the bitmap
96
* and clear it if it was set.
98
* @param[in] bitPos position to test and clear
99
* @return false if the bit was not set; true otherwise
101
bool testAndClear(const uint32_t bitPos);
104
* Set the next bit position that is not yet set starting
105
* from the beginning of the bitmap.
107
* @return the bit position that was set
114
* @return the position of the first bit set.
116
uint32_t getFirstSet();
119
* @return the bits set in this bitmap.
121
uint32_t getBitsSet();
124
* Set/clear all bits above a bit.
125
* Only full bytes can be set/cleared.
126
* The function is meant for the situation that you can copy
127
* a smaller bitmap to a bigger bitmap. Bitmap lengths are
128
* always a multiple of 8 (byte size). Using 'from_byte' saves
129
* multiplication and division by 8 during parameter passing.
131
* @param[in] from_byte bitmap buffer byte offset to start with
132
* @param[in] use_bit bit value (1/0) to use for all upper bits
134
void setAbove(const uint32_t from_byte, const uint32_t use_bit);
138
* Test whether all the bits in the bitmap are clear or not.
140
* @return true if all bits in bitmap are cleared; false otherwise
142
bool isClearAll() const;
144
bool isPrefix(const uint32_t prefix_size) const;
146
void setPrefix(uint32_t prefix_size);
149
* Test whether all the bits in the bitmap are set or not.
151
* @return true if all bits in bitmap are set; false otherwise
153
bool isSetAll() const;
157
* Test if the specified bit is set or not.
159
* @param[in] bit position of bit to test
160
* @return true if the bit is set; false otherwise
162
bool isBitSet(const uint32_t bit) const
164
return (bool)(((unsigned char *)bitmap)[bit / 8] & (1 << ((bit) & 7)));
168
* Set the specified bit.
170
* @param[in] bit position of bit to set in bitmap
172
void setBit(const uint32_t bit)
174
reinterpret_cast<unsigned char *>(bitmap)[bit / 8]=
175
static_cast<unsigned char>(
176
(reinterpret_cast<unsigned char *>(bitmap))[bit / 8] |
181
* Flip the specified bit.
183
* @param[in] bit position of bit to flip in bitmap
185
void flipBit(const uint32_t bit)
187
reinterpret_cast<unsigned char *>(bitmap)[bit / 8]=
188
static_cast<unsigned char>(
189
(reinterpret_cast<unsigned char *>(bitmap))[bit / 8] ^
194
* Clear the specified bit.
196
* @param[in] bit position of bit to clear in bitmap
198
void clearBit(const uint32_t bit)
200
reinterpret_cast<unsigned char *>(bitmap)[bit / 8]=
201
static_cast<unsigned char>(
202
(reinterpret_cast<unsigned char *>(bitmap))[bit / 8] &
203
~ (1 << ((bit) & 7)));
207
* Clear all the bits in the bitmap i.e. set every
212
memset(bitmap, 0, 4*numOfWordsInMap());
216
* Set all the bits in the bitmap i.e. set every
221
memset(bitmap, 0xFF, 4*numOfWordsInMap());
225
* @return the number of words in this bitmap.
227
uint32_t numOfWordsInMap() const
229
return ((n_bits + 31)/32);
233
* @return the number of bytes in this bitmap.
235
uint32_t numOfBytesInMap() const
237
return ((n_bits + 7)/8);
241
* Get the size of the bitmap in bits.
243
* @return the size of the bitmap in bits.
245
uint32_t numOfBitsInMap() const
251
* @return the current bitmap
253
my_bitmap_map *getBitmap() const
259
* Set the bitmap to the specified bitmap.
261
* @param[in] new_bitmap bitmap to use
263
void setBitmap(my_bitmap_map *new_bitmap)
269
* Obtains the number of used bits (1..8) in the last
270
* byte and creates a mask with the upper 'unused' bits
271
* set and the lower 'used' bits clear.
273
void createLastWordMask();
276
* @return the last word pointer for this bitmap
278
my_bitmap_map *getLastWordPtr() const
280
return last_word_ptr;
283
void addMaskToLastWord() const
285
*last_word_ptr|= last_word_mask;
289
* This resets the last bits in the bitmap to 0.
291
void subtractMaskFromLastWord() const
293
*last_word_ptr&= ~last_word_mask;
299
* The bitmap is stored in this variable.
301
my_bitmap_map *bitmap;
304
* Number of bits occupied by the bitmap.
309
* A mask used for the last word in the bitmap.
311
my_bitmap_map last_word_mask;
314
* A pointer to the last word in the bitmap.
316
my_bitmap_map *last_word_ptr;
320
bool bitmap_is_subset(const MyBitmap *map1, const MyBitmap *map2);
321
bool bitmap_is_overlapping(const MyBitmap *map1,
322
const MyBitmap *map2);
324
void bitmap_intersect(MyBitmap *map, const MyBitmap *map2);
325
void bitmap_subtract(MyBitmap *map, const MyBitmap *map2);
326
void bitmap_union(MyBitmap *map, const MyBitmap *map2);
327
void bitmap_xor(MyBitmap *map, const MyBitmap *map2);
328
void bitmap_invert(MyBitmap *map);
330
/* Fast, not thread safe, bitmap functions */
331
/* This one is a define because it gets used in an array decl */
332
#define bitmap_buffer_size(bits) ((bits+31)/32)*4
335
inline T bytes_word_aligned(T bytes)
337
return (4*((bytes + 3)/4));
340
static inline bool bitmap_cmp(const MyBitmap *map1, const MyBitmap *map2)
342
map1->addMaskToLastWord();
343
map2->addMaskToLastWord();
344
return memcmp((map1)->getBitmap(),
346
4*map1->numOfWordsInMap()) == 0;
349
#endif /* MYSYS_MY_BITMAP_H */