aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/codecs/delta_codec.h
blob: 21325825e6abd7c2a289eefc3c860f4dc710b14b (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#pragma once

#include "codecs.h"

#include <util/generic/array_ref.h>
#include <util/generic/typetraits.h>
#include <util/generic/bitops.h>
#include <util/string/cast.h>

namespace NCodecs {
    template <typename T = ui64, bool UnsignedDelta = true>
    class TDeltaCodec: public ICodec {
        static_assert(std::is_integral<T>::value, "expect std::is_integral<T>::value");

    public:
        using TUnsigned = std::make_unsigned_t<T>;
        using TSigned = std::make_signed_t<T>;
        using TDelta = std::conditional_t<UnsignedDelta, TUnsigned, TSigned>;

    private:
        const TDelta MinDelta{Min<TDelta>()};
        const TDelta MaxDelta{Max<TDelta>() - 1};
        const TDelta InvalidDelta{MaxDelta + 1};

        Y_FORCE_INLINE static TDelta AddSafe(TUnsigned a, TUnsigned b) {
            return a + b;
        }

        Y_FORCE_INLINE static TDelta SubSafe(TUnsigned a, TUnsigned b) {
            return a - b;
        }

    public:
        struct TDecoder {
            const TDelta InvalidDelta{Max<TDelta>()};

            T Last = 0;
            T Result = 0;

            bool First = true;
            bool Invalid = false;

            Y_FORCE_INLINE bool Decode(TDelta t) {
                if (Y_UNLIKELY(First)) {
                    First = false;
                    Result = Last = t;
                    return true;
                }

                if (Y_UNLIKELY(Invalid)) {
                    Invalid = false;
                    Last = 0;
                    Result = t;
                    return true;
                }

                Result = (Last += t);
                Invalid = t == InvalidDelta;

                return !Invalid;
            }
        };

    public:
        static TStringBuf MyName();

        TDeltaCodec() {
            MyTraits.SizeOfInputElement = sizeof(T);
            MyTraits.AssumesStructuredInput = true;
        }

        TString GetName() const override {
            return ToString(MyName());
        }

        template <class TItem>
        static void AppendTo(TBuffer& b, TItem t) {
            b.Append((char*)&t, sizeof(t));
        }

        ui8 Encode(TStringBuf s, TBuffer& b) const override {
            b.Clear();
            if (s.empty()) {
                return 0;
            }

            b.Reserve(s.size());
            TArrayRef<const T> tin{(const T*)s.data(), s.size() / sizeof(T)};

            const T* it = tin.begin();
            TDelta last = *(it++);
            AppendTo(b, last);

            TDelta maxt = SubSafe(MaxDelta, last);
            TDelta mint = AddSafe(MinDelta, last);

            for (; it != tin.end(); ++it) {
                TDelta t = *it;

                if (Y_LIKELY((t >= mint) & (t <= maxt))) {
                    AppendTo(b, t - last);
                    last = t;
                    maxt = SubSafe(MaxDelta, last);
                    mint = AddSafe(MinDelta, last);
                } else {
                    // delta overflow
                    AppendTo(b, InvalidDelta);
                    AppendTo(b, t);
                    last = 0;
                    maxt = MaxDelta;
                    mint = MinDelta;
                }
            }

            return 0;
        }

        void Decode(TStringBuf s, TBuffer& b) const override {
            b.Clear();
            if (s.empty()) {
                return;
            }

            b.Reserve(s.size());
            TArrayRef<const T> tin{(const T*)s.data(), s.size() / sizeof(T)};

            TDecoder dec;

            for (const T* it = tin.begin(); it != tin.end(); ++it) {
                T tmp;
                memcpy(&tmp, it, sizeof(tmp));
                if (dec.Decode(tmp)) {
                    AppendTo(b, dec.Result);
                }
            }
        }

    protected:
        void DoLearn(ISequenceReader&) override {
        }
    };

}