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 */
19
#define MY_BIT_NONE (~(uint32_t) 0)
21
#include <mystrings/m_string.h>
23
typedef uint32_t my_bitmap_map;
25
typedef struct st_bitmap
27
my_bitmap_map *bitmap;
28
uint32_t n_bits; /* number of bits occupied by the above */
29
my_bitmap_map last_word_mask;
30
my_bitmap_map *last_word_ptr;
32
mutex will be acquired for the duration of each bitmap operation if
33
thread_safe flag in bitmap_init was set. Otherwise, we optimize by not
36
pthread_mutex_t *mutex;
42
extern void create_last_word_mask(MY_BITMAP *map);
43
extern bool bitmap_init(MY_BITMAP *map, my_bitmap_map *buf, uint32_t n_bits,
45
extern bool bitmap_is_clear_all(const MY_BITMAP *map);
46
extern bool bitmap_is_prefix(const MY_BITMAP *map, uint32_t prefix_size);
47
extern bool bitmap_is_set_all(const MY_BITMAP *map);
48
extern bool bitmap_is_subset(const MY_BITMAP *map1, const MY_BITMAP *map2);
49
extern bool bitmap_is_overlapping(const MY_BITMAP *map1,
50
const MY_BITMAP *map2);
51
extern bool bitmap_test_and_set(MY_BITMAP *map, uint32_t bitmap_bit);
52
extern bool bitmap_test_and_clear(MY_BITMAP *map, uint32_t bitmap_bit);
53
extern bool bitmap_fast_test_and_clear(MY_BITMAP *map, uint32_t bitmap_bit);
54
extern bool bitmap_fast_test_and_set(MY_BITMAP *map, uint32_t bitmap_bit);
55
extern uint32_t bitmap_set_next(MY_BITMAP *map);
56
extern uint32_t bitmap_get_first(const MY_BITMAP *map);
57
extern uint32_t bitmap_get_first_set(const MY_BITMAP *map);
58
extern uint32_t bitmap_bits_set(const MY_BITMAP *map);
59
extern void bitmap_free(MY_BITMAP *map);
60
extern void bitmap_set_above(MY_BITMAP *map, uint32_t from_byte, uint32_t use_bit);
61
extern void bitmap_set_prefix(MY_BITMAP *map, uint32_t prefix_size);
62
extern void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2);
63
extern void bitmap_subtract(MY_BITMAP *map, const MY_BITMAP *map2);
64
extern void bitmap_union(MY_BITMAP *map, const MY_BITMAP *map2);
65
extern void bitmap_xor(MY_BITMAP *map, const MY_BITMAP *map2);
66
extern void bitmap_invert(MY_BITMAP *map);
67
extern void bitmap_copy(MY_BITMAP *map, const MY_BITMAP *map2);
69
extern uint32_t bitmap_lock_set_next(MY_BITMAP *map);
70
extern void bitmap_lock_clear_bit(MY_BITMAP *map, uint32_t bitmap_bit);
72
extern uint32_t bitmap_lock_bits_set(const MY_BITMAP *map);
73
extern bool bitmap_lock_is_set_all(const MY_BITMAP *map);
74
extern uint32_t bitmap_lock_get_first(const MY_BITMAP *map);
75
extern uint32_t bitmap_lock_get_first_set(const MY_BITMAP *map);
76
extern bool bitmap_lock_is_subset(const MY_BITMAP *map1,
77
const MY_BITMAP *map2);
78
extern bool bitmap_lock_is_prefix(const MY_BITMAP *map, uint32_t prefix_size);
79
extern bool bitmap_lock_is_set(const MY_BITMAP *map, uint32_t bitmap_bit);
80
extern bool bitmap_lock_is_clear_all(const MY_BITMAP *map);
81
extern bool bitmap_lock_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2);
82
extern void bitmap_lock_set_all(MY_BITMAP *map);
83
extern void bitmap_lock_clear_all(MY_BITMAP *map);
84
extern void bitmap_lock_set_bit(MY_BITMAP *map, uint32_t bitmap_bit);
85
extern void bitmap_lock_flip_bit(MY_BITMAP *map, uint32_t bitmap_bit);
86
extern void bitmap_lock_set_prefix(MY_BITMAP *map, uint32_t prefix_size);
87
extern void bitmap_lock_intersect(MY_BITMAP *map, const MY_BITMAP *map2);
88
extern void bitmap_lock_subtract(MY_BITMAP *map, const MY_BITMAP *map2);
89
extern void bitmap_lock_union(MY_BITMAP *map, const MY_BITMAP *map2);
90
extern void bitmap_lock_xor(MY_BITMAP *map, const MY_BITMAP *map2);
91
extern void bitmap_lock_invert(MY_BITMAP *map);
93
/* Fast, not thread safe, bitmap functions */
94
#define bitmap_buffer_size(bits) (((bits)+31)/32)*4
95
#define no_bytes_in_map(map) (((map)->n_bits + 7)/8)
96
#define no_words_in_map(map) (((map)->n_bits + 31)/32)
97
#define bytes_word_aligned(bytes) (4*((bytes + 3)/4))
98
#define _bitmap_set_bit(MAP, BIT) (((unsigned char*)(MAP)->bitmap)[(BIT) / 8] \
99
|= (1 << ((BIT) & 7)))
100
#define _bitmap_flip_bit(MAP, BIT) (((unsigned char*)(MAP)->bitmap)[(BIT) / 8] \
101
^= (1 << ((BIT) & 7)))
102
#define _bitmap_clear_bit(MAP, BIT) (((unsigned char*)(MAP)->bitmap)[(BIT) / 8] \
103
&= ~ (1 << ((BIT) & 7)))
104
#define _bitmap_is_set(MAP, BIT) (uint32_t) (((unsigned char*)(MAP)->bitmap)[(BIT) / 8] \
105
& (1 << ((BIT) & 7)))
107
NEVER use an increment/decrement operator with the 'bit' argument.
109
FORBIDDEN: bitmap_set_bit($my_bitmap, (field++)->field_index);
111
#define bitmap_set_bit(MAP, BIT) _bitmap_set_bit(MAP, BIT)
112
#define bitmap_flip_bit(MAP, BIT) _bitmap_flip_bit(MAP, BIT)
113
#define bitmap_clear_bit(MAP, BIT) _bitmap_clear_bit(MAP, BIT)
114
#define bitmap_is_set(MAP, BIT) _bitmap_is_set(MAP, BIT)
116
static inline bool bitmap_cmp(const MY_BITMAP *map1, const MY_BITMAP *map2)
118
*(map1)->last_word_ptr|= (map1)->last_word_mask;
119
*(map2)->last_word_ptr|= (map2)->last_word_mask;
120
return memcmp((map1)->bitmap, (map2)->bitmap, 4*no_words_in_map((map1)))==0;
123
#define bitmap_clear_all(MAP) \
124
{ memset((MAP)->bitmap, 0, 4*no_words_in_map((MAP))); }
125
#define bitmap_set_all(MAP) \
126
(memset((MAP)->bitmap, 0xFF, 4*no_words_in_map((MAP))))
129
check, set and clear a bit of interest of an integer.
131
If the bit is out of range @retval -1. Otherwise
132
bit_is_set @return 0 or 1 reflecting the bit is set or not;
133
bit_do_set @return 1 (bit is set 1)
134
bit_do_clear @return 0 (bit is cleared to 0)
137
#define bit_is_set(I,B) (sizeof(I) * CHAR_BIT > (B) ? \
138
(((I) & (1UL << (B))) == 0 ? 0 : 1) : -1)
139
#define bit_do_set(I,B) (sizeof(I) * CHAR_BIT > (B) ? \
140
((I) |= (1UL << (B)), 1) : -1)
141
#define bit_do_clear(I,B) (sizeof(I) * CHAR_BIT > (B) ? \
142
((I) &= ~(1UL << (B)), 0) : -1)
148
#endif /* _my_bitmap_h_ */