2019-06-03 09:56:50 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <IO/ReadBuffer.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <Core/Types.h>
|
2019-06-17 03:27:42 +00:00
|
|
|
#include <Common/BitHelpers.h>
|
2019-06-03 09:56:50 +00:00
|
|
|
|
2019-06-26 20:32:28 +00:00
|
|
|
#if defined(__OpenBSD__) || defined(__FreeBSD__)
|
|
|
|
# include <sys/endian.h>
|
2019-06-27 15:00:03 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
# include <libkern/OSByteOrder.h>
|
|
|
|
|
|
|
|
# define htobe64(x) OSSwapHostToBigInt64(x)
|
|
|
|
# define be64toh(x) OSSwapBigToHostInt64(x)
|
2019-06-26 20:32:28 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-03 09:56:50 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
/** Reads data from underlying ReadBuffer bit by bit, max 64 bits at once.
|
2019-06-03 09:56:50 +00:00
|
|
|
*
|
|
|
|
* reads MSB bits first, imagine that you have a data:
|
2019-06-13 14:04:38 +00:00
|
|
|
* 11110000 10101010 00100100 11111110
|
2019-06-03 09:56:50 +00:00
|
|
|
*
|
|
|
|
* Given that r is BitReader created with a ReadBuffer that reads from data above:
|
|
|
|
* r.readBits(3) => 0b111
|
|
|
|
* r.readBit() => 0b1
|
|
|
|
* r.readBits(8) => 0b1010 // 4 leading zero-bits are not shown
|
|
|
|
* r.readBit() => 0b1
|
|
|
|
* r.readBit() => 0b0
|
2019-06-12 17:12:08 +00:00
|
|
|
* r.readBits(15) => 0b10001001001111111
|
2019-06-13 14:04:38 +00:00
|
|
|
* r.readBit() => 0b0
|
2019-06-03 09:56:50 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
class BitReader
|
|
|
|
{
|
|
|
|
ReadBuffer & buf;
|
|
|
|
|
2019-06-17 03:27:42 +00:00
|
|
|
UInt64 bits_buffer;
|
2019-06-03 09:56:50 +00:00
|
|
|
UInt8 bits_count;
|
2019-06-13 14:04:38 +00:00
|
|
|
static constexpr UInt8 BIT_BUFFER_SIZE = sizeof(bits_buffer) * 8;
|
2019-06-03 09:56:50 +00:00
|
|
|
|
|
|
|
public:
|
2019-06-13 14:04:38 +00:00
|
|
|
BitReader(ReadBuffer & buf_)
|
|
|
|
: buf(buf_),
|
|
|
|
bits_buffer(0),
|
|
|
|
bits_count(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~BitReader()
|
|
|
|
{}
|
|
|
|
|
|
|
|
inline UInt64 readBits(UInt8 bits)
|
|
|
|
{
|
|
|
|
UInt64 result = 0;
|
|
|
|
bits = std::min(static_cast<UInt8>(sizeof(result) * 8), bits);
|
|
|
|
|
|
|
|
while (bits != 0)
|
|
|
|
{
|
|
|
|
if (bits_count == 0)
|
|
|
|
{
|
|
|
|
fillBuffer();
|
|
|
|
if (bits_count == 0)
|
|
|
|
{
|
|
|
|
// EOF.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto to_read = std::min(bits, bits_count);
|
2019-06-17 03:27:42 +00:00
|
|
|
|
|
|
|
const UInt64 v = bits_buffer >> (bits_count - to_read);
|
|
|
|
const UInt64 mask = maskLowBits<UInt64>(to_read);
|
|
|
|
const UInt64 value = v & mask;
|
2019-06-13 14:04:38 +00:00
|
|
|
result |= value;
|
|
|
|
|
2019-06-17 03:27:42 +00:00
|
|
|
// unset bits that were read
|
2019-06-13 14:04:38 +00:00
|
|
|
bits_buffer &= ~(mask << (bits_count - to_read));
|
|
|
|
bits_count -= to_read;
|
|
|
|
bits -= to_read;
|
|
|
|
|
|
|
|
result <<= std::min(bits, BIT_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline UInt64 peekBits(UInt8 /*bits*/)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline UInt8 readBit()
|
|
|
|
{
|
|
|
|
return static_cast<UInt8>(readBits(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool eof() const
|
|
|
|
{
|
|
|
|
return bits_count == 0 && buf.eof();
|
|
|
|
}
|
2019-06-03 09:56:50 +00:00
|
|
|
|
|
|
|
private:
|
2019-06-13 14:04:38 +00:00
|
|
|
void fillBuffer()
|
|
|
|
{
|
|
|
|
auto read = buf.read(reinterpret_cast<char *>(&bits_buffer), BIT_BUFFER_SIZE / 8);
|
2019-06-17 03:27:42 +00:00
|
|
|
bits_buffer = be64toh(bits_buffer);
|
|
|
|
bits_buffer >>= BIT_BUFFER_SIZE - read * 8;
|
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
bits_count = static_cast<UInt8>(read) * 8;
|
|
|
|
}
|
2019-06-03 09:56:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BitWriter
|
|
|
|
{
|
|
|
|
WriteBuffer & buf;
|
|
|
|
|
2019-06-17 03:27:42 +00:00
|
|
|
UInt64 bits_buffer;
|
2019-06-03 09:56:50 +00:00
|
|
|
UInt8 bits_count;
|
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
static constexpr UInt8 BIT_BUFFER_SIZE = sizeof(bits_buffer) * 8;
|
2019-06-03 09:56:50 +00:00
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
public:
|
|
|
|
BitWriter(WriteBuffer & buf_)
|
|
|
|
: buf(buf_),
|
|
|
|
bits_buffer(0),
|
|
|
|
bits_count(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~BitWriter()
|
|
|
|
{
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void writeBits(UInt8 bits, UInt64 value)
|
|
|
|
{
|
|
|
|
bits = std::min(static_cast<UInt8>(sizeof(value) * 8), bits);
|
|
|
|
|
|
|
|
while (bits > 0)
|
|
|
|
{
|
|
|
|
auto v = value;
|
|
|
|
auto to_write = bits;
|
|
|
|
|
|
|
|
const UInt8 capacity = BIT_BUFFER_SIZE - bits_count;
|
|
|
|
if (capacity < bits)
|
|
|
|
{
|
|
|
|
v >>= bits - capacity;
|
|
|
|
to_write = capacity;
|
|
|
|
}
|
|
|
|
|
2019-06-17 03:27:42 +00:00
|
|
|
const UInt64 mask = maskLowBits<UInt64>(to_write);
|
2019-06-13 14:04:38 +00:00
|
|
|
v &= mask;
|
|
|
|
|
|
|
|
bits_buffer <<= to_write;
|
|
|
|
bits_buffer |= v;
|
|
|
|
bits_count += to_write;
|
|
|
|
|
|
|
|
if (bits_count < BIT_BUFFER_SIZE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
doFlush();
|
|
|
|
bits -= to_write;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void flush()
|
|
|
|
{
|
|
|
|
if (bits_count != 0)
|
|
|
|
{
|
|
|
|
bits_buffer <<= (BIT_BUFFER_SIZE - bits_count);
|
|
|
|
doFlush();
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 09:56:50 +00:00
|
|
|
|
|
|
|
private:
|
2019-06-13 14:04:38 +00:00
|
|
|
void doFlush()
|
|
|
|
{
|
2019-06-17 03:27:42 +00:00
|
|
|
bits_buffer = htobe64(bits_buffer);
|
|
|
|
buf.write(reinterpret_cast<const char *>(&bits_buffer), (bits_count + 7) / 8);
|
2019-06-13 14:04:38 +00:00
|
|
|
|
|
|
|
bits_count = 0;
|
|
|
|
bits_buffer = 0;
|
|
|
|
}
|
2019-06-03 09:56:50 +00:00
|
|
|
};
|
|
|
|
|
2019-06-13 14:04:38 +00:00
|
|
|
}
|