8#ifndef GRIDFORMAT_VTK_DATA_ARRAY_HPP_
9#define GRIDFORMAT_VTK_DATA_ARRAY_HPP_
23namespace GridFormat::VTK {
31template<
typename Encoder,
35 static constexpr bool do_compression = !std::is_same_v<Compressor, None>;
40 Compressor compressor,
41 [[maybe_unused]]
const Precision<HeaderType>& = {})
43 , _encoder{std::move(encoder)}
44 , _compressor{std::move(compressor)} {
46 if constexpr (std::is_same_v<Encoder, GridFormat::Encoding::Ascii>) {
48 _encoder = _encoder.with({
50 .line_prefix = std::string(10,
' '),
51 .entries_per_line = 15
56 friend std::ostream& operator<<(std::ostream& s,
const DataArray& da) {
61 void stream(std::ostream& s)
const {
62 if constexpr (std::is_same_v<Encoder, GridFormat::Encoding::Ascii>)
63 _export_ascii(s, _encoder);
64 else if constexpr (do_compression)
65 _export_compressed_binary(s);
71 template<
typename _Enc>
72 void _export_ascii(std::ostream& s, _Enc encoder)
const {
73 s << EncodedField{_field, encoder};
76 void _export_binary(std::ostream& s)
const {
77 auto encoded = _encoder(s);
78 std::array<const HeaderType, 1> number_of_bytes{
static_cast<HeaderType
>(_field.size_in_bytes())};
79 encoded.write(std::span{number_of_bytes});
80 s << EncodedField{_field, _encoder};
83 void _export_compressed_binary(std::ostream& s)
const requires(Concepts::Compressor<Compressor>) {
84 _field.precision().visit([&] <
typename T> (
const Precision<T>&) {
85 auto encoded = _encoder(s);
86 Serialization serialization = _field.serialized();
87 const auto blocks = _compressor.template compress<HeaderType>(serialization);
89 std::vector<HeaderType> header;
90 header.reserve(blocks.compressed_block_sizes.size() + 3);
91 header.push_back(blocks.number_of_blocks);
92 header.push_back(blocks.block_size);
93 header.push_back(blocks.residual_block_size);
94 std::ranges::copy(blocks.compressed_block_sizes, std::back_inserter(header));
95 encoded.write(std::span{header});
96 encoded.write(serialization.as_span());
102 Compressor _compressor;
Encoder and stream using ascii.
Concepts related to data compression.
Wraps a field and makes it streamable using encoding.
Concepts related to data encoding.