GridFormat 0.2.1
I/O-Library for grid-like data structures
Loading...
Searching...
No Matches
data_array.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de>
2// SPDX-License-Identifier: MIT
8#ifndef GRIDFORMAT_VTK_DATA_ARRAY_HPP_
9#define GRIDFORMAT_VTK_DATA_ARRAY_HPP_
10
11#include <span>
12#include <utility>
13#include <ostream>
14#include <vector>
15#include <iterator>
16#include <type_traits>
17
22
23namespace GridFormat::VTK {
24
31template<typename Encoder,
32 typename Compressor,
33 typename HeaderType>
34class DataArray {
35 static constexpr bool do_compression = !std::is_same_v<Compressor, None>;
36
37 public:
38 DataArray(const Field& field,
39 Encoder encoder,
40 Compressor compressor,
41 [[maybe_unused]] const Precision<HeaderType>& = {})
42 : _field(field)
43 , _encoder{std::move(encoder)}
44 , _compressor{std::move(compressor)} {
45 // if no ascii formatting was specified by the user, set our defaults
46 if constexpr (std::is_same_v<Encoder, GridFormat::Encoding::Ascii>) {
47 if (_encoder.options() == GridFormat::AsciiFormatOptions{})
48 _encoder = _encoder.with({
49 .delimiter = " ",
50 .line_prefix = std::string(10, ' '),
51 .entries_per_line = 15
52 });
53 }
54 }
55
56 friend std::ostream& operator<<(std::ostream& s, const DataArray& da) {
57 da.stream(s);
58 return s;
59 }
60
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);
66 else
67 _export_binary(s);
68 }
69
70 private:
71 template<typename _Enc>
72 void _export_ascii(std::ostream& s, _Enc encoder) const {
73 s << EncodedField{_field, encoder};
74 }
75
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};
81 }
82
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);
88
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());
97 });
98 }
99
100 const Field& _field;
101 Encoder _encoder;
102 Compressor _compressor;
103};
104
105} // namespace GridFormat::VTK
106
107#endif // GRIDFORMAT_VTK_DATA_ARRAY_HPP_
Encoder and stream using ascii.
Abstract interface for fields of values that is used by writers/readers to store fields.
Definition: field.hpp:37
Wraps a field and exposes it as VTK data array. Essentially, this implements the operator<< to stream...
Definition: data_array.hpp:34
Concepts related to data compression.
Wraps a field and makes it streamable using encoding.
Concepts related to data encoding.
Options for fomatted output of ranges with ascii encoding.
Definition: ascii.hpp:47