Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
golombrice.h
Go to the documentation of this file.
1// Copyright (c) 2018-present 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 <util/fastrange.h>
9
10#include <streams.h>
11
12#include <cstdint>
13
14template <typename OStream>
16{
17 // Write quotient as unary-encoded: q 1's followed by one 0.
18 uint64_t q = x >> P;
19 while (q > 0) {
21 bitwriter.Write(~0ULL, nbits);
22 q -= nbits;
23 }
24 bitwriter.Write(0, 1);
25
26 // Write the remainder in P bits. Since the remainder is just the bottom
27 // P bits of x, there is no need to mask first.
28 bitwriter.Write(x, P);
29}
30
31template <typename IStream>
33{
34 // Read unary-encoded quotient: q 1's followed by one 0.
35 uint64_t q = 0;
36 while (bitreader.Read(1) == 1) {
37 ++q;
38 }
39
40 uint64_t r = bitreader.Read(P);
41
42 return (q << P) + r;
43}
44
45#endif // BITCOIN_UTIL_GOLOMBRICE_H
uint64_t GolombRiceDecode(BitStreamReader< IStream > &bitreader, uint8_t P)
Definition golombrice.h:32
void GolombRiceEncode(BitStreamWriter< OStream > &bitwriter, uint8_t P, uint64_t x)
Definition golombrice.h:15
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73