~drizzle-trunk/drizzle/development

1005.2.1 by Monty Taylor
Reverted a crap-ton of padraig's work.
1
/* Copyright (C) 2000 MySQL AB
2
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.
6
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.
11
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 */
15
1241.9.54 by Monty Taylor
Moved bitmap into drizzled.
16
#ifndef DRIZZLED_SQL_BITMAP_H
17
#define DRIZZLED_SQL_BITMAP_H
18
19
/*
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
23
*/
24
25
#include <drizzled/definitions.h>
26
#include <drizzled/util/test.h>
27
#include <drizzled/key_map.h>
1005.2.15 by Monty Taylor
More my_bitmap header cleanup.
28
#include <pthread.h>
1241.9.54 by Monty Taylor
Moved bitmap into drizzled.
29
30
#include <cstring>
31
32
33
34
typedef uint64_t table_map;          /* Used for table bits in join */
35
typedef uint32_t nesting_map;  /* Used for flags of nesting constructs */
36
1005.2.15 by Monty Taylor
More my_bitmap header cleanup.
37
38
static const uint32_t MY_BIT_NONE= UINT32_MAX;
1005.2.1 by Monty Taylor
Reverted a crap-ton of padraig's work.
39
40
typedef uint32_t my_bitmap_map;
41
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
42
/**
43
 * @class MyBitmap
44
 * @brief Represents a dynamically sized bitmap.
45
 *
46
 * Handling of unsigned char arrays as large bitmaps.
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
47
 *
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
48
 * API limitations (or, rather asserted safety assumptions,
49
 * to encourage correct programming)
50
 *   - the internal size is a set of 32 bit words
51
 *   - the number of bits specified in creation can be any number
52
 *     greater than 0
53
 *
54
 * Original version created by Sergei Golubchik 2001 - 2004.
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
55
 * New version written and test program added and some changes to the
56
 * interface was made by Mikael Ronström 2005, with assistance of Tomas
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
57
 * Ulin and Mats Kindahl.
58
 */
