-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack_bits.cpp
More file actions
86 lines (59 loc) · 1.8 KB
/
Copy pathpack_bits.cpp
File metadata and controls
86 lines (59 loc) · 1.8 KB
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
#include "pack_bits.h"
uint calc_count_bits(u64 v) {
uint res = 0;
for ( ; v > 0; v >>= 1) ++res;
return res;
}
void PackBits::pack_count_1(u64 v) {
assert( v > 0 );
uint len = calc_count_bits(v);
for (uint i = 1; i < len; ++i)
append_bit(false);
append_bit(true);
p_append_u64(v, len-1);
}
u64 PackBits::unpack_count_1() const {
int len = 0;
while (get_bit()==false)
++len;
return get_as_u64(len) + (u64(1) << len);
}
void PackBits::insert_bit(int pos, bool v) {
int sz = bits.size();
bits.resize(sz+1);
for (int i = sz; i > pos; --i)
bits.setBit(i, bits.at(i-1));
bits.setBit(pos, v);
}
u64 PackBits::p_get_unpack_u64(int& pos, uint n_discrete, uint c_inc_n_discrete) const {
if (bits.at(pos++)==false)
return 0;
u64 here = p_get_as_u64(pos, n_discrete);
u64 farther = p_get_unpack_u64(pos, n_discrete+c_inc_n_discrete, c_inc_n_discrete);
return (farther << n_discrete) + here;
}
u64 PackBits::p_get_as_u64(int& pos, int cnt_bits) const {
assert( pos >= 0 && cnt_bits >= 0 && pos+cnt_bits <= size_bits() );
pos += cnt_bits;
u64 res = 0;
for (int i = 0; i < cnt_bits; ++i) // в обратном порядке от порядка добавления
res = (res << 1) + (u64)bits.at(pos - i - 1);
return res;
}
void PackBits::p_append_u64(u64 v, int cnt_bits) {
int st = bits.size();
bits.resize(st + cnt_bits);
for (int i = 0; i < cnt_bits; ++i) {
bits.setBit(st+i, v & 1);
v = v >> 1;
}
}
void PackBits::pack_u64(u64 v, uint n_discrete, uint c_inc_n_discrete) {
while (v > 0) {
append_bit(true);
p_append_u64(v, n_discrete);
v = v >> n_discrete;
n_discrete += c_inc_n_discrete;
}
append_bit(false);
}