-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbitmap.c
282 lines (240 loc) · 7.39 KB
/
bitmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @file bignum.c
* @author hutusi ([email protected])
* @brief Refer to bignum.h
* @date 2019-07-20
*
* @copyright Copyright (c) 2019, hutusi.com
*
*/
#include "bitmap.h"
#include <stdlib.h>
#include <string.h>
#include "def.h"
inline void set_bit(word_t *words, unsigned int n)
{
words[WORD_OFFSET(n)] |= ((word_t)1 << BIT_OFFSET(n));
}
inline void clear_bit(word_t *words, unsigned int n)
{
words[WORD_OFFSET(n)] &= ~((word_t)1 << BIT_OFFSET(n));
}
inline int get_bit(const word_t *words, unsigned int n)
{
word_t bit = words[WORD_OFFSET(n)] & ((word_t)1 << BIT_OFFSET(n));
return bit != 0;
}
inline void set_bitmap(word_t *words, unsigned int len)
{
memset(words, 0xFF, len * sizeof(word_t));
}
inline void clear_bitmap(word_t *words, unsigned int len)
{
memset(words, 0x00, len * sizeof(word_t));
}
static inline unsigned int bitmap_num_words_need_by_bits(num_bits)
{
if ((num_bits % BITS_PER_WORD) == 0) {
return num_bits / BITS_PER_WORD;
} else {
return num_bits / BITS_PER_WORD + 1;
}
}
BitMap *bitmap_new(unsigned int num_bits)
{
BitMap *bitmap = (BitMap *)malloc(sizeof(BitMap));
bitmap->num_bits = num_bits;
bitmap->num_words = bitmap_num_words_need_by_bits(num_bits);
if (bitmap->num_words == 0)
bitmap->num_words = 1;
bitmap->words = (word_t *)malloc(sizeof(word_t) * bitmap->num_words);
return bitmap;
}
BitMap *bitmap_clone(const BitMap *bitmap)
{
BitMap *clone = (BitMap *)malloc(sizeof(BitMap));
clone->num_bits = bitmap->num_bits;
clone->num_words = bitmap->num_words;
clone->words = (word_t *)malloc(sizeof(word_t) * bitmap->num_words);
memcpy(clone->words, bitmap->words, sizeof(word_t) * bitmap->num_words);
return clone;
}
void bitmap_free(BitMap *bitmap)
{
free(bitmap->words);
free(bitmap);
}
void bitmap_set(BitMap *bitmap, unsigned int n)
{
set_bit(bitmap->words, n);
}
void bitmap_clear(BitMap *bitmap, unsigned int n)
{
clear_bit(bitmap->words, n);
}
int bitmap_get(const BitMap *bitmap, unsigned int n)
{
return get_bit(bitmap->words, n);
}
void bitmap_set_all(BitMap *bitmap)
{
memset(bitmap->words, 0xFF, sizeof(word_t) * bitmap->num_words);
}
void bitmap_clear_all(BitMap *bitmap)
{
memset(bitmap->words, 0x00, sizeof(word_t) * bitmap->num_words);
}
void bitmap_or(BitMap *bitmap, const BitMap *other)
{
for (int i = 0; i < bitmap->num_words; ++i) {
bitmap->words[i] |= other->words[i];
}
}
void bitmap_and(BitMap *bitmap, const BitMap *other)
{
for (int i = 0; i < bitmap->num_words; ++i) {
bitmap->words[i] &= other->words[i];
}
}
void bitmap_xor(BitMap *bitmap, const BitMap *other)
{
for (int i = 0; i < bitmap->num_words; ++i) {
bitmap->words[i] ^= other->words[i];
}
}
unsigned int bitmap_count(const BitMap *bitmap)
{
unsigned int count = 0;
for (unsigned int i = 0; i < bitmap->num_bits; ++i) {
if (bitmap_get(bitmap, i))
++count;
}
return count;
}
BitMap *bitmap_append(BitMap *bitmap, int flag)
{
if (bitmap->num_words * BITS_PER_WORD <= bitmap->num_bits) {
++(bitmap->num_words);
bitmap->words =
(word_t *)realloc(bitmap->words, sizeof(word_t) * bitmap->num_words);
}
if (flag) {
set_bit(bitmap->words, bitmap->num_bits);
} else {
clear_bit(bitmap->words, bitmap->num_bits);
}
++(bitmap->num_bits);
return bitmap;
}
BitMap *bitmap_append_char(BitMap *bitmap, unsigned char ch)
{
return bitmap_merge(bitmap, bitmap_from_char(ch));
}
char *bitmap_to_string(BitMap *bitmap)
{
char *string = (char *)malloc(sizeof(char) * (bitmap->num_bits + 1));
for (unsigned int i = 0; i < bitmap->num_bits; ++i) {
int flag = bitmap_get(bitmap, i);
string[i] = '0' + flag;
}
string[bitmap->num_bits] = '\0';
return string;
}
BitMap *bitmap_from_string(const char *string)
{
size_t len = strlen(string);
BitMap *bitmap = bitmap_new(len);
for (int i = 0; i < len; ++i) {
if (string[i] == '0') {
clear_bit(bitmap->words, i);
} else {
set_bit(bitmap->words, i);
}
}
return bitmap;
}
BitMap *bitmap_from_word(word_t word)
{
BitMap *bitmap = bitmap_new(BITS_PER_WORD);
bitmap->words[0] = word;
return bitmap;
}
BitMap *bitmap_from_char(unsigned char ch)
{
BitMap *bitmap = bitmap_new(CHAR_BIT);
memcpy(bitmap->words, &ch, 1);
return bitmap;
}
unsigned char bitmap_extract_char(const BitMap *bitmap, unsigned int n)
{
unsigned int word_offset = WORD_OFFSET(n);
unsigned int bit_offset = BIT_OFFSET(n);
unsigned int word;
/** if char streach over two words ... */
if (bit_offset > BITS_PER_WORD - CHAR_BIT) {
unsigned int front_len = BITS_PER_WORD - bit_offset;
unsigned int back_len = CHAR_BIT - (BITS_PER_WORD - bit_offset);
unsigned int front = bitmap->words[word_offset] >> (BITS_PER_WORD - front_len);
unsigned int back = bitmap->words[word_offset + 1] << (BITS_PER_WORD - back_len);
word = front | (back >> (BITS_PER_WORD - back_len - front_len));
} else {
word = bitmap->words[word_offset] >> bit_offset;
}
return (unsigned char)word;
}
BitMap *bitmap_concat(BitMap *bitmap, const BitMap *other)
{
unsigned int total_bits = bitmap->num_bits + other->num_bits;
if (total_bits >= bitmap->num_words * BITS_PER_WORD) {
bitmap->num_words = bitmap_num_words_need_by_bits(total_bits);
bitmap->words = (word_t *)realloc(bitmap->words,
sizeof(word_t) * bitmap->num_words);
}
unsigned int remainder = bitmap->num_bits % BITS_PER_WORD;
unsigned int quotient = bitmap->num_bits / BITS_PER_WORD;
// if target bits is 0, or full of words, just copy other words to the end.
if (remainder == 0) {
memcpy(&(bitmap->words[quotient]), other->words, sizeof(word_t) * other->num_words);
} else {
// -1: 1111 * 8 => 0001 111 ...
bitmap->words[quotient] &= ~(((word_t)(-1)) << remainder);
unsigned int num = bitmap_num_words_need_by_bits(bitmap->num_bits);
if (bitmap->num_words > num) {
memset(&(bitmap->words[num]),
0x0,
sizeof(word_t) * (bitmap->num_words - num));
}
for (int i = 0; i < other->num_words; ++i) {
word_t front = other->words[i] << remainder;
bitmap->words[quotient + i] |= front;
if (quotient + i + 1 < bitmap->num_words) {
word_t back = other->words[i] >> (BITS_PER_WORD - remainder);
bitmap->words[quotient + i + 1] |= back;
}
}
}
bitmap->num_bits += other->num_bits;
return bitmap;
}
BitMap *bitmap_merge(BitMap *bitmap, BitMap *other)
{
bitmap_concat(bitmap, other);
bitmap_free(other);
return bitmap;
}
int bitmap_equal(const BitMap *bitmap1, const BitMap *bitmap2)
{
if (bitmap1->num_bits != bitmap2->num_bits)
return 0;
if (bitmap1->num_words > 1) {
if (memcmp(bitmap1->words,
bitmap2->words,
(bitmap1->num_words - 1) * sizeof(word_t)) != 0) {
return 0;
}
}
return (bitmap1->words[bitmap1->num_words - 1]
<< (BITS_PER_WORD - BIT_OFFSET(bitmap1->num_bits))) ==
(bitmap2->words[bitmap2->num_words - 1]
<< (BITS_PER_WORD - BIT_OFFSET(bitmap2->num_bits)));
}