Loading...
Searching...
No Matches
ShapesFileWriter.h
Go to the documentation of this file.
#ifndef CASACORE_SHAPES_FILE_WRITER_H_
#define CASACORE_SHAPES_FILE_WRITER_H_
#include <fstream>
#include <casacore/casa/Arrays/IPosition.h>
#include "Deflate.h"
namespace casacore {
class ShapesFileWriter {
public:
ShapesFileWriter(const std::string& filename)
: compressor_(9), file_(filename) {}
~ShapesFileWriter() {
if (!buffer_.empty()) Flush();
}
void Write(const IPosition& position) {
buffer_.emplace_back(position.size());
if (buffer_.size() == kMaxBufferSize) Flush();
for (ssize_t value : position) {
buffer_.emplace_back(value);
if (buffer_.size() == kMaxBufferSize) Flush();
}
}
private:
void Flush() {
const uint32_t uncompressed_size = buffer_.size() * sizeof(uint64_t);
compressed_buffer_.resize(compressor_.CompressBound(uncompressed_size));
std::span input(reinterpret_cast<const std::byte*>(buffer_.data()),
uncompressed_size);
const uint32_t compressed_size =
compressor_.Compress(input, compressed_buffer_);
file_.write(reinterpret_cast<const char*>(&uncompressed_size),
sizeof(uint32_t));
file_.write(reinterpret_cast<const char*>(&compressed_size),
sizeof(uint32_t));
file_.write(reinterpret_cast<const char*>(compressed_buffer_.data()),
compressed_size);
buffer_.clear();
}
static constexpr size_t kMaxBufferSize = 1024 * 10 * 128;
std::vector<uint64_t> buffer_;
std::vector<std::byte> compressed_buffer_;
deflate::Compressor compressor_;
std::ofstream file_;
};
} // namespace casacore
#endif
Deflate.h
casacore::IPosition
Definition IPosition.h:117
casacore::IPosition::size
size_t size() const
Definition IPosition.h:570
casacore::ShapesFileWriter::Flush
void Flush()
Definition ShapesFileWriter.h:31
casacore::ShapesFileWriter::kMaxBufferSize
static constexpr size_t kMaxBufferSize
Definition ShapesFileWriter.h:47
casacore::ShapesFileWriter::~ShapesFileWriter
~ShapesFileWriter()
Definition ShapesFileWriter.h:17
casacore::ShapesFileWriter::file_
std::ofstream file_
Definition ShapesFileWriter.h:51
casacore::ShapesFileWriter::compressor_
deflate::Compressor compressor_
Definition ShapesFileWriter.h:50
casacore::ShapesFileWriter::buffer_
std::vector< uint64_t > buffer_
Definition ShapesFileWriter.h:48
casacore::ShapesFileWriter::Write
void Write(const IPosition &position)
Definition ShapesFileWriter.h:21
casacore::ShapesFileWriter::ShapesFileWriter
ShapesFileWriter(const std::string &filename)
Definition ShapesFileWriter.h:14
casacore::ShapesFileWriter::compressed_buffer_
std::vector< std::byte > compressed_buffer_
Definition ShapesFileWriter.h:49
casacore::deflate::Compressor
Definition Deflate.h:12
casacore
this file contains all the compiler specific defines
Definition mainpage.dox:28
casacore::value
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:369