-
Notifications
You must be signed in to change notification settings - Fork 1
/
BitArray.h
executable file
·63 lines (51 loc) · 1.32 KB
/
BitArray.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
52
53
54
55
56
57
58
59
60
61
62
63
/*
* BitArray.h
* MazeInC
*
* Copyright 2009-2018 Matthew T. Pandina. All rights reserved.
*
*/
#ifndef BITARRAY_H
#define BITARRAY_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
typedef struct _BitArray {
uint32_t numBits;
uint8_t *data;
uint32_t data_length;
} BitArray;
typedef BitArray *BitArrayRef;
static inline void BitArray_reset(BitArrayRef ba) {
memset(ba->data, 0, ba->data_length);
}
static inline BitArrayRef BitArray_create(uint32_t numBits, bool zeroData) {
BitArrayRef ba = (BitArrayRef)malloc(sizeof(BitArray));
ba->numBits = numBits;
ba->data_length = numBits / 8 + ((numBits % 8) ? 1 : 0);
ba->data = (uint8_t*)malloc(ba->data_length);
if (zeroData)
BitArray_reset(ba);
return ba;
}
static inline void BitArray_delete(BitArrayRef ba) {
free(ba->data);
free(ba);
}
static inline void BitArray_setBit(BitArrayRef ba, uint32_t index) {
ba->data[index / 8] |= 1 << (index % 8);
}
static inline void BitArray_clearBit(BitArrayRef ba, uint32_t index) {
ba->data[index / 8] &= (1 << (index % 8)) ^ 0xFF;
}
static inline bool BitArray_readBit(BitArrayRef ba, uint32_t index) {
return (ba->data[index / 8] & 1 << (index % 8));
}
#ifdef __cplusplus
}
#endif
#endif // BITARRAY_H