Bitcoin ABC
0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
src
util
golombrice.h
Go to the documentation of this file.
1
// Copyright (c) 2018-2019 The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#ifndef BITCOIN_UTIL_GOLOMBRICE_H
6
#define BITCOIN_UTIL_GOLOMBRICE_H
7
8
#include <
streams.h
>
9
10
#include <cstdint>
11
12
template
<
typename
OStream>
13
static
void
GolombRiceEncode
(
BitStreamWriter<OStream>
&
bitwriter
,
uint8_t
P
,
14
uint64_t
x) {
15
// Write quotient as unary-encoded: q 1's followed by one 0.
16
uint64_t
q = x >>
P
;
17
while
(q > 0) {
18
int
nbits
=
q <= 64 ? static_cast<int>
(q) : 64;
19
bitwriter
.Write(~0
ULL
,
nbits
);
20
q -=
nbits
;
21
}
22
bitwriter
.Write(0, 1);
23
24
// Write the remainder in P bits. Since the remainder is just the bottom
25
// P bits of x, there is no need to mask first.
26
bitwriter
.Write(x,
P
);
27
}
28
29
template
<
typename
IStream>
30
static
uint64_t
GolombRiceDecode
(
BitStreamReader<IStream>
&
bitreader
,
31
uint8_t
P
) {
32
// Read unary-encoded quotient: q 1's followed by one 0.
33
uint64_t
q = 0;
34
while
(
bitreader
.Read(1) == 1) {
35
++q;
36
}
37
38
uint64_t
r =
bitreader
.Read(
P
);
39
40
return
(q <<
P
) + r;
41
}
42
43
#endif
// BITCOIN_UTIL_GOLOMBRICE_H
BitStreamReader
Definition
streams.h:425
BitStreamWriter
Definition
streams.h:467
GolombRiceDecode
static uint64_t GolombRiceDecode(BitStreamReader< IStream > &bitreader, uint8_t P)
Definition
golombrice.h:30
GolombRiceEncode
static void GolombRiceEncode(BitStreamWriter< OStream > &bitwriter, uint8_t P, uint64_t x)
Definition
golombrice.h:13
GetRand
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition
random.h:85
streams.h
Generated on Thu Dec 26 2024 02:38:06 for Bitcoin ABC by
1.9.8