| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de> | ||
| 2 | // SPDX-License-Identifier: MIT | ||
| 3 | /*! | ||
| 4 | * \file | ||
| 5 | * \ingroup Compression | ||
| 6 | * \brief Common classes used in the context of data compression | ||
| 7 | */ | ||
| 8 | #ifndef GRIDFORMAT_COMPRESSION_COMMON_HPP_ | ||
| 9 | #define GRIDFORMAT_COMPRESSION_COMMON_HPP_ | ||
| 10 | |||
| 11 | #include <vector> | ||
| 12 | #include <utility> | ||
| 13 | #include <numeric> | ||
| 14 | |||
| 15 | #include <gridformat/common/exceptions.hpp> | ||
| 16 | #include <gridformat/common/serialization.hpp> | ||
| 17 | |||
| 18 | namespace GridFormat::Compression { | ||
| 19 | |||
| 20 | //! \addtogroup Compression | ||
| 21 | //! \{ | ||
| 22 | |||
| 23 | inline constexpr std::size_t default_block_size = (1 << 15); //!< as in VTK (https://gitlab.kitware.com/vtk/vtk/-/blob/65fc526a83ac829628a9462f61fa57f1801e2c7e/IO/XML/vtkXMLWriterBase.cxx#L44) | ||
| 24 | |||
| 25 | //! Stores the block sizes used for compressing the given amount of bytes | ||
| 26 | template<std::integral HeaderType = std::size_t> | ||
| 27 | struct Blocks { | ||
| 28 | const HeaderType block_size; | ||
| 29 | const HeaderType residual_block_size; | ||
| 30 | const HeaderType number_of_blocks; | ||
| 31 | |||
| 32 | 178059 | Blocks(HeaderType size_in_bytes, HeaderType block_size) | |
| 33 | 178059 | : block_size{block_size} | |
| 34 | 178059 | , residual_block_size{size_in_bytes%block_size} | |
| 35 |
2/2✓ Branch 0 taken 87952 times.
✓ Branch 1 taken 1085 times.
|
178059 | , number_of_blocks{residual_block_size ? size_in_bytes/block_size + 1 : size_in_bytes/block_size} |
| 36 | 178059 | {} | |
| 37 | }; | ||
| 38 | |||
| 39 | //! Stores the uncompressed/compressed block sizes after completion of a compression | ||
| 40 | template<std::integral HeaderType = std::size_t> | ||
| 41 | struct CompressedBlocks { | ||
| 42 | const HeaderType block_size; | ||
| 43 | const HeaderType residual_block_size; | ||
| 44 | const HeaderType number_of_blocks; | ||
| 45 | const std::vector<HeaderType> compressed_block_sizes; | ||
| 46 | |||
| 47 | 178059 | CompressedBlocks(const Blocks<HeaderType>& blocks, | |
| 48 | std::vector<HeaderType>&& comp_block_sizes) | ||
| 49 | 178059 | : block_size{blocks.block_size} | |
| 50 | 178059 | , residual_block_size{blocks.residual_block_size} | |
| 51 | 178059 | , number_of_blocks{blocks.number_of_blocks} | |
| 52 | 178059 | , compressed_block_sizes{std::move(comp_block_sizes)} { | |
| 53 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 89037 times.
|
178059 | if (compressed_block_sizes.size() != number_of_blocks) |
| 54 | ✗ | throw SizeError("Mismatch between blocks and number of compressed blocks"); | |
| 55 | 178059 | } | |
| 56 | |||
| 57 | 81788 | std::size_t compressed_size() const { | |
| 58 | 81788 | return std::accumulate( | |
| 59 | compressed_block_sizes.begin(), | ||
| 60 | compressed_block_sizes.end(), | ||
| 61 | 1 | ||
| 62 | 81788 | ); | |
| 63 | } | ||
| 64 | }; | ||
| 65 | |||
| 66 | //! \} group Compression | ||
| 67 | |||
| 68 | } // end namespace GridFormat::Compression | ||
| 69 | |||
| 70 | #endif // GRIDFORMAT_COMPRESSION_COMMON_HPP_ | ||
| 71 |