-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.h
217 lines (183 loc) · 5.17 KB
/
codec.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
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
#pragma once
#include <vector>
/* Variable byte */
template<typename TData, typename TBlock>
class VB {
private:
static const int blockSize = sizeof(TBlock) * 8;
static const TBlock blockMask = (1 << (blockSize - 1)) - 1;
static const TBlock endMask = 1 << (blockSize - 1);
TBlock *encodedData;
unsigned int encodedDataSize;
TData num;
int offset;
unsigned int id;
public:
VB() {
encodedData = nullptr;
encodedDataSize = 0;
reset();
}
VB(TBlock *encodedData, unsigned int size) {
this->encodedData = encodedData;
encodedDataSize = size;
reset();
}
void reset() {
num = 0;
offset = 0;
id = 0;
}
TData decodeNext() {
while (id < encodedDataSize) {
TBlock i = encodedData[id++];
num |= (TData)(i & blockMask) << ((blockSize - 1) * offset);
++offset;
if (i & endMask) {
TData tmp = num;
num = 0;
offset = 0;
return tmp;
}
}
return 0;
}
bool end() {
return id >= encodedDataSize;
}
unsigned int getOffset() {
return id * blockSize;
}
void setOffset(unsigned int offset) {
id = offset / blockSize;
}
static unsigned int encode(const std::vector<TData> &data, std::vector<TBlock> &encodedData) {
TBlock curBlock;
for (TData num : data) {
do {
curBlock = num & blockMask;
num >>= blockSize - 1;
if (num == 0) {
curBlock |= endMask;
}
encodedData.push_back(curBlock);
} while (num);
}
return encodedData.size() * blockSize;
}
static unsigned int bitSize(TData n) {
unsigned int sz = 0;
do {
sz += blockSize;
n >>= blockSize - 1;
} while (n);
return sz;
}
};
/* Variable half-byte */
template<typename TData, typename TBlock>
class VHB {
private:
static const int halfBlockSize = sizeof(TBlock) * 4;
static const TBlock halfBlockMask = (1 << (halfBlockSize - 1)) - 1;
static const TBlock endMask = 1 << (halfBlockSize - 1);
TBlock *encodedData;
unsigned int encodedDataSize;
TData num;
int offset;
int halfNum;
unsigned int id;
public:
VHB() {
encodedData = nullptr;
encodedDataSize = 0;
reset();
}
VHB(TBlock *encodedData, unsigned int size) {
this->encodedData = encodedData;
encodedDataSize = size;
reset();
}
void reset() {
num = 0;
offset = 0;
halfNum = 0;
id = 0;
}
bool end() {
return id >= encodedDataSize || (id == encodedDataSize - 1 && halfNum == 1 && !(encodedData[id] & endMask));
}
unsigned int getOffset() {
return halfBlockSize * (id * 2 + halfNum);
}
void setOffset(unsigned int offset) {
id = offset / (2 * halfBlockSize);
halfNum = offset - id * halfBlockSize * 2 == 0 ? 0 : 1;
}
TData decodeNext() {
TBlock curHalfBlock;
while (id < encodedDataSize) {
TBlock i = encodedData[id];
if (halfNum == 0) {
curHalfBlock = (i & (halfBlockMask << halfBlockSize)) >> halfBlockSize;
} else {
curHalfBlock = i & halfBlockMask;
}
num |= (TData)curHalfBlock << ((halfBlockSize - 1) * offset);
++offset;
if (halfNum == 0) {
halfNum = 1;
if (i & (endMask << halfBlockSize)) {
TData tmp = num;
num = 0;
offset = 0;
return tmp;
}
} else {
id++;
halfNum = 0;
if (i & endMask) {
TData tmp = num;
num = 0;
offset = 0;
return tmp;
}
}
}
return 0;
}
static unsigned int encode(const std::vector<TData> &data, std::vector<TBlock> &encodedData) {
TBlock curBlock = 0;
TBlock curHalfBlock;
int halfNum = 0;
for (TData num : data) {
do {
curHalfBlock = num & halfBlockMask;
num >>= halfBlockSize - 1;
if (num == 0) {
curHalfBlock |= endMask;
}
if (halfNum == 0) {
halfNum = 1;
curBlock = curHalfBlock << halfBlockSize;
} else {
halfNum = 0;
encodedData.push_back(curBlock | curHalfBlock);
curBlock = 0;
}
} while (num);
}
if (halfNum == 1) {
encodedData.push_back(curBlock);
}
return encodedData.size() * halfBlockSize * 2;
}
static unsigned int bitSize(TData n) {
unsigned int sz = 0;
do {
sz += halfBlockSize;
n >>= halfBlockSize - 1;
} while (n);
return sz;
}
};