Directory: | ./ |
---|---|
File: | include/pdcom5/SizeTypeInfo.h |
Date: | 2024-11-05 15:23:15 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 20 | 20 | 100.0% |
Branches: | 20 | 24 | 83.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /***************************************************************************** | ||
2 | * | ||
3 | * Copyright (C) 2021 Richard Hacker (lerichi at gmx dot net), | ||
4 | * Florian Pose (fp at igh dot de), | ||
5 | * Bjarne von Horn (vh at igh dot de). | ||
6 | * | ||
7 | * This file is part of the PdCom library. | ||
8 | * | ||
9 | * The PdCom library is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU Lesser General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or (at your | ||
12 | * option) any later version. | ||
13 | * | ||
14 | * The PdCom library is distributed in the hope that it will be useful, but | ||
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
17 | * License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU Lesser General Public License | ||
20 | * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | *****************************************************************************/ | ||
23 | |||
24 | #ifndef PDCOM5_SIZETYPEINFO_H | ||
25 | #define PDCOM5_SIZETYPEINFO_H | ||
26 | |||
27 | #include <cstddef> | ||
28 | #include <vector> | ||
29 | |||
30 | namespace PdCom { | ||
31 | |||
32 | /** Type of a Variable */ | ||
33 | struct TypeInfo | ||
34 | { | ||
35 | enum DataType { | ||
36 | DataTypeBegin = 0, | ||
37 | boolean_T = DataTypeBegin, | ||
38 | uint8_T, | ||
39 | int8_T, | ||
40 | uint16_T, | ||
41 | int16_T, | ||
42 | uint32_T, | ||
43 | int32_T, | ||
44 | uint64_T, | ||
45 | int64_T, | ||
46 | double_T, | ||
47 | single_T, | ||
48 | char_T, | ||
49 | DataTypeEnd // keep this as the very last one! | ||
50 | }; | ||
51 | |||
52 | DataType type; | ||
53 | /** name of the correspondig c type (double, char, ...) */ | ||
54 | const char *ctype; | ||
55 | /** Size of one element in bytes */ | ||
56 | size_t element_size; | ||
57 | constexpr TypeInfo( | ||
58 | DataType type, | ||
59 | const char *ctype, | ||
60 | size_t element_size) noexcept : | ||
61 | type(type), ctype(ctype), element_size(element_size) | ||
62 | {} | ||
63 | }; | ||
64 | |||
65 | /** Size of a Variable */ | ||
66 | 953 | class SizeInfo : public std::vector<unsigned int> | |
67 | { | ||
68 | public: | ||
69 | 66 | using std::vector<unsigned int>::vector; | |
70 | |||
71 |
4/4✓ Branch 2 taken 29 times.
✓ Branch 3 taken 57 times.
✓ Branch 7 taken 20 times.
✓ Branch 8 taken 9 times.
|
86 | bool isScalar() const { return size() == 1 && (*this)[0] == 1; } |
72 |
4/4✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 1 times.
|
6 | bool isVector() const { return size() == 1 && (*this)[0] > 1; } |
73 | 6 | bool is2DMatrix() const { return size() == 2; } | |
74 | 73 | std::size_t dimension() const noexcept | |
75 | { | ||
76 |
4/4✓ Branch 2 taken 11 times.
✓ Branch 3 taken 62 times.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 3 times.
|
73 | return size() == 1 && (*this)[0] == 1 ? 0 : size(); |
77 | } | ||
78 | |||
79 | 628 | std::size_t totalElements() const | |
80 | { | ||
81 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 627 times.
|
628 | if (empty()) |
82 | 1 | return 0; | |
83 | 627 | std::size_t ans = 1; | |
84 |
2/2✓ Branch 6 taken 641 times.
✓ Branch 7 taken 627 times.
|
1268 | for (auto i : *this) |
85 | 641 | ans *= i; | |
86 | 627 | return ans; | |
87 | } | ||
88 |
1/2✓ Branch 3 taken 58 times.
✗ Branch 4 not taken.
|
58 | static SizeInfo Scalar() { return {1U}; } |
89 |
1/2✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
4 | static SizeInfo RowVector(unsigned int rows) { return {rows}; } |
90 | 3 | static SizeInfo Matrix(unsigned int rows, unsigned int cols) | |
91 | { | ||
92 |
1/2✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | return {{rows, cols}}; |
93 | } | ||
94 | static SizeInfo | ||
95 | 1 | Matrix(unsigned int layers, unsigned int rows, unsigned int cols) | |
96 | { | ||
97 |
1/2✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
1 | return {{layers, rows, cols}}; |
98 | } | ||
99 | |||
100 | unsigned int layers() const | ||
101 | { | ||
102 | if (empty()) | ||
103 | return 0; | ||
104 | return size() >= 3 ? operator[](size() - 3) : 1; | ||
105 | } | ||
106 | |||
107 | unsigned int rows() const | ||
108 | { | ||
109 | if (empty()) | ||
110 | return 0; | ||
111 | return size() >= 2 ? operator[](size() - 2) : 1; | ||
112 | } | ||
113 | |||
114 | unsigned int columns() const { return empty() ? 0 : back(); } | ||
115 | }; | ||
116 | } // namespace PdCom | ||
117 | |||
118 | |||
119 | #endif // PDCOM5_SIZETYPEINFO_H | ||
120 |