-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitmask.h
51 lines (40 loc) · 1.45 KB
/
bitmask.h
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
/*
* bitmask helper interface
* qianfan Zhao <qianfanguijin@163.com>
*/
#ifndef IMGEDITOR_BITMASK_H
#define IMGEDITOR_BITMASK_H
#include <stdint.h>
#include <stdio.h>
struct bitmask {
size_t total_bits;
size_t bufsize;
uint8_t buffer[0];
};
struct bitmask *alloc_bitmask(size_t total_bits);
void bitmask_free(struct bitmask *b);
int bitmask_write(struct bitmask *b, size_t bit_idx, int v);
int bitmask_set(struct bitmask *b, size_t bit_idx);
int bitmask_set_bits(struct bitmask *b, size_t from_bit, size_t bits);
int bitmask_unset(struct bitmask *b, size_t bit_idx);
int bitmask_memset(struct bitmask *b, uint8_t val);
int bitmask_get(struct bitmask *b, size_t bit_idx);
int bitmask_next_zero(struct bitmask *b, size_t from_bit);
int bitmask_next_one(struct bitmask *b, size_t from_bit);
struct bitmask *bitmask_xor(const struct bitmask *a, const struct bitmask *b);
#define bitmask_foreach(i, b) \
for (int i = bitmask_next_one(b, 0); i >= 0; i = bitmask_next_one(b, i + 1))
struct bitmask_continue_iterator {
int start;
int bits;
struct bitmask *b;
/* private data, do not touch */
int __end;
};
struct bitmask_continue_iterator bitmask_continue_iterator_init(struct bitmask *b);
void bitmask_continue_iterator_next(struct bitmask_continue_iterator *it);
#define bitmask_foreach_continue(it, b) \
for (struct bitmask_continue_iterator it = bitmask_continue_iterator_init(b); \
!(it.start < 0); \
bitmask_continue_iterator_next(&it))
#endif