2011-10-16 01:57:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2011-11-18 20:17:27 +00:00
|
|
|
#include <vector>
|
2011-10-16 01:57:10 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteBuffer.h>
|
2011-10-16 01:57:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define WRITE_BUFFER_FROM_VECTOR_INITIAL_SIZE_IF_EMPTY 32
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-05-28 14:29:40 +00:00
|
|
|
/** Initialized by vector. Writes data to it. When the vector is finished, it doubles its size.
|
|
|
|
* CharType - char or unsigned char.
|
2011-10-16 01:57:10 +00:00
|
|
|
*/
|
2017-08-30 18:13:32 +00:00
|
|
|
template <typename VectorType = std::vector<char>>
|
2011-10-16 01:57:10 +00:00
|
|
|
class WriteBufferFromVector : public WriteBuffer
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
VectorType & vector;
|
2011-10-16 01:57:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void nextImpl() override
|
|
|
|
{
|
|
|
|
size_t old_size = vector.size();
|
|
|
|
vector.resize(old_size * 2);
|
2017-09-11 05:51:15 +00:00
|
|
|
internal_buffer = Buffer(reinterpret_cast<Position>(&vector[old_size]), reinterpret_cast<Position>(vector.data() + vector.size()));
|
2017-04-01 07:20:54 +00:00
|
|
|
working_buffer = internal_buffer;
|
|
|
|
}
|
2011-10-16 01:57:10 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
WriteBufferFromVector(VectorType & vector_)
|
2017-11-15 18:24:48 +00:00
|
|
|
: WriteBuffer(reinterpret_cast<Position>(vector_.data()), vector_.size()), vector(vector_)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (vector.empty())
|
|
|
|
{
|
|
|
|
vector.resize(WRITE_BUFFER_FROM_VECTOR_INITIAL_SIZE_IF_EMPTY);
|
|
|
|
set(reinterpret_cast<Position>(&vector[0]), vector.size());
|
|
|
|
}
|
|
|
|
}
|
2011-10-16 01:57:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|