aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/codecs/float_huffman.h
blob: 786a8eae1d09e17c89169609073c292f508412b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once

#include <util/generic/array_ref.h>
#include <util/generic/vector.h>
#include <util/generic/strbuf.h>

#include <array>

namespace NCodecs::NFloatHuff {
    TString Encode(TArrayRef<const float> factors);

    class TDecoder {
    public:
        explicit TDecoder(TStringBuf data);

        TVector<float> DecodeAll(size_t sizeHint = 0);

        // Returns number of decoded floats. May be fewer than requested if the EOS is found.
        size_t Decode(TArrayRef<float> dest);

        // Returns the number of skipped values.
        size_t Skip(size_t count);

        bool HasMore() const;

    private:
        struct TState {
            ui64 Workspace = 0;
            int WorkspaceSize = 0;
            ui64 Position = 0;
            TStringBuf Data;

            ui64 NextBitsUnmasked(int count); // The upper 64 - count bits may be arbitrary
            ui64 PeekBits(int count);
            void SkipBits(int count);
        };

        void FillDecodeBuffer();

        TState State;
        std::array<float, 128> DecodeBuffer;
        // The range of already decompressed numbers inside the DecodeBuffer.
        // Always kept non-empty until the EOS is encountered.
        float* Begin;
        float* End;
        bool HitEos = false;
    };

    TVector<float> Decode(TStringBuf data, size_t sizeHint = 0);
}