Directory: | ./ |
---|---|
File: | pdcom5/include/pdcom5/SizeTypeInfo.h |
Date: | 2024-11-17 04:08:36 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 15 | 16 | 93.8% |
Branches: | 12 | 18 | 66.7% |
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 | 5812 | class SizeInfo : public std::vector<unsigned int> | |
67 | { | ||
68 | public: | ||
69 | 397 | using std::vector<unsigned int>::vector; | |
70 | |||
71 |
3/4✓ Branch 2 taken 3 times.
✓ Branch 3 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
|
12 | bool isScalar() const { return size() == 1 && (*this)[0] == 1; } |
72 | bool isVector() const { return size() == 1 && (*this)[0] > 1; } | ||
73 | bool is2DMatrix() const { return size() == 2; } | ||
74 | 12 | std::size_t dimension() const noexcept | |
75 | { | ||
76 |
3/4✓ Branch 2 taken 3 times.
✓ Branch 3 taken 9 times.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
|
12 | return size() == 1 && (*this)[0] == 1 ? 0 : size(); |
77 | } | ||
78 | |||
79 | 5015 | std::size_t totalElements() const | |
80 | { | ||
81 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 5015 times.
|
5015 | if (empty()) |
82 | ✗ | return 0; | |
83 | 5015 | std::size_t ans = 1; | |
84 |
2/2✓ Branch 6 taken 5116 times.
✓ Branch 7 taken 5015 times.
|
10131 | for (auto i : *this) |
85 | 5116 | ans *= i; | |
86 | 5015 | return ans; | |
87 | } | ||
88 |
1/2✓ Branch 3 taken 348 times.
✗ Branch 4 not taken.
|
348 | static SizeInfo Scalar() { return {1U}; } |
89 |
1/2✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
|
32 | static SizeInfo RowVector(unsigned int rows) { return {rows}; } |
90 | 17 | static SizeInfo Matrix(unsigned int rows, unsigned int cols) | |
91 | { | ||
92 |
1/2✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
|
17 | return {{rows, cols}}; |
93 | } | ||
94 | static SizeInfo | ||
95 | Matrix(unsigned int layers, unsigned int rows, unsigned int cols) | ||
96 | { | ||
97 | 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 |