float_huffman.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include <util/generic/array_ref.h>
  3. #include <util/generic/vector.h>
  4. #include <util/generic/strbuf.h>
  5. #include <array>
  6. namespace NCodecs::NFloatHuff {
  7. TString Encode(TArrayRef<const float> factors);
  8. class TDecoder {
  9. public:
  10. explicit TDecoder(TStringBuf data);
  11. TVector<float> DecodeAll(size_t sizeHint = 0);
  12. // Returns number of decoded floats. May be fewer than requested if the EOS is found.
  13. size_t Decode(TArrayRef<float> dest);
  14. // Returns the number of skipped values.
  15. size_t Skip(size_t count);
  16. bool HasMore() const;
  17. private:
  18. struct TState {
  19. ui64 Workspace = 0;
  20. int WorkspaceSize = 0;
  21. ui64 Position = 0;
  22. TStringBuf Data;
  23. ui64 NextBitsUnmasked(int count); // The upper 64 - count bits may be arbitrary
  24. ui64 PeekBits(int count);
  25. void SkipBits(int count);
  26. };
  27. void FillDecodeBuffer();
  28. TState State;
  29. std::array<float, 128> DecodeBuffer;
  30. // The range of already decompressed numbers inside the DecodeBuffer.
  31. // Always kept non-empty until the EOS is encountered.
  32. float* Begin;
  33. float* End;
  34. bool HitEos = false;
  35. };
  36. TVector<float> Decode(TStringBuf data, size_t sizeHint = 0);
  37. }