~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_bitmap.h

  • Committer: Padraig O'Sullivan
  • Date: 2009-08-26 03:17:31 UTC
  • mfrom: (1124 staging)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: osullivan.padraig@gmail.com-20090826031731-at2as3ledixngra3
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
typedef uint32_t my_bitmap_map;
25
25
 
26
 
typedef struct st_bitmap
 
26
/**
 
27
 * @class MyBitmap
 
28
 * @brief Represents a dynamically sized bitmap.
 
29
 *
 
30
 * Handling of unsigned char arrays as large bitmaps.
 
31
 * 
 
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
 
36
 *     greater than 0
 
37
 *
 
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.
 
42
 */
 
43
class MyBitmap
27
44
{
 
45
public:
 
46
  MyBitmap()
 
47
    :
 
48
      bitmap(NULL),
 
49
      n_bits(0),
 
50
      last_word_mask(0),
 
51
      last_word_ptr(NULL)
 
52
  {}
 
53
 
 
54
  MyBitmap(const MyBitmap& rhs);
 
55
 
 
56
  ~MyBitmap()
 
57
  {
 
58
      /*
 
59
       * We are not de-allocating memory here correctly at the moment!!!!
 
60
       * Placing a delete [] statement here causes bad bad things to
 
61
       * happen......
 
62
       * Placing this comment here in the hope that someone can help me
 
63
       * resolve this issue....
 
64
       */
 
65
      bitmap= 0;
 
66
  }
 
67
 
 
68
  MyBitmap& operator=(const MyBitmap& rhs);
 
69
 
 
70
  bool init(my_bitmap_map *buf, uint32_t num_bits);
 
71
 
 
72
  /**
 
73
   * Compare the given bitmap with this bitmap.
 
74
   *
 
75
   * @param[in] map bitmap to perform a comparison against
 
76
   * @return true if bitmaps are equal; false otherwise
 
77
   */
 
78
  bool operator==(const MyBitmap *map)
 
79
  {
 
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;
 
83
  }
 
84
 
 
85
  /**
 
86
   * Test whether the specified bit is set in the bitmap
 
87
   * already and set it if it was not already set.
 
88
   *
 
89
   * @param[in] bitPos position to test and set
 
90
   * @return false if the bit was not set; true otherwise
 
91
   */
 
92
  bool testAndSet(const uint32_t bitPos);
 
93
 
 
94
  /**
 
95
   * Test whether the specified bit is set in the bitmap
 
96
   * and clear it if it was set.
 
97
   *
 
98
   * @param[in] bitPos position to test and clear
 
99
   * @return false if the bit was not set; true otherwise
 
100
   */
 
101
  bool testAndClear(const uint32_t bitPos);
 
102
 
 
103
  /**
 
104
   * Set the next bit position that is not yet set starting
 
105
   * from the beginning of the bitmap.
 
106
   *
 
107
   * @return the bit position that was set
 
108
   */
 
109
  uint32_t setNext();
 
110
 
 
111
  uint32_t getFirst();
 
112
 
 
113
  /**
 
114
   * @return the position of the first bit set.
 
115
   */
 
116
  uint32_t getFirstSet();
 
117
 
 
118
  /**
 
119
   * @return the bits set in this bitmap.
 
120
   */
 
121
  uint32_t getBitsSet();
 
122
 
 
123
  /**
 
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.
 
130
   *
 
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
 
133
   */
 
134
  void setAbove(const uint32_t from_byte, const uint32_t use_bit);
 
135
 
 
136
 
 
137
  /**
 
138
   * Test whether all the bits in the bitmap are clear or not.
 
139
   *
 
140
   * @return true if all bits in bitmap are cleared; false otherwise
 
141
   */
 
142
  bool isClearAll() const;
 
143
 
 
144
  bool isPrefix(const uint32_t prefix_size) const;
 
145
 
 
146
  void setPrefix(uint32_t prefix_size);
 
147
 
 
148
  /**
 
149
   * Test whether all the bits in the bitmap are set or not.
 
150
   *
 
151
   * @return true if all bits in bitmap are set; false otherwise
 
152
   */
 
153
  bool isSetAll() const;
 
154
 
 
155
 
 
156
  /**
 
157
   * Test if the specified bit is set or not.
 
158
   *
 
159
   * @param[in] bit position of bit to test
 
160
   * @return true if the bit is set; false otherwise
 
161
   */
 
162
  bool isBitSet(const uint32_t bit) const
 
163
  {
 
164
    return (bool)(((unsigned char *)bitmap)[bit / 8] &  (1 << ((bit) & 7)));
 
165
  }
 
166
 
 
167
  /**
 
168
   * Set the specified bit.
 
169
   *
 
170
   * @param[in] bit position of bit to set in bitmap
 
171
   */
 
172
  void setBit(const uint32_t bit)
 
173
  {
 
174
    ((unsigned char *)bitmap)[bit / 8] |= (1 << ((bit) & 7));
 
175
  }
 
176
 
 
177
  /**
 
178
   * Flip the specified bit.
 
179
   *
 
180
   * @param[in] bit position of bit to flip in bitmap
 
181
   */
 
182
  void flipBit(const uint32_t bit)
 
183
  {
 
184
    ((unsigned char *)bitmap)[bit / 8] ^= (1 << ((bit) & 7));
 
185
  }
 
186
 
 
187
  /**
 
188
   * Clear the specified bit.
 
189
   *
 
190
   * @param[in] bit position of bit to clear in bitmap
 
191
   */
 
192
  void clearBit(const uint32_t bit)
 
193
  {
 
194
    ((unsigned char *)bitmap)[bit / 8] &= ~ (1 << ((bit) & 7));
 
195
  }
 
196
 
 
197
  /**
 
198
   * Clear all the bits in the bitmap i.e. set every
 
199
   * bit to 0.
 
200
   */
 
201
  void clearAll()
 
202
  {
 
203
     memset(bitmap, 0, 4*numOfWordsInMap());
 
204
  }
 
205
 
 
206
  /**
 
207
   * Set all the bits in the bitmap i.e. set every
 
208
   * bit to 1.
 
209
   */
 
210
  void setAll()
 
211
  {
 
212
     memset(bitmap, 0xFF, 4*numOfWordsInMap());
 
213
  }
 
214
 
 
215
  /**
 
216
   * @return the number of words in this bitmap.
 
217
   */
 
218
  uint32_t numOfWordsInMap() const
 
219
  {
 
220
    return ((n_bits + 31)/32);
 
221
  }
 
222
 
 
223
  /**
 
224
   * @return the number of bytes in this bitmap.
 
225
   */
 
226
  uint32_t numOfBytesInMap() const
 
227
  {
 
228
    return ((n_bits + 7)/8);
 
229
  }
 
230
 
 
231
  /**
 
232
   * Get the size of the bitmap in bits.
 
233
   *
 
234
   * @return the size of the bitmap in bits.
 
235
   */
 
236
  uint32_t numOfBitsInMap() const
 
237
  {
 
238
    return n_bits;
 
239
  }
 
240
 
 
241
  /**
 
242
   * @return the current bitmap
 
243
   */
 
244
  my_bitmap_map *getBitmap() const
 
245
  {
 
246
    return bitmap;
 
247
  }
 
248
 
 
249
  /**
 
250
   * Set the bitmap to the specified bitmap.
 
251
   *
 
252
   * @param[in] new_bitmap bitmap to use
 
253
   */
 
254
  void setBitmap(my_bitmap_map *new_bitmap)
 
255
  {
 
256
    bitmap= new_bitmap;
 
257
  }
 
258
 
 
259
  /**
 
260
   * Obtains the number of used bits (1..8) in the last
 
261
   * byte and creates a mask with the upper 'unused' bits
 
262
   * set and the lower 'used' bits clear. 
 
263
   */
 
264
  void createLastWordMask();
 
265
 
 
266
  /**
 
267
   * @return the last word pointer for this bitmap
 
268
   */
 
269
  my_bitmap_map *getLastWordPtr() const
 
270
  {
 
271
    return last_word_ptr;
 
272
  }
 
273
 
 
274
  void addMaskToLastWord() const
 
275
  {
 
276
    *last_word_ptr|= last_word_mask;
 
277
  }
 
278
 
 
279
  /**
 
280
   * This resets the last bits in the bitmap to 0.
 
281
   */
 
282
  void subtractMaskFromLastWord() const
 
283
  {
 
284
    *last_word_ptr&= ~last_word_mask;
 
285
  }
 
286
 
 
287
private:
 
288
 
 
289
  /**
 
290
   * The bitmap is stored in this variable.
 
291
   */
28
292
  my_bitmap_map *bitmap;
29
 
  uint32_t n_bits; /* number of bits occupied by the above */
 
293
 
 
294
  /**
 
295
   * Number of bits occupied by the bitmap.
 
296
   */
 
297
  uint32_t n_bits;
 
298
 
 
299
  /**
 
300
   * A mask used for the last word in the bitmap.
 
301
   */
30
302
  my_bitmap_map last_word_mask;
 
303
 
 
304
  /**
 
305
   * A pointer to the last word in the bitmap.
 
306
   */
31
307
  my_bitmap_map *last_word_ptr;
32
 
  st_bitmap()
33
 
    : bitmap(NULL), n_bits(0), last_word_mask(0), last_word_ptr(NULL) {}
34
 
} MY_BITMAP;
35
 
 
36
 
