comptable.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include <util/generic/vector.h>
  3. #include <util/memory/blob.h>
  4. #include <util/ysaveload.h>
  5. #include <library/cpp/compproto/huff.h>
  6. namespace NCompTable {
  7. struct TCompressorTable {
  8. ui32 Table[65536];
  9. ui32 HashMul;
  10. NCompProto::TCoderEntry HuffCodes[10];
  11. ui8 HuffIndices[256];
  12. void GetHuffCode(const NCompProto::TCoderEntry& entry, ui32 value, ui64& bitCode, ui8& bitLength) const;
  13. void GetLastHuffCode(ui32 value, ui64& bitCode, ui8& bitLength) const;
  14. void GetHuffCode(ui32 value, ui64& bitCode, ui8& bitLength) const;
  15. ui8 GetHuffIndex(ui8 prefix);
  16. void BuildHuffCodes(i64 totalFreq, i64 freqs[65536]);
  17. bool BuildHuffCodes(i64 totalFreq, i64 freqs[65536], i64 add);
  18. };
  19. struct TDataSampler {
  20. enum {
  21. Size = 1 << 18,
  22. };
  23. ui32 EntryVal[Size];
  24. i64 EntryHit[Size];
  25. i64 Counter;
  26. public:
  27. TDataSampler();
  28. void BuildTable(TCompressorTable& table) const;
  29. void AddStat(ui32 val);
  30. void AddStat(const TStringBuf& stringBuf);
  31. };
  32. class TDataCompressor;
  33. class TDataDecompressor;
  34. class TChunkCompressor {
  35. public:
  36. TChunkCompressor(bool highQuality, const TCompressorTable& table);
  37. void Compress(TStringBuf data, TVector<char>* result) const;
  38. ~TChunkCompressor();
  39. private:
  40. bool HighQuality;
  41. THolder<TDataCompressor> Compressor;
  42. };
  43. class TChunkDecompressor {
  44. public:
  45. TChunkDecompressor(bool highQuality, const TCompressorTable& table);
  46. void Decompress(TStringBuf data, TVector<char>* result) const;
  47. ~TChunkDecompressor();
  48. private:
  49. bool HighQuality;
  50. THolder<TDataDecompressor> Decompressor;
  51. };
  52. }
  53. template <>
  54. class TSerializer<NCompTable::TCompressorTable> {
  55. public:
  56. static inline void Save(IOutputStream* out, const NCompTable::TCompressorTable& entry) {
  57. SavePodType(out, entry);
  58. }
  59. static inline void Load(IInputStream* in, NCompTable::TCompressorTable& entry) {
  60. LoadPodType(in, entry);
  61. }
  62. };