MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_bitmap.h
1 /*
2  Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 
17 #ifndef _my_bitmap_h_
18 #define _my_bitmap_h_
19 
20 #define MY_BIT_NONE (~(uint) 0)
21 
22 #include <m_string.h>
23 
24 typedef uint32 my_bitmap_map;
25 
26 typedef struct st_bitmap
27 {
28  my_bitmap_map *bitmap;
29  uint n_bits; /* number of bits occupied by the above */
30  my_bitmap_map last_word_mask;
31  my_bitmap_map *last_word_ptr;
32  /*
33  mutex will be acquired for the duration of each bitmap operation if
34  thread_safe flag in bitmap_init was set. Otherwise, we optimize by not
35  acquiring the mutex
36  */
37  mysql_mutex_t *mutex;
38 } MY_BITMAP;
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 extern void create_last_word_mask(MY_BITMAP *map);
44 extern my_bool bitmap_init(MY_BITMAP *map, my_bitmap_map *buf, uint n_bits,
45  my_bool thread_safe);
46 extern my_bool bitmap_is_clear_all(const MY_BITMAP *map);
47 extern my_bool bitmap_is_prefix(const MY_BITMAP *map, uint prefix_size);
48 extern my_bool bitmap_is_set_all(const MY_BITMAP *map);
49 extern my_bool bitmap_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2);
50 extern my_bool bitmap_is_overlapping(const MY_BITMAP *map1,
51  const MY_BITMAP *map2);
52 extern my_bool bitmap_test_and_set(MY_BITMAP *map, uint bitmap_bit);
53 extern my_bool bitmap_test_and_clear(MY_BITMAP *map, uint bitmap_bit);
54 extern my_bool bitmap_fast_test_and_set(MY_BITMAP *map, uint bitmap_bit);
55 extern uint bitmap_set_next(MY_BITMAP *map);
56 extern uint bitmap_get_first(const MY_BITMAP *map);
57 extern uint bitmap_get_first_set(const MY_BITMAP *map);
58 extern uint bitmap_get_next_set(const MY_BITMAP *map, uint bitmap_bit);
59 extern uint bitmap_bits_set(const MY_BITMAP *map);
60 extern void bitmap_free(MY_BITMAP *map);
61 extern void bitmap_set_above(MY_BITMAP *map, uint from_byte, uint use_bit);
62 extern void bitmap_set_prefix(MY_BITMAP *map, uint prefix_size);
63 extern void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2);
64 extern void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2);
65 extern void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2);
66 extern void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2);
67 extern void bitmap_invert(MY_BITMAP *map);
68 extern void bitmap_copy(MY_BITMAP *map, const MY_BITMAP *map2);
69 
70 extern uint bitmap_lock_set_next(MY_BITMAP *map);
71 extern void bitmap_lock_clear_bit(MY_BITMAP *map, uint bitmap_bit);
72 /* Fast, not thread safe, bitmap functions */
73 #define bitmap_buffer_size(bits) (((bits)+31)/32)*4
74 #define no_bytes_in_map(map) (((map)->n_bits + 7)/8)
75 #define no_words_in_map(map) (((map)->n_bits + 31)/32)
76 
77 static inline void bitmap_set_bit(MY_BITMAP *map, uint bit)
78 {
79  DBUG_ASSERT(bit < map->n_bits);
80  ((uchar*)map->bitmap)[bit / 8] |= (1 << (bit & 7));
81 }
82 
83 
84 static inline void bitmap_flip_bit(MY_BITMAP *map, uint bit)
85 {
86  DBUG_ASSERT(bit < map->n_bits);
87  ((uchar*)map->bitmap)[bit / 8] ^= (1 << (bit & 7));
88 }
89 
90 
91 static inline void bitmap_clear_bit(MY_BITMAP *map, uint bit)
92 {
93  DBUG_ASSERT(bit < map->n_bits);
94  ((uchar*)map->bitmap)[bit / 8] &= ~(1 << (bit & 7));
95 }
96 
97 
98 static inline my_bool bitmap_is_set(const MY_BITMAP *map, uint bit)
99 {
100  DBUG_ASSERT(bit < map->n_bits);
101  return ((uchar*)map->bitmap)[bit / 8] & (1 << (bit & 7));
102 }
103 
112 static inline my_bool bitmap_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2)
113 {
114  if (memcmp(map1->bitmap, map2->bitmap, 4*(no_words_in_map(map1)-1)) != 0)
115  return FALSE;
116  return ((*map1->last_word_ptr | map1->last_word_mask) ==
117  (*map2->last_word_ptr | map2->last_word_mask));
118 }
119 
120 #define bitmap_clear_all(MAP) \
121  { memset((MAP)->bitmap, 0, 4*no_words_in_map((MAP))); }
122 #define bitmap_set_all(MAP) \
123  (memset((MAP)->bitmap, 0xFF, 4*no_words_in_map((MAP))))
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 
129 #endif /* _my_bitmap_h_ */