Convert lz77 to use fixed buffer sizes.

This commit is contained in:
James Grogan 2022-11-29 12:05:08 +00:00
parent a6e31c8d39
commit af6fad72eb
9 changed files with 362 additions and 110 deletions

View file

@ -12,10 +12,11 @@ class PrefixCodeGenerator
{
public:
virtual ~PrefixCodeGenerator() = default;
virtual const PrefixCode& getLiteralValue(unsigned char value) const = 0;
virtual const PrefixCode& getEndOfStreamValue() const = 0;
virtual std::optional<PrefixCode> getLiteralValue(unsigned char symbol) const = 0;
virtual std::optional<PrefixCode> getLengthValue(unsigned length) const = 0;
virtual std::optional<PrefixCode> getDistanceValue(unsigned distance) const = 0;
virtual std::optional<PrefixCode> getEndOfStreamValue() const = 0;
};
class HuffmanEncoder : public PrefixCodeGenerator
@ -27,36 +28,23 @@ public:
void encode(const DataStream& stream);
void encode(const std::unordered_map<unsigned char, unsigned>& counts);
void setUseFixedCode(bool useFixed)
{
mUseFixedCode = useFixed;
}
uint32_t getLengthValue(unsigned length);
uint32_t getLengthValue(unsigned length)
{
return 0;
}
std::optional<PrefixCode> getLiteralValue(unsigned char symbol) const override;
const PrefixCode& getLiteralValue(unsigned char value) const override
{
return mLiteralLengthTable.getCode(value);
}
std::optional<PrefixCode> getLengthValue(unsigned length) const override;
const PrefixCode& getEndOfStreamValue() const override
{
return mLiteralLengthTable.getCode(256);
}
std::optional<PrefixCode> getDistanceValue(unsigned distance) const override;
void initializeLiteralLengthTable()
{
if(mUseFixedCode)
{
mLiteralLengthTable.setInputLengthSequence(HuffmanFixedCodes::getDeflateFixedHuffmanCodes(), false);
mLiteralLengthTable.buildPrefixCodes();
}
}
std::optional<PrefixCode> getEndOfStreamValue() const override;
void initializeTrees();
void setUseFixedCode(bool useFixed);
private:
void initializeLiteralLengthTable();
void dumpTree(const RawTree<CountPair>& tree) const;
void dumpNode(RawNode<CountPair>* node, unsigned depth) const;