/src/proj/src/projections/polyhedral/nets/parent_tree.h
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: PROJ |
4 | | * Purpose: Self-validating parent tree type |
5 | | * Author: Felix Palmer |
6 | | * |
7 | | ****************************************************************************/ |
8 | | |
9 | | #ifndef NETS_PARENT_TREE_H |
10 | | #define NETS_PARENT_TREE_H |
11 | | |
12 | | namespace nets { |
13 | | |
14 | | // Wrapper around int[NF] that ensures: |
15 | | // - No entry is out of range [0, NF] |
16 | | // - Exactly one entry is 0 |
17 | | template <int NF> struct ParentTree { |
18 | | int indices[NF]; |
19 | | |
20 | | // cppcheck-suppress noExplicitConstructor |
21 | | constexpr ParentTree(const int (&p)[NF]) : indices{} { |
22 | | int roots = 0; |
23 | | for (int i = 0; i < NF; i++) { |
24 | | if (p[i] < 0 || p[i] > NF) |
25 | | throw "parent tree entry must be in [0, NF]"; |
26 | | if (p[i] == 0) |
27 | | roots++; |
28 | | indices[i] = p[i]; |
29 | | } |
30 | | if (roots != 1) |
31 | | throw "parent tree must have exactly one root (a single 0 entry)"; |
32 | | } |
33 | | |
34 | | // Allow direct access, as if using a normal array |
35 | 602k | constexpr int operator[](int i) const { return indices[i]; }nets::ParentTree<12>::operator[](int) const Line | Count | Source | 35 | 64.3k | constexpr int operator[](int i) const { return indices[i]; } |
nets::ParentTree<20>::operator[](int) const Line | Count | Source | 35 | 538k | constexpr int operator[](int i) const { return indices[i]; } |
nets::ParentTree<4>::operator[](int) const Line | Count | Source | 35 | 75 | constexpr int operator[](int i) const { return indices[i]; } |
|
36 | | }; |
37 | | |
38 | | } // namespace nets |
39 | | |
40 | | #endif // NETS_PARENT_TREE_H |