#pragma once #include "RawTree.h" #include "HuffmanCodeLengthTable.h" #include "HuffmanFixedCodes.h" #include #include class PrefixCodeGenerator { public: virtual ~PrefixCodeGenerator() = default; virtual std::optional getLiteralValue(unsigned char symbol) const = 0; virtual std::optional getLengthValue(unsigned length) const = 0; virtual std::optional getDistanceValue(unsigned distance) const = 0; virtual std::optional getEndOfStreamValue() const = 0; }; class HuffmanEncoder : public PrefixCodeGenerator { using DataStream = std::vector; using CountPair = std::pair; public: void encode(const DataStream& stream); void encode(const std::unordered_map& counts); uint32_t getLengthValue(unsigned length); std::optional getLiteralValue(unsigned char symbol) const override; std::optional getLengthValue(unsigned length) const override; std::optional getDistanceValue(unsigned distance) const override; std::optional getEndOfStreamValue() const override; void initializeTrees(); void setUseFixedCode(bool useFixed); private: void initializeLiteralLengthTable(); void dumpTree(const RawTree& tree) const; void dumpNode(RawNode* node, unsigned depth) const; bool mUseFixedCode{false}; bool mTableIsInitialized{false}; HuffmanCodeLengthTable mLiteralLengthTable; HuffmanCodeLengthTable mDistanceTable; };