stuff-from-scratch/src/base/compression/huffman/HuffmanCodeLengthTable.h
2024-01-02 16:14:23 +00:00

56 lines
1.5 KiB
C++

#pragma once
#include "HuffmanTree.h"
#include "Vector.h"
#include "String.h"
#include <optional>
class BitStream;
class HuffmanCodeLengthTable
{
public:
void buildPrefixCodes();
void buildCompressedLengthSequence();
String dumpPrefixCodes() const;
Optional<HuffmanTree::Symbol> findMatch(size_t treeIndex, uint32_t code) const;
const HuffmanTree& getTree() const;
const PrefixCode& getCode(size_t index) const;
Optional<PrefixCode> getCodeForSymbol(unsigned symbol) const;
using CompressedSequenceEntry = std::pair<unsigned, unsigned>;
const Vector<CompressedSequenceEntry>& getCompressedLengthSequence() const;
const Vector<size_t> getCompressedLengthCounts() const;
size_t getNumCodeLengths() const;
unsigned getCodeLength(size_t treeIndex) const;
size_t mapToDeflateIndex(size_t index) const;
void setInputLengthSequence(const Vector<unsigned char>& sequence, bool targetDeflate = true);
bool readNextSymbol(unsigned& buffer, BitStream* stream);
private:
HuffmanTree mTree;
bool mTargetDeflate{true};
Vector<unsigned char> mInputLengthSequence;
Vector<PrefixCode> mCodes;
Vector<CompressedSequenceEntry> mCompressedLengthSequence;
Vector<size_t> mCompressedLengthCounts;
static constexpr unsigned DEFLATE_PERMUTATION_SIZE{19};
static constexpr unsigned DEFLATE_PERMUTATION[DEFLATE_PERMUTATION_SIZE]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
};