Line | Branch | Exec | Source |
---|---|---|---|
1 | // SPDX-FileCopyrightText: 2023 Carl Zeiss Microscopy GmbH | ||
2 | // | ||
3 | // SPDX-License-Identifier: MIT | ||
4 | |||
5 | #pragma once | ||
6 | |||
7 | #include <algorithm> | ||
8 | #include <map> | ||
9 | #include <vector> | ||
10 | #include "ITileCoordinate.h" | ||
11 | |||
12 | namespace imgdoc2 | ||
13 | { | ||
14 | /// A structure combining a dimension and a value. | ||
15 | struct DimensionAndValue | ||
16 | { | ||
17 | imgdoc2::Dimension dimension; ///< The dimension. | ||
18 | int value; ///< The value (for this dimension). | ||
19 | }; | ||
20 | |||
21 | /// Implementation of "tile coordinate object". | ||
22 | class TileCoordinate : public ITileCoordinateMutate | ||
23 | { | ||
24 | private: | ||
25 | std::vector<DimensionAndValue> coordinates; | ||
26 | public: | ||
27 | /// Default constructor, this reserves size for 5 dimensions. | ||
28 | 5452 | TileCoordinate() : TileCoordinate(5) {} | |
29 | |||
30 | /// Constructor allowing to reserve size of the specified number of dimension. This is purely | ||
31 | /// an opportunity for performance optimization, preventing a re-allocation should more dimensions | ||
32 | /// be added than specified here. | ||
33 | /// \param reservedCapacity The reserved capacity for the number of dimensions. | ||
34 | 47390 | explicit TileCoordinate(size_t reservedCapacity) | |
35 | 47390 | { | |
36 |
1/2✓ Branch 1 taken 47390 times.
✗ Branch 2 not taken.
|
47390 | this->coordinates.reserve(reservedCapacity); |
37 | 47390 | } | |
38 | |||
39 | /// Constructor initializing the object with the specified list. | ||
40 | /// \param list The initialization list. | ||
41 | 41938 | TileCoordinate(std::initializer_list<DimensionAndValue> list) : TileCoordinate(list.size()) | |
42 | { | ||
43 |
2/2✓ Branch 2 taken 42384 times.
✓ Branch 3 taken 41938 times.
|
84322 | for (auto d : list) |
44 | { | ||
45 |
1/2✓ Branch 1 taken 42384 times.
✗ Branch 2 not taken.
|
42384 | this->TileCoordinate::Set(d.dimension, d.value); |
46 | } | ||
47 | 41938 | } | |
48 | |||
49 | //! @copydoc imgdoc2::ITileCoordinateMutate::Set(imgdoc2::Dimension d, int value) | ||
50 | 47832 | void Set(imgdoc2::Dimension d, int value) override | |
51 | { | ||
52 |
1/2✓ Branch 3 taken 47832 times.
✗ Branch 4 not taken.
|
47832 | const auto it = std::find_if( |
53 | this->coordinates.begin(), | ||
54 | this->coordinates.end(), | ||
55 | 456 | [=](const DimensionAndValue& s) -> bool | |
56 | { | ||
57 | 456 | return s.dimension == d; | |
58 | }); | ||
59 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 47832 times.
|
47832 | if (it != this->coordinates.end()) |
60 | { | ||
61 | ✗ | it->value = value; | |
62 | } | ||
63 | else | ||
64 | { | ||
65 |
1/2✓ Branch 1 taken 47832 times.
✗ Branch 2 not taken.
|
47832 | this->coordinates.push_back(DimensionAndValue{ d, value }); |
66 | } | ||
67 | 47832 | } | |
68 | |||
69 | //! @copydoc imgdoc2::ITileCoordinateMutate::Clear() | ||
70 | 5448 | void Clear() override | |
71 | { | ||
72 | 5448 | this->coordinates.clear(); | |
73 | 5448 | } | |
74 | |||
75 | public: | ||
76 | 5514 | bool TryGetCoordinate(imgdoc2::Dimension dim, int* coordVal) const override | |
77 | { | ||
78 |
1/2✓ Branch 3 taken 5514 times.
✗ Branch 4 not taken.
|
11080 | const auto it = std::find_if(this->coordinates.cbegin(), this->coordinates.cend(), [=](const DimensionAndValue& s) { return s.dimension == dim; }); |
79 |
2/2✓ Branch 2 taken 5508 times.
✓ Branch 3 taken 6 times.
|
5514 | if (it != this->coordinates.cend()) |
80 | { | ||
81 |
2/2✓ Branch 0 taken 5484 times.
✓ Branch 1 taken 24 times.
|
5508 | if (coordVal != nullptr) |
82 | { | ||
83 | 5484 | *coordVal = it->value; | |
84 | } | ||
85 | |||
86 | 5508 | return true; | |
87 | } | ||
88 | |||
89 | 6 | return false; | |
90 | } | ||
91 | |||
92 | 41954 | void EnumCoordinates(const std::function<bool(imgdoc2::Dimension, int)>& f) const override | |
93 | { | ||
94 |
2/2✓ Branch 4 taken 42408 times.
✓ Branch 5 taken 41944 times.
|
84352 | for (auto it = this->coordinates.cbegin(); it != this->coordinates.cend(); ++it) |
95 | { | ||
96 |
1/2✓ Branch 3 taken 42408 times.
✗ Branch 4 not taken.
|
42408 | const bool b = f(it->dimension, it->value); |
97 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 42398 times.
|
42408 | if (b == false) |
98 | { | ||
99 | 10 | break; | |
100 | } | ||
101 | } | ||
102 | 41954 | } | |
103 | }; | ||
104 | } | ||
105 |