59
class MyBitmap
1005.2.1 by Monty Taylor
Reverted a crap-ton of padraig's work.
60
{
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
61
public:
62
  MyBitmap()
63
    :
64
      bitmap(NULL),
65
      n_bits(0),
66
      last_word_mask(0),
1103.6.2 by Padraig O'Sullivan
Removing references to MY_BITMAP throughout the code base and updating calls
67
      last_word_ptr(NULL)
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
68
  {}
69
70
  MyBitmap(const MyBitmap& rhs);
71
72
  ~MyBitmap()
73
  {
1103.6.6 by Padraig O'Sullivan
Updating desctructor in MyBitmap class to work for now (although not
74
      /*
75
       * We are not de-allocating memory here correctly at the moment!!!!
76
       * Placing a delete [] statement here causes bad bad things to
77
       * happen......
78
       * Placing this comment here in the hope that someone can help me
79
       * resolve this issue....
80
       */
81
      bitmap= 0;
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
82
  }
83
84
  MyBitmap& operator=(const MyBitmap& rhs);
85
86
  bool init(my_bitmap_map *buf, uint32_t num_bits);
87
88
  /**
89
   * Compare the given bitmap with this bitmap.
90
   *
91
   * @param[in] map bitmap to perform a comparison against
92
   * @return true if bitmaps are equal; false otherwise
93
   */
94
  bool operator==(const MyBitmap *map)
95
  {
96
    *last_word_ptr|= last_word_mask;
97
    *(map)->last_word_ptr|= (map)->last_word_mask;
98
    return memcmp(bitmap, map->getBitmap(), 4*numOfWordsInMap())==0;
99
  }
100
101
  /**
102
   * Test whether the specified bit is set in the bitmap
103
   * already and set it if it was not already set.
104
   *
105
   * @param[in] bitPos position to test and set
106
   * @return false if the bit was not set; true otherwise
107
   */
108
  bool testAndSet(const uint32_t bitPos);
109
110
  /**
111
   * Test whether the specified bit is set in the bitmap
112
   * and clear it if it was set.
113
   *
114
   * @param[in] bitPos position to test and clear
115
   * @return false if the bit was not set; true otherwise
116
   */
117
  bool testAndClear(const uint32_t bitPos);
118
119
  /**
120
   * Set the next bit position that is not yet set starting
121
   * from the beginning of the bitmap.
122
   *
123
   * @return the bit position that was set
124
   */
125
  uint32_t setNext();
126
127
  uint32_t getFirst();
128
129
  /**
130
   * @return the position of the first bit set.
131
   */
132
  uint32_t getFirstSet();
133
134
  /**
135
   * @return the bits set in this bitmap.
136
   */
137
  uint32_t getBitsSet();
138
139
  /**
140
   * Set/clear all bits above a bit.
141
   * Only full bytes can be set/cleared.
142
   * The function is meant for the situation that you can copy
143
   * a smaller bitmap to a bigger bitmap. Bitmap lengths are
144
   * always a multiple of 8 (byte size). Using 'from_byte' saves
145
   * multiplication and division by 8 during parameter passing.
146
   *
147
   * @param[in] from_byte bitmap buffer byte offset to start with
148
   * @param[in] use_bit bit value (1/0) to use for all upper bits
149
   */
150
  void setAbove(const uint32_t from_byte, const uint32_t use_bit);
151
152
153
  /**
154
   * Test whether all the bits in the bitmap are clear or not.
155
   *
156
   * @return true if all bits in bitmap are cleared; false otherwise
157
   */
158
  bool isClearAll() const;
159
160
  bool isPrefix(const uint32_t prefix_size) const;
161
162
  void setPrefix(uint32_t prefix_size);
163
164
  /**
165
   * Test whether all the bits in the bitmap are set or not.
166
   *
167
   * @return true if all bits in bitmap are set; false otherwise
168
   */
169
  bool isSetAll() const;
170
171
172
  /**
173
   * Test if the specified bit is set or not.
174
   *
175
   * @param[in] bit position of bit to test
176
   * @return true if the bit is set; false otherwise
177
   */
178
  bool isBitSet(const uint32_t bit) const
179
  {
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
180
     return (bool)((reinterpret_cast<unsigned char *>(bitmap))[bit / 8] &  (1 << ((bit) & 7)));
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
181
  }
182
183
  /**
184
   * Set the specified bit.
185
   *
186
   * @param[in] bit position of bit to set in bitmap
187
   */
188
  void setBit(const uint32_t bit)
189
  {
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
190
    reinterpret_cast<unsigned char *>(bitmap)[bit / 8]=
1126.8.1 by Joe Daly
changes to allow -Wconversion flag to be turned on
191
      static_cast<unsigned char>(
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
192
      (reinterpret_cast<unsigned char *>(bitmap))[bit / 8] |
1126.8.1 by Joe Daly
changes to allow -Wconversion flag to be turned on
193
      (1 << ((bit) & 7)));
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
194
  }
195
196
  /**
197
   * Flip the specified bit.
198
   *
199
   * @param[in] bit position of bit to flip in bitmap
200
   */
201
  void flipBit(const uint32_t bit)
202
  {
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
203
    reinterpret_cast<unsigned char *>(bitmap)[bit / 8]=
1126.8.1 by Joe Daly
changes to allow -Wconversion flag to be turned on
204
      static_cast<unsigned char>(
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
205
      (reinterpret_cast<unsigned char *>(bitmap))[bit / 8] ^
1126.8.1 by Joe Daly
changes to allow -Wconversion flag to be turned on
206
      (1 << ((bit) & 7)));
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
207
  }
208
209
  /**
210
   * Clear the specified bit.
211
   *
212
   * @param[in] bit position of bit to clear in bitmap
213
   */
214
  void clearBit(const uint32_t bit)
215
  {
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
216
    reinterpret_cast<unsigned char *>(bitmap)[bit / 8]=
1126.8.1 by Joe Daly
changes to allow -Wconversion flag to be turned on
217
      static_cast<unsigned char>(
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
218
      (reinterpret_cast<unsigned char *>(bitmap))[bit / 8] &
1126.8.1 by Joe Daly
changes to allow -Wconversion flag to be turned on
219
      ~ (1 << ((bit) & 7)));
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
220
  }
221
222
  /**
223
   * Clear all the bits in the bitmap i.e. set every
224
   * bit to 0.
225
   */
226
  void clearAll()
227
  {
228
     memset(bitmap, 0, 4*numOfWordsInMap());
229
  }
230
231
  /**
232
   * Set all the bits in the bitmap i.e. set every
233
   * bit to 1.
234
   */
235
  void setAll()
236
  {
237
     memset(bitmap, 0xFF, 4*numOfWordsInMap());
238
  }
239
240
  /**
241
   * @return the number of words in this bitmap.
242
   */
243
  uint32_t numOfWordsInMap() const
244
  {
245
    return ((n_bits + 31)/32);
246
  }
247
248
  /**
249
   * @return the number of bytes in this bitmap.
250
   */
251
  uint32_t numOfBytesInMap() const
252
  {
253
    return ((n_bits + 7)/8);
254
  }
255
256
  /**
257
   * Get the size of the bitmap in bits.
258
   *
259
   * @return the size of the bitmap in bits.
260
   */
261
  uint32_t numOfBitsInMap() const
262
  {
263
    return n_bits;
264
  }
265
266
  /**
267
   * @return the current bitmap
268
   */
269
  my_bitmap_map *getBitmap() const
270
  {
271
    return bitmap;
272
  }
273
274
  /**
1103.6.2 by Padraig O'Sullivan
Removing references to MY_BITMAP throughout the code base and updating calls
275
   * Set the bitmap to the specified bitmap.
276
   *
277
   * @param[in] new_bitmap bitmap to use
278
   */
279
  void setBitmap(my_bitmap_map *new_bitmap)
280
  {
281
    bitmap= new_bitmap;
282
  }
283
284
  /**
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
285
   * Obtains the number of used bits (1..8) in the last
286
   * byte and creates a mask with the upper 'unused' bits
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
287
   * set and the lower 'used' bits clear.
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
288
   */
289
  void createLastWordMask();
290
291
  /**
292
   * @return the last word pointer for this bitmap
293
   */
294
  my_bitmap_map *getLastWordPtr() const
295
  {
296
    return last_word_ptr;
297
  }
298
299
  void addMaskToLastWord() const
300
  {
301
    *last_word_ptr|= last_word_mask;
302
  }
303
304
  /**
305
   * This resets the last bits in the bitmap to 0.
306
   */
307
  void subtractMaskFromLastWord() const
308
  {
309
    *last_word_ptr&= ~last_word_mask;
310
  }
311
312
private:
313
314
  /**
315
   * The bitmap is stored in this variable.
316
   */
1005.2.1 by Monty Taylor
Reverted a crap-ton of padraig's work.
317
  my_bitmap_map *bitmap;
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
318
319
  /**
320
   * Number of bits occupied by the bitmap.
321
   */
322
  uint32_t n_bits;
323
324
  /**
325
   * A mask used for the last word in the bitmap.
326
   */
1005.2.1 by Monty Taylor
Reverted a crap-ton of padraig's work.
327
  my_bitmap_map last_word_mask;
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
328
329
  /**
330
   * A pointer to the last word in the bitmap.
331
   */
1005.2.1 by Monty Taylor
Reverted a crap-ton of padraig's work.
332
  my_bitmap_map *last_word_ptr;
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
333
334
};
335
336
bool bitmap_is_subset(const MyBitmap *map1, const MyBitmap *map2);
337
bool bitmap_is_overlapping(const MyBitmap *map1,
338
                           const MyBitmap *map2);
339
340
void bitmap_intersect(MyBitmap *map, const MyBitmap *map2);
341
void bitmap_subtract(MyBitmap *map, const MyBitmap *map2);
342
void bitmap_union(MyBitmap *map, const MyBitmap *map2);
343
void bitmap_xor(MyBitmap *map, const MyBitmap *map2);
344
void bitmap_invert(MyBitmap *map);
1005.2.15 by Monty Taylor
More my_bitmap header cleanup.
345
1005.2.1 by Monty Taylor
Reverted a crap-ton of padraig's work.
346
/* Fast, not thread safe, bitmap functions */
1005.2.15 by Monty Taylor
More my_bitmap header cleanup.
347
/* This one is a define because it gets used in an array decl */
348
#define bitmap_buffer_size(bits) ((bits+31)/32)*4
349
350
template <class T>
351
inline T bytes_word_aligned(T bytes)
352
{
353
  return (4*((bytes + 3)/4));
354
}
1005.2.13 by Monty Taylor
Turned macros in my_bitmap into honest inline methods.
355
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
356
static inline bool bitmap_cmp(const MyBitmap *map1, const MyBitmap *map2)
357
{
358
  map1->addMaskToLastWord();
359
  map2->addMaskToLastWord();
1241.3.3 by Trond Norbye
Cleanup: use c++ style casting
360
  return memcmp((map1)->getBitmap(),
1103.6.1 by Padraig O'Sullivan
Converted MY_BITMAP from a struct to a class named MyBitmap. Added a copy
361
                (map2)->getBitmap(),
362
                4*map1->numOfWordsInMap()) == 0;
1005.2.13 by Monty Taylor
Turned macros in my_bitmap into honest inline methods.
363
}
1005.2.1 by Monty Taylor
Reverted a crap-ton of padraig's work.
364
1241.9.54 by Monty Taylor
Moved bitmap into drizzled.
365
#endif /* DRIZZLED_SQL_BITMAP_H */