aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/coding/varint-inl.h
blob: 8a2eca7068ad06b1cdefdb82bcc7d81fe3ac1993 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#ifndef VARINT_INL_H_
#error "Direct inclusion of this file is not allowed, include varint.h"
// For the sake of sane code completion.
#include "varint.h"
#endif

#include "zig_zag.h"

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <class TWriteCallback>
Y_FORCE_INLINE int WriteVarUint64Impl(TWriteCallback doWrite, ui64 value)
{
    bool stop = false;
    int bytesWritten = 0;
    while (!stop) {
        ++bytesWritten;
        ui8 byte = static_cast<ui8>(value | 0x80);
        value >>= 7;
        if (value == 0) {
            stop = true;
            byte &= 0x7F;
        }
        doWrite(byte);
    }
    return bytesWritten;
}

// These are optimized versions of these Read/Write functions in protobuf/io/coded_stream.cc.
Y_FORCE_INLINE int WriteVarUint64(IOutputStream* output, ui64 value)
{
    return WriteVarUint64Impl([&] (ui8 byte) {
        output->Write(byte);
    }, value);
}

Y_FORCE_INLINE int WriteVarUint64(char* output, ui64 value)
{
    return WriteVarUint64Impl([&] (ui8 byte) {
        *output++ = byte;
    }, value);
}

////////////////////////////////////////////////////////////////////////////////

template <class TOutput>
Y_FORCE_INLINE int WriteVarUint32Impl(TOutput output, ui32 value)
{
    return WriteVarUint64(output, static_cast<ui64>(value));
}

Y_FORCE_INLINE int WriteVarUint32(IOutputStream* output, ui32 value)
{
    return WriteVarUint32Impl(output, value);
}

Y_FORCE_INLINE int WriteVarUint32(char* output, ui32 value)
{
    return WriteVarUint32Impl(output, value);
}

////////////////////////////////////////////////////////////////////////////////

template <class TOutput>
Y_FORCE_INLINE int WriteVarInt32Impl(TOutput output, i32 value)
{
    return WriteVarUint64(output, static_cast<ui64>(ZigZagEncode32(value)));
}

Y_FORCE_INLINE int WriteVarInt32(IOutputStream* output, i32 value)
{
    return WriteVarInt32Impl(output, value);
}

Y_FORCE_INLINE int WriteVarInt32(char* output, i32 value)
{
    return WriteVarInt32Impl(output, value);
}

////////////////////////////////////////////////////////////////////////////////

template <class TOutput>
Y_FORCE_INLINE int WriteVarInt64Impl(TOutput output, i64 value)
{
    return WriteVarUint64(output, static_cast<ui64>(ZigZagEncode64(value)));
}

Y_FORCE_INLINE int WriteVarInt64(IOutputStream* output, i64 value)
{
    return WriteVarInt64Impl(output, value);
}

Y_FORCE_INLINE int WriteVarInt64(char* output, i64 value)
{
    return WriteVarInt64Impl(output, value);
}

////////////////////////////////////////////////////////////////////////////////

template <class TReadCallback>
Y_FORCE_INLINE int ReadVarUint64Impl(TReadCallback doRead, ui64* value)
{
    size_t count = 0;
    ui64 result = 0;

    ui8 byte;
    do {
        if (7 * count > 8 * sizeof(ui64) ) {
            throw TSimpleException("Value is too big for varuint64");
        }
        byte = doRead();
        result |= (static_cast<ui64> (byte & 0x7F)) << (7 * count);
        ++count;
    } while (byte & 0x80);

    *value = result;
    return count;
}

Y_FORCE_INLINE int ReadVarUint64(IInputStream* input, ui64* value)
{
    return ReadVarUint64Impl([&] () {
        char byte;
        if (input->Read(&byte, 1) != 1) {
            throw TSimpleException("Premature end of stream while reading varuint64");
        }
        return byte;
    }, value);
}

Y_FORCE_INLINE int ReadVarUint64(const char* input, ui64* value)
{
    return ReadVarUint64Impl([&] () {
        char byte = *input;
        ++input;
        return byte;
    }, value);
}

Y_FORCE_INLINE int ReadVarUint64(const char* input, const char* end, ui64* value) 
{ 
    return ReadVarUint64Impl([&] () { 
        if (input == end) { 
            throw TSimpleException("Premature end of data while reading varuint64");
        } 
        char byte = *input; 
        ++input; 
        return byte; 
    }, value); 
} 
 
////////////////////////////////////////////////////////////////////////////////

template <class... Args> 
Y_FORCE_INLINE int ReadVarUint32Impl(ui32* value, Args... args) 
{
    ui64 varInt;
    int bytesRead = ReadVarUint64(args..., &varInt); 
    if (varInt > std::numeric_limits<ui32>::max()) {
        throw TSimpleException("Value is too big for varuint32");
    }
    *value = static_cast<ui32>(varInt);
    return bytesRead;
}

Y_FORCE_INLINE int ReadVarUint32(IInputStream* input, ui32* value)
{
    return ReadVarUint32Impl(value, input); 
}

Y_FORCE_INLINE int ReadVarUint32(const char* input, ui32* value)
{
    return ReadVarUint32Impl(value, input); 
}

Y_FORCE_INLINE int ReadVarUint32(const char* input, const char* end, ui32* value) 
{ 
    return ReadVarUint32Impl(value, input, end); 
} 
 
////////////////////////////////////////////////////////////////////////////////

template <class... Args> 
Y_FORCE_INLINE int ReadVarInt32Impl(i32* value, Args... args) 
{
    ui64 varInt;
    int bytesRead = ReadVarUint64(args..., &varInt); 
    if (varInt > std::numeric_limits<ui32>::max()) {
        throw TSimpleException("Value is too big for varint32");
    }
    *value = ZigZagDecode32(static_cast<ui32>(varInt));
    return bytesRead;
}

Y_FORCE_INLINE int ReadVarInt32(IInputStream* input, i32* value)
{
    return ReadVarInt32Impl(value, input); 
}

Y_FORCE_INLINE int ReadVarInt32(const char* input, i32* value)
{
    return ReadVarInt32Impl(value, input); 
}

Y_FORCE_INLINE int ReadVarInt32(const char* input, const char* end, i32* value) 
{ 
    return ReadVarInt32Impl(value, input, end); 
} 
 
////////////////////////////////////////////////////////////////////////////////

template <class... Args> 
Y_FORCE_INLINE int ReadVarInt64Impl(i64* value, Args... args) 
{
    ui64 varInt;
    int bytesRead = ReadVarUint64(args..., &varInt); 
    *value = ZigZagDecode64(varInt);
    return bytesRead;
}

Y_FORCE_INLINE int ReadVarInt64(IInputStream* input, i64* value)
{
    return ReadVarInt64Impl(value, input); 
}

Y_FORCE_INLINE int ReadVarInt64(const char* input, i64* value)
{
    return ReadVarInt64Impl(value, input); 
}

Y_FORCE_INLINE int ReadVarInt64(const char* input, const char* end, i64* value) 
{ 
    return ReadVarInt64Impl(value, input, end); 
} 
 
////////////////////////////////////////////////////////////////////////////////

} // namespace NYT