ClickHouse/dbms/include/DB/IO/VarInt.h

240 lines
5.1 KiB
C++
Raw Normal View History

#pragma once
2010-03-01 16:59:51 +00:00
#include <DB/Core/Types.h>
2010-06-04 18:25:25 +00:00
#include <DB/IO/ReadBuffer.h>
#include <DB/IO/WriteBuffer.h>
2010-03-01 16:59:51 +00:00
namespace DB
{
2016-08-04 05:47:59 +00:00
/** Записать UInt64 в формате переменной длины (base128) NOTE Only up to 2^63 - 1 are supported.*/
2010-03-01 16:59:51 +00:00
void writeVarUInt(UInt64 x, std::ostream & ostr);
2010-06-01 14:12:28 +00:00
void writeVarUInt(UInt64 x, WriteBuffer & ostr);
2011-03-17 19:45:21 +00:00
char * writeVarUInt(UInt64 x, char * ostr);
2010-03-01 16:59:51 +00:00
/** Прочитать UInt64, записанный в формате переменной длины (base128) */
void readVarUInt(UInt64 & x, std::istream & istr);
2010-06-01 14:12:28 +00:00
void readVarUInt(UInt64 & x, ReadBuffer & istr);
2011-03-22 20:36:01 +00:00
const char * readVarUInt(UInt64 & x, const char * istr, size_t size);
2010-03-01 16:59:51 +00:00
/** Получить длину UInt64 в формате VarUInt */
size_t getLengthOfVarUInt(UInt64 x);
/** Получить длину Int64 в формате VarInt */
size_t getLengthOfVarInt(Int64 x);
2010-03-01 16:59:51 +00:00
/** Записать Int64 в формате переменной длины (base128) */
2010-06-01 14:12:28 +00:00
template <typename OUT>
inline void writeVarInt(Int64 x, OUT & ostr)
2010-03-01 16:59:51 +00:00
{
writeVarUInt(static_cast<UInt64>((x << 1) ^ (x >> 63)), ostr);
}
2011-03-22 20:36:01 +00:00
inline char * writeVarInt(Int64 x, char * ostr)
{
return writeVarUInt(static_cast<UInt64>((x << 1) ^ (x >> 63)), ostr);
}
2010-03-01 16:59:51 +00:00
/** Прочитать Int64, записанный в формате переменной длины (base128) */
2010-06-01 14:12:28 +00:00
template <typename IN>
inline void readVarInt(Int64 & x, IN & istr)
2010-03-01 16:59:51 +00:00
{
readVarUInt(*reinterpret_cast<UInt64*>(&x), istr);
x = (static_cast<UInt64>(x) >> 1) ^ -(x & 1);
}
2011-03-22 20:36:01 +00:00
inline const char * readVarInt(Int64 & x, const char * istr, size_t size)
{
const char * res = readVarUInt(*reinterpret_cast<UInt64*>(&x), istr, size);
x = (static_cast<UInt64>(x) >> 1) ^ -(x & 1);
return res;
}
2010-03-01 16:59:51 +00:00
2010-06-01 14:12:28 +00:00
inline void writeVarT(UInt64 x, std::ostream & ostr) { writeVarUInt(x, ostr); }
inline void writeVarT(Int64 x, std::ostream & ostr) { writeVarInt(x, ostr); }
inline void writeVarT(UInt64 x, WriteBuffer & ostr) { writeVarUInt(x, ostr); }
inline void writeVarT(Int64 x, WriteBuffer & ostr) { writeVarInt(x, ostr); }
2011-03-22 20:36:01 +00:00
inline char * writeVarT(UInt64 x, char * & ostr) { return writeVarUInt(x, ostr); }
inline char * writeVarT(Int64 x, char * & ostr) { return writeVarInt(x, ostr); }
2010-05-18 18:51:51 +00:00
2010-06-01 14:12:28 +00:00
inline void readVarT(UInt64 & x, std::istream & istr) { readVarUInt(x, istr); }
inline void readVarT(Int64 & x, std::istream & istr) { readVarInt(x, istr); }
inline void readVarT(UInt64 & x, ReadBuffer & istr) { readVarUInt(x, istr); }
inline void readVarT(Int64 & x, ReadBuffer & istr) { readVarInt(x, istr); }
2011-03-22 20:36:01 +00:00
inline const char * readVarT(UInt64 & x, const char * istr, size_t size) { return readVarUInt(x, istr, size); }
inline const char * readVarT(Int64 & x, const char * istr, size_t size) { return readVarInt(x, istr, size); }
2010-05-18 18:51:51 +00:00
/// Для [U]Int32, [U]Int16.
inline void readVarUInt(UInt32 & x, ReadBuffer & istr)
{
UInt64 tmp;
readVarUInt(tmp, istr);
x = tmp;
2010-03-01 16:59:51 +00:00
}
inline void readVarInt(Int32 & x, ReadBuffer & istr)
{
Int64 tmp;
readVarInt(tmp, istr);
x = tmp;
}
inline void readVarUInt(UInt16 & x, ReadBuffer & istr)
{
UInt64 tmp;
readVarUInt(tmp, istr);
x = tmp;
}
inline void readVarInt(Int16 & x, ReadBuffer & istr)
{
Int64 tmp;
readVarInt(tmp, istr);
x = tmp;
}
inline void throwReadAfterEOF()
{
throw Exception("Attempt to read after eof", ErrorCodes::ATTEMPT_TO_READ_AFTER_EOF);
}
inline void readVarUInt(UInt64 & x, ReadBuffer & istr)
{
x = 0;
for (size_t i = 0; i < 9; ++i)
{
if (istr.eof())
throwReadAfterEOF();
2016-08-04 05:19:37 +00:00
uint64_t byte = *istr.position();
++istr.position();
2016-08-04 05:19:37 +00:00
x |= (byte & 0x7F) << (7 * i);
if (!(byte & 0x80))
return;
}
}
inline void readVarUInt(UInt64 & x, std::istream & istr)
{
2016-08-04 05:19:37 +00:00
x = 0;
for (size_t i = 0; i < 9; ++i)
{
2016-08-04 05:19:37 +00:00
uint64_t byte = istr.get();
x |= (byte & 0x7F) << (7 * i);
if (!(byte & 0x80))
return;
}
}
inline const char * readVarUInt(UInt64 & x, const char * istr, size_t size)
{
const char * end = istr + size;
x = 0;
for (size_t i = 0; i < 9; ++i)
{
if (istr == end)
throwReadAfterEOF();
uint64_t byte = *istr;
++istr;
x |= (byte & 0x7F) << (7 * i);
if (!(byte & 0x80))
return istr;
}
return istr;
}
inline void writeVarUInt(UInt64 x, WriteBuffer & ostr)
{
2016-08-04 05:19:37 +00:00
for (size_t i = 0; i < 9; ++i)
{
2016-08-04 05:19:37 +00:00
uint8_t byte = x & 0x7F;
if (x > 0x7F)
byte |= 0x80;
2016-08-04 05:19:37 +00:00
ostr.nextIfAtEnd();
*ostr.position() = byte;
2016-08-04 05:19:37 +00:00
++ostr.position();
x >>= 7;
if (!x)
return;
}
}
inline void writeVarUInt(UInt64 x, std::ostream & ostr)
{
2016-08-04 05:19:37 +00:00
for (size_t i = 0; i < 9; ++i)
{
2016-08-04 05:19:37 +00:00
uint8_t byte = x & 0x7F;
if (x > 0x7F)
byte |= 0x80;
2016-08-04 05:19:37 +00:00
ostr.put(byte);
2016-08-04 05:19:37 +00:00
x >>= 7;
if (!x)
return;
}
}
inline char * writeVarUInt(UInt64 x, char * ostr)
{
2016-08-04 05:19:37 +00:00
for (size_t i = 0; i < 9; ++i)
{
uint8_t byte = x & 0x7F;
if (x > 0x7F)
byte |= 0x80;
2016-08-04 05:19:37 +00:00
*ostr = byte;
++ostr;
2016-08-04 05:19:37 +00:00
x >>= 7;
if (!x)
return ostr;
}
return ostr;
}
inline size_t getLengthOfVarUInt(UInt64 x)
{
return x < (1ULL << 7) ? 1
: (x < (1ULL << 14) ? 2
: (x < (1ULL << 21) ? 3
: (x < (1ULL << 28) ? 4
: (x < (1ULL << 35) ? 5
: (x < (1ULL << 42) ? 6
: (x < (1ULL << 49) ? 7
: (x < (1ULL << 56) ? 8
: 9)))))));
}
inline size_t getLengthOfVarInt(Int64 x)
{
return getLengthOfVarUInt(static_cast<UInt64>((x << 1) ^ (x >> 63)));
}
}