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 Decompress compressed data. | ||
7 | */ | ||
8 | #ifndef GRIDFORMAT_COMPRESSION_DECOMPRESS_HPP_ | ||
9 | #define GRIDFORMAT_COMPRESSION_DECOMPRESS_HPP_ | ||
10 | |||
11 | #include <string> | ||
12 | |||
13 | #include <gridformat/common/exceptions.hpp> | ||
14 | #include <gridformat/common/serialization.hpp> | ||
15 | |||
16 | #include <gridformat/compression/concepts.hpp> | ||
17 | #include <gridformat/compression/common.hpp> | ||
18 | |||
19 | namespace GridFormat::Compression { | ||
20 | |||
21 | /*! | ||
22 | * \ingroup Compression | ||
23 | * \brief Decompress compressed data. | ||
24 | */ | ||
25 | template<std::integral HeaderType, Concepts::BlockDecompressor Decompressor> | ||
26 | 14518 | void decompress(Serialization& in, | |
27 | const CompressedBlocks<HeaderType>& blocks, | ||
28 | const Decompressor& block_decompressor) { | ||
29 | using Byte = typename Decompressor::ByteType; | ||
30 | |||
31 |
2/2✓ Branch 0 taken 7184 times.
✓ Branch 1 taken 78 times.
|
14518 | const auto last_block_size = blocks.residual_block_size > 0 ? blocks.residual_block_size : blocks.block_size; |
32 | 14518 | const auto out_size = blocks.block_size*(blocks.number_of_blocks-1) + last_block_size; | |
33 | |||
34 | 14518 | std::size_t in_offset = 0; | |
35 | 14518 | std::size_t out_offset = 0; | |
36 |
1/2✓ Branch 1 taken 7262 times.
✗ Branch 2 not taken.
|
14518 | Serialization out{out_size}; |
37 | |||
38 |
1/2✓ Branch 1 taken 7262 times.
✗ Branch 2 not taken.
|
14518 | const auto* in_data = in.template as_span_of<const Byte>().data(); |
39 |
1/2✓ Branch 1 taken 7262 times.
✗ Branch 2 not taken.
|
14518 | auto* out_data = out.template as_span_of<Byte>().data(); |
40 |
2/5✓ Branch 1 taken 7262 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7262 times.
✗ Branch 5 not taken.
|
28880 | std::ranges::for_each(std::views::iota(HeaderType{0}, blocks.number_of_blocks), [&] (HeaderType i) { |
41 |
6/12✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 273 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6089 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 273 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 546 times.
✗ Branch 11 not taken.
|
7193 | const auto out_block_size = (i == blocks.number_of_blocks - 1) ? last_block_size : blocks.block_size; |
42 | |||
43 |
5/12✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 273 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 6089 times.
✗ Branch 11 not taken.
✓ Branch 14 taken 273 times.
✗ Branch 15 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✓ Branch 22 taken 546 times.
✗ Branch 23 not taken.
|
7193 | block_decompressor( |
44 | 7193 | std::span{in_data + in_offset, blocks.compressed_block_sizes[i]}, | |
45 | 7193 | std::span{out_data + out_offset, out_block_size} | |
46 | ); | ||
47 | |||
48 | 7193 | in_offset += blocks.compressed_block_sizes[i]; | |
49 | 7193 | out_offset += out_block_size; | |
50 | }); | ||
51 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7262 times.
|
14518 | if (out_offset != out.size()) |
52 | ✗ | throw SizeError( | |
53 | ✗ | "Unexpected number of bytes written: " + std::to_string(out_offset) + " vs. " + std::to_string(out.size()) | |
54 | ); | ||
55 |
1/2✓ Branch 1 taken 7262 times.
✗ Branch 2 not taken.
|
14518 | in = out; |
56 | 14518 | } | |
57 | |||
58 | } // end namespace GridFormat::Compression | ||
59 | |||
60 | #endif // GRIDFORMAT_COMPRESSION_DECOMPRESS_HPP_ | ||
61 |