-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.h
70 lines (60 loc) · 1017 Bytes
/
code.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
64
65
66
67
68
69
# ifndef _CODE_H
# define _CODE_H
# include <stdint.h>
# include <stdbool.h>
typedef struct code
{
uint8_t bits[32];
uint32_t l;
} code;
static inline code newCode()
{
code t;
// initialize bits to 0
for (int i = 0; i < 32; i += 1)
{
t.bits[i] = 0;
}
t.l = 0;
return t;
}
static inline bool pushCode(code *c, uint32_t k)
{
if(c->l > 256)
{
return false;
}
else if(k == 0)
{
c->bits[c->l / 8] &= ~(0x1 << (c->l % 8));
c->l += 1;
}
else
{
c->bits[c->l / 8] |= (0x1 << (c->l % 8));
c->l += 1;
}
return true;
}
static inline bool popCode(code *c, uint32_t *k)
{
if(c->l == 0)
{
return false;
}
else
{
c->l -= 1;
*k = ((0x1 << (c->l % 8)) & c->bits[c->l / 8]) >> (c->l % 8);
return true;
}
}
static inline bool emptyCode(code *c)
{
return c->l == 0;
}
static inline bool fullCode(code *c)
{
return c->l == 256;
}
#endif