-
Notifications
You must be signed in to change notification settings - Fork 25
/
integer_codes.hpp
46 lines (39 loc) · 1.17 KB
/
integer_codes.hpp
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
#pragma once
namespace ds2i {
// note: n can be 0
void write_gamma(succinct::bit_vector_builder& bvb, uint64_t n)
{
uint64_t nn = n + 1;
uint64_t l = succinct::broadword::msb(nn);
uint64_t hb = uint64_t(1) << l;
bvb.append_bits(hb, l + 1);
bvb.append_bits(nn ^ hb, l);
}
void write_gamma_nonzero(succinct::bit_vector_builder& bvb, uint64_t n)
{
assert(n > 0);
write_gamma(bvb, n - 1);
}
uint64_t read_gamma(succinct::bit_vector::enumerator& it)
{
uint64_t l = it.skip_zeros();
return (it.take(l) | (uint64_t(1) << l)) - 1;
}
uint64_t read_gamma_nonzero(succinct::bit_vector::enumerator& it)
{
return read_gamma(it) + 1;
}
void write_delta(succinct::bit_vector_builder& bvb, uint64_t n)
{
uint64_t nn = n + 1;
uint64_t l = succinct::broadword::msb(nn);
uint64_t hb = uint64_t(1) << l;
write_gamma(bvb, l);
bvb.append_bits(nn ^ hb, l);
}
uint64_t read_delta(succinct::bit_vector::enumerator& it)
{
uint64_t l = read_gamma(it);
return (it.take(l) | (uint64_t(1) << l)) - 1;
}
}