16#ifndef ALICEO2_EXTERNALENTROPYCODER_H_
17#define ALICEO2_EXTERNALENTROPYCODER_H_
32template <
typename source_T>
43 template <
typename dst_T = u
int8_t>
46 template <
typename src_IT,
typename dst_IT>
47 [[nodiscard]] dst_IT
encode(src_IT srcBegin, src_IT srcEnd, dst_IT dstBegin, dst_IT dstEnd);
55 template <
typename dst_T = u
int8_t>
58 template <typename dst_T>
63 std::vector<source_type> mIncompressibleBuffer{};
64 Packer<source_type> mIncompressiblePacker{};
67template <
typename source_T>
70 if (!
getEncoder().getSymbolTable().hasEscapeSymbol()) {
71 throw std::runtime_error(
"External entropy encoder must be able to handle incompressible symbols.");
75template <
typename source_T>
76template <
typename dst_T>
80 const double_t RelativeSafetyFactor = 2.0 * safetyFactor;
81 const size_t messageSizeB = nElements *
sizeof(
source_type);
82 return rans::utils::nBytesTo<dst_T>(std::ceil(RelativeSafetyFactor * messageSizeB) + Overhead);
85template <
typename source_T>
86template <
typename src_IT,
typename dst_IT>
89 const size_t incompressibleSymbolFrequency = [&]() {
90 const auto& symbolTable = mEncoder->getSymbolTable();
91 const double_t incompressibleSymbolProbability =
static_cast<double_t
>(symbolTable.getEscapeSymbol().getFrequency()) /
rans::utils::pow2(symbolTable.getPrecision());
92 return std::ceil(std::distance(srcBegin, srcEnd) * incompressibleSymbolProbability);
95 mIncompressibleBuffer.reserve(incompressibleSymbolFrequency);
96 auto [encodedMessageEnd, literalsEnd] = mEncoder->process(srcBegin, srcEnd, dstBegin, std::back_inserter(mIncompressibleBuffer));
98 mIncompressiblePacker =
Packer<source_type>{mIncompressibleBuffer.data(), mIncompressibleBuffer.data() + mIncompressibleBuffer.size()};
100 return encodedMessageEnd;
103template <
typename source_T>
104template <
typename dst_T>
107 return mIncompressiblePacker.template getPackingBufferSize<dst_T>(mIncompressibleBuffer.size());
110template <
typename source_T>
111template <
typename dst_T>
114 return mIncompressiblePacker.pack(mIncompressibleBuffer.data(), mIncompressibleBuffer.size(), dstBegin, dstEnd);
Interfaces for BitPacking using librans.
size_t getIncompressibleSymbolPackingBits() const noexcept
size_t computePayloadSizeEstimate(size_t nElements, double_t safetyFactor=1)
ExternalEntropyCoder(const encoder_type &encoder)
size_t getNIncompressibleSamples() const noexcept
source_type getIncompressibleSymbolOffset() const noexcept
const encoder_type & getEncoder() const noexcept
size_t computePackedIncompressibleSize() const noexcept
typename rans::denseEncoder_type< source_type > encoder_type
dst_T * writeIncompressible(dst_T *dstBegin, dst_T *dstEnd) const
dst_IT encode(src_IT srcBegin, src_IT srcEnd, dst_IT dstBegin, dst_IT dstEnd)
source_type getOffset() const noexcept
size_t getPackingWidth() const noexcept
public interface for encoding.
static factory classes for building histograms, encoders and decoders.
public interface for building and renorming histograms from source data.
constexpr size_t pow2(size_t n) noexcept
void checkBounds(IT iteratorPosition, IT upperBound)
decltype(makeDenseEncoder<>::fromRenormed(RenormedDenseHistogram< source_T >{})) denseEncoder_type
public interface for serializing histograms (dictionaries) to JSON or compressed binary.