void create_last_word_mask(MY_BITMAP *map);
37
 
bool bitmap_init(MY_BITMAP *map, my_bitmap_map *buf, uint32_t n_bits);
38
 
bool bitmap_is_clear_all(const MY_BITMAP *map);
39
 
bool bitmap_is_prefix(const MY_BITMAP *map, uint32_t prefix_size);
40
 
bool bitmap_is_set_all(const MY_BITMAP *map);
41
 
bool bitmap_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2);
42
 
bool bitmap_is_overlapping(const MY_BITMAP *map1,
43
 
                                     const MY_BITMAP *map2);
44
 
bool bitmap_test_and_set(MY_BITMAP *map, uint32_t bitmap_bit);
45
 
bool bitmap_test_and_clear(MY_BITMAP *map, uint32_t bitmap_bit);
46
 
uint32_t bitmap_set_next(MY_BITMAP *map);
47
 
uint32_t bitmap_get_first(const MY_BITMAP *map);
48
 
uint32_t bitmap_get_first_set(const MY_BITMAP *map);
49
 
uint32_t bitmap_bits_set(const MY_BITMAP *map);
50
 
void bitmap_free(MY_BITMAP *map);
51
 
void bitmap_set_above(MY_BITMAP *map, uint32_t from_byte, uint32_t use_bit);
52
 
void bitmap_set_prefix(MY_BITMAP *map, uint32_t prefix_size);
53
 
void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2);
54
 
void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2);
55
 
void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2);
56
 
void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2);
57
 
void bitmap_invert(MY_BITMAP *map);
58
 
void bitmap_copy(MY_BITMAP *map, const MY_BITMAP *map2);
59
 
 
 
308
 
 
309
};
 
310
 
 
311
bool bitmap_is_subset(const MyBitmap *map1, const MyBitmap *map2);
 
312
bool bitmap_is_overlapping(const MyBitmap *map1,
 
313
                           const MyBitmap *map2);
 
314
 
 
315
void bitmap_intersect(MyBitmap *map, const MyBitmap *map2);
 
316
void bitmap_subtract(MyBitmap *map, const MyBitmap *map2);
 
317
void bitmap_union(MyBitmap *map, const MyBitmap *map2);
 
318
void bitmap_xor(MyBitmap *map, const MyBitmap *map2);
 
319
void bitmap_invert(MyBitmap *map);
60
320
 
61
321
/* Fast, not thread safe, bitmap functions */
62
322
/* This one is a define because it gets used in an array decl */
63
323
#define bitmap_buffer_size(bits) ((bits+31)/32)*4
64
324
 
65
 
static inline uint32_t no_bytes_in_map(const MY_BITMAP *map)
66
 
{
67
 
  return ((map->n_bits + 7)/8);
68
 
}
69
 
 
70
 
static inline uint32_t no_words_in_map(const MY_BITMAP *map)
71
 
{
72
 
  return ((map->n_bits + 31)/32);
73
 
}
74
 
 
75
325
template <class T>
76
326
inline T bytes_word_aligned(T bytes)
77
327
{
78
328
  return (4*((bytes + 3)/4));
79
329
}
80
330
 
81
 
static inline void bitmap_set_bit(MY_BITMAP const *map, uint32_t bit)
82
 
{
83
 
  ((unsigned char *)map->bitmap)[bit / 8] |= (unsigned char)(1 << ((bit) & 7));
84
 
}
85
 
 
86
 
static inline void bitmap_flip_bit(MY_BITMAP const *map, uint32_t bit)
87
 
{
88
 
  ((unsigned char *)map->bitmap)[bit / 8] ^= (1 << ((bit) & 7));
89
 
}
90
 
 
91
 
static inline void bitmap_clear_bit(MY_BITMAP const *map, uint32_t bit)
92
 
{
93
 
  ((unsigned char *)map->bitmap)[bit / 8] &= ~ (1 << ((bit) & 7));
94
 
}
95
 
 
96
 
static inline bool bitmap_is_set(const MY_BITMAP *map, uint32_t bit)
97
 
{
98
 
  return (bool)(((unsigned char *)map->bitmap)[bit / 8] &  (1 << ((bit) & 7)));
99
 
}
100
 
 
101
 
static inline bool bitmap_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2)
102
 
{
103
 
  *(map1)->last_word_ptr|= (map1)->last_word_mask;
104
 
  *(map2)->last_word_ptr|= (map2)->last_word_mask;
105
 
  return memcmp((map1)->bitmap, (map2)->bitmap, 4*no_words_in_map((map1)))==0;
106
 
}
107
 
 
108
 
static inline void bitmap_clear_all(MY_BITMAP const *map)
109
 
{
110
 
   memset(map->bitmap, 0, 4*no_words_in_map(map));
111
 
}
112
 
 
113
 
static inline void bitmap_set_all(MY_BITMAP const *map)
114
 
{
115
 
   memset(map->bitmap, 0xFF, 4*no_words_in_map(map));
 
331
static inline bool bitmap_cmp(const MyBitmap *map1, const MyBitmap *map2)
 
332
{
 
333
  map1->addMaskToLastWord();
 
334
  map2->addMaskToLastWord();
 
335
  return memcmp((map1)->getBitmap(), 
 
336
                (map2)->getBitmap(),
 
337
                4*map1->numOfWordsInMap()) == 0;
116
338
}
117
339
 
118
340
#endif /* MYSYS_MY_BITMAP_H */